-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbloomfilter_test.go
106 lines (87 loc) · 2.41 KB
/
bloomfilter_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
package bloomfilter
import "testing"
func TestCanMakeBloomFilter(t *testing.T) {
bf := New(100, 2)
if bf == nil {
t.Errorf("bf in nil")
}
}
func TestCanMakeOptimalBloomFilter(t *testing.T) {
items, fr := 300, 0.01
bf := NewFromEstimate(int64(items), fr)
if bf == nil {
t.Errorf("Unable to make new bloom filter")
}
}
func TestCanMakeFilterUnder32(t *testing.T) {
v := "test"
bf := New(10, 2)
bf.Add(v)
if !bf.Check(v) {
t.Error("value was set but not found")
}
}
func TestCanAddToFilter(t *testing.T) {
v := "test"
bf := New(100, 2)
bf.Add(v)
}
func TestCanCheckFilter(t *testing.T) {
v := "test"
bf := New(100, 2)
bf.Add(v)
if !bf.Check(v) {
t.Errorf("value was not found but was set")
}
if bf.Check("not actually set") {
t.Errorf("found value that has not been set")
}
}
func TestCanCheckFilterFromOptimal(t *testing.T) {
v := "test"
items, fr := 300, 0.01
bf := NewFromEstimate(int64(items), fr)
if bf == nil {
t.Errorf("Unable to make new bloom filter")
}
bf.Add(v)
if !bf.Check(v) {
t.Errorf("value was not found but was set")
}
if bf.Check("not actually set") {
t.Errorf("found value that has not been set")
}
}
func TestEstimateOptimalSize(t *testing.T) {
items, fr := 3000000, 0.01
expectedOptimalSize := 28755176
optimalSize := estimateOptimalSize(int64(items), fr)
if optimalSize != int64(expectedOptimalSize) {
t.Errorf("Invalid optimal size. Got %v Exptected %v", optimalSize, expectedOptimalSize)
}
}
func TestEstimateOptimalHashFns(t *testing.T) {
items, fr := int64(3000000), 0.01
expectedOptimalSize := 28755176
expectedHashFns := int64(7)
optimalSize := estimateOptimalSize(items, fr)
if optimalSize != int64(expectedOptimalSize) {
t.Errorf("Invalid optimal size. Got %v Exptected %v", optimalSize, expectedOptimalSize)
}
hashFns := estimateOptimalHashFns(items, optimalSize)
if hashFns != expectedHashFns {
t.Errorf("Invalid optimal hash fns. Got %v expected %v", hashFns, expectedHashFns)
}
}
func TestEstimateOptimalBloomSize(t *testing.T) {
items, fr := int64(3000000), 0.01
expectedOptimalSize := 28755176
expectedHashFns := int64(7)
optimalSize, hashFns := estimateOptimalBloomSize(items, fr)
if optimalSize != int64(expectedOptimalSize) {
t.Errorf("Invalid optimal size. Got %v Exptected %v", optimalSize, expectedOptimalSize)
}
if hashFns != expectedHashFns {
t.Errorf("Invalid optimal hash fns. Got %v expected %v", hashFns, expectedHashFns)
}
}