-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathlist_test.go
394 lines (320 loc) · 12 KB
/
list_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
package structure
import (
_const "github.com/ByteStorage/FlyDB/lib/const"
"github.com/ByteStorage/FlyDB/lib/randkv"
"os"
"testing"
"time"
"github.com/ByteStorage/FlyDB/config"
"github.com/stretchr/testify/assert"
)
var listErr error
func initList() (*ListStructure, *config.Options) {
opts := config.DefaultOptions
dir, _ := os.MkdirTemp("", "TestListStructure")
opts.DirPath = dir
list, _ := NewListStructure(opts)
return list, &opts
}
func TestListStructure_TTL(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
//err := list.LPush("1", "123123", 0)
//assert.Nil(t, err)
//
//ttl, err := list.TTL("1")
//assert.Nil(t, err)
//assert.Equal(t, ttl, int64(0))
err = list.LPush("2", "123123", 2)
assert.Nil(t, err)
ttl, err := list.TTL("2")
assert.Nil(t, err)
assert.Equal(t, ttl, int64(2))
time.Sleep(1 * time.Second)
ttl, err = list.TTL("2")
assert.Nil(t, err)
assert.Equal(t, ttl, int64(1))
time.Sleep(2 * time.Second)
ttl, err = list.TTL("1")
assert.NotNil(t, err)
assert.Equal(t, ttl, int64(-1))
}
func TestListStructure_LPush(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LPush function when the key exists
listErr = list.LPush("key", "w", 10)
assert.Nil(t, listErr)
//// Test LPush function when the key does not exist
//listErr = list.LPush(string(randkv.GetTestKey(2)), randkv.RandomValue(100))
//assert.Nil(t, listErr)
}
func TestListStructure_LPushs(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LPushs function when the key exists
listErr = list.LPushs(string(randkv.GetTestKey(1)), 0, randkv.RandomValue(100), randkv.RandomValue(100))
assert.Nil(t, listErr)
// Test LPushs function when the key does not exist
listErr = list.LPushs(string(randkv.GetTestKey(2)), 0, randkv.RandomValue(100), randkv.RandomValue(100))
assert.Nil(t, listErr)
}
func TestListStructure_RPush(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test RPush function when the key exists
listErr = list.RPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
// Test RPush function when the key does not exist
listErr = list.RPush(string(randkv.GetTestKey(2)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
}
func TestListStructure_RPushs(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test RPushs function when the key exists
listErr = list.RPushs(string(randkv.GetTestKey(1)), 0, randkv.RandomValue(100), randkv.RandomValue(100))
assert.Nil(t, listErr)
// Test RPushs function when the key does not exist
listErr = list.RPushs(string(randkv.GetTestKey(2)), 0, randkv.RandomValue(100), randkv.RandomValue(100))
assert.Nil(t, listErr)
}
func TestListStructure_LPop(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LPop function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
value, err := list.LPop(string(randkv.GetTestKey(1)))
assert.Nil(t, err)
assert.NotNil(t, value)
// Test LPop function when the key does not exist
_, err = list.LPop(string(randkv.GetTestKey(2)))
assert.Equal(t, err, _const.ErrKeyNotFound)
// Test LPop function when the list is empty
err = list.LPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, err)
_, err = list.LPop(string(randkv.GetTestKey(3)))
assert.Nil(t, err)
_, err = list.LPop(string(randkv.GetTestKey(3)))
assert.Equal(t, err, ErrListEmpty)
}
func TestListStructure_RPop(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test RPop function when the key exists
listErr = list.RPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
value, err := list.RPop(string(randkv.GetTestKey(1)))
assert.Nil(t, err)
assert.NotNil(t, value)
// Test RPop function when the key does not exist
_, err = list.RPop(string(randkv.GetTestKey(2)))
assert.Equal(t, err, _const.ErrKeyNotFound)
// Test RPop function when the list is empty
err = list.RPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, err)
_, err = list.RPop(string(randkv.GetTestKey(3)))
assert.Nil(t, err)
_, err = list.RPop(string(randkv.GetTestKey(3)))
assert.Equal(t, err, ErrListEmpty)
}
func TestListStructure_LRange(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LRange function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
values, err := list.LRange(string(randkv.GetTestKey(1)), 0, 1)
assert.Nil(t, err)
assert.NotNil(t, values)
// Test LRange function when the key does not exist
_, err = list.LRange(string(randkv.GetTestKey(2)), 0, 1)
assert.Equal(t, err, _const.ErrKeyNotFound)
// Test LRange function when the list is empty
err = list.LPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, err)
_, err = list.LPop(string(randkv.GetTestKey(3)))
assert.Nil(t, err)
_, err = list.LRange(string(randkv.GetTestKey(3)), 0, 1)
assert.Equal(t, err, ErrListEmpty)
}
func TestListStructure_LLen(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LLen function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
len, err := list.LLen(string(randkv.GetTestKey(1)))
assert.Nil(t, err)
assert.Equal(t, len, 1)
// Test LLen function when the key does not exist
_, err = list.LLen(string(randkv.GetTestKey(2)))
assert.Equal(t, err, _const.ErrKeyNotFound)
}
func TestListStructure_LRem(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LRem function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.LRem(string(randkv.GetTestKey(1)), 1, randkv.RandomValue(100))
assert.Nil(t, listErr)
// Test LRem function when the key does not exist
listErr = list.LRem(string(randkv.GetTestKey(2)), 1, randkv.RandomValue(100))
assert.Equal(t, listErr, _const.ErrKeyNotFound)
}
func TestListStructure_LSet(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LSet function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.LSet(string(randkv.GetTestKey(1)), 0, randkv.RandomValue(200), 0)
assert.Nil(t, listErr)
// Test LSet function when the key does not exist
listErr = list.LSet(string(randkv.GetTestKey(2)), 0, randkv.RandomValue(100), 0)
assert.Equal(t, listErr, _const.ErrKeyNotFound)
// Test LSet function when the list is empty
listErr = list.LPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
_, listErr = list.LPop(string(randkv.GetTestKey(3)))
assert.Nil(t, listErr)
listErr = list.LSet(string(randkv.GetTestKey(3)), 0, randkv.RandomValue(100), 0)
assert.Equal(t, listErr, ErrIndexOutOfRange)
}
func TestListStructure_LTrim(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LTrim function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.LTrim(string(randkv.GetTestKey(1)), 0, 1)
assert.Nil(t, listErr)
// Test LTrim function when the key does not exist
listErr = list.LTrim(string(randkv.GetTestKey(2)), 0, 1)
assert.Equal(t, listErr, _const.ErrKeyNotFound)
// Test LTrim function when the list is empty
listErr = list.LPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
_, listErr = list.LPop(string(randkv.GetTestKey(3)))
assert.Nil(t, listErr)
listErr = list.LTrim(string(randkv.GetTestKey(3)), 0, 1)
assert.Equal(t, listErr, ErrListEmpty)
}
func TestListStructure_LIndex(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test LIndex function when the key exists
listErr = list.LPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
value, err := list.LIndex(string(randkv.GetTestKey(1)), 0)
assert.Nil(t, err)
assert.NotNil(t, value)
// Test LIndex function when the key does not exist
_, err = list.LIndex(string(randkv.GetTestKey(2)), 0)
assert.Equal(t, err, _const.ErrKeyNotFound)
// Test LIndex function when the list is empty
err = list.LPush(string(randkv.GetTestKey(3)), randkv.RandomValue(100), 0)
assert.Nil(t, err)
_, err = list.LPop(string(randkv.GetTestKey(3)))
assert.Nil(t, err)
_, err = list.LIndex(string(randkv.GetTestKey(3)), 0)
assert.Equal(t, err, ErrListEmpty)
}
func TestListStructure_RPOPLPUSH(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Test RPOPLPUSH function when the source list exists
listErr = list.RPush(string(randkv.GetTestKey(1)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.RPOPLPUSH(string(randkv.GetTestKey(1)), string(randkv.GetTestKey(2)), 0)
assert.Nil(t, listErr)
// Test RPOPLPUSH function when the source list does not exist
listErr = list.RPOPLPUSH(string(randkv.GetTestKey(3)), string(randkv.GetTestKey(2)), 0)
assert.Equal(t, listErr, _const.ErrKeyNotFound)
// Test RPOPLPUSH function when the source list is empty
listErr = list.RPush(string(randkv.GetTestKey(4)), randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
_, listErr = list.RPop(string(randkv.GetTestKey(4)))
assert.Nil(t, listErr)
listErr = list.RPOPLPUSH(string(randkv.GetTestKey(4)), string(randkv.GetTestKey(2)), 0)
assert.Equal(t, listErr, ErrListEmpty)
}
func TestListStructure_Integration(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
// Create a key and use LPush to add some values
key := string(randkv.GetTestKey(1))
values := [][]byte{randkv.RandomValue(100), randkv.RandomValue(100), randkv.RandomValue(100)}
for _, value := range values {
listErr = list.RPush(key, value, 0)
assert.Nil(t, listErr)
}
// Use LLen to check the length of the list
tmplen, err := list.LLen(key)
assert.Nil(t, err)
assert.Equal(t, tmplen, len(values))
// Use LRange to get all values of the list and check if they are correct
rangeValues, err := list.LRange(key, 0, -1)
assert.Nil(t, err)
bytesRangeValues := make([][]byte, len(rangeValues))
for i := 0; i < len(rangeValues); i++ {
bytesRangeValues[i] = rangeValues[i].([]byte)
}
assert.Equal(t, values, bytesRangeValues)
// Use LRem to remove a value and check if it is properly removed
err = list.LRem(key, 1, values[0])
assert.Nil(t, err)
rangeValues, err = list.LRange(key, 0, -1)
assert.Nil(t, err)
assert.NotContains(t, rangeValues, values[0])
// Use LSet to modify a value and check if it is properly modified
newValue := randkv.RandomValue(100)
err = list.LSet(key, 0, newValue, 0)
assert.Nil(t, err)
rangeValues, err = list.LRange(key, 0, -1)
assert.Nil(t, err)
assert.Contains(t, rangeValues, newValue)
// Use LTrim to trim the list and check if it is properly trimmed
err = list.LTrim(key, 0, 0)
assert.Nil(t, err)
rangeValues, err = list.LRange(key, 0, -1)
assert.Nil(t, err)
assert.Equal(t, len(rangeValues), 1)
// Use RPOPLPUSH to move a value to another list and check if it is properly moved
destination := string(randkv.GetTestKey(2))
err = list.RPOPLPUSH(key, destination, 0)
assert.Nil(t, err)
rangeValues, err = list.LRange(key, 0, -1)
assert.Equal(t, ErrListEmpty, err)
assert.Equal(t, len(rangeValues), 0)
rangeValues, err = list.LRange(destination, 0, -1)
assert.Nil(t, err)
assert.Equal(t, len(rangeValues), 1)
}
func TestListStructure_Keys(t *testing.T) {
list, _ := initList()
defer list.db.Clean()
listErr = list.LPush("111", randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.LPush("122", randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
listErr = list.LPush("123", randkv.RandomValue(100), 0)
assert.Nil(t, listErr)
keys, err := list.Keys("*")
assert.Nil(t, err)
assert.Equal(t, len(keys), 3)
keys, err = list.Keys("1*")
assert.Nil(t, err)
assert.Equal(t, len(keys), 3)
keys, err = list.Keys("12*")
assert.Nil(t, err)
assert.Equal(t, len(keys), 2)
keys, err = list.Keys("123")
assert.Nil(t, err)
assert.Equal(t, len(keys), 1)
keys, err = list.Keys("1?*")
assert.Nil(t, err)
assert.Equal(t, len(keys), 3)
}