forked from spaolacci/murmur3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
murmur_test.go
288 lines (260 loc) · 6.79 KB
/
murmur_test.go
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package murmur3
import (
"crypto/rand"
"fmt"
"hash"
"io"
"testing"
"testing/quick"
"github.com/twmb/murmur3/testdata"
)
var data = []struct {
h32 uint32
h64_1 uint64
h64_2 uint64
s string
}{
{0x00000000, 0x0000000000000000, 0x0000000000000000, ""},
{0x248bfa47, 0xcbd8a7b341bd9b02, 0x5b1e906a48ae1d19, "hello"},
{0x149bbb7f, 0x342fac623a5ebc8e, 0x4cdcbc079642414d, "hello, world"},
{0xe31e8a70, 0xb89e5988b737affc, 0x664fc2950231b2cb, "19 Jan 2038 at 3:14:07 AM"},
{0xd5c48bfc, 0xcd99481f9ee902c9, 0x695da1a38987b6e7, "The quick brown fox jumps over the lazy dog."},
}
func TestRef(t *testing.T) {
for _, elem := range data {
var h32 hash.Hash32 = New32()
h32.Write([]byte(elem.s))
if v := h32.Sum32(); v != elem.h32 {
t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
}
var h32_byte hash.Hash32 = New32()
h32_byte.Write([]byte(elem.s))
target := fmt.Sprintf("%08x", elem.h32)
if p := fmt.Sprintf("%x", h32_byte.Sum(nil)); p != target {
t.Errorf("'%s': %s (want %s)", elem.s, p, target)
}
if v := Sum32([]byte(elem.s)); v != elem.h32 {
t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
}
var h64 hash.Hash64 = New64()
h64.Write([]byte(elem.s))
if v := h64.Sum64(); v != elem.h64_1 {
t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
}
var h64_byte hash.Hash64 = New64()
h64_byte.Write([]byte(elem.s))
target = fmt.Sprintf("%016x", elem.h64_1)
if p := fmt.Sprintf("%x", h64_byte.Sum(nil)); p != target {
t.Errorf("Sum64: '%s': %s (want %s)", elem.s, p, target)
}
if v := Sum64([]byte(elem.s)); v != elem.h64_1 {
t.Errorf("Sum64: '%s': 0x%x (want 0x%x)", elem.s, v, elem.h64_1)
}
var h128 Hash128 = New128()
h128.Write([]byte(elem.s))
if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("New128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
}
var h128_byte Hash128 = New128()
h128_byte.Write([]byte(elem.s))
target = fmt.Sprintf("%016x%016x", elem.h64_1, elem.h64_2)
if p := fmt.Sprintf("%x", h128_byte.Sum(nil)); p != target {
t.Errorf("New128: '%s': %s (want %s)", elem.s, p, target)
}
if v1, v2 := Sum128([]byte(elem.s)); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("Sum128: '%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
}
}
}
func TestQuickSeedSum32(t *testing.T) {
f := func(seed uint32, data []byte) bool {
goh1 := SeedSum32(seed, data)
cpph1 := testdata.SeedSum32(seed, data)
return goh1 == cpph1
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestQuickSeedSum64(t *testing.T) {
f := func(seed uint32, data []byte) bool {
goh1 := SeedSum64(uint64(seed), data)
cpph1 := testdata.SeedSum64(seed, data)
return goh1 == cpph1
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func TestQuickSeedSum128(t *testing.T) {
f := func(seed uint32, data []byte) bool {
goh1, goh2 := SeedSum128(uint64(seed), uint64(seed), data)
cpph1, cpph2 := testdata.SeedSum128(seed, data)
return goh1 == cpph1 && goh2 == cpph2
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
// TestBoundaries forces every block/tail path to be exercised for Sum32 and Sum128.
func TestBoundaries(t *testing.T) {
var data [17]byte
for i := 0; !t.Failed() && i < 20; i++ {
io.ReadFull(rand.Reader, data[:])
for size := 0; size <= 17; size++ {
test := data[:size]
g32h1 := Sum32(test)
c32h1 := testdata.SeedSum32(0, test)
if g32h1 != c32h1 {
t.Errorf("size #%d: in: %x, g32h1 (%d) != c32h1 (%d); attempt #%d", size, test, g32h1, c32h1, i)
}
g64h1 := Sum64(test)
c64h1 := testdata.SeedSum64(0, test)
if g64h1 != c64h1 {
t.Errorf("size #%d: in: %x, g64h1 (%d) != c64h1 (%d); attempt #%d", size, test, g64h1, c64h1, i)
}
g128h1, g128h2 := Sum128(test)
c128h1, c128h2 := testdata.SeedSum128(0, test)
if g128h1 != c128h1 {
t.Errorf("size #%d: in: %x, g128h1 (%d) != c128h1 (%d); attempt #%d", size, test, g128h1, c128h1, i)
}
if g128h2 != c128h2 {
t.Errorf("size #%d: in: %x, g128h2 (%d) != c128h2 (%d); attempt #%d", size, test, g128h2, c128h2, i)
}
}
}
}
func TestIncremental(t *testing.T) {
for _, elem := range data {
h32 := New32()
h128 := New128()
for i, j, k := 0, 0, len(elem.s); i < k; i = j {
j = 2*i + 3
if j > k {
j = k
}
s := elem.s[i:j]
print(s + "|")
h32.Write([]byte(s))
h128.Write([]byte(s))
}
println()
if v := h32.Sum32(); v != elem.h32 {
t.Errorf("'%s': 0x%x (want 0x%x)", elem.s, v, elem.h32)
}
if v1, v2 := h128.Sum128(); v1 != elem.h64_1 || v2 != elem.h64_2 {
t.Errorf("'%s': 0x%x-0x%x (want 0x%x-0x%x)", elem.s, v1, v2, elem.h64_1, elem.h64_2)
}
}
}
//---
func bench32(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum32(buf)
}
}
func BenchmarkBase32(b *testing.B) {
bench32(b, 0)
}
func BenchmarkLoop32(b *testing.B) {
bench32(b, 4)
}
func BenchmarkTail32_1(b *testing.B) {
bench32(b, 1)
}
func BenchmarkTail32_2(b *testing.B) {
bench32(b, 2)
}
func BenchmarkTail32_3(b *testing.B) {
bench32(b, 3)
}
//---
func benchPartial32(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
hasher := New32()
hasher.Write(buf)
hasher.Sum32()
}
}
func BenchmarkBasePartial32(b *testing.B) {
benchPartial32(b, 0)
}
func BenchmarkLoopPartial32(b *testing.B) {
benchPartial32(b, 4)
}
func BenchmarkTailPartial32_1(b *testing.B) {
benchPartial32(b, 1)
}
func BenchmarkTailPartial32_2(b *testing.B) {
benchPartial32(b, 2)
}
func BenchmarkTailPartial32_3(b *testing.B) {
benchPartial32(b, 3)
}
//---
func bench128(b *testing.B, length int) {
buf := make([]byte, length)
b.SetBytes(int64(length))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum128(buf)
}
}
func BenchmarkBase128(b *testing.B) {
bench128(b, 0)
}
func BenchmarkLoop128(b *testing.B) {
bench128(b, 16)
}
func BenchmarkTail128_1(b *testing.B) {
bench128(b, 1)
}
func BenchmarkTail128_2(b *testing.B) {
bench128(b, 2)
}
func BenchmarkTail128_3(b *testing.B) {
bench128(b, 3)
}
func BenchmarkTail128_4(b *testing.B) {
bench128(b, 4)
}
func BenchmarkTail128_5(b *testing.B) {
bench128(b, 5)
}
func BenchmarkTail128_6(b *testing.B) {
bench128(b, 6)
}
func BenchmarkTail128_7(b *testing.B) {
bench128(b, 7)
}
func BenchmarkTail128_8(b *testing.B) {
bench128(b, 8)
}
func BenchmarkTail128_9(b *testing.B) {
bench128(b, 9)
}
func BenchmarkTail128_10(b *testing.B) {
bench128(b, 10)
}
func BenchmarkTail128_11(b *testing.B) {
bench128(b, 11)
}
func BenchmarkTail128_12(b *testing.B) {
bench128(b, 12)
}
func BenchmarkTail128_13(b *testing.B) {
bench128(b, 13)
}
func BenchmarkTail128_14(b *testing.B) {
bench128(b, 14)
}
func BenchmarkTail128_15(b *testing.B) {
bench128(b, 15)
}
//---