@@ -4,45 +4,60 @@ import (
4
4
"bytes"
5
5
"encoding/binary"
6
6
"fmt"
7
+ "math"
7
8
"strings"
8
9
"testing"
9
10
)
10
11
11
12
func TestAll (t * testing.T ) {
13
+ // Exactly 63 characters, which exercises all code paths.
14
+ const s63 = "Call me Ishmael. Some years ago--never mind how long precisely-"
12
15
for _ , tt := range []struct {
13
- name string
14
16
input string
17
+ seed uint64
15
18
want uint64
16
19
}{
17
- {"empty" , "" , 0xef46db3751d8e999 },
18
- {"a" , "a" , 0xd24ec4f1a98c6e5b },
19
- {"as" , "as" , 0x1c330fb2d66be179 },
20
- {"asd" , "asd" , 0x631c37ce72a97393 },
21
- {"asdf" , "asdf" , 0x415872f599cea71e },
22
- {
23
- "len=63" ,
24
- // Exactly 63 characters, which exercises all code paths.
25
- "Call me Ishmael. Some years ago--never mind how long precisely-" ,
26
- 0x02a2e85470d6fd96 ,
27
- },
20
+ {"" , 0 , 0xef46db3751d8e999 },
21
+ {"a" , 0 , 0xd24ec4f1a98c6e5b },
22
+ {"as" , 0 , 0x1c330fb2d66be179 },
23
+ {"asd" , 0 , 0x631c37ce72a97393 },
24
+ {"asdf" , 0 , 0x415872f599cea71e },
25
+ {s63 , 0 , 0x02a2e85470d6fd96 },
26
+
27
+ {"" , 123 , 0xe0db84de91f3e198 },
28
+ {"asdf" , math .MaxUint64 , 0x9a2fd8473be539b6 },
29
+ {s63 , 54321 , 0x1736d186daf5d1cd },
28
30
} {
29
31
lastChunkSize := len (tt .input )
30
32
if lastChunkSize == 0 {
31
33
lastChunkSize = 1
32
34
}
35
+ var name string
36
+ if tt .input == "" {
37
+ name = "input=empty"
38
+ } else if len (tt .input ) > 10 {
39
+ name = fmt .Sprintf ("input=len-%d" , len (tt .input ))
40
+ } else {
41
+ name = fmt .Sprintf ("input=%q" , tt .input )
42
+ }
43
+ if tt .seed != 0 {
44
+ name += fmt .Sprintf (",seed=%d" , tt .seed )
45
+ }
33
46
for chunkSize := 1 ; chunkSize <= lastChunkSize ; chunkSize ++ {
34
- name := fmt .Sprintf ("%s,chunkSize=%d" , tt . name , chunkSize )
47
+ name := fmt .Sprintf ("%s,chunkSize=%d" , name , chunkSize )
35
48
t .Run (name , func (t * testing.T ) {
36
- testDigest (t , tt .input , chunkSize , tt .want )
49
+ testDigest (t , tt .input , tt . seed , chunkSize , tt .want )
37
50
})
38
51
}
39
- t .Run (tt .name , func (t * testing.T ) { testSum (t , tt .input , tt .want ) })
52
+ if tt .seed == 0 {
53
+ t .Run (name , func (t * testing.T ) { testSum (t , tt .input , tt .want ) })
54
+ }
40
55
}
41
56
}
42
57
43
- func testDigest (t * testing.T , input string , chunkSize int , want uint64 ) {
44
- d := New ( )
45
- ds := New ( ) // uses WriteString
58
+ func testDigest (t * testing.T , input string , seed uint64 , chunkSize int , want uint64 ) {
59
+ d := NewWithSeed ( seed )
60
+ ds := NewWithSeed ( seed ) // uses WriteString
46
61
for i := 0 ; i < len (input ); i += chunkSize {
47
62
chunk := input [i :]
48
63
if len (chunk ) > chunkSize {
@@ -96,6 +111,23 @@ func TestReset(t *testing.T) {
96
111
}
97
112
}
98
113
114
+ func TestResetWithSeed (t * testing.T ) {
115
+ parts := []string {"The quic" , "k br" , "o" , "wn fox jumps" , " ov" , "er the lazy " , "dog." }
116
+ d := NewWithSeed (123 )
117
+ for _ , part := range parts {
118
+ d .Write ([]byte (part ))
119
+ }
120
+ h0 := d .Sum64 ()
121
+
122
+ d .ResetWithSeed (123 )
123
+ d .Write ([]byte (strings .Join (parts , "" )))
124
+ h1 := d .Sum64 ()
125
+
126
+ if h0 != h1 {
127
+ t .Errorf ("0x%x != 0x%x" , h0 , h1 )
128
+ }
129
+ }
130
+
99
131
func TestBinaryMarshaling (t * testing.T ) {
100
132
d := New ()
101
133
d .WriteString ("abc" )
0 commit comments