-
Notifications
You must be signed in to change notification settings - Fork 217
/
cmd_hll_test.go
223 lines (193 loc) · 3.96 KB
/
cmd_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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package miniredis
import (
"testing"
"github.com/alicebob/miniredis/v2/proto"
)
// Test PFADD
func TestPfadd(t *testing.T) {
s, c := runWithClient(t)
mustDo(t, c,
"PFADD", "h", "aap", "noot", "mies",
proto.Int(1),
)
mustDo(t, c,
"PFADD", "h", "aap", // already exists in hll => returns 0
proto.Int(0),
)
mustDo(t, c,
"TYPE", "h",
proto.Inline("hll"),
)
t.Run("direct usage", func(t *testing.T) {
added, err := s.SetAdd("s1", "aap")
ok(t, err)
equals(t, 1, added)
members, err := s.Members("s1")
ok(t, err)
equals(t, []string{"aap"}, members)
})
t.Run("errors", func(t *testing.T) {
mustOK(t, c, "SET", "str", "value")
mustDo(t, c,
"PFADD", "str", "hi",
proto.Error(msgNotValidHllValue),
)
// Wrong argument counts
mustDo(t, c,
"PFADD",
proto.Error(errWrongNumber("pfadd")),
)
})
}
// Test PFCOUNT
func TestPfcount(t *testing.T) {
s, c := runWithClient(t)
// Add 100 unique random values
for i := 0; i < 100; i++ {
mustDo(t, c,
"PFADD", "h1", randomStr(10),
proto.Int(1), // hll changes each time
)
}
// Add 1 more unique value
specificValue := randomStr(10)
mustDo(t, c,
"PFADD", "h1", specificValue,
proto.Int(1), // hll changes because of new element
)
for i := 0; i < 50; i++ {
mustDo(t, c,
"PFADD", "h1", specificValue,
proto.Int(0), // hll doesn't change because this element has already been added before
)
}
mustDo(t, c,
"PFCOUNT", "h1",
proto.Int(101),
)
// Create a new hll
mustDo(t, c,
"PFADD", "h2", randomStr(10), randomStr(10), randomStr(10),
proto.Int(1),
)
mustDo(t, c,
"PFCOUNT", "h2",
proto.Int(3),
)
// Several hlls are involved - a sum of all the counts is returned
mustDo(t, c,
"PFCOUNT",
"h1", // has 101 unique values
"h2", // has 3 unique values
"h3", // empty key
proto.Int(104),
)
// A nonexisting key
mustDo(t, c,
"PFCOUNT", "h9",
proto.Int(0),
)
t.Run("errors", func(t *testing.T) {
s.Set("str", "value")
mustDo(t, c,
"PFCOUNT",
proto.Error(errWrongNumber("pfcount")),
)
mustDo(t, c,
"PFCOUNT", "str",
proto.Error(msgNotValidHllValue),
)
mustDo(t, c,
"PFCOUNT", "h1", "str",
proto.Error(msgNotValidHllValue),
)
})
}
// Test PFMERGE
func TestPfmerge(t *testing.T) {
s, c := runWithClient(t)
// Add 100 unique random values to h1 and 50 of these 100 to h2
for i := 0; i < 100; i++ {
value := randomStr(10)
mustDo(t, c,
"PFADD", "h1", value,
proto.Int(1), // hll changes each time
)
if i%2 == 0 {
mustDo(t, c,
"PFADD", "h2", value,
proto.Int(1), // hll changes each time
)
}
}
for i := 0; i < 100; i++ {
mustDo(t, c,
"PFADD", "h3", randomStr(10),
proto.Int(1), // hll changes each time
)
}
// Merge non-intersecting hlls
{
mustOK(t, c,
"PFMERGE",
"res1",
"h1", // count 100
"h3", // count 100
)
mustDo(t, c,
"PFCOUNT", "res1",
proto.Int(200),
)
}
// Merge intersecting hlls
{
mustOK(t, c,
"PFMERGE",
"res2",
"h1", // count 100
"h2", // count 50 (all 50 are presented in h1)
)
mustDo(t, c,
"PFCOUNT", "res2",
proto.Int(100),
)
}
// Merge all hlls
{
mustOK(t, c,
"PFMERGE",
"res3",
"h1", // count 100
"h2", // count 50 (all 50 are presented in h1)
"h3", // count 100
"h4", // empty key
)
mustDo(t, c,
"PFCOUNT", "res3",
proto.Int(200),
)
}
t.Run("direct", func(t *testing.T) {
commonElem := randomStr(10)
s.PfAdd("h5", commonElem, randomStr(10), randomStr(10), randomStr(10), randomStr(10))
s.PfAdd("h6", commonElem, randomStr(10), randomStr(10))
sum, err := s.PfCount("h5", "h6", "h7") // h7 is empty
ok(t, err)
equals(t, sum, 8)
s.PfMerge("h8", "h5", "h6")
sum, err = s.PfCount("h8")
ok(t, err)
equals(t, sum, 7) // common elem is counted once
})
t.Run("errors", func(t *testing.T) {
s.Set("str", "value")
mustDo(t, c,
"PFMERGE",
proto.Error(errWrongNumber("pfmerge")),
)
mustDo(t, c,
"PFMERGE", "h10", "str",
proto.Error(msgNotValidHllValue),
)
})
}