-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsbf_test.go
324 lines (285 loc) · 8.12 KB
/
sbf_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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package sbf
import (
"fmt"
"math"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestHashIndex(t *testing.T) {
// Initialize a StableBloomFilter instance
sbf, err := NewStableBloomFilter(1024, nil, 0.0, time.Hour)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
data := []byte("test_data")
for i := uint32(0); i < sbf.k; i++ {
idx := sbf.hashIndex(data, i)
if idx >= sbf.m {
t.Errorf("hashIndex returned index out of bounds: %d", idx)
}
}
}
func TestAtomicSetAndGetBit(t *testing.T) {
var val uint64
// Test setting and getting bits
for i := uint32(0); i < 64; i++ {
atomicSetBit(&val, i)
if !atomicGetBit(&val, i) {
t.Errorf("Bit %d was not set correctly", i)
}
}
// Ensure other bits are not affected
for i := uint32(0); i < 64; i++ {
if !atomicGetBit(&val, i) {
t.Errorf("Bit %d should be set", i)
}
}
}
// Too much randomness
// func TestDecayBucket(t *testing.T) {
// // Create a bucket with all bits set
// var bucket uint64 = ^uint64(0)
// decayRate := 0.3
// randSrc := rand.New(rand.NewSource(42))
// decayedBucket := decayBucket(bucket, decayRate, randSrc)
// bitsCleared := bucket ^ decayedBucket
// bitsRemaining := decayedBucket
// if bitsCleared == 0 {
// t.Error("No bits were decayed")
// }
// if bitsRemaining == 0 {
// t.Error("All bits were decayed")
// }
// }
func TestDecayFunction(t *testing.T) {
// Initialize a StableBloomFilter instance with decayRate
sbf, err := NewStableBloomFilter(1024, nil, 1.0, time.Millisecond*10)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
defer sbf.StopDecay()
// Set all bits
for i := range sbf.filter {
atomic.StoreUint64(&sbf.filter[i], ^uint64(0))
}
// Wait for decay to happen
time.Sleep(time.Millisecond * 20)
// Check that all bits have been decayed
for _, bucket := range sbf.filter {
val := atomic.LoadUint64(&bucket)
if val != 0 {
t.Error("Decay did not clear all bits as expected")
break
}
}
}
func TestStartDecay(t *testing.T) {
// Initialize a StableBloomFilter instance with a short decay interval
sbf, err := NewStableBloomFilter(1024, nil, 0.5, time.Millisecond*10)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
defer sbf.StopDecay()
// Set some bits
sbf.Add([]byte("test1"))
sbf.Add([]byte("test2"))
// Capture the state before decay
preDecay := make([]uint64, len(sbf.filter))
for i := range sbf.filter {
preDecay[i] = atomic.LoadUint64(&sbf.filter[i])
}
// Wait for decay to happen
time.Sleep(time.Millisecond * 20)
// Capture the state after decay
postDecay := make([]uint64, len(sbf.filter))
for i := range sbf.filter {
postDecay[i] = atomic.LoadUint64(&sbf.filter[i])
}
// Ensure that decay has changed the filter state
identical := true
for i := range preDecay {
if preDecay[i] != postDecay[i] {
identical = false
break
}
}
if identical {
t.Error("Decay did not change the filter state")
}
}
func TestConcurrency(t *testing.T) {
// Initialize a StableBloomFilter instance
sbf, err := NewStableBloomFilter(1024, nil, 0.0, time.Hour)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
var wg sync.WaitGroup
numGoroutines := 100
numOperations := 1000
// Start multiple goroutines to add and check elements concurrently
for i := 0; i < numGoroutines; i++ {
wg.Add(1)
go func(id int) {
defer wg.Done()
for j := 0; j < numOperations; j++ {
data := []byte(fmt.Sprintf("goroutine_%d_data_%d", id, j))
sbf.Add(data)
sbf.Check(data)
}
}(i)
}
wg.Wait()
}
func TestOptimalM(t *testing.T) {
tests := []struct {
n uint32
p float64
wantM uint32
wantErr bool
}{
{n: 1000, p: 0.01, wantM: 9586, wantErr: false},
{n: 1000, p: 0.001, wantM: 14378, wantErr: false},
{n: 0, p: 0.01, wantM: 0, wantErr: true}, // Edge case: n = 0
{n: 1000, p: 0.0, wantM: 0, wantErr: true}, // Invalid p
{n: 1000, p: 1.0, wantM: 0, wantErr: true}, // Invalid p
{n: 1000, p: -0.1, wantM: 0, wantErr: true}, // Invalid p
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
m, err := OptimalM(tt.n, tt.p)
if tt.wantErr {
if err == nil {
t.Errorf("Expected an error but got nil")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if m != tt.wantM {
t.Errorf("OptimalM(%d, %f) = %d; want %d", tt.n, tt.p, m, tt.wantM)
}
}
})
}
}
func TestOptimalK(t *testing.T) {
tests := []struct {
m uint32
n uint32
wantK uint32
wantErr bool
}{
{m: 9586, n: 1000, wantK: 7, wantErr: false},
{m: 14378, n: 1000, wantK: 10, wantErr: false},
{m: 0, n: 1000, wantK: 0, wantErr: true}, // Edge case: m = 0
{m: 1000, n: 0, wantK: 0, wantErr: true}, // Edge case: n = 0
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
k, err := OptimalK(tt.m, tt.n)
if tt.wantErr {
if err == nil {
t.Errorf("Expected an error but got nil")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if k != tt.wantK {
t.Errorf("OptimalK(%d, %d) = %d; want %d", tt.m, tt.n, k, tt.wantK)
}
}
})
}
}
func TestNewDefaultStableBloomFilter(t *testing.T) {
tests := []struct {
expectedItems uint32
falsePositiveRate float64
decayRate float64
decayInterval time.Duration
wantErr bool
}{
{expectedItems: 1000, falsePositiveRate: 0.01, decayRate: 0.01, decayInterval: time.Minute, wantErr: false},
{expectedItems: 0, falsePositiveRate: 0.01, decayRate: 0.01, decayInterval: time.Minute, wantErr: true}, // Edge case: expectedItems = 0
{expectedItems: 1000, falsePositiveRate: 0.0, decayRate: 0.01, decayInterval: time.Minute, wantErr: true}, // Invalid p
{expectedItems: 1000, falsePositiveRate: 1.0, decayRate: 0.01, decayInterval: time.Minute, wantErr: true}, // Invalid p
{expectedItems: 1000, falsePositiveRate: -0.1, decayRate: 0.01, decayInterval: time.Minute, wantErr: true}, // Invalid p
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
sbf, err := NewDefaultStableBloomFilter(tt.expectedItems, tt.falsePositiveRate, tt.decayRate, tt.decayInterval)
if tt.wantErr {
if err == nil {
t.Errorf("Expected error but got nil")
}
} else {
if err != nil {
t.Errorf("Unexpected error: %v", err)
}
if sbf == nil {
t.Error("Expected sbf to be non-nil")
} else {
// Additional checks can be added here
sbf.StopDecay() // Clean up
}
}
})
}
}
func TestEstimateFalsePositiveRate(t *testing.T) {
// Initialize a StableBloomFilter instance
sbf, err := NewStableBloomFilter(1024, nil, 0.0, time.Hour)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
// Test when no bits are set
fpr := sbf.EstimateFalsePositiveRate()
expectedFPR := 0.0
if fpr != expectedFPR {
t.Errorf("Expected FPR %f, got %f", expectedFPR, fpr)
}
// Set all bits
for i := range sbf.filter {
atomic.StoreUint64(&sbf.filter[i], ^uint64(0))
}
// Test when all bits are set
fpr = sbf.EstimateFalsePositiveRate()
expectedFPR = 1.0
if fpr != expectedFPR {
t.Errorf("Expected FPR %f, got %f", expectedFPR, fpr)
}
// Set some bits
for i := range sbf.filter {
atomic.StoreUint64(&sbf.filter[i], 0xAAAAAAAAAAAAAAAA) // Pattern with half bits set
}
// Test when half the bits are set
fpr = sbf.EstimateFalsePositiveRate()
fractionBitsSet := 0.5
expectedFPR = math.Pow(fractionBitsSet, float64(sbf.k))
if math.Abs(fpr-expectedFPR) > 0.0001 {
t.Errorf("Expected FPR %f, got %f", expectedFPR, fpr)
}
}
func TestEstimateFalsePositiveRateAfterAdditions(t *testing.T) {
sbf, err := NewDefaultStableBloomFilter(1000, 0.01, 0.0, time.Hour)
if err != nil {
t.Fatalf("Failed to create StableBloomFilter: %v", err)
}
defer sbf.StopDecay()
// Add elements
numElements := 1000
for i := 0; i < numElements; i++ {
data := []byte(fmt.Sprintf("element%d", i))
sbf.Add(data)
}
// Estimate FPR
fpr := sbf.EstimateFalsePositiveRate()
// The estimated FPR should be close to the desired false positive rate
desiredFPR := 0.01
if math.Abs(fpr-desiredFPR) > 0.005 {
t.Errorf("Expected FPR close to %f, got %f", desiredFPR, fpr)
}
}