forked from bits-and-blooms/bloom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bloom_test.go
261 lines (244 loc) · 5.38 KB
/
bloom_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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package bloom
import (
"bytes"
"encoding/binary"
"encoding/gob"
"encoding/json"
"fmt"
"testing"
)
func TestBasic(t *testing.T) {
f := New(1000, 4)
n1 := []byte("Bess")
n2 := []byte("Jane")
n3 := []byte("Emma")
f.Add(n1)
n3a := f.TestAndAdd(n3)
n1b := f.Test(n1)
n2b := f.Test(n2)
n3b := f.Test(n3)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
}
func TestBasicUint32(t *testing.T) {
f := New(1000, 4)
n1 := make([]byte, 4)
n2 := make([]byte, 4)
n3 := make([]byte, 4)
n4 := make([]byte, 4)
binary.BigEndian.PutUint32(n1, 100)
binary.BigEndian.PutUint32(n2, 101)
binary.BigEndian.PutUint32(n3, 102)
binary.BigEndian.PutUint32(n4, 103)
f.Add(n1)
n3a := f.TestAndAdd(n3)
n1b := f.Test(n1)
n2b := f.Test(n2)
n3b := f.Test(n3)
f.Test(n4)
if !n1b {
t.Errorf("%v should be in.", n1)
}
if n2b {
t.Errorf("%v should not be in.", n2)
}
if n3a {
t.Errorf("%v should not be in the first time we look.", n3)
}
if !n3b {
t.Errorf("%v should be in the second time we look.", n3)
}
}
func TestDirect20_5(t *testing.T) {
n := uint(10000)
k := uint(5)
load := uint(20)
f := New(n*load, k)
fp_rate := f.EstimateFalsePositiveRate(n)
if fp_rate > 0.0001 {
t.Errorf("False positive rate too high: load=%v, k=%v, %f", load, k, fp_rate)
}
}
func TestDirect15_10(t *testing.T) {
n := uint(10000)
k := uint(10)
load := uint(15)
f := New(n*load, k)
fp_rate := f.EstimateFalsePositiveRate(n)
if fp_rate > 0.0001 {
t.Errorf("False positive rate too high: load=%v, k=%v, %f", load, k, fp_rate)
}
}
func TestEstimated10_0001(t *testing.T) {
n := uint(10000)
fp := 0.0001
m, k := estimateParameters(n, fp)
f := NewWithEstimates(n, fp)
fp_rate := f.EstimateFalsePositiveRate(n)
if fp_rate > fp {
t.Errorf("False positive rate too high: n: %v, fp: %f, n: %v, k: %v result: %f", n, fp, m, k, fp_rate)
}
}
func TestEstimated10_001(t *testing.T) {
n := uint(10000)
fp := 0.001
m, k := estimateParameters(n, fp)
f := NewWithEstimates(n, fp)
fp_rate := f.EstimateFalsePositiveRate(n)
if fp_rate > fp {
t.Errorf("False positive rate too high: n: %v, fp: %f, n: %v, k: %v result: %f", n, fp, m, k, fp_rate)
}
}
func TestMarshalUnmarshalJSON(t *testing.T) {
f := New(1000, 4)
data, err := json.Marshal(f)
if err != nil {
t.Fatal(err.Error())
}
var g BloomFilter
err = json.Unmarshal(data, &g)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
}
func TestReadWriteBinary(t *testing.T) {
f := New(1000, 4)
var buf bytes.Buffer
bytesWritten, err := f.WriteTo(&buf)
if err != nil {
t.Fatal(err.Error())
}
if bytesWritten != int64(buf.Len()) {
t.Errorf("incorrect write length %d != %d", bytesWritten, buf.Len())
}
var g BloomFilter
bytesRead, err := g.ReadFrom(&buf)
if err != nil {
t.Fatal(err.Error())
}
if bytesRead != bytesWritten {
t.Errorf("read unexpected number of bytes %d != %d", bytesRead, bytesWritten)
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
}
func TestEncodeDecodeGob(t *testing.T) {
f := New(1000, 4)
f.Add([]byte("one"))
f.Add([]byte("two"))
f.Add([]byte("three"))
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(f)
if err != nil {
t.Fatal(err.Error())
}
var g BloomFilter
err = gob.NewDecoder(&buf).Decode(&g)
if err != nil {
t.Fatal(err.Error())
}
if g.m != f.m {
t.Error("invalid m value")
}
if g.k != f.k {
t.Error("invalid k value")
}
if g.b == nil {
t.Fatal("bitset is nil")
}
if !g.b.Equal(f.b) {
t.Error("bitsets are not equal")
}
if !g.Test([]byte("three")) {
t.Errorf("missing value 'three'")
}
if !g.Test([]byte("two")) {
t.Errorf("missing value 'two'")
}
if !g.Test([]byte("one")) {
t.Errorf("missing value 'one'")
}
}
func BenchmarkDirect(b *testing.B) {
n := uint(10000)
max_k := uint(10)
max_load := uint(20)
fmt.Printf("m/n")
for k := uint(2); k <= max_k; k++ {
fmt.Printf("\tk=%v", k)
}
fmt.Println()
for load := uint(2); load <= max_load; load++ {
fmt.Print(load)
for k := uint(2); k <= max_k; k++ {
f := New(n*load, k)
fp_rate := f.EstimateFalsePositiveRate(n)
fmt.Printf("\t%f", fp_rate)
}
fmt.Println()
}
}
func BenchmarkEstimted(b *testing.B) {
for n := uint(5000); n <= 50000; n += 5000 {
fmt.Printf("%v", n)
for fp := 0.1; fp >= 0.00001; fp /= 10.0 {
fmt.Printf("\t%f", fp)
m, k := estimateParameters(n, fp)
f := NewWithEstimates(n, fp)
fp_rate := f.EstimateFalsePositiveRate(n)
fmt.Printf("\t%v\t%v\t%f", m, k, fp_rate)
}
fmt.Println()
}
}
func BenchmarkSeparateTestAndAdd(b *testing.B) {
f := NewWithEstimates(uint(b.N), 0.0001)
key := make([]byte, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint32(key, uint32(i))
f.Test(key)
f.Add(key)
}
}
func BenchmarkCombinedTestAndAdd(b *testing.B) {
f := NewWithEstimates(uint(b.N), 0.0001)
key := make([]byte, 100)
b.ResetTimer()
for i := 0; i < b.N; i++ {
binary.BigEndian.PutUint32(key, uint32(i))
f.TestAndAdd(key)
}
}