-
Notifications
You must be signed in to change notification settings - Fork 12
/
sparse_test.go
96 lines (71 loc) · 1.61 KB
/
sparse_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
// Copyright (c) 2018, RetailNext, Inc.
// All rights reserved.
package hllpp
import (
"math/rand"
"testing"
"time"
)
func TestSparseReaderWriter(t *testing.T) {
writer := newSparseWriter()
if writer.Len() != 0 {
t.Errorf("got %d", writer.Len())
}
if len(writer.Bytes()) != 0 {
t.Errorf("got %+v", writer.Bytes())
}
reader := newSparseReader(writer.Bytes())
if !reader.Done() {
t.Errorf("should be done")
}
writer.Append(127, 0, 1)
// same idx, but bigger rho than previous, use this one
writer.Append(126, 0, 2)
if writer.Len() != 1 || len(writer.Bytes()) != 1 {
t.Errorf("got %d", writer.Len())
}
// show we are storing deltas since 128 takes two bytes as
// a varint
writer.Append(128, 1, 0)
if writer.Len() != 2 || len(writer.Bytes()) != 2 {
t.Errorf("got %d", writer.Len())
}
reader = newSparseReader(writer.Bytes())
if reader.Done() {
t.Errorf("shouldn't be done")
}
if reader.Peek() != 126 {
t.Errorf("got %d", reader.Peek())
}
if reader.Peek() != 126 {
t.Errorf("got %d", reader.Peek())
}
reader.Advance()
if reader.Done() {
t.Errorf("shouldn't be done")
}
if reader.Peek() != 128 {
t.Errorf("got %d", reader.Peek())
}
reader.Advance()
if !reader.Done() {
t.Errorf("should be done")
}
}
func TestSparseMerge(t *testing.T) {
gen := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 1000; i++ {
v1 := intToBytes(gen.Uint64())
v2 := intToBytes(gen.Uint64())
h := New()
h.Add(v1)
h.Add(v2)
other := New()
other.Add(v1)
h.flushTmpSet()
h.Merge(other)
if h.Count() != 2 {
t.Fatalf("iter %d got %d", i, h.Count())
}
}
}