-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopp_bloom_test.go
137 lines (111 loc) · 3.47 KB
/
opp_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
package oppbloom_test
import (
"math/rand"
"testing"
"time"
"fmt"
"github.com/HoMuChen/opp-bloom"
)
func TestInit(t *testing.T) {
set := oppbloom.New(1000)
if set.Cap() != 1000 {
t.Errorf("Expected capacity %v but got: %v", 1000, set.Cap())
}
}
func TestAddAndContain(t *testing.T) {
set := oppbloom.New(
1000,
oppbloom.WithHasher(oppbloom.FNVHasher{}),
)
set.Add([]byte(`1`))
exist := set.Contain([]byte(`1`))
if exist != true {
t.Errorf("Expected containg 1 but got false")
}
}
func TestLimitedSize(t *testing.T) {
set := oppbloom.New(
1,
)
set.Add([]byte(`1`))
set.Add([]byte(`2`))
exist1 := set.Contain([]byte(`1`))
exist2 := set.Contain([]byte(`2`))
if exist1 != false {
t.Errorf("Expected not containg 1 but got true")
}
if exist2 != true {
t.Errorf("Expected containg 2 but got false")
}
}
func testFalseNegative(size int, numKeys int) {
rand.Seed(time.Now().UnixNano())
set := oppbloom.New(
size,
)
answer := make(map[string]bool)
for i := 0; i < numKeys; i++ {
key := make([]byte, 16)
rand.Read(key)
set.Add(key)
answer[string(key)] = true
}
count := 0
for key := range answer {
if exist := set.Contain([]byte(key)); !exist {
count += 1
}
}
fmt.Printf("Set size: %v\n", size)
fmt.Printf("Numbe of keys: %v\n", numKeys)
fmt.Printf("False negative rate %v %%\n\n", (float64(count) / float64(numKeys)) * 100)
}
func testRecentFalseNegative(size int, numKeys int, n int) {
rand.Seed(time.Now().UnixNano())
set := oppbloom.New(
size,
)
for i := 0; i < n; i++ {
for j := 0; j < numKeys; j++ {
key := make([]byte, 16)
rand.Read(key)
set.Add(key)
}
}
answer := make(map[string]bool)
for j := 0; j < numKeys; j++ {
key := make([]byte, 16)
rand.Read(key)
set.Add(key)
answer[string(key)] = true
}
count := 0
for key := range answer {
if exist := set.Contain([]byte(key)); !exist {
count += 1
}
}
fmt.Printf("Set size: %v\n", size)
fmt.Printf("Numbe of keys: %v\n", numKeys*(n+1))
fmt.Printf("False negative rate of rencent %v keys: %v %%\n\n", numKeys, (float64(count) / float64(numKeys)) * 100)
}
func TestFalseNegative(t *testing.T) {
testFalseNegative(10000, 100)
testFalseNegative(10000, 500)
testFalseNegative(10000, 1000)
testFalseNegative(10000, 2000)
}
func TestRecentFalseNegative(t *testing.T) {
testRecentFalseNegative(10000, 1000, 10)
testRecentFalseNegative(10000, 2000, 5)
testRecentFalseNegative(3000000, 100000, 30)
}
func BenchmarkAdd(b *testing.B) {
set := oppbloom.New(100000)
rand.Seed(time.Now().UnixNano())
key := make([]byte, 16)
for i := 0; i < b.N; i++ {
rand.Read(key)
set.Add(key)
}
}