-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwriten_test.go
More file actions
201 lines (180 loc) · 3.93 KB
/
writen_test.go
File metadata and controls
201 lines (180 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package md5vec
import (
"bytes"
"crypto/md5"
"fmt"
"hash"
"math/rand"
"strings"
"testing"
"unsafe"
)
const (
nbufs = 8
bufsize = 1024
)
func TestWriteNull(t *testing.T) {
n, err := WriteN(nil, nil)
if n != 0 || err != nil {
t.Fatal(err)
}
}
func TestWriteZero(t *testing.T) {
n, err := WriteN([]hash.Hash{md5.New()}, [][]byte{nil})
if n != 0 || err != nil {
t.Fatal(err)
}
}
func TestMismatchCount(t *testing.T) {
n, err := WriteN(nil, [][]byte{nil})
if err == nil || !strings.Contains(err.Error(), "mismatched buffer count") {
t.Fatalf("expected mismatch, got: %v %v", n, err)
}
}
func TestMismatchLength(t *testing.T) {
n, err := WriteN([]hash.Hash{md5.New(), md5.New()}, [][]byte{[]byte("a"), []byte("aa")})
if err == nil || !strings.Contains(err.Error(), "mismatched buf lengths") {
t.Fatalf("expected mismatch, got: %v %v", n, err)
}
}
func TestNonBlocksize(t *testing.T) {
src := bytes.Repeat([]byte("a"), 99)
h8 := md5.New()
h := md5.New()
WriteN([]hash.Hash{h8}, [][]byte{src})
h.Write(src)
if !bytes.Equal(h8.Sum(nil), h.Sum(nil)) {
t.Fatal("compare")
}
}
func TestPanicPartialWrite(t *testing.T) {
// N/A for scalar md5 code
if !hasAVX2 {
t.Skip("avx unsupported")
}
h1 := md5.New()
h2 := md5.New()
h1.Write([]byte("hi"))
h2.Write([]byte("hi"))
sixty4 := bytes.Repeat([]byte("a"), 64)
n, err := WriteN([]hash.Hash{h1, h2}, [][]byte{sixty4, sixty4})
if err == nil || !strings.Contains(err.Error(), "partial state") {
t.Fatalf("expected partial state, got: %v %v", n, err)
}
}
func TestWriteN(t *testing.T) {
for i := 1; i < nbufs; i++ {
if err := testWriteN(t, i); err != nil {
t.Errorf("nbufs %d: %v", i, err)
}
}
}
func testWriteN(t *testing.T, nb int) error {
p := make([][]byte, nb)
for i := range p {
p[i] = make([]byte, bufsize)
for j := range p[i] {
p[i][j] = byte(rand.Int())
}
}
mh := make([]hash.Hash, nb)
for i := range mh {
mh[i] = md5.New()
}
mh8 := make([]hash.Hash, nb)
for i := range mh {
mh8[i] = md5.New()
}
for i := range mh {
mh[i].Write(p[i])
}
n, err := WriteN(mh8, p)
if err != nil {
return err
}
if n != bufsize {
return fmt.Errorf("n(%d) != bufsize(%d)", n, bufsize)
}
var badslots []int
var offsets []int
for i := range p {
exp := mh[i].Sum(nil)
got := mh8[i].Sum(nil)
if !bytes.Equal(exp, got) {
badslots = append(badslots, i)
offsets = append(offsets, int(uintptr(unsafe.Pointer(&p[i][0]))-uintptr(unsafe.Pointer(&p[0][0]))))
}
}
if badslots != nil {
for i := range p {
t.Logf("%p %d", &p[i][0], int(uintptr(unsafe.Pointer(&p[i][0]))-uintptr(unsafe.Pointer(&p[0][0]))))
}
return fmt.Errorf("bad compares at %v %v", badslots, offsets)
}
return nil
}
func TestPartialN(t *testing.T) {
p := make([][]byte, 2)
mh := make([]hash.Hash, 2)
for i := range p {
p[i] = make([]byte, bufsize)
mh[i] = md5.New()
for j := range p[i] {
p[i][j] = byte(rand.Int())
}
}
for i := 0; i < bufsize; i += md5.BlockSize {
q := make([][]byte, 2)
for j := range q {
q[j] = p[j][i : i+md5.BlockSize]
}
WriteN(mh, q)
}
for i := range mh {
h := md5.New()
h.Write(p[i])
if !bytes.Equal(mh[i].Sum(nil), h.Sum(nil)) {
t.Fatal("compare")
}
}
}
func BenchmarkMd5(b *testing.B) {
p := make([][]byte, 8)
for i := range p {
p[i] = make([]byte, 1024*bufsize)
for j := range p[i] {
p[i][j] = byte(rand.Int())
}
}
b.ResetTimer()
b.SetBytes(1024 * 8 * bufsize)
for i := 0; i < b.N; i++ {
for j := range p {
h := md5.New()
h.Write(p[j])
h.Sum(nil)
}
}
}
func BenchmarkMd5by8(b *testing.B) {
xbuf := make([]byte, 8*1024*bufsize)
p := make([][]byte, 8)
for i := range p {
p[i] = xbuf[i*1024*bufsize : (i*1024*bufsize)+1024*bufsize]
for j := range p[i] {
p[i][j] = byte(rand.Int())
}
}
b.ResetTimer()
b.SetBytes(8 * 1024 * bufsize)
for i := 0; i < b.N; i++ {
h := make([]hash.Hash, len(p))
for j := range h {
h[j] = md5.New()
}
WriteN(h, p)
for j := range h {
h[j].Sum(nil)
}
}
}