-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
bloom_test.go
718 lines (644 loc) · 14.8 KB
/
bloom_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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
package bloom
import (
"fmt"
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"math"
"testing"
"github.com/bits-and-blooms/bitset"
)
// This implementation of Bloom filters is _not_
// safe for concurrent use. Uncomment the following
// method and run go test -race
//
// func TestConcurrent(t *testing.T) {
// gmp := runtime.GOMAXPROCS(2)
// defer runtime.GOMAXPROCS(gmp)
//
// f := New(1000, 4)
// n1 := []byte("Bess")
// n2 := []byte("Jane")
// f.Add(n1)
// f.Add(n2)
//
// var wg sync.WaitGroup
// const try = 1000
// var err1, err2 error
//
// wg.Add(1)
// go func() {
// for i := 0; i < try; i++ {
// n1b := f.Test(n1)
// if !n1b {
// err1 = fmt.Errorf("%v should be in", n1)
// break
// }
// }
// wg.Done()
// }()
//
// wg.Add(1)
// go func() {
// for i := 0; i < try; i++ {
// n2b := f.Test(n2)
// if !n2b {
// err2 = fmt.Errorf("%v should be in", n2)
// break
// }
// }
// wg.Done()
// }()
//
// wg.Wait()
//
// if err1 != nil {
// t.Fatal(err1)
// }
// if err2 != nil {
// t.Fatal(err2)
// }
// }
func TestBasic(t *testing.T) {
f := New(1000, 4)
n1 := []byte("Bess")
n2 := []byte("Jane")
n3 := []byte("Emma")
f.Add(n1)
n3a := f.TestAndAdd(n3)
n1b := f.Test(n1)
n2b := f.Test(n2)
n3b := f.Test(n3)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
}
func TestBasicUint32(t *testing.T) {
f := New(1000, 4)
n1 := make([]byte, 4)
n2 := make([]byte, 4)
n3 := make([]byte, 4)
n4 := make([]byte, 4)
n5 := make([]byte, 4)
binary.BigEndian.PutUint32(n1, 100)
binary.BigEndian.PutUint32(n2, 101)
binary.BigEndian.PutUint32(n3, 102)
binary.BigEndian.PutUint32(n4, 103)
binary.BigEndian.PutUint32(n5, 104)
f.Add(n1)
n3a := f.TestAndAdd(n3)
n1b := f.Test(n1)
n2b := f.Test(n2)
n3b := f.Test(n3)
n5a := f.TestOrAdd(n5)
n5b := f.Test(n5)
f.Test(n4)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
if n5a {
t.Errorf("%v should not be in the first time we look.", n5)
}
if !n5b {
t.Errorf("%v should be in the second time we look.", n5)
}
}
func TestNewWithLowNumbers(t *testing.T) {
f := New(0, 0)
if f.k != 1 {
t.Errorf("%v should be 1", f.k)
}
if f.m != 1 {
t.Errorf("%v should be 1", f.m)
}
}
func TestString(t *testing.T) {
f := NewWithEstimates(1000, 0.001)
n1 := "Love"
n2 := "is"
n3 := "in"
n4 := "bloom"
n5 := "blooms"
f.AddString(n1)
n3a := f.TestAndAddString(n3)
n1b := f.TestString(n1)
n2b := f.TestString(n2)
n3b := f.TestString(n3)
n5a := f.TestOrAddString(n5)
n5b := f.TestString(n5)
f.TestString(n4)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
if n5a {
t.Errorf("%v should not be in the first time we look.", n5)
}
if !n5b {
t.Errorf("%v should be in the second time we look.", n5)
}
}
func testEstimated(n uint, maxFp float64, t *testing.T) {
m, k := EstimateParameters(n, maxFp)
fpRate := EstimateFalsePositiveRate(m, k, n)
if fpRate > 1.5*maxFp {
t.Errorf("False positive rate too high: n: %v; m: %v; k: %v; maxFp: %f; fpRate: %f, fpRate/maxFp: %f", n, m, k, maxFp, fpRate, fpRate/maxFp)
}
}
func TestEstimated1000_0001(t *testing.T) { testEstimated(1000, 0.000100, t) }
func TestEstimated10000_0001(t *testing.T) { testEstimated(10000, 0.000100, t) }
func TestEstimated100000_0001(t *testing.T) { testEstimated(100000, 0.000100, t) }
func TestEstimated1000_001(t *testing.T) { testEstimated(1000, 0.001000, t) }
func TestEstimated10000_001(t *testing.T) { testEstimated(10000, 0.001000, t) }
func TestEstimated100000_001(t *testing.T) { testEstimated(100000, 0.001000, t) }
func TestEstimated1000_01(t *testing.T) { testEstimated(1000, 0.010000, t) }
func TestEstimated10000_01(t *testing.T) { testEstimated(10000, 0.010000, t) }
func TestEstimated100000_01(t *testing.T) { testEstimated(100000, 0.010000, t) }
func min(a, b uint) uint {
if a < b {
return a
}
return b
}
// The following function courtesy of Nick @turgon
// This helper function ranges over the input data, applying the hashing
// which returns the bit locations to set in the filter.
// For each location, increment a counter for that bit address.
//
// If the Bloom Filter's location() method distributes locations uniformly
// at random, a property it should inherit from its hash function, then
// each bit location in the filter should end up with roughly the same
// number of hits. Importantly, the value of k should not matter.
//
// Once the results are collected, we can run a chi squared goodness of fit
// test, comparing the result histogram with the uniform distribition.
// This yields a test statistic with degrees-of-freedom of m-1.
func chiTestBloom(m, k, rounds uint, elements [][]byte) (succeeds bool) {
f := New(m, k)
results := make([]uint, m)
chi := make([]float64, m)
for _, data := range elements {
h := baseHashes(data)
for i := uint(0); i < f.k; i++ {
results[f.location(h, i)]++
}
}
// Each element of results should contain the same value: k * rounds / m.
// Let's run a chi-square goodness of fit and see how it fares.
var chiStatistic float64
e := float64(k*rounds) / float64(m)
for i := uint(0); i < m; i++ {
chi[i] = math.Pow(float64(results[i])-e, 2.0) / e
chiStatistic += chi[i]
}
// this tests at significant level 0.005 up to 20 degrees of freedom
table := [20]float64{
7.879, 10.597, 12.838, 14.86, 16.75, 18.548, 20.278,
21.955, 23.589, 25.188, 26.757, 28.3, 29.819, 31.319, 32.801, 34.267,
35.718, 37.156, 38.582, 39.997}
df := min(m-1, 20)
succeeds = table[df-1] > chiStatistic
return
}
func TestLocation(t *testing.T) {
var m, k, rounds uint
m = 8
k = 3
rounds = 100000 // 15000000
elements := make([][]byte, rounds)
for x := uint(0); x < rounds; x++ {
ctrlist := make([]uint8, 4)
ctrlist[0] = uint8(x)
ctrlist[1] = uint8(x >> 8)
ctrlist[2] = uint8(x >> 16)
ctrlist[3] = uint8(x >> 24)
data := []byte(ctrlist)
elements[x] = data
}
succeeds := chiTestBloom(m, k, rounds, elements)
if !succeeds {
t.Error("random assignment is too unrandom")
}
}
func TestCap(t *testing.T) {
f := New(1000, 4)
if f.Cap() != f.m {
t.Error("not accessing Cap() correctly")
}
}
func TestK(t *testing.T) {
f := New(1000, 4)
if f.K() != f.k {
t.Error("not accessing K() correctly")
}
}
func TestMarshalUnmarshalJSON(t *testing.T) {
f := New(1000, 4)
data, err := json.Marshal(f)
if err != nil {
t.Fatal(err.Error())
}
fmt.Println(string(data))
var g BloomFilter
err = json.Unmarshal(data, &g)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
}
func TestMarshalUnmarshalJSONValue(t *testing.T) {
f:= BloomFilter{1000, 4, bitset.New(1000)}
data, err := json.Marshal(f)
if err != nil {
t.Fatal(err.Error())
}
fmt.Println(string(data))
var g BloomFilter
err = json.Unmarshal(data, &g)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
}
func TestUnmarshalInvalidJSON(t *testing.T) {
data := []byte("{invalid}")
var g BloomFilter
err := g.UnmarshalJSON(data)
if err == nil {
t.Error("expected error while unmarshalling invalid data")
}
}
func TestWriteToReadFrom(t *testing.T) {
var b bytes.Buffer
f := New(1000, 4)
_, err := f.WriteTo(&b)
if err != nil {
t.Fatal(err)
}
g := New(1000, 1)
_, err = g.ReadFrom(&b)
if err != nil {
t.Fatal(err)
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
g.Test([]byte(""))
}
func TestReadWriteBinary(t *testing.T) {
f := New(1000, 4)
var buf bytes.Buffer
bytesWritten, err := f.WriteTo(&buf)
if err != nil {
t.Fatal(err.Error())
}
if bytesWritten != int64(buf.Len()) {
t.Errorf("incorrect write length %d != %d", bytesWritten, buf.Len())
}
var g BloomFilter
bytesRead, err := g.ReadFrom(&buf)
if err != nil {
t.Fatal(err.Error())
}
if bytesRead != bytesWritten {
t.Errorf("read unexpected number of bytes %d != %d", bytesRead, bytesWritten)
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
}
func TestEncodeDecodeGob(t *testing.T) {
f := New(1000, 4)
f.Add([]byte("one"))
f.Add([]byte("two"))
f.Add([]byte("three"))
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(f)
if err != nil {
t.Fatal(err.Error())
}
var g BloomFilter
err = gob.NewDecoder(&buf).Decode(&g)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
if !g.Test([]byte("three")) {
t.Errorf("missing value 'three'")
}
if !g.Test([]byte("two")) {
t.Errorf("missing value 'two'")
}
if !g.Test([]byte("one")) {
t.Errorf("missing value 'one'")
}
}
func TestEqual(t *testing.T) {
f := New(1000, 4)
f1 := New(1000, 4)
g := New(1000, 20)
h := New(10, 20)
n1 := []byte("Bess")
f1.Add(n1)
if !f.Equal(f) {
t.Errorf("%v should be equal to itself", f)
}
if f.Equal(f1) {
t.Errorf("%v should not be equal to %v", f, f1)
}
if f.Equal(g) {
t.Errorf("%v should not be equal to %v", f, g)
}
if f.Equal(h) {
t.Errorf("%v should not be equal to %v", f, h)
}
}
func BenchmarkEstimated(b *testing.B) {
for n := uint(100000); n <= 100000; n *= 10 {
for fp := 0.1; fp >= 0.0001; fp /= 10.0 {
f := NewWithEstimates(n, fp)
EstimateFalsePositiveRate(f.m, f.k, n)
}
}
}
func BenchmarkSeparateTestAndAdd(b *testing.B) {
f := NewWithEstimates(uint(b.N), 0.0001)
key := make([]byte, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint32(key, uint32(i))
f.Test(key)
f.Add(key)
}
}
func BenchmarkCombinedTestAndAdd(b *testing.B) {
f := NewWithEstimates(uint(b.N), 0.0001)
key := make([]byte, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint32(key, uint32(i))
f.TestAndAdd(key)
}
}
func TestMerge(t *testing.T) {
f := New(1000, 4)
n1 := []byte("f")
f.Add(n1)
g := New(1000, 4)
n2 := []byte("g")
g.Add(n2)
h := New(999, 4)
n3 := []byte("h")
h.Add(n3)
j := New(1000, 5)
n4 := []byte("j")
j.Add(n4)
err := f.Merge(g)
if err != nil {
t.Errorf("There should be no error when merging two similar filters")
}
err = f.Merge(h)
if err == nil {
t.Errorf("There should be an error when merging filters with mismatched m")
}
err = f.Merge(j)
if err == nil {
t.Errorf("There should be an error when merging filters with mismatched k")
}
n2b := f.Test(n2)
if !n2b {
t.Errorf("The value doesn't exist after a valid merge")
}
n3b := f.Test(n3)
if n3b {
t.Errorf("The value exists after an invalid merge")
}
n4b := f.Test(n4)
if n4b {
t.Errorf("The value exists after an invalid merge")
}
}
func TestCopy(t *testing.T) {
f := New(1000, 4)
n1 := []byte("f")
f.Add(n1)
// copy here instead of New
g := f.Copy()
n2 := []byte("g")
g.Add(n2)
n1fb := f.Test(n1)
if !n1fb {
t.Errorf("The value doesn't exist in original after making a copy")
}
n1gb := g.Test(n1)
if !n1gb {
t.Errorf("The value doesn't exist in the copy")
}
n2fb := f.Test(n2)
if n2fb {
t.Errorf("The value exists in the original, it should only exist in copy")
}
n2gb := g.Test(n2)
if !n2gb {
t.Errorf("The value doesn't exist in copy after Add()")
}
}
func TestFrom(t *testing.T) {
var (
k = uint(5)
data = make([]uint64, 10)
test = []byte("test")
)
bf := From(data, k)
if bf.K() != k {
t.Errorf("Constant k does not match the expected value")
}
if bf.Cap() != uint(len(data)*64) {
t.Errorf("Capacity does not match the expected value")
}
if bf.Test(test) {
t.Errorf("Bloom filter should not contain the value")
}
bf.Add(test)
if !bf.Test(test) {
t.Errorf("Bloom filter should contain the value")
}
// create a new Bloom filter from an existing (populated) data slice.
bf = From(data, k)
if !bf.Test(test) {
t.Errorf("Bloom filter should contain the value")
}
}
func TestTestLocations(t *testing.T) {
f := NewWithEstimates(1000, 0.001)
n1 := []byte("Love")
n2 := []byte("is")
n3 := []byte("in")
n4 := []byte("bloom")
f.Add(n1)
n3a := f.TestLocations(Locations(n3, f.K()))
f.Add(n3)
n1b := f.TestLocations(Locations(n1, f.K()))
n2b := f.TestLocations(Locations(n2, f.K()))
n3b := f.TestLocations(Locations(n3, f.K()))
n4b := f.TestLocations(Locations(n4, f.K()))
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
if n4b {
t.Errorf("%v should be in.", n4)
}
}
func TestApproximatedSize(t *testing.T) {
f := NewWithEstimates(1000, 0.001)
f.Add([]byte("Love"))
f.Add([]byte("is"))
f.Add([]byte("in"))
f.Add([]byte("bloom"))
size := f.ApproximatedSize()
if size != 4 {
t.Errorf("%d should equal 4.", size)
}
}
func TestFPP(t *testing.T) {
f := NewWithEstimates(1000, 0.001)
for i := uint32(0); i < 1000; i++ {
n := make([]byte, 4)
binary.BigEndian.PutUint32(n, i)
f.Add(n)
}
count := 0
for i := uint32(0); i < 1000; i++ {
n := make([]byte, 4)
binary.BigEndian.PutUint32(n, i+1000)
if f.Test(n) {
count += 1
}
}
if float64(count)/1000.0 > 0.001 {
t.Errorf("Excessive fpp")
}
}
func TestEncodeDecodeBinary(t *testing.T) {
f := New(1000, 4)
f.Add([]byte("one"))
f.Add([]byte("two"))
f.Add([]byte("three"))
data, err := f.MarshalBinary()
if err != nil {
t.Fatal(err.Error())
}
var g BloomFilter
err = g.UnmarshalBinary(data)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
if !g.Test([]byte("three")) {
t.Errorf("missing value 'three'")
}
if !g.Test([]byte("two")) {
t.Errorf("missing value 'two'")
}
if !g.Test([]byte("one")) {
t.Errorf("missing value 'one'")
}
}