-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbloomTree_test.go
125 lines (114 loc) · 3.01 KB
/
bloomTree_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
package bloomtree
import (
"fmt"
"testing"
"github.com/labbloom/DBF"
)
func TestNewBloomTree64(t *testing.T) {
// set chunkSize to 64
SetChunkSize(64)
var tests = []struct {
elements [][]byte
hashAt [3]int
}{
{
elements: [][]byte{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}},
hashAt: [3]int{0, 1, 16},
},
{
elements: [][]byte{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13},
{14}, {15}, {16}},
hashAt: [3]int{2, 3, 17},
},
{
elements: [][]byte{{0}, {1}},
hashAt: [3]int{28, 29, 30},
},
}
for _, test := range tests {
dbf := generateDBF(200, "secret seed", test.elements...)
tree, err := NewBloomTree(dbf)
if err != nil {
t.Fatal(err)
}
if hashChild(tree.nodes[test.hashAt[0]], tree.nodes[test.hashAt[1]]) != tree.nodes[test.hashAt[2]] {
t.Fatalf("h(%d, %d) != %d", test.hashAt[0], test.hashAt[1], test.hashAt[2])
}
}
}
func TestNewBloomTree512(t *testing.T) {
// set chunkSize to 512
SetChunkSize(512)
var tests = []struct {
elements [][]byte
hashAt [3]int
}{
{
elements: [][]byte{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}},
hashAt: [3]int{0, 1, 16},
},
{
elements: [][]byte{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13},
{14}, {15}, {16}},
hashAt: [3]int{2, 3, 17},
},
{
elements: [][]byte{{0}, {1}},
hashAt: [3]int{28, 29, 30},
},
}
for _, test := range tests {
dbf := generateDBF(2000, "secret seed", test.elements...)
tree, err := NewBloomTree(dbf)
if err != nil {
t.Fatal(err)
}
if hashChild(tree.nodes[test.hashAt[0]], tree.nodes[test.hashAt[1]]) != tree.nodes[test.hashAt[2]] {
t.Fatalf("h(%d, %d) != %d", test.hashAt[0], test.hashAt[1], test.hashAt[2])
}
}
}
func TestGetBloomFilter(t *testing.T) {
var tests = []struct {
elements [][]byte
}{
{
elements: [][]byte{{1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}},
},
{
elements: [][]byte{{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, {11}, {12}, {13},
{14}, {15}, {16}},
},
{
elements: [][]byte{{0}, {1}},
},
}
for _, test := range tests {
dbf := generateDBF(200, "secret seed", test.elements...)
tree, err := NewBloomTree(dbf)
if err != nil {
t.Fatal(err)
}
if !tree.GetBloomFilter().BitArray().Equal(dbf.BitArray()) {
t.Fatal("bloom filter is not equal")
}
}
}
func TestBloomTreeExceedingK64(t *testing.T) {
// set chunkSize to 64
SetChunkSize(64)
dbf := DBF.NewDbf(200, 1e-100, []byte("secret seed"))
_, err := NewBloomTree(dbf)
if err == nil {
t.Fatalf("expected error %v", fmt.Errorf("parameter k of the bloom filter must be smaller than %d", maxK))
} else if err.Error() != fmt.Errorf("parameter k of the bloom filter must be smaller than %d", maxK).Error() {
t.Fatalf("expected error %v", fmt.Errorf("parameter k of the bloom filter must be smaller than %d", maxK))
}
}
func generateDBF(numElem uint, seed string, elements ...[]byte) *DBF.DistBF {
dbf := DBF.NewDbf(numElem, 0.2, []byte(seed))
for _, elem := range elements {
dbf.Add(elem)
}
return dbf
}