-
Notifications
You must be signed in to change notification settings - Fork 4
/
cuckoo_test.go
388 lines (329 loc) · 5.78 KB
/
cuckoo_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package cuckoo
import (
"bufio"
"bytes"
"fmt"
"os"
"reflect"
"testing"
"github.com/spaolacci/murmur3"
)
func Test_fingerprintOf(t *testing.T) {
tests := []struct {
b []byte
r fingerprint
h uint32
}{
{
b: []byte("hello"),
r: 26725,
h: 19595036,
},
{
b: []byte("12345"),
r: 12594,
h: 19870548,
},
}
h := murmur3.New32WithSeed(1234)
for _, c := range tests {
fp := fingerprintOf(c.b)
fph := fingerprintHash(fp, h)
if c.r != fp {
t.Fatalf("expected %v bytes but got %v", c.r, fp)
}
if fph != c.h {
t.Fatalf("expected %d hash but got %d", c.h, fph)
}
}
}
func Test_addToBucket(t *testing.T) {
tests := []struct {
i uint64
fp fingerprint
r bool
}{
{
i: 100,
fp: 123,
r: true,
},
{
i: 120,
fp: 2345,
r: true,
},
{
i: 156400,
fp: 1223,
r: true,
},
}
f := StdFilter()
for _, c := range tests {
r := addToBucket(&f.buckets[c.i], f.bucketSize, c.fp)
if r != c.r {
t.Fatalf("expected %t but got %t", c.r, r)
}
}
}
func TestFilter_Insert(t *testing.T) {
tests := []struct {
item string
count uint32
}{
{
item: "hello",
count: 1,
},
{
item: "hello, World",
count: 2,
},
{
item: "hello, World",
count: 3,
},
}
f := StdFilter()
for _, c := range tests {
f.Insert([]byte(c.item))
if f.Count() != c.count {
t.Fatalf("expected %d count but got %d", c.count, f.Count())
}
}
}
func TestFilter_Exists(t *testing.T) {
f := StdFilter()
for _, s := range []string{"hello", "hello, World", "This Worked"} {
f.Insert([]byte(s))
}
tests := []struct {
item string
exist bool
}{
{
item: "hello",
exist: true,
},
{
item: "hello, World",
exist: true,
},
{
item: "This is test11",
},
{
item: "This Worked",
exist: true,
},
}
for _, c := range tests {
ok := f.Lookup([]byte(c.item))
if ok != c.exist {
t.Fatalf("extected %s item to give %t but gave %t", c.item, c.exist, ok)
}
}
}
func TestFilter_Delete(t *testing.T) {
f := StdFilter()
for _, s := range []string{"hello", "hello, World", "This Worked"} {
f.Insert([]byte(s))
}
tests := []struct {
item string
ok bool
count uint32
}{
{
item: "hello",
ok: true,
count: 2,
},
{
item: "hello, World",
ok: true,
count: 1,
},
{
item: "This is test",
count: 1,
},
{
item: "This Worked",
ok: true,
count: 0,
},
}
for _, c := range tests {
ok := f.Delete([]byte(c.item))
if ok != c.ok {
t.Fatalf("extected %s item to give %t but gave %t", c.item, c.ok, ok)
}
if f.Count() != c.count {
t.Fatalf("expected %d count but got %d", c.count, f.Count())
}
}
}
func TestFilter_EncodeDecode(t *testing.T) {
f := StdFilter()
data := []string{"hello", "hello, World", "This Worked"}
for _, s := range data {
f.Insert([]byte(s))
}
var b bytes.Buffer
err := f.Encode(&b)
if err != nil {
t.Fatalf("unexpected error while encoding: %v", err)
}
df, err := Decode(&b)
if err != nil {
t.Fatalf("unexpected error while decoding: %v", err)
}
if f.count != df.count {
t.Fatalf("count mismatch")
}
if f.bucketSize != df.bucketSize {
t.Fatalf("bucketsize mismatch")
}
if f.totalBuckets != df.totalBuckets {
t.Fatalf("totalbuckets mismatch")
}
if f.maxKicks != df.maxKicks {
t.Fatalf("maxkicks mismatch")
}
if !reflect.DeepEqual(f.buckets, df.buckets) {
t.Fatalf("buckets mismatch")
}
for _, s := range data {
if !df.Lookup([]byte(s)) {
t.Fatalf("lookup failed: %s", s)
}
}
}
func Test_nextPowerOf2(t *testing.T) {
tests := []struct {
v uint32
e uint32
}{
{
v: 1,
e: 4,
},
{
v: 2,
e: 4,
},
{
v: 3,
e: 4,
},
{
v: 4,
e: 4,
},
{
v: 100,
e: 128,
},
}
for _, c := range tests {
g := nextPowerOf2(c.v)
if g != c.e {
t.Fatalf("expected %d but got %d", c.e, g)
}
}
}
// Benchmark tests taken from https://github.com/mtchavez/cuckoo
var filter *Filter
var okay bool
func BenchmarkCuckooNew(b *testing.B) {
b.ReportAllocs()
var f *Filter
for i := 0; i < b.N; i++ {
f = StdFilter()
}
filter = f
}
func BenchmarkInsert(b *testing.B) {
var ok bool
filter := StdFilter()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok = filter.Insert([]byte(fmt.Sprintf("item-%d", i%50000)))
}
okay = ok
}
func BenchmarkInsertUnique(b *testing.B) {
var ok bool
filter := StdFilter()
fd, _ := os.Open("/usr/share/dict/words")
defer fd.Close()
scanner := bufio.NewScanner(fd)
var wordCount int
var totalWords int
var values [][]byte
for scanner.Scan() {
word := []byte(scanner.Text())
totalWords++
if filter.Insert(word) {
wordCount++
}
values = append(values, word)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok = filter.InsertUnique(values[i%totalWords])
}
okay = ok
}
func BenchmarkLookup(b *testing.B) {
var ok bool
filter := StdFilter()
fd, _ := os.Open("/usr/share/dict/words")
defer fd.Close()
scanner := bufio.NewScanner(fd)
var wordCount int
var totalWords int
var values [][]byte
for scanner.Scan() {
word := []byte(scanner.Text())
totalWords++
if filter.Insert(word) {
wordCount++
}
values = append(values, word)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok = filter.Lookup(values[i%totalWords])
}
okay = ok
}
func BenchmarkDelete(b *testing.B) {
var ok bool
filter := StdFilter()
fd, _ := os.Open("/usr/share/dict/words")
defer fd.Close()
scanner := bufio.NewScanner(fd)
var wordCount int
var totalWords int
var values [][]byte
for scanner.Scan() {
word := []byte(scanner.Text())
totalWords++
if filter.Insert(word) {
wordCount++
}
values = append(values, word)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ok = filter.Delete(values[i%totalWords])
}
okay = ok
}