-
Notifications
You must be signed in to change notification settings - Fork 108
/
cuckoofilter_test.go
111 lines (94 loc) · 1.89 KB
/
cuckoofilter_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
package cuckoo
import (
"bufio"
"crypto/rand"
"io"
"os"
"reflect"
"testing"
)
func TestInsertion(t *testing.T) {
cf := NewFilter(1000000)
fd, err := os.Open("/usr/share/dict/words")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(fd)
var values [][]byte
var lineCount uint
for scanner.Scan() {
s := []byte(scanner.Text())
if cf.InsertUnique(s) {
lineCount++
}
values = append(values, s)
}
count := cf.Count()
if count != lineCount {
t.Errorf("Expected count = %d, instead count = %d", lineCount, count)
}
for _, v := range values {
cf.Delete(v)
}
count = cf.Count()
if count != 0 {
t.Errorf("Expected count = 0, instead count == %d", count)
}
}
func TestEncodeDecode(t *testing.T) {
cf := NewFilter(8)
cf.buckets = []bucket{
[4]fingerprint{1, 2, 3, 4},
[4]fingerprint{5, 6, 7, 8},
}
cf.count = 8
bytes := cf.Encode()
ncf, err := Decode(bytes)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if !reflect.DeepEqual(cf, ncf) {
t.Errorf("Expected %v, got %v", cf, ncf)
}
}
func TestDecode(t *testing.T) {
ncf, err := Decode([]byte(""))
if err == nil {
t.Errorf("Expected err, got nil")
}
if ncf != nil {
t.Errorf("Expected nil, got %v", ncf)
}
}
func BenchmarkFilter_Reset(b *testing.B) {
const cap = 10000
filter := NewFilter(cap)
b.ResetTimer()
for i := 0; i < b.N; i++ {
filter.Reset()
}
}
func BenchmarkFilter_Insert(b *testing.B) {
const cap = 10000
filter := NewFilter(cap)
b.ResetTimer()
var hash [32]byte
for i := 0; i < b.N; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Insert(hash[:])
}
}
func BenchmarkFilter_Lookup(b *testing.B) {
const cap = 10000
filter := NewFilter(cap)
var hash [32]byte
for i := 0; i < 10000; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Insert(hash[:])
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
io.ReadFull(rand.Reader, hash[:])
filter.Lookup(hash[:])
}
}