-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbloom_test.go
82 lines (58 loc) · 1.25 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
// Copyright (c) 2011 Damian Gryski <[email protected]>
// Licensed under the GPLv3, or at your option any later version.
package dgobloom
import (
"bufio"
"hash/fnv"
"math/rand"
"os"
"testing"
)
const CAPACITY = 10000
const ERRPCT = 0.01
func TestBloomFilter(t *testing.T) {
saltsNeeded := SaltsRequired(CAPACITY, ERRPCT)
t.Log("generating", saltsNeeded, "salts")
salts := make([]uint32, saltsNeeded)
for i := uint(0); i < saltsNeeded; i++ {
salts[i] = rand.Uint32()
}
b := NewBloomFilter(CAPACITY, ERRPCT, fnv.New32a(), salts)
b2 := NewBloomFilter(CAPACITY, ERRPCT, fnv.New32a(), salts)
fh, _ := os.Open("/usr/share/dict/words")
buf := bufio.NewReader(fh)
for {
l, _, err := buf.ReadLine()
if err != nil {
break
}
b2.Insert(l)
if !b.Insert(l) {
break
}
}
t.Log("inserted", b.Elements(), "elements")
total := 0.0
errors := 0.0
errors2 := 0.0
b2.Compress()
for {
l, _, err := buf.ReadLine()
if err != nil {
break
}
if b.Exists(l) {
errors++
}
if b2.Exists(l) {
errors2++
}
total++
}
errorPct := errors / total
t.Log("error percentage: (", errors, "/", total, ")=", errorPct)
t.Log("error percentage2: (", errors2, "/", total, ")=", errors2/total)
if errorPct > ERRPCT {
t.Fail()
}
}