-
Notifications
You must be signed in to change notification settings - Fork 3
/
bloomfilter_test.go
237 lines (212 loc) · 5.49 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
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
package bloomfilter
import (
"bytes"
"io"
"os"
"testing"
)
func TestNumOfBits(t *testing.T) {
type testCase struct {
expectedInsertions int
errRate float64
expectedNumBits int
}
testCases := []testCase{{500, 0.01, 4792}, {500, 0.0, 774727}, {10, 0.01, 95}}
var numBits int
for _, item := range testCases {
numBits = numOfBits(item.expectedInsertions, item.errRate)
if numBits != item.expectedNumBits {
t.Errorf("Expected %d bits, but got %d", item.expectedNumBits, numBits)
}
}
}
func TestNumOfHashFunctions(t *testing.T) {
testCases := [][]int{{500, 4792, 7}, {500, 774727, 1074}}
var numHashFunctions int
for _, item := range testCases {
numHashFunctions = numOfHashFunctions(item[0], item[1])
if numHashFunctions != item[2] {
t.Errorf("Expected %d hash functions, but got %d", item[2], numHashFunctions)
}
}
}
func TestBloomFilter(t *testing.T) {
bf, err := NewBloomFilter(500, 0.01)
if err != nil {
t.Errorf("Unexpected error happened on initializing Bloomfilter: %v", err)
}
for i := 0; i < 100; i++ {
bf.Put(i)
}
for i := 0; i < 100; i++ {
if !bf.MightContain(i) {
t.Errorf("Expected number %d in BloomFilter", i)
}
}
for i := 200; i < 300; i++ {
if bf.MightContain(i) {
t.Errorf("Expected number %d not in Bloomfilter", i)
}
}
bf2, err := NewBloomFilterWithStrategy(300, 0.01, &Murur128Mitz32{})
if err != nil {
t.Errorf("Unexpected error happened on initializing Bloomfilter with strategy: %v", err)
}
for i := 0; i < 100; i++ {
bf2.Put(i)
}
for i := 0; i < 100; i++ {
if !bf2.MightContain(i) {
t.Errorf("Expected number %d in BloomFilter", i)
}
}
for i := 200; i < 300; i++ {
if bf2.MightContain(i) {
t.Errorf("Expected number %d not in Bloomfilter", i)
}
}
_, err = NewBloomFilterWithStrategy(0, 0.01, &Murur128Mitz32{})
if err != nil {
t.Errorf("Unexpected error happened on initializing Bloomfilter with strategy: %v", err)
}
}
func TestBloomFilterWithUnusualKeys(t *testing.T) {
bf, _ := NewBloomFilter(500, 0.01)
if ok := bf.Put([]int{1, 2, 3}); ok {
t.Error("Expected false result when putting an int array")
}
if ok := bf.MightContain([]int{1, 2, 3}); ok {
t.Error("Expected false result when checking existence of an int array")
}
}
func TestBloomFilterSerialization(t *testing.T) {
bf, _ := NewBloomFilter(500, 0.01)
for i := 0; i < 100; i++ {
bf.Put(i)
}
bf2, err := FromBytes(bf.ToBytes())
if err != nil {
t.Error("Deserialization failed")
}
if !bytes.Equal(bf2.ToBytes(), bf.ToBytes()) {
t.Error("Deserialize then serialize failed")
}
for i := 0; i < 100; i++ {
if !bf2.MightContain(i) {
t.Errorf("Expected number %d in BloomFilter2", i)
}
}
for i := 100; i < 200; i++ {
if bf2.MightContain(i) {
t.Errorf("Expected number %d NOT in Bloomfilter2", i)
}
}
bf3, _ := NewBloomFilterWithStrategy(300, 0.01, &Murur128Mitz32{})
for i := 100; i < 200; i++ {
bf3.Put(i)
}
bf4, err := FromBytes(bf3.ToBytes())
if err != nil {
t.Error("Deserialization failed")
}
if !bytes.Equal(bf3.ToBytes(), bf4.ToBytes()) {
t.Error("Deserialize then serialize failed")
}
for i := 100; i < 200; i++ {
if !bf4.MightContain(i) {
t.Errorf("Expected number %d in BloomFilter4", i)
}
}
for i := 0; i < 100; i++ {
if bf4.MightContain(i) {
t.Errorf("Expected number %d NOT in BloomFilter4", i)
}
}
}
func TestJavaCompatibility(t *testing.T) {
file1, _ := os.Open("guava_dump_files/100_0_001_0_to_49_test.dump")
defer file1.Close()
b, _ := io.ReadAll(file1)
bf1, err := FromBytes(b)
if err != nil {
t.Errorf("Deserialization from Guava dump file failed: %v", err)
}
for i := 0; i < 50; i++ {
if !bf1.MightContain(i) {
t.Errorf("Expected number %d in Bloomfilter1", i)
}
}
for i := 50; i < 100; i++ {
if bf1.MightContain(i) {
t.Errorf("Expected number %d NOT in Bloomfilter1", i)
}
}
file2, _ := os.Open("guava_dump_files/500_0_01_0_to_99_test.dump")
defer file1.Close()
b, _ = io.ReadAll(file2)
bf2, err := FromBytes(b)
if err != nil {
t.Errorf("Deserialization from Guava dump file failed: %v", err)
}
for i := 0; i < 100; i++ {
if !bf2.MightContain(i) {
t.Errorf("Expected number %d in Bloomfilter2", i)
}
}
for i := 100; i < 150; i++ {
if bf2.MightContain(i) {
t.Errorf("Expected number %d NOT in Bloomfilter1", i)
}
}
}
func TestGarbageByteArray(t *testing.T) {
garbageBytes := [][]byte{
[]byte("this-is-a-line-of-garbage"),
{},
{0},
{0, 1},
{0, 1, 2},
{0, 1, 2, 3, 4, 5},
}
for _, bytes := range garbageBytes {
_, err := FromBytes(bytes)
if err == nil {
t.Errorf("Expected error on garbage byte array: %v", bytes)
}
}
}
func TestBloomFilterInvalidParams(t *testing.T) {
// too small error rate
_, err := NewBloomFilter(500, 0.0)
if err == nil {
t.Error("Expected error on non-positive error rate.")
}
// too large error rate
_, err = NewBloomFilter(500, 1.0)
if err == nil {
t.Error("Expected error on error rate larger than or equal to 1.0.")
}
// negative insertions
_, err = NewBloomFilter(-1, 0.01)
if err == nil {
t.Error("Expected error on negative insertions.")
}
}
var bf *BloomFilter
func BenchmarkBloomfilterInsertion(b *testing.B) {
bf, _ = NewBloomFilter(b.N, 0.01)
for i := 0; i < b.N; i++ {
bf.Put(i)
}
}
func BenchmarkBloomfilterQuery(b *testing.B) {
for i := 0; i < b.N; i++ {
bf.MightContain(i)
}
}
var fileContent, _ = os.ReadFile("guava_dump_files/100_0_001_0_to_49_test.dump")
func BenchmarkBloomfilterDeserialization(b *testing.B) {
for i := 0; i < b.N; i++ {
FromBytes(fileContent)
}
}