-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_bitmap_test.go
297 lines (241 loc) · 7.22 KB
/
cmd_bitmap_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package redisson
import (
"context"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
const testCacheTTL = 1 * time.Minute
func cacheCmd(c Cmdable) CacheCmdable {
return c.Cache(testCacheTTL)
}
func testBitCount(ctx context.Context, c Cmdable) []string {
var key, value = "mykey", "foobar"
s := c.Set(ctx, key, value, 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
for _, v := range []struct {
key string
bc *BitCount
expected int64
}{
{key, nil, 26},
{key, &BitCount{Start: 0, End: 0}, 4},
{key, &BitCount{Start: 1, End: 1}, 6},
{value, nil, 0},
} {
bc := c.BitCount(ctx, v.key, v.bc)
So(bc.Err(), ShouldBeNil)
So(bc.Val(), ShouldEqual, v.expected)
bc = cacheCmd(c).BitCount(ctx, v.key, v.bc)
So(bc.Err(), ShouldBeNil)
So(bc.Val(), ShouldEqual, v.expected)
bc = cacheCmd(c).BitCount(ctx, v.key, v.bc)
So(bc.Err(), ShouldBeNil)
So(bc.Val(), ShouldEqual, v.expected)
}
return []string{key}
}
func testBitField(ctx context.Context, c Cmdable) []string {
var keys = []string{"mykey:{1}", "mystring:{1}"}
for _, v := range []struct {
key string
args []any
expected []int64
}{
{keys[0], []any{"INCRBY", "i5", 100, 1, "GET", "u4", 0}, []int64{1, 0}},
{keys[1], []any{"SET", "i8", "#0", 100, "SET", "i8", "#1", 200}, []int64{0, 0}},
{keys[0], []any{"incrby", "u2", 100, 1, "OVERFLOW", "SAT", "incrby", "u2", 102, 1}, []int64{1, 1}},
{keys[0], []any{"incrby", "u2", 100, 1, "OVERFLOW", "SAT", "incrby", "u2", 102, 1}, []int64{2, 2}},
{keys[0], []any{"incrby", "u2", 100, 1, "OVERFLOW", "SAT", "incrby", "u2", 102, 1}, []int64{3, 3}},
{keys[0], []any{"incrby", "u2", 100, 1, "OVERFLOW", "SAT", "incrby", "u2", 102, 1}, []int64{0, 3}},
} {
bc := c.BitField(ctx, v.key, v.args...)
So(bc.Err(), ShouldBeNil)
So(len(bc.Val()), ShouldEqual, len(v.expected))
So(bc.Val(), ShouldResemble, v.expected)
}
return keys
}
func testBitOpAnd(ctx context.Context, c Cmdable) []string {
var key1, key2, dest = "key1:{1}", "key2:{1}", "dest:{1}"
s := c.Set(ctx, key1, "1", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
s = c.Set(ctx, key2, "0", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
bo := c.BitOpAnd(ctx, dest, key1, key2)
So(bo.Err(), ShouldBeNil)
So(bo.Val(), ShouldEqual, 1)
g := c.Get(ctx, dest)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, "0")
return nil
}
func testBitOpOr(ctx context.Context, c Cmdable) []string {
var key1, key2, dest = "key1:{1}", "key2:{1}", "dest:{1}"
s := c.Set(ctx, key1, "1", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
s = c.Set(ctx, key2, "0", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
bo := c.BitOpOr(ctx, dest, key1, key2)
So(bo.Err(), ShouldBeNil)
So(bo.Val(), ShouldEqual, 1)
g := c.Get(ctx, dest)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, "1")
return nil
}
func testBitOpXor(ctx context.Context, c Cmdable) []string {
var key1, key2, dest = "key1:{1}", "key2:{1}", "dest:{1}"
s := c.Set(ctx, key1, "\xff", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
s = c.Set(ctx, key2, "\x0f", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
bo := c.BitOpXor(ctx, dest, key1, key2)
So(bo.Err(), ShouldBeNil)
So(bo.Val(), ShouldEqual, 1)
g := c.Get(ctx, dest)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, "\xf0")
return nil
}
func testBitOpNot(ctx context.Context, c Cmdable) []string {
var key1, key2, dest = "key1:{1}", "key2:{1}", "dest:{1}"
s := c.Set(ctx, key1, "\x00", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
bo := c.BitOpNot(ctx, dest, key1)
So(bo.Err(), ShouldBeNil)
So(bo.Val(), ShouldEqual, 1)
g := c.Get(ctx, dest)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, "\xff")
return []string{key1, key2, dest}
}
func testBitPos(ctx context.Context, c Cmdable) []string {
var key1 = "key1"
s := c.Set(ctx, key1, "\xff\xf0\x00", 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, OK)
pos := c.BitPos(ctx, key1, 0)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, 12)
pos = c.BitPos(ctx, key1, 1)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, 0)
pos = c.BitPos(ctx, key1, 0, 2)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, 16)
pos = c.BitPos(ctx, key1, 1, 2)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, -1)
pos = c.BitPos(ctx, key1, 0, -1)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, 16)
pos = c.BitPos(ctx, key1, 1, -1)
So(s.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, -1)
pos = c.BitPos(ctx, key1, 0, 2, 1)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, -1)
pos = c.BitPos(ctx, key1, 0, 0, -3)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, -1)
pos = c.BitPos(ctx, key1, 0, 0, 0)
So(pos.Err(), ShouldBeNil)
So(pos.Val(), ShouldEqual, -1)
return []string{key1}
}
func testGetBit(ctx context.Context, c Cmdable) []string {
var key1 = "key1"
s := c.SetBit(ctx, key1, 7, 1)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, 0)
s = c.SetBit(ctx, key1, 7, 1)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, 1)
g := c.GetBit(ctx, key1, 0)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, 0)
g = c.GetBit(ctx, key1, 7)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, 1)
g = cacheCmd(c).GetBit(ctx, key1, 100)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, 0)
d := c.Del(ctx, key1)
So(d.Err(), ShouldBeNil)
So(d.Val(), ShouldEqual, 1)
return []string{key1}
}
func testSetBit(ctx context.Context, c Cmdable) []string {
var key1 = "key1"
s := c.SetBit(ctx, key1, 7, 1)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, 0)
s = c.SetBit(ctx, key1, 7, 0)
So(s.Err(), ShouldBeNil)
So(s.Val(), ShouldEqual, 1)
g := c.Get(ctx, key1)
So(g.Err(), ShouldBeNil)
So(g.Val(), ShouldEqual, "\x00")
d := c.Del(ctx, key1)
So(d.Err(), ShouldBeNil)
So(d.Val(), ShouldEqual, 1)
return []string{key1}
}
func doTestUnitClean(ctx context.Context, c Cmdable, keys []string) {
if len(keys) > 0 {
So(c.Del(ctx, keys...).Err(), ShouldBeNil)
}
if !c.Options().GetDevelopment() {
c.FlushAll(context.Background())
}
}
type TestUnitName interface {
String() string
}
type TestUnit struct {
Name TestUnitName
Func func(ctx context.Context, c Cmdable) []string
}
func bitMapTestUnits() []TestUnit {
return []TestUnit{
{CommandBitCount, testBitCount},
{CommandBitField, testBitField},
{CommandBitOpAnd, testBitOpAnd},
{CommandBitOpOr, testBitOpOr},
{CommandBitOpXor, testBitOpXor},
{CommandBitOpNot, testBitOpNot},
{CommandBitPos, testBitPos},
{CommandGetBit, testGetBit},
{CommandSetBit, testSetBit},
}
}
func _doTestUnits(t *testing.T, c Cmdable, unitsFunc func() []TestUnit) {
t.Cleanup(func() {
_ = c.Close()
})
if !c.Options().GetDevelopment() {
c.FlushAll(context.Background())
}
var ctx = context.Background()
for _, v := range unitsFunc() {
Convey(v.Name.String(), t, func() { doTestUnitClean(ctx, c, v.Func(ctx, c)) })
}
}
func doTestUnits(t *testing.T, unitsFunc func() []TestUnit) {
c := MustNewClient(NewConf(WithDevelopment(false)))
_doTestUnits(t, c, unitsFunc)
}
func doClusterTestUnits(t *testing.T, unitsFunc func() []TestUnit) {
c := MustNewClient(NewConf(WithDevelopment(true)))
_doTestUnits(t, c, unitsFunc)
}
func TestClient_BitMap(t *testing.T) { doTestUnits(t, bitMapTestUnits) }