forked from redis/redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-redis.tcl
2083 lines (1848 loc) · 57.8 KB
/
test-redis.tcl
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# test-redis.tcl
# Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo [email protected]
# This softare is released under the BSD License. See the COPYING file for
# more information.
set tcl_precision 17
source redis.tcl
set ::passed 0
set ::failed 0
set ::testnum 0
proc test {name code okpattern} {
incr ::testnum
if {$::testnum < $::first || $::testnum > $::last} return
puts -nonewline [format "%-70s " "#$::testnum $name"]
flush stdout
set retval [uplevel 1 $code]
if {$okpattern eq $retval || [string match $okpattern $retval]} {
puts "PASSED"
incr ::passed
} else {
puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
incr ::failed
}
}
proc randstring {min max {type binary}} {
set len [expr {$min+int(rand()*($max-$min+1))}]
set output {}
if {$type eq {binary}} {
set minval 0
set maxval 255
} elseif {$type eq {alpha}} {
set minval 48
set maxval 122
} elseif {$type eq {compr}} {
set minval 48
set maxval 52
}
while {$len} {
append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
incr len -1
}
return $output
}
# Useful for some test
proc zlistAlikeSort {a b} {
if {[lindex $a 0] > [lindex $b 0]} {return 1}
if {[lindex $a 0] < [lindex $b 0]} {return -1}
string compare [lindex $a 1] [lindex $b 1]
}
proc waitForBgsave r {
while 1 {
set i [$r info]
if {[string match {*bgsave_in_progress:1*} $i]} {
puts -nonewline "\nWaiting for background save to finish... "
flush stdout
after 1000
} else {
break
}
}
}
proc waitForBgrewriteaof r {
while 1 {
set i [$r info]
if {[string match {*bgrewriteaof_in_progress:1*} $i]} {
puts -nonewline "\nWaiting for background AOF rewrite to finish... "
flush stdout
after 1000
} else {
break
}
}
}
proc randomInt {max} {
expr {int(rand()*$max)}
}
proc randpath args {
set path [expr {int(rand()*[llength $args])}]
uplevel 1 [lindex $args $path]
}
proc randomValue {} {
randpath {
# Small enough to likely collide
randomInt 1000
} {
# 32 bit compressible signed/unsigned
randpath {randomInt 2000000000} {randomInt 4000000000}
} {
# 64 bit
randpath {randomInt 1000000000000}
} {
# Random string
randpath {randstring 0 256 alpha} \
{randstring 0 256 compr} \
{randstring 0 256 binary}
}
}
proc randomKey {} {
randpath {
# Small enough to likely collide
randomInt 1000
} {
# 32 bit compressible signed/unsigned
randpath {randomInt 2000000000} {randomInt 4000000000}
} {
# 64 bit
randpath {randomInt 1000000000000}
} {
# Random string
randpath {randstring 1 256 alpha} \
{randstring 1 256 compr}
}
}
proc createComplexDataset {r ops} {
for {set j 0} {$j < $ops} {incr j} {
set k [randomKey]
set f [randomValue]
set v [randomValue]
randpath {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
set d [expr {rand()}]
} {
randpath {set d +inf} {set d -inf}
}
set t [$r type $k]
if {$t eq {none}} {
randpath {
$r set $k $v
} {
$r lpush $k $v
} {
$r sadd $k $v
} {
$r zadd $k $d $v
} {
$r hset $k $f $v
}
set t [$r type $k]
}
switch $t {
{string} {
# Nothing to do
}
{list} {
randpath {$r lpush $k $v} \
{$r rpush $k $v} \
{$r lrem $k 0 $v} \
{$r rpop $k} \
{$r lpop $k}
}
{set} {
randpath {$r sadd $k $v} \
{$r srem $k $v}
}
{zset} {
randpath {$r zadd $k $d $v} \
{$r zrem $k $v}
}
{hash} {
randpath {$r hset $k $f $v} \
{$r hdel $k $f}
}
}
}
}
proc datasetDigest r {
set keys [lsort [$r keys *]]
set digest {}
foreach k $keys {
set t [$r type $k]
switch $t {
{string} {
set aux [::sha1::sha1 -hex [$r get $k]]
} {list} {
if {[$r llen $k] == 0} {
set aux {}
} else {
set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]]
}
} {set} {
if {[$r scard $k] == 0} {
set aux {}
} else {
set aux [::sha1::sha1 -hex [lsort [$r smembers $k]]]
}
} {zset} {
if {[$r zcard $k] == 0} {
set aux {}
} else {
set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]]
}
} {hash} {
if {[$r hlen $k] == 0} {
set aux {}
} else {
set aux [::sha1::sha1 -hex [lsort [$r hgetall $k]]]
}
} default {
error "Type not supported: $t"
}
}
if {$aux eq {}} continue
set digest [::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
}
return $digest
}
proc main {server port} {
set r [redis $server $port]
$r select 9
set err ""
set res ""
# The following AUTH test should be enabled only when requirepass
# <PASSWORD> is set in redis.conf and redis-server was started with
# redis.conf as the first argument.
#test {AUTH with requirepass in redis.conf} {
# $r auth foobared
#} {OK}
test {DEL all keys to start with a clean DB} {
foreach key [$r keys *] {$r del $key}
$r dbsize
} {0}
test {SET and GET an item} {
$r set x foobar
$r get x
} {foobar}
test {SET and GET an empty item} {
$r set x {}
$r get x
} {}
test {DEL against a single item} {
$r del x
$r get x
} {}
test {Vararg DEL} {
$r set foo1 a
$r set foo2 b
$r set foo3 c
list [$r del foo1 foo2 foo3 foo4] [$r mget foo1 foo2 foo3]
} {3 {{} {} {}}}
test {KEYS with pattern} {
foreach key {key_x key_y key_z foo_a foo_b foo_c} {
$r set $key hello
}
lsort [$r keys foo*]
} {foo_a foo_b foo_c}
test {KEYS to get all keys} {
lsort [$r keys *]
} {foo_a foo_b foo_c key_x key_y key_z}
test {DBSIZE} {
$r dbsize
} {6}
test {DEL all keys} {
foreach key [$r keys *] {$r del $key}
$r dbsize
} {0}
test {Very big payload in GET/SET} {
set buf [string repeat "abcd" 1000000]
$r set foo $buf
$r get foo
} [string repeat "abcd" 1000000]
test {Very big payload random access} {
set err {}
array set payload {}
for {set j 0} {$j < 100} {incr j} {
set size [expr 1+[randomInt 100000]]
set buf [string repeat "pl-$j" $size]
set payload($j) $buf
$r set bigpayload_$j $buf
}
for {set j 0} {$j < 1000} {incr j} {
set index [randomInt 100]
set buf [$r get bigpayload_$index]
if {$buf != $payload($index)} {
set err "Values differ: I set '$payload($index)' but I read back '$buf'"
break
}
}
unset payload
set _ $err
} {}
test {SET 10000 numeric keys and access all them in reverse order} {
set err {}
for {set x 0} {$x < 10000} {incr x} {
$r set $x $x
}
set sum 0
for {set x 9999} {$x >= 0} {incr x -1} {
set val [$r get $x]
if {$val ne $x} {
set err "Eleemnt at position $x is $val instead of $x"
break
}
}
set _ $err
} {}
test {DBSIZE should be 10101 now} {
$r dbsize
} {10101}
test {INCR against non existing key} {
set res {}
append res [$r incr novar]
append res [$r get novar]
} {11}
test {INCR against key created by incr itself} {
$r incr novar
} {2}
test {INCR against key originally set with SET} {
$r set novar 100
$r incr novar
} {101}
test {INCR over 32bit value} {
$r set novar 17179869184
$r incr novar
} {17179869185}
test {INCRBY over 32bit value with over 32bit increment} {
$r set novar 17179869184
$r incrby novar 17179869184
} {34359738368}
test {INCR against key with spaces (no integer encoded)} {
$r set novar " 11 "
$r incr novar
} {12}
test {DECRBY over 32bit value with over 32bit increment, negative res} {
$r set novar 17179869184
$r decrby novar 17179869185
} {-1}
test {SETNX target key missing} {
$r setnx novar2 foobared
$r get novar2
} {foobared}
test {SETNX target key exists} {
$r setnx novar2 blabla
$r get novar2
} {foobared}
test {SETNX will overwrite EXPIREing key} {
$r set x 10
$r expire x 10000
$r setnx x 20
$r get x
} {20}
test {EXISTS} {
set res {}
$r set newkey test
append res [$r exists newkey]
$r del newkey
append res [$r exists newkey]
} {10}
test {Zero length value in key. SET/GET/EXISTS} {
$r set emptykey {}
set res [$r get emptykey]
append res [$r exists emptykey]
$r del emptykey
append res [$r exists emptykey]
} {10}
test {Commands pipelining} {
set fd [$r channel]
puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
flush $fd
set res {}
append res [string match OK* [::redis::redis_read_reply $fd]]
append res [::redis::redis_read_reply $fd]
append res [string match PONG* [::redis::redis_read_reply $fd]]
format $res
} {1xyzk1}
test {Non existing command} {
catch {$r foobaredcommand} err
string match ERR* $err
} {1}
test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
set res [$r lpush mylist a]
append res [$r lpush mylist b]
append res [$r rpush mylist c]
append res [$r llen mylist]
append res [$r rpush anotherlist d]
append res [$r lpush anotherlist e]
append res [$r llen anotherlist]
append res [$r lindex mylist 0]
append res [$r lindex mylist 1]
append res [$r lindex mylist 2]
append res [$r lindex anotherlist 0]
append res [$r lindex anotherlist 1]
list $res [$r lindex mylist 100]
} {1233122baced {}}
test {DEL a list} {
$r del mylist
$r exists mylist
} {0}
test {Create a long list and check every single element with LINDEX} {
set ok 0
for {set i 0} {$i < 1000} {incr i} {
$r rpush mylist $i
}
for {set i 0} {$i < 1000} {incr i} {
if {[$r lindex mylist $i] eq $i} {incr ok}
if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
incr ok
}
}
format $ok
} {2000}
test {Test elements with LINDEX in random access} {
set ok 0
for {set i 0} {$i < 1000} {incr i} {
set rint [expr int(rand()*1000)]
if {[$r lindex mylist $rint] eq $rint} {incr ok}
if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
incr ok
}
}
format $ok
} {2000}
test {Check if the list is still ok after a DEBUG RELOAD} {
$r debug reload
set ok 0
for {set i 0} {$i < 1000} {incr i} {
set rint [expr int(rand()*1000)]
if {[$r lindex mylist $rint] eq $rint} {incr ok}
if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
incr ok
}
}
format $ok
} {2000}
test {LLEN against non-list value error} {
$r del mylist
$r set mylist foobar
catch {$r llen mylist} err
format $err
} {ERR*}
test {LLEN against non existing key} {
$r llen not-a-key
} {0}
test {LINDEX against non-list value error} {
catch {$r lindex mylist 0} err
format $err
} {ERR*}
test {LINDEX against non existing key} {
$r lindex not-a-key 10
} {}
test {LPUSH against non-list value error} {
catch {$r lpush mylist 0} err
format $err
} {ERR*}
test {RPUSH against non-list value error} {
catch {$r rpush mylist 0} err
format $err
} {ERR*}
test {RPOPLPUSH base case} {
$r del mylist
$r rpush mylist a
$r rpush mylist b
$r rpush mylist c
$r rpush mylist d
set v1 [$r rpoplpush mylist newlist]
set v2 [$r rpoplpush mylist newlist]
set l1 [$r lrange mylist 0 -1]
set l2 [$r lrange newlist 0 -1]
list $v1 $v2 $l1 $l2
} {d c {a b} {c d}}
test {RPOPLPUSH with the same list as src and dst} {
$r del mylist
$r rpush mylist a
$r rpush mylist b
$r rpush mylist c
set l1 [$r lrange mylist 0 -1]
set v [$r rpoplpush mylist mylist]
set l2 [$r lrange mylist 0 -1]
list $l1 $v $l2
} {{a b c} c {c a b}}
test {RPOPLPUSH target list already exists} {
$r del mylist
$r del newlist
$r rpush mylist a
$r rpush mylist b
$r rpush mylist c
$r rpush mylist d
$r rpush newlist x
set v1 [$r rpoplpush mylist newlist]
set v2 [$r rpoplpush mylist newlist]
set l1 [$r lrange mylist 0 -1]
set l2 [$r lrange newlist 0 -1]
list $v1 $v2 $l1 $l2
} {d c {a b} {c d x}}
test {RPOPLPUSH against non existing key} {
$r del mylist
$r del newlist
set v1 [$r rpoplpush mylist newlist]
list $v1 [$r exists mylist] [$r exists newlist]
} {{} 0 0}
test {RPOPLPUSH against non list src key} {
$r del mylist
$r del newlist
$r set mylist x
catch {$r rpoplpush mylist newlist} err
list [$r type mylist] [$r exists newlist] [string range $err 0 2]
} {string 0 ERR}
test {RPOPLPUSH against non list dst key} {
$r del mylist
$r del newlist
$r rpush mylist a
$r rpush mylist b
$r rpush mylist c
$r rpush mylist d
$r set newlist x
catch {$r rpoplpush mylist newlist} err
list [$r lrange mylist 0 -1] [$r type newlist] [string range $err 0 2]
} {{a b c d} string ERR}
test {RPOPLPUSH against non existing src key} {
$r del mylist
$r del newlist
$r rpoplpush mylist newlist
} {}
test {RENAME basic usage} {
$r set mykey hello
$r rename mykey mykey1
$r rename mykey1 mykey2
$r get mykey2
} {hello}
test {RENAME source key should no longer exist} {
$r exists mykey
} {0}
test {RENAME against already existing key} {
$r set mykey a
$r set mykey2 b
$r rename mykey2 mykey
set res [$r get mykey]
append res [$r exists mykey2]
} {b0}
test {RENAMENX basic usage} {
$r del mykey
$r del mykey2
$r set mykey foobar
$r renamenx mykey mykey2
set res [$r get mykey2]
append res [$r exists mykey]
} {foobar0}
test {RENAMENX against already existing key} {
$r set mykey foo
$r set mykey2 bar
$r renamenx mykey mykey2
} {0}
test {RENAMENX against already existing key (2)} {
set res [$r get mykey]
append res [$r get mykey2]
} {foobar}
test {RENAME against non existing source key} {
catch {$r rename nokey foobar} err
format $err
} {ERR*}
test {RENAME where source and dest key is the same} {
catch {$r rename mykey mykey} err
format $err
} {ERR*}
test {DEL all keys again (DB 0)} {
foreach key [$r keys *] {
$r del $key
}
$r dbsize
} {0}
test {DEL all keys again (DB 1)} {
$r select 10
foreach key [$r keys *] {
$r del $key
}
set res [$r dbsize]
$r select 9
format $res
} {0}
test {MOVE basic usage} {
$r set mykey foobar
$r move mykey 10
set res {}
lappend res [$r exists mykey]
lappend res [$r dbsize]
$r select 10
lappend res [$r get mykey]
lappend res [$r dbsize]
$r select 9
format $res
} [list 0 0 foobar 1]
test {MOVE against key existing in the target DB} {
$r set mykey hello
$r move mykey 10
} {0}
test {SET/GET keys in different DBs} {
$r set a hello
$r set b world
$r select 10
$r set a foo
$r set b bared
$r select 9
set res {}
lappend res [$r get a]
lappend res [$r get b]
$r select 10
lappend res [$r get a]
lappend res [$r get b]
$r select 9
format $res
} {hello world foo bared}
test {Basic LPOP/RPOP} {
$r del mylist
$r rpush mylist 1
$r rpush mylist 2
$r lpush mylist 0
list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
} [list 0 2 1 0]
test {LPOP/RPOP against empty list} {
$r lpop mylist
} {}
test {LPOP against non list value} {
$r set notalist foo
catch {$r lpop notalist} err
format $err
} {ERR*kind*}
test {Mass LPUSH/LPOP} {
set sum 0
for {set i 0} {$i < 1000} {incr i} {
$r lpush mylist $i
incr sum $i
}
set sum2 0
for {set i 0} {$i < 500} {incr i} {
incr sum2 [$r lpop mylist]
incr sum2 [$r rpop mylist]
}
expr $sum == $sum2
} {1}
test {LRANGE basics} {
for {set i 0} {$i < 10} {incr i} {
$r rpush mylist $i
}
list [$r lrange mylist 1 -2] \
[$r lrange mylist -3 -1] \
[$r lrange mylist 4 4]
} {{1 2 3 4 5 6 7 8} {7 8 9} 4}
test {LRANGE inverted indexes} {
$r lrange mylist 6 2
} {}
test {LRANGE out of range indexes including the full list} {
$r lrange mylist -1000 1000
} {0 1 2 3 4 5 6 7 8 9}
test {LRANGE against non existing key} {
$r lrange nosuchkey 0 1
} {}
test {LTRIM basics} {
$r del mylist
for {set i 0} {$i < 100} {incr i} {
$r lpush mylist $i
$r ltrim mylist 0 4
}
$r lrange mylist 0 -1
} {99 98 97 96 95}
test {LTRIM stress testing} {
set mylist {}
set err {}
for {set i 0} {$i < 20} {incr i} {
lappend mylist $i
}
for {set j 0} {$j < 100} {incr j} {
# Fill the list
$r del mylist
for {set i 0} {$i < 20} {incr i} {
$r rpush mylist $i
}
# Trim at random
set a [randomInt 20]
set b [randomInt 20]
$r ltrim mylist $a $b
if {[$r lrange mylist 0 -1] ne [lrange $mylist $a $b]} {
set err "[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
break
}
}
set _ $err
} {}
test {LSET} {
$r del mylist
foreach x {99 98 97 96 95} {
$r rpush mylist $x
}
$r lset mylist 1 foo
$r lset mylist -1 bar
$r lrange mylist 0 -1
} {99 foo 97 96 bar}
test {LSET out of range index} {
catch {$r lset mylist 10 foo} err
format $err
} {ERR*range*}
test {LSET against non existing key} {
catch {$r lset nosuchkey 10 foo} err
format $err
} {ERR*key*}
test {LSET against non list value} {
$r set nolist foobar
catch {$r lset nolist 0 foo} err
format $err
} {ERR*value*}
test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
$r sadd myset foo
$r sadd myset bar
list [$r scard myset] [$r sismember myset foo] \
[$r sismember myset bar] [$r sismember myset bla] \
[lsort [$r smembers myset]]
} {2 1 1 0 {bar foo}}
test {SADD adding the same element multiple times} {
$r sadd myset foo
$r sadd myset foo
$r sadd myset foo
$r scard myset
} {2}
test {SADD against non set} {
catch {$r sadd mylist foo} err
format $err
} {ERR*kind*}
test {SREM basics} {
$r sadd myset ciao
$r srem myset foo
lsort [$r smembers myset]
} {bar ciao}
test {Mass SADD and SINTER with two sets} {
for {set i 0} {$i < 1000} {incr i} {
$r sadd set1 $i
$r sadd set2 [expr $i+995]
}
lsort [$r sinter set1 set2]
} {995 996 997 998 999}
test {SUNION with two sets} {
lsort [$r sunion set1 set2]
} [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
test {SINTERSTORE with two sets} {
$r sinterstore setres set1 set2
lsort [$r smembers setres]
} {995 996 997 998 999}
test {SINTERSTORE with two sets, after a DEBUG RELOAD} {
$r debug reload
$r sinterstore setres set1 set2
lsort [$r smembers setres]
} {995 996 997 998 999}
test {SUNIONSTORE with two sets} {
$r sunionstore setres set1 set2
lsort [$r smembers setres]
} [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
test {SUNIONSTORE against non existing keys} {
$r set setres xxx
list [$r sunionstore setres foo111 bar222] [$r exists xxx]
} {0 0}
test {SINTER against three sets} {
$r sadd set3 999
$r sadd set3 995
$r sadd set3 1000
$r sadd set3 2000
lsort [$r sinter set1 set2 set3]
} {995 999}
test {SINTERSTORE with three sets} {
$r sinterstore setres set1 set2 set3
lsort [$r smembers setres]
} {995 999}
test {SUNION with non existing keys} {
lsort [$r sunion nokey1 set1 set2 nokey2]
} [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
test {SDIFF with two sets} {
for {set i 5} {$i < 1000} {incr i} {
$r sadd set4 $i
}
lsort [$r sdiff set1 set4]
} {0 1 2 3 4}
test {SDIFF with three sets} {
$r sadd set5 0
lsort [$r sdiff set1 set4 set5]
} {1 2 3 4}
test {SDIFFSTORE with three sets} {
$r sdiffstore sres set1 set4 set5
lsort [$r smembers sres]
} {1 2 3 4}
test {SPOP basics} {
$r del myset
$r sadd myset 1
$r sadd myset 2
$r sadd myset 3
list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset]
} {{1 2 3} 0}
test {SAVE - make sure there are all the types as values} {
# Wait for a background saving in progress to terminate
waitForBgsave $r
$r lpush mysavelist hello
$r lpush mysavelist world
$r set myemptykey {}
$r set mynormalkey {blablablba}
$r zadd mytestzset a 10
$r zadd mytestzset b 20
$r zadd mytestzset c 30
$r save
} {OK}
test {SRANDMEMBER} {
$r del myset
$r sadd myset a
$r sadd myset b
$r sadd myset c
unset -nocomplain myset
array set myset {}
for {set i 0} {$i < 100} {incr i} {
set myset([$r srandmember myset]) 1
}
lsort [array names myset]
} {a b c}
test {Create a random list and a random set} {
set tosort {}
array set seenrand {}
for {set i 0} {$i < 10000} {incr i} {
while 1 {
# Make sure all the weights are different because
# Redis does not use a stable sort but Tcl does.
randpath {
set rint [expr int(rand()*1000000)]
} {
set rint [expr rand()]
}
if {![info exists seenrand($rint)]} break
}
set seenrand($rint) x
$r lpush tosort $i
$r sadd tosort-set $i
$r set weight_$i $rint
lappend tosort [list $i $rint]
}
set sorted [lsort -index 1 -real $tosort]
set res {}
for {set i 0} {$i < 10000} {incr i} {
lappend res [lindex $sorted $i 0]
}
format {}
} {}
test {SORT with BY against the newly created list} {
$r sort tosort {BY weight_*}
} $res
test {the same SORT with BY, but against the newly created set} {
$r sort tosort-set {BY weight_*}
} $res
test {SORT with BY and STORE against the newly created list} {
$r sort tosort {BY weight_*} store sort-res
$r lrange sort-res 0 -1
} $res
test {SORT direct, numeric, against the newly created list} {
$r sort tosort
} [lsort -integer $res]
test {SORT decreasing sort} {
$r sort tosort {DESC}
} [lsort -decreasing -integer $res]
test {SORT speed, sorting 10000 elements list using BY, 100 times} {
set start [clock clicks -milliseconds]
for {set i 0} {$i < 100} {incr i} {
set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
}
set elapsed [expr [clock clicks -milliseconds]-$start]
puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
flush stdout
format {}
} {}
test {SORT speed, sorting 10000 elements list directly, 100 times} {
set start [clock clicks -milliseconds]
for {set i 0} {$i < 100} {incr i} {
set sorted [$r sort tosort {LIMIT 0 10}]
}
set elapsed [expr [clock clicks -milliseconds]-$start]
puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
flush stdout
format {}
} {}
test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
set start [clock clicks -milliseconds]
for {set i 0} {$i < 100} {incr i} {
set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
}
set elapsed [expr [clock clicks -milliseconds]-$start]