forked from AndreasBriese/bbloom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbloom_test.go
224 lines (185 loc) · 4.99 KB
/
bbloom_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
package bbloom
import (
"bufio"
"fmt"
"log"
"os"
"testing"
)
var (
wordlist1 [][]byte
n = 1 << 16
bf Bloom
)
func TestMain(m *testing.M) {
file, err := os.Open("words.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
wordlist1 = make([][]byte, n)
for i := range wordlist1 {
if scanner.Scan() {
wordlist1[i] = []byte(scanner.Text())
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
fmt.Println("\n###############\nbbloom_test.go")
fmt.Print("Benchmarks relate to 2**16 OP. --> output/65536 op/ns\n###############\n\n")
m.Run()
}
func TestM_NumberOfWrongs(t *testing.T) {
bf = New(float64(n*10), float64(7))
cnt := 0
for i := range wordlist1 {
if !bf.AddIfNotHas(wordlist1[i]) {
cnt++
}
}
fmt.Printf("Bloomfilter New(7* 2**16, 7) (-> size=%v bit): \n Check for 'false positives': %v wrong positive 'Has' results on 2**16 entries => %v %%\n", len(bf.bitset)<<6, cnt, float64(cnt)/float64(n))
}
func TestM_JSON(t *testing.T) {
const shallBe = int(1 << 16)
bf = New(float64(n*10), float64(7))
cnt := 0
for i := range wordlist1 {
if !bf.AddIfNotHas(wordlist1[i]) {
cnt++
}
}
Json := bf.JSONMarshal()
// create new bloomfilter from bloomfilter's JSON representation
bf2 := JSONUnmarshal(Json)
cnt2 := 0
for i := range wordlist1 {
if !bf2.AddIfNotHas(wordlist1[i]) {
cnt2++
}
}
if cnt2 != shallBe {
t.Errorf("FAILED !AddIfNotHas = %v; want %v", cnt2, shallBe)
}
}
func ExampleM_NewAddHasAddIfNotHas() {
bf := New(float64(512), float64(1))
fmt.Printf("%v %v %v %v\n", bf.sizeExp, bf.size, bf.setLocs, bf.shift)
bf.Add([]byte("Manfred"))
fmt.Println("bf.Add([]byte(\"Manfred\"))")
fmt.Printf("bf.Has([]byte(\"Manfred\")) -> %v - should be true\n", bf.Has([]byte("Manfred")))
fmt.Printf("bf.Add([]byte(\"manfred\")) -> %v - should be false\n", bf.Has([]byte("manfred")))
fmt.Printf("bf.AddIfNotHas([]byte(\"Manfred\")) -> %v - should be false\n", bf.AddIfNotHas([]byte("Manfred")))
fmt.Printf("bf.AddIfNotHas([]byte(\"manfred\")) -> %v - should be true\n", bf.AddIfNotHas([]byte("manfred")))
bf.AddTS([]byte("Hans"))
fmt.Println("bf.AddTS([]byte(\"Hans\")")
fmt.Printf("bf.HasTS([]byte(\"Hans\")) -> %v - should be true\n", bf.HasTS([]byte("Hans")))
fmt.Printf("bf.AddTS([]byte(\"hans\")) -> %v - should be false\n", bf.HasTS([]byte("hans")))
fmt.Printf("bf.AddIfNotHasTS([]byte(\"Hans\")) -> %v - should be false\n", bf.AddIfNotHasTS([]byte("Hans")))
fmt.Printf("bf.AddIfNotHasTS([]byte(\"hans\")) -> %v - should be true\n", bf.AddIfNotHasTS([]byte("hans")))
// Output: 9 511 1 55
// bf.Add([]byte("Manfred"))
// bf.Has([]byte("Manfred")) -> true - should be true
// bf.Add([]byte("manfred")) -> false - should be false
// bf.AddIfNotHas([]byte("Manfred")) -> false - should be false
// bf.AddIfNotHas([]byte("manfred")) -> true - should be true
// bf.AddTS([]byte("Hans")
// bf.HasTS([]byte("Hans")) -> true - should be true
// bf.AddTS([]byte("hans")) -> false - should be false
// bf.AddIfNotHasTS([]byte("Hans")) -> false - should be false
// bf.AddIfNotHasTS([]byte("hans")) -> true - should be true
}
func BenchmarkM_New(b *testing.B) {
for r := 0; r < b.N; r++ {
_ = New(float64(n*10), float64(7))
}
}
func BenchmarkM_Clear(b *testing.B) {
bf = New(float64(n*10), float64(7))
for i := range wordlist1 {
bf.Add(wordlist1[i])
}
b.ResetTimer()
for r := 0; r < b.N; r++ {
bf.Clear()
}
}
func BenchmarkM_Add(b *testing.B) {
bf = New(float64(n*10), float64(7))
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.Add(wordlist1[i])
}
}
}
func BenchmarkM_Has(b *testing.B) {
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.Has(wordlist1[i])
}
}
}
func BenchmarkM_AddIfNotHasFALSE(b *testing.B) {
bf = New(float64(n*10), float64(7))
for i := range wordlist1 {
bf.Has(wordlist1[i])
}
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.AddIfNotHas(wordlist1[i])
}
}
}
func BenchmarkM_AddIfNotHasClearTRUE(b *testing.B) {
bf = New(float64(n*10), float64(7))
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.AddIfNotHas(wordlist1[i])
}
bf.Clear()
}
}
func BenchmarkM_AddTS(b *testing.B) {
bf = New(float64(n*10), float64(7))
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.AddTS(wordlist1[i])
}
}
}
func BenchmarkM_HasTS(b *testing.B) {
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.HasTS(wordlist1[i])
}
}
}
func BenchmarkM_AddIfNotHasTSFALSE(b *testing.B) {
bf = New(float64(n*10), float64(7))
for i := range wordlist1 {
bf.Has(wordlist1[i])
}
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.AddIfNotHasTS(wordlist1[i])
}
}
}
func BenchmarkM_AddIfNotHasTSClearTRUE(b *testing.B) {
bf = New(float64(n*10), float64(7))
b.ResetTimer()
for r := 0; r < b.N; r++ {
for i := range wordlist1 {
bf.AddIfNotHasTS(wordlist1[i])
}
bf.Clear()
}
}