-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhyperbitbit_test.go
86 lines (74 loc) · 1.58 KB
/
hyperbitbit_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
package hyperbitbit
import (
"math"
"math/rand"
"testing"
"time"
)
func TestP(t *testing.T) {
tests := map[uint64]uint64{
8: 1,
7: 3,
6: 2,
5: 2,
4: 1,
3: 2,
2: 1,
1: 1,
0: 0,
}
for val, expected := range tests {
res := p(val)
if res != expected {
t.Errorf("Expected %d bits set to 1 in %d, got %d", expected, val, res)
}
}
}
func TestR(t *testing.T) {
tests := map[uint64]uint64{
8: 0,
7: 3,
6: 0,
5: 1,
4: 0,
3: 2,
2: 0,
1: 1,
0: 0,
}
for val, expected := range tests {
res := rho(val, 58)
if res != expected {
t.Errorf("Expected %d trailing 1 in %d, got %d", expected, val, res)
}
}
}
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var src = rand.NewSource(time.Now().UnixNano())
func RandStringBytesMaskImprSrc(n uint32) string {
b := make([]byte, n)
for i := uint32(0); i < n; i++ {
b[i] = letterBytes[rand.Int()%len(letterBytes)]
}
return string(b)
}
func TestCardinality(t *testing.T) {
hbb := New()
step := 10000
unique := map[string]bool{}
for i := 1; len(unique) <= 10000000; i++ {
str := RandStringBytesMaskImprSrc(rand.Uint32() % 32)
hbb.Add([]byte(str))
unique[str] = true
if len(unique)%step == 0 {
exact := len(unique)
step *= 10
res := int(hbb.Cardinality())
ratio := 100 * math.Abs(float64(res-exact)) / float64(exact)
expectedError := 0.1
if float64(res) < float64(exact)-(float64(exact)*expectedError) || float64(res) > float64(exact)+(float64(exact)*expectedError) {
t.Errorf("Exact %d, got %d which is %.2f%% error", exact, res, ratio)
}
}
}
}