-
Notifications
You must be signed in to change notification settings - Fork 5
/
xxhash_test.go
199 lines (175 loc) · 4.16 KB
/
xxhash_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
//stollen from bitbucket.org/StephaneBunel/xxhash-go
package xxhash
import (
"encoding/binary"
"hash/adler32"
"hash/crc32"
"hash/fnv"
"testing"
)
var (
blob1 = []byte("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ")
blob2 = []byte("sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.")
blob3 = []byte("Cookies")
blob4 = []byte("1234567890123456")
VeryBigFile = "a-very-big-file"
)
func Test_Checksum32(t *testing.T) {
h32 := Checksum32(blob1)
if h32 != 0x1130e7d4 {
t.Errorf("Checksum32(\"%v\") = 0x%08x need 0x1130e7d4\n", string(blob1), h32)
}
h32 = Checksum32(blob2)
if h32 != 0x24ca2992 {
t.Errorf("Checksum32(\"%v\") = 0x%08x need 0x24ca2992\n", string(blob2), h32)
}
h32 = Checksum32(blob3)
if h32 != 0x99dd2ca5 {
t.Errorf("Checksum32(\"%v\") = 0x%08x need 0x99dd2ca5\n", string(blob3), h32)
}
h32 = Checksum32(blob4)
if h32 != 0x03bf5152 {
t.Errorf("Checksum32(\"%v\") = 0x%08x need 0x03bf5152\n", string(blob4), h32)
}
}
func Test_Checksum32Seed(t *testing.T) {
h32 := Checksum32Seed(blob1, 1471)
if h32 != 0xba59a258 {
t.Errorf("Checksum32Seed(\"%v\", 1471) = 0x%08x\n need 0xba59a258", string(blob1), h32)
}
h32 = Checksum32Seed(blob2, 1596234)
if h32 != 0xf15f3e02 {
t.Errorf("Checksum32Seed(\"%v\", 1596234) = 0x%08x need 0xf15f3e02\n", string(blob2), h32)
}
h32 = Checksum32Seed(blob3, 9999666)
if h32 != 0xcd3ae44c {
t.Errorf("Checksum32Seed(\"%v\", 9999666) = 0x%08x need 0xcd3ae44c\n", string(blob3), h32)
}
h32 = Checksum32Seed(blob4, 1)
if h32 != 0x606913c4 {
t.Errorf("Checksum32Seed(\"%v\", 1) = 0x%08x need 0x606913c4\n", string(blob4), h32)
}
}
func Test_New32(t *testing.T) {
var digest = New(0)
digest.Write(blob1)
digest.Write(blob2)
h32 := digest.Sum32()
if h32 != 0x0d44373a {
t.Errorf("Sum32 = 0x%08x need 0x0d44373a\n", h32)
}
digest = New(0)
digest.Write(blob3)
h32 = digest.Sum32()
if h32 != 0x99dd2ca5 {
t.Errorf("Sum32 = 0x%08x need 0x99dd2ca5\n", h32)
}
digest = New(0)
digest.Write(blob4)
h32 = digest.Sum32()
if h32 != 0x3bf5152 {
t.Errorf("Sum32 = 0x%08x need 0x3bf5152\n", h32)
}
}
func Test_New32Seed(t *testing.T) {
var digest = New(1471)
digest.Write(blob1)
digest.Write(blob2)
h32 := digest.Sum32()
if h32 != 0x3265e220 {
t.Errorf("Sum32 = 0x%08x need 0x3265e220\n", h32)
}
digest = New(615324687)
digest.Write(blob3)
h32 = digest.Sum32()
if h32 != 0xb90e95cb {
t.Errorf("Sum32 = 0x%08x need 0x89f56371\n", h32)
}
digest = New(1)
digest.Write(blob4)
h32 = digest.Sum32()
if h32 != 0x606913c4 {
t.Errorf("Sum32 = 0x%08x need 0x606913c4\n", h32)
}
}
func Test_Reset(t *testing.T) {
var digest = New(0)
digest.Write(blob2)
digest.Reset()
digest.Write(blob1)
h32 := digest.Sum32()
if h32 != 0x1130e7d4 {
t.Errorf("Sum32 = 0x%08x need 0x1130e7d4\n", h32)
}
}
func Benchmark_xxhash32(b *testing.B) {
for i := 0; i < b.N; i++ {
Checksum32(blob1)
}
}
func Benchmark_CRC32IEEE(b *testing.B) {
for i := 0; i < b.N; i++ {
crc32.ChecksumIEEE(blob1)
}
}
func Benchmark_Adler32(b *testing.B) {
for i := 0; i < b.N; i++ {
adler32.Checksum(blob1)
}
}
func Benchmark_Fnv32(b *testing.B) {
h := fnv.New32()
for i := 0; i < b.N; i++ {
h.Sum(blob1)
}
}
func Benchmark_MurmurHash3Hash32(b *testing.B) {
for i := 0; i < b.N; i++ {
mmh3Hash32(blob1)
}
}
// MurmurHash 3
// mmh3.Hash32 stollen from https://github.com/reusee/mmh3
func mmh3Hash32(key []byte) uint32 {
length := len(key)
if length == 0 {
return 0
}
var c1, c2 uint32 = 0xcc9e2d51, 0x1b873593
nblocks := length / 4
var h, k uint32
buf := key
for i := 0; i < nblocks; i++ {
k = binary.LittleEndian.Uint32(buf)
buf = buf[4:]
k *= c1
k = (k << 15) | (k >> (32 - 15))
k *= c2
h ^= k
h = (h << 13) | (h >> (32 - 13))
h = (h * 5) + 0xe6546b64
}
k = 0
tailIndex := nblocks * 4
switch length & 3 {
case 3:
k ^= uint32(key[tailIndex+2]) << 16
fallthrough
case 2:
k ^= uint32(key[tailIndex+1]) << 8
fallthrough
case 1:
k ^= uint32(key[tailIndex])
k *= c1
k = (k << 13) | (k >> (32 - 15))
k *= c2
h ^= k
}
h ^= uint32(length)
h ^= h >> 16
h *= 0x85ebca6b
h ^= h >> 13
h *= 0xc2b2ae35
h ^= h >> 16
return h
}