-
Notifications
You must be signed in to change notification settings - Fork 1
/
ibf_test.go
128 lines (112 loc) · 3.5 KB
/
ibf_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
package reconcile
import (
"bytes"
"encoding/hex"
"log"
"math/rand"
"testing"
)
func elementName(key []byte) string {
return "'" + hex.EncodeToString(key) + "'"
}
func makeRandomElements(count, keysize int) [][]byte {
elements := make([][]byte, count)
for i := 0; i < count; i++ {
element := make([]byte, keysize)
_, err := rand.Read(element)
if err != nil {
log.Panicln("Could not get random bytes for set element")
}
elements[i] = element
}
return elements
}
func containsElement(list [][]byte, element []byte) bool {
for _, listElement := range list {
if bytes.Equal(listElement, element) {
return true
}
}
return false
}
func MakeTestSets(keysize, numSimilar, numDifferent int) (a, b, c, au, bu [][]byte) {
common := makeRandomElements(numSimilar, keysize)
partition := rand.Intn(numDifferent + 1)
elementsAunique := makeRandomElements(partition, keysize)
elementsBunique := makeRandomElements(numDifferent-partition, keysize)
elementsA := append(append(make([][]byte, 0, numSimilar+len(elementsAunique)), common...), elementsAunique...)
elementsB := append(append(make([][]byte, 0, numSimilar+len(elementsBunique)), common...), elementsBunique...)
return elementsA, elementsB, common, elementsAunique, elementsBunique
}
func TestIBF(t *testing.T) {
keysize := 32
for base := 1; base <= 9; base += 4 {
for diffs := 1; diffs <= 8; diffs++ {
cells := 2 + diffs*4
t.Logf("Testing with %d common and %d different elements in %d cells", base, diffs, cells)
// Prepare elements
elementsA, elementsB, common, elementsAunique, elementsBunique := MakeTestSets(keysize, base, diffs)
for _, element := range common {
t.Logf("Letting %s ∈ A ∩ B", elementName(element))
}
for _, element := range elementsAunique {
t.Logf("Letting %s ∈ A", elementName(element))
}
for _, element := range elementsBunique {
t.Logf("Letting %s ∈ B", elementName(element))
}
// Construct filters
filterA := NewIBF(cells, keysize)
filterB := NewIBF(cells, keysize)
for _, element := range elementsA {
if err := filterA.Add(element); err != nil {
t.Error(err)
}
}
for _, element := range elementsB {
if err := filterB.Add(element); err != nil {
t.Error(err)
}
}
// Perform decoding
filterA.Subtract(filterB)
local, remote, complete := filterA.Decode()
t.Logf("We have %s deduction",
(map[bool]string{true: "a complete", false: "an incomplete"})[complete])
// Expect local ⊆ A − B
for _, element := range local {
if containsElement(elementsAunique, element) {
t.Logf("Local's %s ∈ A − B", elementName(element))
} else {
t.Errorf("Local's %s ∉ A − B", elementName(element))
}
}
// Expect remote ⊆ B − A
for _, element := range remote {
if containsElement(elementsBunique, element) {
t.Logf("Remote's %s ∈ B − A", elementName(element))
} else {
t.Errorf("Remote's %s ∉ B − A", elementName(element))
}
}
if complete {
// Expect A − B ⊆ local
for _, element := range elementsAunique {
if containsElement(local, element) {
t.Logf("A's %s ∈ local", elementName(element))
} else {
t.Errorf("A's %s ∉ local", elementName(element))
}
}
// Expect B − A ⊆ remote
for _, element := range elementsBunique {
if containsElement(remote, element) {
t.Logf("B's %s ∈ remote", elementName(element))
} else {
t.Errorf("B's %s ∉ remote", elementName(element))
}
}
}
}
}
}