-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloomer_test.go
63 lines (53 loc) · 1.12 KB
/
bloomer_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
package bloomer
import (
"math"
"testing"
"crypto/rand"
)
func TestBasic(t *testing.T) {
N := int(1e4)
E := 1e-3
b := NewSuggested(N, E)
// generate and add random keys
keys := make([][]byte, N)
for i := range keys {
keys[i] = make([]byte, 10)
rand.Read(keys[i])
b.Add(keys[i])
if !b.Test(keys[i]) {
t.Error("uh oh, bloom filter is busted")
t.FailNow()
}
}
// generate and test missing random keys
key := make([]byte, 9)
falsePos := 0.0
for i := 0; i < int(N); i++ {
rand.Read(key)
if b.Test(key) {
falsePos++
}
}
// memory usage and false positive rate
sizeBytes := float64(len(b.field)) / math.Pow(2, 20)
falseRate := falsePos / float64(N)
if sizeBytes > 2 {
t.Errorf("Used too much memory: %.2fMB\n", sizeBytes)
}
// this is probabilistic... so give it some room
if falseRate > (E * 1.5) {
t.Errorf("False positive rate too high: %.5f\n", falseRate)
}
}
func BenchmarkAdd(b *testing.B) {
f := NewSuggested(b.N, 1e-3)
keys := make([][]byte, b.N)
for i := range keys {
keys[i] = make([]byte, 10)
rand.Read(keys[i])
}
b.ResetTimer()
for _, key := range keys {
f.Add(key)
}
}