-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloomfilter_test.go
149 lines (120 loc) · 3 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package bloomflt
import (
"fmt"
"math"
"testing"
)
func TestNewMK(t *testing.T) {
b := NewMK(64, 2)
if b == nil {
t.Errorf("NewMK(64, 2) = nil, expected a struct")
}
}
func TestCalcOptimalMK(t *testing.T) {
gotM, gotK := CalcOptimalMK(216553, 0.01)
wantM := 2075673
if gotM != wantM {
t.Errorf("CalcOptimalMK(216553, 0.01) = %v, want %v", gotM, wantM)
}
wantK := 7
if gotK != wantK {
t.Errorf("CalcOptimalMK(216553, 0.01) = %v, want %v", gotK, wantK)
}
}
func TestHighFalsePositiveRate(t *testing.T) {
b := New(100, 0.01)
ok := b.ContainsString("SomeValue")
if ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", "SomeValue", ok, false)
}
b.AddString("SomeValue")
b.AddString("AnotherValue")
ok = b.ContainsString("SomeValue")
if !ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", "SomeValue", ok, true)
}
ok = b.ContainsString("AnotherValue")
if !ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", "AnotherValue", ok, true)
}
}
func TestLowFalsePositiveRate(t *testing.T) {
b := New(1000, 0.00001)
value := "AnotherValue"
ok := b.ContainsString(value)
if ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, false)
}
b.AddString(value)
ok = b.ContainsString(value)
if !ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, true)
}
}
func TestMinimal(t *testing.T) {
b := New(0, 0.01)
value := "AnotherValue"
ok := b.ContainsString(value)
if ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, false)
}
b.AddString(value)
ok = b.ContainsString(value)
if !ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, true)
}
}
func TestMaximum(t *testing.T) {
b := New(math.MaxInt32, 0.001)
value := "AnotherValue"
ok := b.ContainsString(value)
if ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, false)
}
b.AddString(value)
ok = b.ContainsString(value)
if !ok {
t.Errorf("b.ContainsString(%q) = %v, want %v", value, ok, true)
}
}
func TestUInt32(t *testing.T) {
b := New(100, 0.01)
value := uint32(math.MaxUint32 / 2)
ok := b.ContainsUInt32(value)
if ok {
t.Errorf("b.ContainsString(%v) = %v, want %v", value, ok, false)
}
b.AddUInt32(value)
ok = b.ContainsUInt32(value)
if !ok {
t.Errorf("b.ContainsString(%v) = %v, want %v", value, ok, true)
}
}
func TestUInt64(t *testing.T) {
b := New(100, 0.01)
value := uint64(math.MaxUint64 / 2)
ok := b.ContainsUInt64(value)
if ok {
t.Errorf("b.ContainsString(%v) = %v, want %v", value, ok, false)
}
b.AddUInt64(value)
ok = b.ContainsUInt64(value)
if !ok {
t.Errorf("b.ContainsString(%v) = %v, want %v", value, ok, true)
}
}
func Example() {
// We expect the set to contains up to 100 elements with acceptable false-positive rate of 0.01%
b := New(100, 0.01)
b.AddString("value1")
if b.ContainsString("value1") {
fmt.Println("The set now has 'value1'.")
}
someID := uint64(123)
b.AddUInt64(someID)
if b.ContainsUInt64(someID) {
fmt.Printf("The set now has ID %v.", someID)
}
// Output: The set now has 'value1'.
// The set now has ID 123.
}