/
loglogbeta_test.go
123 lines (101 loc) · 2.64 KB
/
loglogbeta_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
package loglogbeta
import (
"math"
"math/rand"
"testing"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func estimateError(got, exp uint64) float64 {
var delta uint64
if got > exp {
delta = got - exp
} else {
delta = exp - got
}
return float64(delta) / float64(exp)
}
func TestZeros(t *testing.T) {
registers := [m]uint8{}
exp := 0.0
for i := range registers {
val := uint8(rand.Intn(32))
if val == 0 {
exp++
}
registers[i] = val
}
_, got := regSumAndZeros(registers[:])
if got != exp {
t.Errorf("expected %.2f, got %.2f", exp, got)
}
}
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) {
llb := New()
step := 10000
unique := map[string]bool{}
for i := 1; len(unique) <= 1000000; i++ {
str := RandStringBytesMaskImprSrc(rand.Uint32() % 32)
llb.Add([]byte(str))
unique[str] = true
if len(unique)%step == 0 {
exact := uint64(len(unique))
res := uint64(llb.Cardinality())
step *= 10
ratio := 100 * estimateError(res, exact)
if ratio > 2 {
t.Errorf("Exact %d, got %d which is %.2f%% error", exact, res, ratio)
}
}
}
}
func TestMerge(t *testing.T) {
llb1 := New()
llb2 := New()
unique := map[string]bool{}
for i := 1; i <= 3500000; i++ {
str := RandStringBytesMaskImprSrc(rand.Uint32() % 32)
llb1.Add([]byte(str))
unique[str] = true
str = RandStringBytesMaskImprSrc(rand.Uint32() % 32)
llb2.Add([]byte(str))
unique[str] = true
}
llb1.Merge(llb2)
exact := len(unique)
res := int(llb1.Cardinality())
ratio := 100 * math.Abs(float64(res-exact)) / float64(exact)
expectedError := 1.04 / math.Sqrt(float64(m))
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)
}
llb1.Merge(llb2)
exact = res
res = int(llb1.Cardinality())
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)
}
}
func TestMarshal(t *testing.T) {
llb := New()
unique := map[string]bool{}
for i := 1; len(unique) <= 1000000; i++ {
str := RandStringBytesMaskImprSrc(rand.Uint32() % 32)
llb.Add([]byte(str))
unique[str] = true
}
bytes := llb.Marshal()
ullb, err := Unmarshal(bytes)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if *ullb != *llb {
t.Errorf("Expected %d,\n\n got %d", llb, ullb)
}
}