-
Notifications
You must be signed in to change notification settings - Fork 20
/
hash32_test.go
88 lines (79 loc) · 2.02 KB
/
hash32_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
package mmh3
import (
"crypto/rand"
"fmt"
"io"
"testing"
)
func TestHash32(t *testing.T) {
h := New32()
cases := map[string]string{
"": "00000000",
"hello": "47fa8b24",
"foobar": "bdd4c4a4",
"ooooooo": "cc77ff34",
"我能吞下玻璃而不伤身体": "841a69c4",
}
for key, hex := range cases {
h.Write([]byte(key))
if fmt.Sprintf("%x", h.Sum(nil)) != hex {
t.Fatal()
}
h.Reset()
for _, c := range key {
h.Write([]byte(string(c)))
}
if fmt.Sprintf("%x", h.Sum(nil)) != hex {
t.Fatal()
}
h.Reset()
}
cases2 := map[string]uint32{
"": 0,
"hello": 613153351,
"foobar": 2764362941,
"ooooooo": 889157580,
"我能吞下玻璃而不伤身体": 3295222404,
}
for key, hash := range cases2 {
h.Write([]byte(key))
if h.Sum32() != hash {
t.Fatal()
}
h.Reset()
if Sum32([]byte(key)) != hash {
t.Fatal()
}
}
// for coverage
if h.BlockSize() != 4 {
t.Fatal()
}
if h.Size() != 4 {
t.Fatal()
}
h.Sum([]byte{'o'})
}
func bench32(b *testing.B, bytes int) {
bs := make([]byte, bytes)
io.ReadFull(rand.Reader, bs)
b.SetBytes(int64(bytes))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Sum32(bs)
}
}
func BenchmarkHash32_1(b *testing.B) { bench32(b, 1) }
func BenchmarkHash32_2(b *testing.B) { bench32(b, 2) }
func BenchmarkHash32_4(b *testing.B) { bench32(b, 4) }
func BenchmarkHash32_8(b *testing.B) { bench32(b, 8) }
func BenchmarkHash32_16(b *testing.B) { bench32(b, 16) }
func BenchmarkHash32_32(b *testing.B) { bench32(b, 32) }
func BenchmarkHash32_64(b *testing.B) { bench32(b, 64) }
func BenchmarkHash32_128(b *testing.B) { bench32(b, 128) }
func BenchmarkHash32_256(b *testing.B) { bench32(b, 256) }
func BenchmarkHash32_512(b *testing.B) { bench32(b, 512) }
func BenchmarkHash32_1024(b *testing.B) { bench32(b, 1024) }
func BenchmarkHash32_2048(b *testing.B) { bench32(b, 2048) }
func BenchmarkHash32_4096(b *testing.B) { bench32(b, 4096) }
func BenchmarkHash32_8192(b *testing.B) { bench32(b, 8192) }