-
Notifications
You must be signed in to change notification settings - Fork 8
/
gob_test.go
93 lines (78 loc) · 2.03 KB
/
gob_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
package gohll
import (
"bytes"
"encoding/gob"
"fmt"
"math"
"math/rand"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGobEmpty(t *testing.T) {
h := &HLL{}
var buf bytes.Buffer
err := gob.NewEncoder(&buf).Encode(h)
assert.Nil(t, err)
var h2 HLL
err = gob.NewDecoder(&buf).Decode(&h2)
assert.Nil(t, err)
if h2.tempSet == nil {
t.Fatal("Gob decode failed for h2.tempSet")
}
if h2.sparseList == nil {
t.Fatal("Gob decode failed for h2.sparseList")
}
}
func TestGobSparse(t *testing.T) {
h, err := NewHLL(20)
assert.Nil(t, err)
var i float64
for i = 0; i <= 50000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
}
assert.Equal(t, h.format, SPARSE, "Not using sparse mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m2))
checkErrorBounds(t, c, i, errorRate)
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(h)
assert.Nil(t, err)
var h2 HLL
err = gob.NewDecoder(&buf).Decode(&h2)
assert.Nil(t, err)
assert.Equal(t, h2.format, SPARSE, "Not using sparse mode")
assert.Equal(t, c, h2.Cardinality())
for i = 0; i <= 40000; i++ {
v := rand.Uint32()
h.Add(fmt.Sprintf("%d-%d", int(i), v))
h2.Add(fmt.Sprintf("%d-%d", int(i), v))
}
assert.Equal(t, h.Cardinality(), h2.Cardinality())
}
func TestGobNormal(t *testing.T) {
h, err := NewHLL(10)
assert.Nil(t, err)
h.ToNormal()
var i float64
for i = 0; i <= 100000; i++ {
h.Add(fmt.Sprintf("%d-%d", int(i), rand.Uint32()))
}
assert.Equal(t, h.format, NORMAL, "Not using normal mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m1))
checkErrorBounds(t, c, i, errorRate)
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(h)
assert.Nil(t, err)
var h2 HLL
err = gob.NewDecoder(&buf).Decode(&h2)
assert.Nil(t, err)
assert.Equal(t, h2.format, NORMAL, "Not using normal mode")
assert.Equal(t, c, h2.Cardinality())
for i = 0; i <= 40000; i++ {
v := rand.Uint32()
h.Add(fmt.Sprintf("%d-%d", int(i), v))
h2.Add(fmt.Sprintf("%d-%d", int(i), v))
}
assert.Equal(t, h.Cardinality(), h2.Cardinality())
}