forked from nutsdb/nutsdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set_test.go
445 lines (375 loc) · 11.3 KB
/
set_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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
// Copyright 2023 The nutsdb Author. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nutsdb
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestSet_SAdd(t *testing.T) {
set := NewSet()
key := "key"
expectRecords := generateRecords(3)
values := make([][]byte, 3)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key, values, expectRecords))
ok, err := set.SAreMembers(key, values...)
require.True(t, ok)
require.NoError(t, err)
}
func TestSet_SRem(t *testing.T) {
set := NewSet()
key := "key"
expectRecords := generateRecords(4)
values := make([][]byte, 4)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key, values, expectRecords))
require.NoError(t, set.SRem(key, values[0]))
require.NoError(t, set.SRem(key, values[1:]...))
require.NoError(t, set.SAdd(key, values, expectRecords))
require.NoError(t, set.SRem(key, values...))
require.Error(t, set.SRem("fake key", values...))
require.Error(t, set.SRem(key, nil))
}
func TestSet_SDiff(t *testing.T) {
set := NewSet()
key1 := "set1"
key2 := "set2"
key3 := "set3"
key4 := "set4"
expectRecords := generateRecords(10)
values := make([][]byte, 10)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key1, values[:5], expectRecords[:5]))
require.NoError(t, set.SAdd(key2, values[2:6], expectRecords[2:6]))
require.NoError(t, set.SAdd(key3, values[2:7], expectRecords[2:7]))
require.NoError(t, set.SAdd(key4, values[7:], expectRecords[7:]))
type args struct {
key1 string
key2 string
}
tests := []struct {
name string
args args
set *Set
want []*Record
wantErr bool
}{
{"normal set diff1", args{key1, key2}, set, expectRecords[:2], false},
{"normal set diff2", args{key1, key4}, set, expectRecords[:5], false},
{"normal set diff3", args{key3, key2}, set, expectRecords[6:7], false},
{"first fake set", args{"fake_key1", key2}, set, nil, true},
{"second fake set", args{key1, "fake_key2"}, set, nil, true},
{"two fake set", args{"fake_key1", "fake_key2"}, set, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.set.SDiff(tt.args.key1, tt.args.key2)
if (err != nil) != tt.wantErr {
require.Errorf(t, err, "Get() error = %v, wantErr %v", tt.wantErr)
}
require.ElementsMatchf(t, tt.want, got, "Get() got = %v, want %v", got, tt.want)
})
}
}
func TestSet_SCard(t *testing.T) {
set := NewSet()
key1 := "set1"
key2 := "set2"
key3 := "set3"
key4 := "set4"
expectRecords := generateRecords(10)
values := make([][]byte, 10)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key1, values[:5], expectRecords[:5]))
require.NoError(t, set.SAdd(key2, values[2:6], expectRecords[2:6]))
require.NoError(t, set.SAdd(key3, values[2:7], expectRecords[2:7]))
require.NoError(t, set.SAdd(key4, values[7:], expectRecords[7:]))
tests := []struct {
name string
key string
set *Set
want int
}{
{"normal set", key1, set, 5},
{"normal set", key2, set, 4},
{"normal set", key3, set, 5},
{"fake key", "key_fake", set, 0},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := tt.set.SCard(tt.key)
require.Equalf(t, tt.want, got, "TestSet_SCard err")
})
}
}
func TestSet_SInter(t *testing.T) {
set := NewSet()
key1 := "set1"
key2 := "set2"
key3 := "set3"
key4 := "set4"
expectRecords := generateRecords(10)
values := make([][]byte, 10)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key1, values[:5], expectRecords[:5]))
require.NoError(t, set.SAdd(key2, values[2:6], expectRecords[2:6]))
require.NoError(t, set.SAdd(key3, values[2:7], expectRecords[2:7]))
require.NoError(t, set.SAdd(key4, values[7:], expectRecords[7:]))
type args struct {
key1 string
key2 string
}
tests := []struct {
name string
args args
set *Set
want []*Record
wantErr bool
}{
{"normal set inter1", args{key1, key2}, set, []*Record{expectRecords[2], expectRecords[3], expectRecords[4]}, false},
{"normal set inter1", args{key2, key3}, set, []*Record{expectRecords[2], expectRecords[3], expectRecords[4], expectRecords[5]}, false},
{"normal set inter2", args{key1, key4}, set, nil, false},
{"first fake set", args{"fake_key1", key2}, set, nil, true},
{"second fake set", args{key1, "fake_key2"}, set, nil, true},
{"two fake set", args{"fake_key1", "fake_key2"}, set, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.set.SInter(tt.args.key1, tt.args.key2)
if (err != nil) != tt.wantErr {
require.Errorf(t, err, "Get() error = %v, wantErr %v", tt.wantErr)
}
require.ElementsMatchf(t, tt.want, got, "Get() got = %v, want %v", got, tt.want)
})
}
}
func TestSet_SMembers(t *testing.T) {
set := NewSet()
key := "set"
expectRecords := generateRecords(3)
values := make([][]byte, 3)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key, values, expectRecords))
tests := []struct {
name string
key string
set *Set
want []*Record
wantErr bool
}{
{"normal SMembers", key, set, expectRecords[0:1], false},
{"normal SMembers", key, set, expectRecords[1:], false},
{"normal SMembers", key, set, expectRecords, false},
{"fake key", "fake_key", set, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.set.SMembers(tt.key)
if (err != nil) != tt.wantErr {
require.Errorf(t, err, "Get() error = %v, wantErr %v", tt.wantErr)
}
require.Subsetf(t, got, tt.want, "SInter() got = %v, want = %v", got, tt.want)
})
}
}
func TestSet_SMove(t *testing.T) {
set := NewSet()
key1 := "set1"
key2 := "set2"
expectRecords := generateRecords(3)
values := make([][]byte, 3)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key1, values[:2], expectRecords[:2]))
require.NoError(t, set.SAdd(key2, values[2:], expectRecords[2:]))
type args struct {
key1 string
key2 string
item []byte
}
tests := []struct {
name string
args args
set *Set
want1 []*Record
want2 []*Record
expectErr error
}{
{"normal SMove", args{key1, key2, values[1]}, set, expectRecords[0:1], expectRecords[1:], nil},
{"not exist member SMove", args{key1, key2, values[2]}, set, nil, nil, ErrSetMemberNotExist},
{"fake key SMove1", args{"fake key", key2, values[2]}, set, nil, nil, ErrSetNotExist},
{"fake key SMove", args{key1, "fake key", values[2]}, set, nil, nil, ErrSetNotExist},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := tt.set.SMove(tt.args.key1, tt.args.key2, tt.args.item)
if tt.expectErr != nil {
require.Error(t, err)
require.Equal(t, tt.expectErr, err)
} else {
got1, _ := tt.set.SMembers(tt.args.key1)
got2, _ := tt.set.SMembers(tt.args.key2)
require.ElementsMatchf(t, got1, tt.want1, "SMove() got = %v, want = %v", got1, tt.want1)
require.ElementsMatchf(t, got2, tt.want2, "SMove() got = %v, want = %v", got2, tt.want2)
}
})
}
}
func TestSet_SPop(t *testing.T) {
set := NewSet()
key := "set"
expectRecords := generateRecords(2)
values := make([][]byte, 2)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
m := map[*Record]struct{}{}
for _, expectRecord := range expectRecords {
m[expectRecord] = struct{}{}
}
require.NoError(t, set.SAdd(key, values, expectRecords))
tests := []struct {
name string
key string
set *Set
ok bool
}{
{"normal set SPop", key, set, true},
{"normal set SPop", key, set, true},
{"normal set SPop", key, set, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
record := tt.set.SPop(tt.key)
_, ok := m[record]
require.Equal(t, tt.ok, ok)
})
}
}
func TestSet_SIsMember(t *testing.T) {
set := NewSet()
key := "key"
expectRecords := generateRecords(1)
values := [][]byte{expectRecords[0].Value}
require.NoError(t, set.SAdd(key, values, expectRecords))
tests := []struct {
key string
val []byte
ok bool
expectErr error
}{
{key, values[0], true, nil},
{key, GetRandomBytes(24), false, nil},
{"fake key", GetRandomBytes(24), false, ErrSetNotExist},
}
for _, tt := range tests {
ok, err := set.SIsMember(tt.key, tt.val)
if tt.expectErr != nil {
require.Equal(t, tt.expectErr, err)
} else {
require.Equal(t, tt.ok, ok)
}
}
}
func TestSet_SAreMembers(t *testing.T) {
set := NewSet()
key := "set"
expectRecords := generateRecords(4)
values := make([][]byte, 4)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key, values, expectRecords))
tests := []struct {
key string
val [][]byte
ok bool
expectErr error
}{
{key, values[0:2], true, nil},
{key, values[2:], true, nil},
{key, values, true, nil},
{key, [][]byte{GetRandomBytes(24)}, false, nil},
{"fake key", values, true, ErrSetNotExist},
}
for _, tt := range tests {
ok, err := set.SAreMembers(tt.key, tt.val...)
if tt.expectErr != nil {
require.Equal(t, tt.expectErr, err)
} else {
require.Equal(t, tt.ok, ok)
}
}
}
func TestSet_SUnion(t *testing.T) {
set := NewSet()
key1 := "set1"
key2 := "set2"
key3 := "set3"
key4 := "set4"
expectRecords := generateRecords(10)
values := make([][]byte, 10)
for i := range expectRecords {
values[i] = expectRecords[i].Value
}
require.NoError(t, set.SAdd(key1, values[:5], expectRecords[:5]))
require.NoError(t, set.SAdd(key2, values[2:6], expectRecords[2:6]))
require.NoError(t, set.SAdd(key3, values[2:7], expectRecords[2:7]))
require.NoError(t, set.SAdd(key4, values[7:], expectRecords[7:]))
type args struct {
key1 string
key2 string
}
tests := []struct {
name string
args args
set *Set
want []*Record
wantErr bool
}{
{"normal set Union1", args{key1, key4}, set,
[]*Record{expectRecords[0], expectRecords[1], expectRecords[2], expectRecords[3], expectRecords[4], expectRecords[7], expectRecords[8], expectRecords[9]},
false},
{
"normal set Union2", args{key2, key3}, set,
[]*Record{expectRecords[2], expectRecords[3], expectRecords[4], expectRecords[5], expectRecords[6]},
false,
},
{"first fake set", args{"fake_key1", key2}, set, nil, true},
{"second fake set", args{key1, "fake_key2"}, set, nil, true},
{"two fake set", args{"fake_key1", "fake_key2"}, set, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.set.SUnion(tt.args.key1, tt.args.key2)
if (err != nil) != tt.wantErr {
require.Errorf(t, err, "Get() error = %v, wantErr %v", tt.wantErr)
}
require.ElementsMatchf(t, tt.want, got, "Get() got = %v, want %v", got, tt.want)
})
}
}