-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch_test.go
69 lines (61 loc) · 1.31 KB
/
sketch_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
package pcsa
import (
"math"
"math/rand"
"testing"
"time"
)
func estimateError(got, exp uint64) float64 {
var delta uint64
sign := 1.0
if got > exp {
delta = got - exp
} else {
delta = exp - got
sign = -1
}
return sign * float64(delta) / float64(exp)
}
func TestCardinalityZero(t *testing.T) {
sk, _ := New(4)
if card := sk.Cardinality(); card != 0 {
t.Error("exepcted cardinality == 0, got", card)
}
}
func TestCardinality10(t *testing.T) {
sk, _ := New(14)
for i := uint64(0); i < 10; i++ {
sk.AddHash(i)
}
if card := sk.Cardinality(); card != 10 {
t.Error("exepcted cardinality == 10, got", card)
}
}
/*
func TestCardinalityOne(t *testing.T) {
sk := NewDefault()
sk.AddHash(1)
if card := sk.Cardinality(); card != 1 {
t.Error("exepcted cardinality == 1, got", card)
}
}
*/
func TestCardinalityLinear(t *testing.T) {
sk := NewDefault()
rand.Seed(time.Now().Unix())
step := 1000000
unique := map[uint64]bool{}
for i := 1; len(unique) < 100000000; i++ {
hash := rand.Uint64()
sk.AddHash(hash)
unique[hash] = true
if len(unique)%step == 0 {
exact := uint64(len(unique))
res1 := uint64(sk.Cardinality())
ratio1 := 100 * estimateError(res1, exact)
if math.Abs(ratio1) > 2 {
t.Errorf("Normal: Exact %d, got %d which is %.2f%% error", exact, res1, ratio1)
}
}
}
}