forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhll_test.go
101 lines (88 loc) · 2.17 KB
/
hll_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
//go:build int
// +build int
package main
// Hash keys.
import (
"math/rand"
"testing"
)
func TestHll(t *testing.T) {
t.Run("basics", func(t *testing.T) {
testRaw(t, func(c *client) {
// Add 100 unique random values to h1 and 50 of these 100 to h2
for i := 0; i < 100; i++ {
value := randomStr(10)
c.Do("PFADD", "h1", value)
if i%2 == 0 {
c.Do("PFADD", "h2", value)
}
}
for i := 0; i < 100; i++ {
c.Do("PFADD", "h3", randomStr(10))
}
// Merge non-intersecting hlls
{
c.Do(
"PFMERGE",
"res1",
"h1", // count 100
"h3", // count 100
)
c.DoApprox(2, "PFCOUNT", "res1")
}
// Merge intersecting hlls
{
c.Do(
"PFMERGE",
"res2",
"h1", // count 100
"h2", // count 50 (all 50 are presented in h1)
)
c.DoApprox(2, "PFCOUNT", "res2")
}
// Merge all hlls
{
c.Do(
"PFMERGE",
"res3",
"h1", // count 100
"h2", // count 50 (all 50 are presented in h1)
"h3", // count 100
"h4", // empty key
)
c.DoApprox(2, "PFCOUNT", "res3")
}
// failure cases
c.Error("wrong number", "PFADD")
c.Error("wrong number", "PFCOUNT")
c.Error("wrong number", "PFMERGE")
c.Do("SET", "str", "I am a string")
c.Error("not a valid HyperLogLog", "PFADD", "str", "noot", "mies")
c.Error("not a valid HyperLogLog", "PFCOUNT", "str", "h1")
c.Error("not a valid HyperLogLog", "PFMERGE", "str", "noot")
c.Error("not a valid HyperLogLog", "PFMERGE", "noot", "str")
c.Do("DEL", "h1", "h2", "h3", "h4", "res1", "res2", "res3")
c.Do("PFCOUNT", "h1", "h2", "h3", "h4", "res1", "res2", "res3")
})
})
t.Run("tx", func(t *testing.T) {
testRaw(t, func(c *client) {
c.Do("MULTI")
c.Do("PFADD", "h1", "noot", "mies", "vuur", "wim")
c.Do("PFADD", "h2", "noot1", "mies1", "vuur1", "wim1")
c.Do("PFMERGE", "h3", "h1", "h2")
c.Do("PFCOUNT", "h1")
c.Do("PFCOUNT", "h2")
c.Do("PFCOUNT", "h3")
c.Do("EXEC")
})
})
}
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
func randomStr(length int) string {
b := make([]byte, length)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}