-
Notifications
You must be signed in to change notification settings - Fork 37
/
cfilter_test.go
104 lines (86 loc) · 2.06 KB
/
cfilter_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
package cfilter_test
import (
"bufio"
"hash/fnv"
"os"
"testing"
"github.com/irfansharif/cfilter"
)
func TestMultipleInsertions(t *testing.T) {
cf := cfilter.New()
fd, err := os.Open("/usr/share/dict/words")
if err != nil {
t.Errorf(err.Error())
}
scanner := bufio.NewScanner(fd)
var words [][]byte
var wordCount uint
for scanner.Scan() {
word := []byte(scanner.Text())
if !cf.Lookup(word) && cf.Insert(word) {
wordCount++
}
words = append(words, word)
}
size := cf.Count()
if size != wordCount {
t.Errorf("Expected word count = %d, not %d", wordCount, size)
}
for _, word := range words {
cf.Delete(word)
}
size = cf.Count()
if size != 0 {
t.Errorf("Expected word count = 0, not %d", size)
}
}
func TestBasicInsertion(t *testing.T) {
cf := cfilter.New()
if !cf.Insert([]byte("buongiorno")) {
t.Errorf("Wasn't able to insert very first word, 'buongiorno'")
}
size := cf.Count()
if size != 1 {
t.Errorf("Expected size after insertion to be 1, not %d", size)
}
if !cf.Lookup([]byte("buongiorno")) {
t.Errorf("Expected to find 'buongiorno' in filter set membership query")
}
if !cf.Delete([]byte("buongiorno")) {
t.Errorf("Expected to be able to delete 'buongiorno' in filter")
}
if cf.Lookup([]byte("buongiorno")) {
t.Errorf("Did not expect to find 'buongiorno' in filter after deletion")
}
size = cf.Count()
if size != 0 {
t.Errorf("Expected size after deletion to be 0, not %d", size)
}
}
func TestInitialization(t *testing.T) {
cf := cfilter.New()
size := cf.Count()
if size != 0 {
t.Errorf("Expected initial size to be 0, not %d", size)
}
}
func TestConfigurationOptions(t *testing.T) {
cf := cfilter.New(
cfilter.Size(1<<18),
cfilter.BucketSize(4),
cfilter.FingerprintSize(2),
cfilter.MaximumKicks(500),
cfilter.HashFn(fnv.New64()),
)
size := cf.Count()
if size != 0 {
t.Errorf("Expected size to be 10, not %d", size)
}
}
func BenchmarkInsertionAndDeletion(b *testing.B) {
cf := cfilter.New()
for n := 0; n < b.N; n++ {
cf.Insert([]byte("buongiorno"))
cf.Delete([]byte("buongiorno"))
}
}