-
Notifications
You must be signed in to change notification settings - Fork 6
/
marshal_test.go
194 lines (155 loc) · 4.24 KB
/
marshal_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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package hll
import (
"bytes"
crand "crypto/rand"
"encoding/gob"
"encoding/json"
mrand "math/rand"
"testing"
"github.com/bmizerany/assert"
)
func TestMarshalRoundTrip(t *testing.T) {
const p, pPrime = 14, 25
testCases := []struct {
p, pPrime uint
}{
{5, 10},
{10, 25},
{15, 25},
}
for _, testCase := range testCases {
h := NewHll(testCase.p, testCase.pPrime)
for i := uint64(0); i <= 1e5; i++ {
if i%5000 == 0 {
// Every N elements, do a round-trip marshal and unmarshal and make sure cardinality is
// preserved.
jBuf, err := json.Marshal(h)
assert.Equalf(t, nil, err, "%v", err)
rt := &Hll{}
err = json.Unmarshal(jBuf, rt)
assert.Equalf(t, nil, err, "%v", err)
assert.Equal(t, rt.Cardinality(), h.Cardinality())
}
h.Add(randUint64(t))
}
assert.T(t, !h.isSparse) // Ensure we stored enough to use the dense representation.
}
}
// Make sure that after roundtripping, an Hll is still usable and behaves identically.
func TestUsageAfterMarshalRoundTrip(t *testing.T) {
h := NewHll(10, 20)
h.Add(randUint64(t))
h.Add(randUint64(t))
h.Add(randUint64(t))
jBuf, err := json.Marshal(h)
assert.Equalf(t, nil, err, "%v", err)
rt := &Hll{}
err = json.Unmarshal(jBuf, rt)
assert.Equalf(t, nil, err, "%v", err)
for i := uint64(100); i < 1000; i++ {
r := randUint64(t)
rt.Add(r)
h.Add(r)
assert.Equalf(t, rt.isSparse, h.isSparse, "%v", i)
// fmt.Printf("2 calling rt.Cardinality(), rt.isSparse=%v\n", rt.isSparse)
rtCard := rt.Cardinality()
// fmt.Printf("3\n")
hCard := h.Cardinality()
assert.Equal(t, rtCard, hCard, i)
}
assert.T(t, !h.isSparse)
}
// The JSON-marshaled form of an Hll should include either a sparseList or a dense/normal register
// array, and not both. This checks whether we got the JSON library omitempty usage right.
func TestMarshalOmit(t *testing.T) {
h := NewHll(10, 25)
check := func() {
jBuf, err := json.Marshal(h)
assert.Equal(t, nil, err)
m := map[string]interface{}{}
err = json.Unmarshal(jBuf, &m)
assert.Equal(t, nil, err)
_, hasDense := m["M"]
_, hasSparse := m["s"]
assert.T(t, hasDense || hasSparse)
assert.T(t, !(hasDense && hasSparse))
}
for h.isSparse {
h.Add(randUint64(t))
check() // This checks the sparse case.
}
check() // This checks the dense case.
}
func TestMarshalPbRoundtrip(t *testing.T) {
const p, pPrime = 14, 25
testCases := []struct {
p, pPrime uint
}{
{5, 10},
{10, 25},
{15, 25},
}
for _, testCase := range testCases {
h := NewHll(testCase.p, testCase.pPrime)
for i := uint64(0); i <= 1e5; i++ {
if i%5000 == 0 {
// Every N elements, do a round-trip marshal and unmarshal and make sure cardinality is
// preserved.
pbBuf, err := h.MarshalPb()
assert.Equalf(t, nil, err, "%v", err)
rt := &Hll{}
err = rt.UnmarshalPb(pbBuf)
assert.Equalf(t, nil, err, "%v", err)
assert.Equal(t, rt.Cardinality(), h.Cardinality())
}
h.Add(randUint64(t))
}
assert.T(t, !h.isSparse) // Ensure we stored enough to use the dense representation.
}
}
func TestMarshalGobRoundTrip(t *testing.T) {
const p, pPrime = 14, 25
testCases := []struct {
p, pPrime uint
}{
{5, 10},
{10, 25},
{15, 25},
}
for _, testCase := range testCases {
h := NewHll(testCase.p, testCase.pPrime)
for i := uint64(0); i <= 1e5; i++ {
if i%5000 == 0 {
// Every N elements, do a round-trip marshal and unmarshal and make sure cardinality is
// preserved.
var val bytes.Buffer
enc := gob.NewEncoder(&val)
err := enc.Encode(h)
assert.Equal(t, err, nil)
// decode
dec := gob.NewDecoder(&val)
rt := &Hll{}
err = dec.Decode(rt)
assert.Equal(t, err, nil)
assert.Equal(t, rt.Cardinality(), h.Cardinality())
}
h.Add(randUint64(t))
}
assert.T(t, !h.isSparse) // Ensure we stored enough to use the dense representation.
}
}
func TestCompression(t *testing.T) {
const numTests = 1000
for i := 0; i < numTests; i++ {
numToRead := mrand.Intn(100)
buf := make([]byte, numToRead)
n, err := crand.Read(buf)
assert.Equal(t, nil, err)
assert.Equal(t, n, numToRead)
compressed, err := snappyB64(buf)
assert.Equal(t, nil, err)
roundTripped, err := unsnappyB64(compressed)
assert.Equal(t, nil, err)
assert.Equal(t, buf, roundTripped)
}
}