forked from seiflotfy/sllb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sllb_test.go
79 lines (67 loc) · 1.6 KB
/
sllb_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
package sllb
import (
"bytes"
"encoding/gob"
"fmt"
"math"
"math/rand"
"reflect"
"testing"
)
func sumFromIndex(counts []uint64, index uint64) uint64 {
var count uint64
for i := int(index); i < len(counts); i++ {
count += counts[i]
}
return count
}
func TestInsertEstimate(t *testing.T) {
sllb, err := New(0.008)
if err != nil {
t.Error("Expected no error on NewSlidingHyperLogLog, got", err)
}
counts := make([]uint64, 100)
for i := 0; i < len(counts); i++ {
for j := 0; j <= rand.Intn(100000); j++ {
e := fmt.Sprintf("e-%d-%d", i, j)
sllb.InsertValue(uint64(i), []byte(e))
counts[i]++
}
}
for i := uint64(0); i <= uint64(len(counts)); i++ {
est := sllb.Estimate(i)
exp := sumFromIndex(counts, i)
offset := uint64(math.Abs(5 * float64(exp) / 100))
if est < exp-offset || est > exp+offset {
t.Errorf("%d Expected error <= 5.0%% for %d, got %d", i, exp, est)
}
}
}
func TestCodec(t *testing.T) {
c1, err := New(0.008)
if err != nil {
t.Error("Expected no error on NewSlidingHyperLogLog, got", err)
}
c2, err := New(0.008)
if err != nil {
t.Error("Expected no error on NewSlidingHyperLogLog, got", err)
}
counts := make([]uint64, 100)
for i := 0; i < len(counts); i++ {
for j := 0; j <= rand.Intn(100000); j++ {
e := fmt.Sprintf("e-%d-%d", i, j)
c1.InsertValue(uint64(i), []byte(e))
counts[i]++
}
}
var buf bytes.Buffer
if err := gob.NewEncoder(&buf).Encode(c1); err != nil {
t.Error(err)
}
if err := gob.NewDecoder(&buf).Decode(&c2); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(c1, c2) {
t.Errorf("unmarshaled structure differs")
}
}