Source file
src/hash/fnv/fnv_test.go
1
2
3
4
5 package fnv
6
7 import (
8 "bytes"
9 "encoding"
10 "encoding/binary"
11 "hash"
12 "io"
13 "testing"
14 )
15
16 type golden struct {
17 out []byte
18 in string
19 halfState string
20 }
21
22 var golden32 = []golden{
23 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, "", "fnv\x01\x81\x1c\x9d\xc5"},
24 {[]byte{0x05, 0x0c, 0x5d, 0x7e}, "a", "fnv\x01\x81\x1c\x9d\xc5"},
25 {[]byte{0x70, 0x77, 0x2d, 0x38}, "ab", "fnv\x01\x05\f]~"},
26 {[]byte{0x43, 0x9c, 0x2f, 0x4b}, "abc", "fnv\x01\x05\f]~"},
27 }
28
29 var golden32a = []golden{
30 {[]byte{0x81, 0x1c, 0x9d, 0xc5}, "", "fnv\x02\x81\x1c\x9d\xc5"},
31 {[]byte{0xe4, 0x0c, 0x29, 0x2c}, "a", "fnv\x02\x81\x1c\x9d\xc5"},
32 {[]byte{0x4d, 0x25, 0x05, 0xca}, "ab", "fnv\x02\xe4\f),"},
33 {[]byte{0x1a, 0x47, 0xe9, 0x0b}, "abc", "fnv\x02\xe4\f),"},
34 }
35
36 var golden64 = []golden{
37 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, "", "fnv\x03\xcb\xf2\x9c\xe4\x84\"#%"},
38 {[]byte{0xaf, 0x63, 0xbd, 0x4c, 0x86, 0x01, 0xb7, 0xbe}, "a", "fnv\x03\xcb\xf2\x9c\xe4\x84\"#%"},
39 {[]byte{0x08, 0x32, 0x67, 0x07, 0xb4, 0xeb, 0x37, 0xb8}, "ab", "fnv\x03\xafc\xbdL\x86\x01\xb7\xbe"},
40 {[]byte{0xd8, 0xdc, 0xca, 0x18, 0x6b, 0xaf, 0xad, 0xcb}, "abc", "fnv\x03\xafc\xbdL\x86\x01\xb7\xbe"},
41 }
42
43 var golden64a = []golden{
44 {[]byte{0xcb, 0xf2, 0x9c, 0xe4, 0x84, 0x22, 0x23, 0x25}, "", "fnv\x04\xcb\xf2\x9c\xe4\x84\"#%"},
45 {[]byte{0xaf, 0x63, 0xdc, 0x4c, 0x86, 0x01, 0xec, 0x8c}, "a", "fnv\x04\xcb\xf2\x9c\xe4\x84\"#%"},
46 {[]byte{0x08, 0x9c, 0x44, 0x07, 0xb5, 0x45, 0x98, 0x6a}, "ab", "fnv\x04\xafc\xdcL\x86\x01\xec\x8c"},
47 {[]byte{0xe7, 0x1f, 0xa2, 0x19, 0x05, 0x41, 0x57, 0x4b}, "abc", "fnv\x04\xafc\xdcL\x86\x01\xec\x8c"},
48 }
49
50 var golden128 = []golden{
51 {[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, "", "fnv\x05lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
52 {[]byte{0xd2, 0x28, 0xcb, 0x69, 0x10, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x14, 0x1e}, "a", "fnv\x05lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
53 {[]byte{0x8, 0x80, 0x94, 0x5a, 0xee, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0x26, 0xc0, 0x88}, "ab", "fnv\x05\xd2(\xcbi\x10\x1a\x8c\xafx\x91+pNJ\x14\x1e"},
54 {[]byte{0xa6, 0x8b, 0xb2, 0xa4, 0x34, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x78, 0xc6, 0xae, 0xe7, 0x3b}, "abc", "fnv\x05\xd2(\xcbi\x10\x1a\x8c\xafx\x91+pNJ\x14\x1e"},
55 }
56
57 var golden128a = []golden{
58 {[]byte{0x6c, 0x62, 0x27, 0x2e, 0x07, 0xbb, 0x01, 0x42, 0x62, 0xb8, 0x21, 0x75, 0x62, 0x95, 0xc5, 0x8d}, "", "fnv\x06lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
59 {[]byte{0xd2, 0x28, 0xcb, 0x69, 0x6f, 0x1a, 0x8c, 0xaf, 0x78, 0x91, 0x2b, 0x70, 0x4e, 0x4a, 0x89, 0x64}, "a", "fnv\x06lb'.\a\xbb\x01Bb\xb8!ub\x95ō"},
60 {[]byte{0x08, 0x80, 0x95, 0x44, 0xbb, 0xab, 0x1b, 0xe9, 0x5a, 0xa0, 0x73, 0x30, 0x55, 0xb6, 0x9a, 0x62}, "ab", "fnv\x06\xd2(\xcbio\x1a\x8c\xafx\x91+pNJ\x89d"},
61 {[]byte{0xa6, 0x8d, 0x62, 0x2c, 0xec, 0x8b, 0x58, 0x22, 0x83, 0x6d, 0xbc, 0x79, 0x77, 0xaf, 0x7f, 0x3b}, "abc", "fnv\x06\xd2(\xcbio\x1a\x8c\xafx\x91+pNJ\x89d"},
62 }
63
64 func TestGolden32(t *testing.T) {
65 testGolden(t, New32(), golden32)
66 }
67
68 func TestGolden32a(t *testing.T) {
69 testGolden(t, New32a(), golden32a)
70 }
71
72 func TestGolden64(t *testing.T) {
73 testGolden(t, New64(), golden64)
74 }
75
76 func TestGolden64a(t *testing.T) {
77 testGolden(t, New64a(), golden64a)
78 }
79
80 func TestGolden128(t *testing.T) {
81 testGolden(t, New128(), golden128)
82 }
83
84 func TestGolden128a(t *testing.T) {
85 testGolden(t, New128a(), golden128a)
86 }
87
88 func testGolden(t *testing.T, hash hash.Hash, gold []golden) {
89 for _, g := range gold {
90 hash.Reset()
91 done, error := hash.Write([]byte(g.in))
92 if error != nil {
93 t.Fatalf("write error: %s", error)
94 }
95 if done != len(g.in) {
96 t.Fatalf("wrote only %d out of %d bytes", done, len(g.in))
97 }
98 if actual := hash.Sum(nil); !bytes.Equal(g.out, actual) {
99 t.Errorf("hash(%q) = 0x%x want 0x%x", g.in, actual, g.out)
100 }
101 }
102 }
103
104 func TestGoldenMarshal(t *testing.T) {
105 tests := []struct {
106 name string
107 newHash func() hash.Hash
108 gold []golden
109 }{
110 {"32", func() hash.Hash { return New32() }, golden32},
111 {"32a", func() hash.Hash { return New32a() }, golden32a},
112 {"64", func() hash.Hash { return New64() }, golden64},
113 {"64a", func() hash.Hash { return New64a() }, golden64a},
114 {"128", func() hash.Hash { return New128() }, golden128},
115 {"128a", func() hash.Hash { return New128a() }, golden128a},
116 }
117 for _, tt := range tests {
118 t.Run(tt.name, func(t *testing.T) {
119 for _, g := range tt.gold {
120 h := tt.newHash()
121 h2 := tt.newHash()
122
123 io.WriteString(h, g.in[:len(g.in)/2])
124
125 state, err := h.(encoding.BinaryMarshaler).MarshalBinary()
126 if err != nil {
127 t.Errorf("could not marshal: %v", err)
128 continue
129 }
130
131 stateAppend, err := h.(encoding.BinaryAppender).AppendBinary(make([]byte, 4, 32))
132 if err != nil {
133 t.Errorf("could not marshal: %v", err)
134 continue
135 }
136 stateAppend = stateAppend[4:]
137
138 if string(state) != g.halfState {
139 t.Errorf("checksum(%q) state = %q, want %q", g.in, state, g.halfState)
140 continue
141 }
142
143 if string(stateAppend) != g.halfState {
144 t.Errorf("checksum(%q) state = %q, want %q", g.in, stateAppend, g.halfState)
145 continue
146 }
147
148 if err := h2.(encoding.BinaryUnmarshaler).UnmarshalBinary(state); err != nil {
149 t.Errorf("could not unmarshal: %v", err)
150 continue
151 }
152
153 io.WriteString(h, g.in[len(g.in)/2:])
154 io.WriteString(h2, g.in[len(g.in)/2:])
155
156 if actual, actual2 := h.Sum(nil), h2.Sum(nil); !bytes.Equal(actual, actual2) {
157 t.Errorf("hash(%q) = 0x%x != marshaled 0x%x", g.in, actual, actual2)
158 }
159 }
160 })
161 }
162 }
163
164 func TestIntegrity32(t *testing.T) {
165 testIntegrity(t, New32())
166 }
167
168 func TestIntegrity32a(t *testing.T) {
169 testIntegrity(t, New32a())
170 }
171
172 func TestIntegrity64(t *testing.T) {
173 testIntegrity(t, New64())
174 }
175
176 func TestIntegrity64a(t *testing.T) {
177 testIntegrity(t, New64a())
178 }
179 func TestIntegrity128(t *testing.T) {
180 testIntegrity(t, New128())
181 }
182
183 func TestIntegrity128a(t *testing.T) {
184 testIntegrity(t, New128a())
185 }
186
187 func testIntegrity(t *testing.T, h hash.Hash) {
188 data := []byte{'1', '2', 3, 4, 5}
189 h.Write(data)
190 sum := h.Sum(nil)
191
192 if size := h.Size(); size != len(sum) {
193 t.Fatalf("Size()=%d but len(Sum())=%d", size, len(sum))
194 }
195
196 if a := h.Sum(nil); !bytes.Equal(sum, a) {
197 t.Fatalf("first Sum()=0x%x, second Sum()=0x%x", sum, a)
198 }
199
200 h.Reset()
201 h.Write(data)
202 if a := h.Sum(nil); !bytes.Equal(sum, a) {
203 t.Fatalf("Sum()=0x%x, but after Reset() Sum()=0x%x", sum, a)
204 }
205
206 h.Reset()
207 h.Write(data[:2])
208 h.Write(data[2:])
209 if a := h.Sum(nil); !bytes.Equal(sum, a) {
210 t.Fatalf("Sum()=0x%x, but with partial writes, Sum()=0x%x", sum, a)
211 }
212
213 switch h.Size() {
214 case 4:
215 sum32 := h.(hash.Hash32).Sum32()
216 if sum32 != binary.BigEndian.Uint32(sum) {
217 t.Fatalf("Sum()=0x%x, but Sum32()=0x%x", sum, sum32)
218 }
219 case 8:
220 sum64 := h.(hash.Hash64).Sum64()
221 if sum64 != binary.BigEndian.Uint64(sum) {
222 t.Fatalf("Sum()=0x%x, but Sum64()=0x%x", sum, sum64)
223 }
224 case 16:
225
226 }
227 }
228
229 func BenchmarkFnv32KB(b *testing.B) {
230 benchmarkKB(b, New32())
231 }
232
233 func BenchmarkFnv32aKB(b *testing.B) {
234 benchmarkKB(b, New32a())
235 }
236
237 func BenchmarkFnv64KB(b *testing.B) {
238 benchmarkKB(b, New64())
239 }
240
241 func BenchmarkFnv64aKB(b *testing.B) {
242 benchmarkKB(b, New64a())
243 }
244
245 func BenchmarkFnv128KB(b *testing.B) {
246 benchmarkKB(b, New128())
247 }
248
249 func BenchmarkFnv128aKB(b *testing.B) {
250 benchmarkKB(b, New128a())
251 }
252
253 func benchmarkKB(b *testing.B, h hash.Hash) {
254 b.SetBytes(1024)
255 data := make([]byte, 1024)
256 for i := range data {
257 data[i] = byte(i)
258 }
259 in := make([]byte, 0, h.Size())
260
261 b.ResetTimer()
262 for i := 0; i < b.N; i++ {
263 h.Reset()
264 h.Write(data)
265 h.Sum(in)
266 }
267 }
268
View as plain text