-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathrollinghash_test.go
281 lines (236 loc) · 6.65 KB
/
rollinghash_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
package rollinghash_test
import (
"bytes"
"hash"
"math/rand"
"testing"
"github.com/chmduquesne/rollinghash"
_adler32 "github.com/chmduquesne/rollinghash/adler32"
"github.com/chmduquesne/rollinghash/bozo32"
"github.com/chmduquesne/rollinghash/buzhash32"
"github.com/chmduquesne/rollinghash/buzhash64"
"github.com/chmduquesne/rollinghash/rabinkarp64"
)
var allHashes = []struct {
name string
classic hash.Hash
rolling rollinghash.Hash
}{
{"adler32", _adler32.New(), _adler32.New()},
{"buzhash32", buzhash32.New(), buzhash32.New()},
{"buzhash64", buzhash64.New(), buzhash64.New()},
{"bozo32", bozo32.New(), bozo32.New()},
{"rabinkarp64", rabinkarp64.New(), rabinkarp64.New()},
}
// Gets the hash sum as a uint64
func sum64(h hash.Hash) (res uint64) {
buf := make([]byte, 0, 8)
s := h.Sum(buf)
for _, b := range s {
res <<= 8
res |= uint64(b)
}
return
}
// Compute the hash by creating a byte slice with an additionnal '\0' at
// the beginning, writing the slice without the last byte, and then
// rolling the last byte.
func SumByWriteAndRoll(h rollinghash.Hash, b []byte) uint64 {
q := []byte("\x00")
q = append(q, b...)
h.Reset()
h.Write(q[:len(q)-1])
h.Roll(q[len(q)-1])
return sum64(h)
}
// Compute the hash the classic way
func SumByWriteOnly(h hash.Hash, b []byte) uint64 {
h.Reset()
h.Write(b)
return sum64(h)
}
// Create some random slice (length betwen 0 and 1KB, random content)
func RandomBytes() (res []byte) {
n := rand.Intn(1024)
res = make([]byte, n)
rand.Read(res)
return res
}
// Verify that, on random inputs, the classic hash and the rollinghash
// return the same values
func blackBox(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
for i := 0; i < 100; i++ {
in := RandomBytes()
if len(in) > 0 {
sum := SumByWriteAndRoll(rolling, in)
ref := SumByWriteOnly(classic, in)
if ref != sum {
t.Errorf("[%s] Expected 0x%x, got 0x%x", hashname, ref, sum)
}
}
}
}
// Roll a window of 16 bytes with a classic hash and a rolling hash and
// compare the results
func foxDog(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
s := []byte("The quick brown fox jumps over the lazy dog")
// Window len
n := 16
// Load the window into the rolling hash
rolling.Write(s[:n])
// Roll it and compare the result with full re-calculus every time
for i := n; i < len(s); i++ {
// Reset and write the window in classic
classic.Reset()
classic.Write(s[i-n+1 : i+1])
// Roll the incoming byte in rolling
rolling.Roll(s[i])
// Compare the hashes
sumc := sum64(classic)
sumr := sum64(rolling)
if sumc != sumr {
t.Errorf("[%s] %v: expected %x, got %x",
hashname, s[i-n+1:i+1], sumc, sumr)
}
}
}
func rollEmptyWindow(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
defer func() {
if r := recover(); r == nil {
t.Errorf("[%s] Rolling an empty window should cause a panic", hashname)
}
}()
// This should panic
rolling.Roll(byte('x'))
}
func writeTwice(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
rolling.Write([]byte("hello "))
rolling.Write([]byte("world"))
classic.Write([]byte("hello world"))
if sum64(rolling) != sum64(classic) {
t.Errorf("[%s] Expected same results on rolling and classic", hashname)
}
}
func writeRollWrite(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
rolling.Write([]byte(" hello"))
rolling.Roll(byte(' '))
rolling.Write([]byte("world"))
classic.Write([]byte("hello world"))
if sum64(rolling) != sum64(classic) {
t.Errorf("[%s] Expected same results on rolling and classic", hashname)
}
}
func writeThenWriteNothing(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
rolling.Write([]byte("hello"))
rolling.Write([]byte(""))
classic.Write([]byte("hello"))
if sum64(rolling) != sum64(classic) {
t.Errorf("[%s] Expected same results on rolling and classic", hashname)
}
}
func writeNothing(t *testing.T, hashname string, classic hash.Hash, rolling rollinghash.Hash) {
rolling.Write([]byte(""))
if sum64(rolling) != sum64(classic) {
t.Errorf("[%s] Expected same results on rolling and classic", hashname)
}
}
func read(t *testing.T, hashname string, rolling rollinghash.Hash) {
rolling.Write([]byte("hello "))
var buf bytes.Buffer
readWindow := func() []byte {
buf.Reset()
rolling.WriteWindow(&buf)
return buf.Bytes()
}
rolling.Roll(byte('w'))
window := readWindow()
expected := "ello w"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
rolling.Roll(byte('o'))
window = readWindow()
expected = "llo wo"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
rolling.Roll(byte('r'))
window = readWindow()
expected = "lo wor"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
rolling.Roll(byte('l'))
window = readWindow()
expected = "o worl"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
rolling.Roll(byte('d'))
window = readWindow()
expected = " world"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
rolling.Roll(byte('!'))
window = readWindow()
expected = "world!"
if string(window) != expected {
t.Errorf("[%s] Expected the window to be '%s'", hashname, expected)
}
}
func TestFoxDog(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
foxDog(t, h.name, h.classic, h.rolling)
}
}
func TestBlackBox(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
blackBox(t, h.name, h.classic, h.rolling)
}
}
func TestRollEmptyWindow(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
rollEmptyWindow(t, h.name, h.classic, h.rolling)
}
}
func TestwriteTwice(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
writeTwice(t, h.name, h.classic, h.rolling)
}
}
func TestwriteRollWrite(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
writeRollWrite(t, h.name, h.classic, h.rolling)
}
}
func TestWriteThenWriteNothing(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
writeThenWriteNothing(t, h.name, h.classic, h.rolling)
}
}
func TestWriteNothing(t *testing.T) {
for _, h := range allHashes {
h.classic.Reset()
h.rolling.Reset()
writeNothing(t, h.name, h.classic, h.rolling)
}
}
func TestRead(t *testing.T) {
for _, h := range allHashes {
h.rolling.Reset()
read(t, h.name, h.rolling)
}
}