/
sbloom_test.go
58 lines (49 loc) · 1.23 KB
/
sbloom_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
package sbloom
import (
"hash/fnv"
"testing"
)
func TestFilterFalsePositive(t *testing.T) {
const (
prob = 1
items = 15
)
f := NewFilter(fnv.New64(), prob)
for i := 0; i < 1<<items; i++ {
f.Add(randSeed())
}
//query the same number of times
//expect around 1 << 14
var found int
for i := 0; i < 1<<items; i++ {
if f.Lookup(randSeed()) {
found++
}
}
if exp := 1 << (items - prob); found > exp {
t.Fatalf("Found too many items. Expected %d <= %d", found, exp)
}
}
func TestFilterFalseNegative(t *testing.T) {
f := NewFilter(fnv.New64(), 20) // really hard to get false positive
const items = 10000
for i := 0; i < items; i++ {
p := randSeed()
f.Add(p)
if !f.Lookup(p) {
t.Fatalf("Failed to find %v on iteration %d", p, i)
}
}
}
func BenchmarkFnvSFilterAdd5(b *testing.B) { benchmarkFnvSFilterAdd(b, 5) }
func BenchmarkFnvSFilterAdd10(b *testing.B) { benchmarkFnvSFilterAdd(b, 10) }
func BenchmarkFnvSFilterAdd15(b *testing.B) { benchmarkFnvSFilterAdd(b, 15) }
func BenchmarkFnvSFilterAdd20(b *testing.B) { benchmarkFnvSFilterAdd(b, 20) }
func benchmarkFnvSFilterAdd(b *testing.B, k int) {
f := NewFilter(fnv.New64(), k)
b.ResetTimer()
for i := 0; i < b.N; i++ {
f.Add(randSeed())
b.SetBytes(10)
}
}