-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
73 lines (65 loc) · 1.6 KB
/
filter_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
package filter
import (
"fmt"
"testing"
)
func TestNew(t *testing.T) {
s := uint(1000)
b := New(s)
l := uint(len(b.bits))
if l != s {
t.Errorf("Number of bits was incorrect. Got: %d, want: %d", l, s)
}
if b.n != 0 {
t.Errorf("Number of items should be 0, got: %d", b.n)
}
if len(b.hashFuncs) < 1 {
t.Errorf("Invalid number of hash functions. Should contain at least one, got: %d", len(b.hashFuncs))
}
}
func TestAdd(t *testing.T) {
b := New(1000)
b.Add([]byte("potato"))
if b.n != 1 {
t.Errorf("Number of items was incorrect. Got: %d, want %d", b.n, 1)
}
b.Add([]byte("another potato"))
if b.n != 2 {
t.Errorf("Number of items was incorrect. Got: %d, want %d", b.n, 2)
}
for i := 0; i < 200; i++ {
b.Add([]byte(string(i)))
}
if b.n != 202 {
t.Errorf("Number of items was incorrect. Got: %d, want %d", b.n, 202)
}
}
func TestLookup(t *testing.T) {
n := uint(10000000)
c := n / 256
b := New(n)
b.Add([]byte("potato"))
if !b.Lookup([]byte("potato")) {
t.Errorf("False negative when searching for %s.", "potato")
}
if b.Lookup([]byte("llama")) {
t.Errorf("False positive when searching for %s.", "llama")
}
for i := uint(0); i < c; i++ {
b.Add([]byte(string(i)))
}
fp := uint(0)
for i := uint(0); i < c; i++ {
if !b.Lookup([]byte(string(i))) {
t.Errorf("False negative when searching for %d.", i)
}
if b.Lookup([]byte(string(n - i))) {
fp++
fmt.Println("False Positive:", n-i)
}
}
fmt.Printf("Size of Filter: %d\n"+
"Number of items: %d\n"+
"Number of False Positives: %d\n"+
"Percent False Positives: %f%%\n", n, b.n, fp, float64(fp)*100/float64(n/2))
}