-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomfilter_test.go
104 lines (72 loc) · 1.55 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
package inbloom
import (
"bytes"
"crypto/rand"
"testing"
)
func TestAddToFilter(t *testing.T) {
data := bytes.NewBufferString("rhinof is on the moo").Bytes()
filter, _ := NewFilter(0.1, 10000)
err := filter.Add(&data)
if err != nil {
t.Fail()
}
}
func TestNotInFilterReturnsFalse(t *testing.T) {
data := bytes.NewBufferString("rhinof is on the moo").Bytes()
data1 := bytes.NewBufferString("shama is the king").Bytes()
filter, _ := NewFilter(0.1, 10000)
err := filter.Add(&data1)
if err != nil {
t.Fail()
}
result, _ := filter.Test(&data)
if result == true {
t.Fail()
}
}
func TestDataIsInFilterReturnsTrue(t *testing.T) {
data := bytes.NewBufferString("rhinof is on the moo").Bytes()
filter, e := NewFilter(0.1, 1000000000)
if e != nil {
t.Errorf("%s", e)
t.FailNow()
}
err := filter.Add(&data)
if err != nil {
t.Fail()
}
result, _ := filter.Test(&data)
if result == false {
t.Fail()
}
}
func TestPoFPInRange(t *testing.T) {
n := 100000
p := 0.01
filter, _ := NewFilter(p, n)
i := 0
for filter.PoFP() <= p {
data := make([]byte, 30)
rand.Read(data)
filter.Add(&data)
i++
}
if float64(n)*float64(0.9) > float64(i) {
t.Fatal("filter error rate wasn't in acceptable range")
}
}
func BenchmarkFilter(b *testing.B) {
filter, _ := NewFilter(0.01, 100000)
for i := 0; i < 100000; i++ {
data := bytes.NewBufferString("rhinof is on the moo" + string(i)).Bytes()
err := filter.Add(&data)
if err != nil {
b.Fail()
}
result, _ := filter.Test(&data)
if result == false {
b.Fail()
}
}
}