]> git.saurik.com Git - redis.git/blame - test-redis.tcl
Optimization fixed and re-activated
[redis.git] / test-redis.tcl
CommitLineData
7a932b74 1# test-redis.tcl
2# Redis test suite. Copyright (C) 2009 Salvatore Sanfilippo antirez@gmail.com
3# This softare is released under the BSD License. See the COPYING file for
4# more information.
ed9b544e 5
405b0a6a 6set tcl_precision 17
f89c3a35 7source redis.tcl
43c9dc7b 8
ed9b544e 9set ::passed 0
10set ::failed 0
fc77604c 11set ::testnum 0
ed9b544e 12
13proc test {name code okpattern} {
fc77604c 14 incr ::testnum
15 if {$::testnum < $::first || $::testnum > $::last} return
16 puts -nonewline [format "%-70s " "#$::testnum $name"]
ed9b544e 17 flush stdout
18 set retval [uplevel 1 $code]
19 if {$okpattern eq $retval || [string match $okpattern $retval]} {
20 puts "PASSED"
21 incr ::passed
22 } else {
23 puts "!! ERROR expected\n'$okpattern'\nbut got\n'$retval'"
24 incr ::failed
25 }
26}
27
75398fbc 28proc randstring {min max {type binary}} {
29 set len [expr {$min+int(rand()*($max-$min+1))}]
30 set output {}
31 if {$type eq {binary}} {
32 set minval 0
33 set maxval 255
34 } elseif {$type eq {alpha}} {
35 set minval 48
36 set maxval 122
37 } elseif {$type eq {compr}} {
38 set minval 48
39 set maxval 52
40 }
41 while {$len} {
42 append output [format "%c" [expr {$minval+int(rand()*($maxval-$minval+1))}]]
43 incr len -1
44 }
45 return $output
46}
47
d7f43c08 48# Useful for some test
49proc zlistAlikeSort {a b} {
50 if {[lindex $a 0] > [lindex $b 0]} {return 1}
51 if {[lindex $a 0] < [lindex $b 0]} {return -1}
52 string compare [lindex $a 1] [lindex $b 1]
53}
54
0d36ded0 55proc waitForBgsave r {
56 while 1 {
57 set i [$r info]
58 if {[string match {*bgsave_in_progress:1*} $i]} {
59 puts -nonewline "\nWaiting for background save to finish... "
60 flush stdout
61 after 1000
62 } else {
63 break
64 }
65 }
66}
67
e96e4fbf 68proc waitForBgrewriteaof r {
69 while 1 {
70 set i [$r info]
71 if {[string match {*bgrewriteaof_in_progress:1*} $i]} {
72 puts -nonewline "\nWaiting for background AOF rewrite to finish... "
73 flush stdout
74 after 1000
75 } else {
76 break
77 }
78 }
79}
80
e054afda 81proc randomInt {max} {
82 expr {int(rand()*$max)}
83}
84
85proc randpath args {
86 set path [expr {int(rand()*[llength $args])}]
87 uplevel 1 [lindex $args $path]
88}
89
90proc randomValue {} {
91 randpath {
92 # Small enough to likely collide
93 randomInt 1000
94 } {
95 # 32 bit compressible signed/unsigned
96 randpath {randomInt 2000000000} {randomInt 4000000000}
97 } {
98 # 64 bit
99 randpath {randomInt 1000000000000}
100 } {
101 # Random string
102 randpath {randstring 0 256 alpha} \
103 {randstring 0 256 compr} \
104 {randstring 0 256 binary}
105 }
106}
107
108proc randomKey {} {
109 randpath {
110 # Small enough to likely collide
111 randomInt 1000
112 } {
113 # 32 bit compressible signed/unsigned
114 randpath {randomInt 2000000000} {randomInt 4000000000}
115 } {
116 # 64 bit
117 randpath {randomInt 1000000000000}
118 } {
119 # Random string
120 randpath {randstring 1 256 alpha} \
121 {randstring 1 256 compr}
122 }
123}
124
125proc createComplexDataset {r ops} {
126 for {set j 0} {$j < $ops} {incr j} {
127 set k [randomKey]
2a1198b4 128 set f [randomValue]
e054afda 129 set v [randomValue]
ab9d4cb1 130 randpath {
131 set d [expr {rand()}]
132 } {
133 set d [expr {rand()}]
134 } {
135 set d [expr {rand()}]
136 } {
137 set d [expr {rand()}]
138 } {
139 set d [expr {rand()}]
140 } {
141 randpath {set d +inf} {set d -inf}
142 }
e054afda 143 set t [$r type $k]
144
145 if {$t eq {none}} {
146 randpath {
147 $r set $k $v
148 } {
149 $r lpush $k $v
150 } {
151 $r sadd $k $v
152 } {
153 $r zadd $k $d $v
2a1198b4 154 } {
155 $r hset $k $f $v
e054afda 156 }
157 set t [$r type $k]
158 }
159
160 switch $t {
161 {string} {
162 # Nothing to do
163 }
164 {list} {
165 randpath {$r lpush $k $v} \
166 {$r rpush $k $v} \
167 {$r lrem $k 0 $v} \
168 {$r rpop $k} \
169 {$r lpop $k}
170 }
171 {set} {
172 randpath {$r sadd $k $v} \
173 {$r srem $k $v}
174 }
175 {zset} {
176 randpath {$r zadd $k $d $v} \
177 {$r zrem $k $v}
178 }
2a1198b4 179 {hash} {
180 randpath {$r hset $k $f $v} \
181 {$r hdel $k $f}
182 }
e054afda 183 }
184 }
185}
186
187proc datasetDigest r {
a3f9eec2 188 set keys [lsort [$r keys *]]
e96e4fbf 189 set digest {}
e054afda 190 foreach k $keys {
191 set t [$r type $k]
e96e4fbf 192 switch $t {
193 {string} {
194 set aux [::sha1::sha1 -hex [$r get $k]]
195 } {list} {
196 if {[$r llen $k] == 0} {
197 set aux {}
198 } else {
199 set aux [::sha1::sha1 -hex [$r lrange $k 0 -1]]
200 }
201 } {set} {
202 if {[$r scard $k] == 0} {
203 set aux {}
204 } else {
205 set aux [::sha1::sha1 -hex [lsort [$r smembers $k]]]
206 }
207 } {zset} {
208 if {[$r zcard $k] == 0} {
209 set aux {}
210 } else {
211 set aux [::sha1::sha1 -hex [$r zrange $k 0 -1]]
212 }
213 } default {
a3f9eec2 214 error "Type not supported: $t"
e96e4fbf 215 }
e054afda 216 }
e96e4fbf 217 if {$aux eq {}} continue
218 set digest [::sha1::sha1 -hex [join [list $aux $digest $k] "\n"]]
e054afda 219 }
220 return $digest
221}
222
ed9b544e 223proc main {server port} {
43c9dc7b 224 set r [redis $server $port]
eea4baf7 225 $r select 9
43c9dc7b 226 set err ""
fc77604c 227 set res ""
ed9b544e 228
abcb223e
BH
229 # The following AUTH test should be enabled only when requirepass
230 # <PASSWORD> is set in redis.conf and redis-server was started with
231 # redis.conf as the first argument.
232
233 #test {AUTH with requirepass in redis.conf} {
234 # $r auth foobared
235 #} {OK}
236
ed9b544e 237 test {DEL all keys to start with a clean DB} {
43c9dc7b 238 foreach key [$r keys *] {$r del $key}
239 $r dbsize
ed9b544e 240 } {0}
241
242 test {SET and GET an item} {
43c9dc7b 243 $r set x foobar
244 $r get x
ed9b544e 245 } {foobar}
246
7c49733c 247 test {SET and GET an empty item} {
248 $r set x {}
249 $r get x
250 } {}
251
ed9b544e 252 test {DEL against a single item} {
43c9dc7b 253 $r del x
254 $r get x
ed9b544e 255 } {}
256
cc582a77 257 test {Vararg DEL} {
258 $r set foo1 a
259 $r set foo2 b
260 $r set foo3 c
261 list [$r del foo1 foo2 foo3 foo4] [$r mget foo1 foo2 foo3]
262 } {3 {{} {} {}}}
263
ed9b544e 264 test {KEYS with pattern} {
265 foreach key {key_x key_y key_z foo_a foo_b foo_c} {
43c9dc7b 266 $r set $key hello
ed9b544e 267 }
43c9dc7b 268 lsort [$r keys foo*]
ed9b544e 269 } {foo_a foo_b foo_c}
270
271 test {KEYS to get all keys} {
43c9dc7b 272 lsort [$r keys *]
ed9b544e 273 } {foo_a foo_b foo_c key_x key_y key_z}
274
275 test {DBSIZE} {
43c9dc7b 276 $r dbsize
ed9b544e 277 } {6}
278
279 test {DEL all keys} {
43c9dc7b 280 foreach key [$r keys *] {$r del $key}
281 $r dbsize
ed9b544e 282 } {0}
283
284 test {Very big payload in GET/SET} {
285 set buf [string repeat "abcd" 1000000]
43c9dc7b 286 $r set foo $buf
287 $r get foo
ed9b544e 288 } [string repeat "abcd" 1000000]
289
72766462 290 test {Very big payload random access} {
291 set err {}
292 array set payload {}
293 for {set j 0} {$j < 100} {incr j} {
294 set size [expr 1+[randomInt 100000]]
054e426d 295 set buf [string repeat "pl-$j" $size]
72766462 296 set payload($j) $buf
297 $r set bigpayload_$j $buf
298 }
299 for {set j 0} {$j < 1000} {incr j} {
300 set index [randomInt 100]
301 set buf [$r get bigpayload_$index]
302 if {$buf != $payload($index)} {
5f8e5d7c 303 set err "Values differ: I set '$payload($index)' but I read back '$buf'"
72766462 304 break
305 }
306 }
307 unset payload
308 set _ $err
309 } {}
310
ed9b544e 311 test {SET 10000 numeric keys and access all them in reverse order} {
25fd2cb2 312 set err {}
ed9b544e 313 for {set x 0} {$x < 10000} {incr x} {
43c9dc7b 314 $r set $x $x
ed9b544e 315 }
316 set sum 0
317 for {set x 9999} {$x >= 0} {incr x -1} {
25fd2cb2 318 set val [$r get $x]
319 if {$val ne $x} {
320 set err "Eleemnt at position $x is $val instead of $x"
321 break
322 }
ed9b544e 323 }
25fd2cb2 324 set _ $err
d663729a 325 } {}
ed9b544e 326
fefed597 327 test {DBSIZE should be 10101 now} {
43c9dc7b 328 $r dbsize
fefed597 329 } {10101}
ed9b544e 330
331 test {INCR against non existing key} {
332 set res {}
43c9dc7b 333 append res [$r incr novar]
334 append res [$r get novar]
ed9b544e 335 } {11}
336
337 test {INCR against key created by incr itself} {
43c9dc7b 338 $r incr novar
ed9b544e 339 } {2}
340
341 test {INCR against key originally set with SET} {
43c9dc7b 342 $r set novar 100
343 $r incr novar
ed9b544e 344 } {101}
345
d68ed120 346 test {INCR over 32bit value} {
347 $r set novar 17179869184
348 $r incr novar
349 } {17179869185}
350
351 test {INCRBY over 32bit value with over 32bit increment} {
352 $r set novar 17179869184
353 $r incrby novar 17179869184
354 } {34359738368}
355
ab9d4cb1 356 test {INCR against key with spaces (no integer encoded)} {
357 $r set novar " 11 "
358 $r incr novar
359 } {12}
360
d68ed120 361 test {DECRBY over 32bit value with over 32bit increment, negative res} {
362 $r set novar 17179869184
363 $r decrby novar 17179869185
364 } {-1}
365
ed9b544e 366 test {SETNX target key missing} {
43c9dc7b 367 $r setnx novar2 foobared
368 $r get novar2
ed9b544e 369 } {foobared}
370
371 test {SETNX target key exists} {
43c9dc7b 372 $r setnx novar2 blabla
373 $r get novar2
ed9b544e 374 } {foobared}
375
5acdc75d 376 test {SETNX will overwrite EXPIREing key} {
377 $r set x 10
378 $r expire x 10000
379 $r setnx x 20
380 $r get x
381 } {20}
382
ed9b544e 383 test {EXISTS} {
384 set res {}
43c9dc7b 385 $r set newkey test
386 append res [$r exists newkey]
387 $r del newkey
388 append res [$r exists newkey]
ed9b544e 389 } {10}
390
391 test {Zero length value in key. SET/GET/EXISTS} {
43c9dc7b 392 $r set emptykey {}
393 set res [$r get emptykey]
394 append res [$r exists emptykey]
395 $r del emptykey
396 append res [$r exists emptykey]
ed9b544e 397 } {10}
398
399 test {Commands pipelining} {
43c9dc7b 400 set fd [$r channel]
ed9b544e 401 puts -nonewline $fd "SET k1 4\r\nxyzk\r\nGET k1\r\nPING\r\n"
402 flush $fd
403 set res {}
43c9dc7b 404 append res [string match OK* [::redis::redis_read_reply $fd]]
405 append res [::redis::redis_read_reply $fd]
406 append res [string match PONG* [::redis::redis_read_reply $fd]]
ed9b544e 407 format $res
408 } {1xyzk1}
409
410 test {Non existing command} {
43c9dc7b 411 catch {$r foobaredcommand} err
412 string match ERR* $err
ed9b544e 413 } {1}
414
415 test {Basic LPUSH, RPUSH, LLENGTH, LINDEX} {
520b5a33 416 set res [$r lpush mylist a]
417 append res [$r lpush mylist b]
418 append res [$r rpush mylist c]
419 append res [$r llen mylist]
420 append res [$r rpush anotherlist d]
421 append res [$r lpush anotherlist e]
422 append res [$r llen anotherlist]
43c9dc7b 423 append res [$r lindex mylist 0]
424 append res [$r lindex mylist 1]
425 append res [$r lindex mylist 2]
520b5a33 426 append res [$r lindex anotherlist 0]
427 append res [$r lindex anotherlist 1]
ab9d4cb1 428 list $res [$r lindex mylist 100]
520b5a33 429 } {1233122baced {}}
ed9b544e 430
431 test {DEL a list} {
43c9dc7b 432 $r del mylist
433 $r exists mylist
ed9b544e 434 } {0}
435
436 test {Create a long list and check every single element with LINDEX} {
437 set ok 0
438 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 439 $r rpush mylist $i
ed9b544e 440 }
441 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 442 if {[$r lindex mylist $i] eq $i} {incr ok}
443 if {[$r lindex mylist [expr (-$i)-1]] eq [expr 999-$i]} {
ed9b544e 444 incr ok
445 }
446 }
447 format $ok
448 } {2000}
449
450 test {Test elements with LINDEX in random access} {
451 set ok 0
452 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 453 set rint [expr int(rand()*1000)]
454 if {[$r lindex mylist $rint] eq $rint} {incr ok}
455 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
ed9b544e 456 incr ok
457 }
458 }
459 format $ok
460 } {2000}
461
210e29f7 462 test {Check if the list is still ok after a DEBUG RELOAD} {
463 $r debug reload
464 set ok 0
465 for {set i 0} {$i < 1000} {incr i} {
466 set rint [expr int(rand()*1000)]
467 if {[$r lindex mylist $rint] eq $rint} {incr ok}
468 if {[$r lindex mylist [expr (-$rint)-1]] eq [expr 999-$rint]} {
469 incr ok
470 }
471 }
472 format $ok
473 } {2000}
474
ed9b544e 475 test {LLEN against non-list value error} {
43c9dc7b 476 $r del mylist
477 $r set mylist foobar
478 catch {$r llen mylist} err
479 format $err
c937aa89 480 } {ERR*}
ed9b544e 481
ab9d4cb1 482 test {LLEN against non existing key} {
483 $r llen not-a-key
484 } {0}
485
ed9b544e 486 test {LINDEX against non-list value error} {
43c9dc7b 487 catch {$r lindex mylist 0} err
488 format $err
c937aa89 489 } {ERR*}
ed9b544e 490
ab9d4cb1 491 test {LINDEX against non existing key} {
492 $r lindex not-a-key 10
493 } {}
494
ed9b544e 495 test {LPUSH against non-list value error} {
43c9dc7b 496 catch {$r lpush mylist 0} err
497 format $err
c937aa89 498 } {ERR*}
ed9b544e 499
500 test {RPUSH against non-list value error} {
43c9dc7b 501 catch {$r rpush mylist 0} err
502 format $err
c937aa89 503 } {ERR*}
ed9b544e 504
c08f1734 505 test {RPOPLPUSH base case} {
506 $r del mylist
507 $r rpush mylist a
508 $r rpush mylist b
509 $r rpush mylist c
510 $r rpush mylist d
511 set v1 [$r rpoplpush mylist newlist]
512 set v2 [$r rpoplpush mylist newlist]
513 set l1 [$r lrange mylist 0 -1]
514 set l2 [$r lrange newlist 0 -1]
515 list $v1 $v2 $l1 $l2
516 } {d c {a b} {c d}}
517
518 test {RPOPLPUSH with the same list as src and dst} {
519 $r del mylist
520 $r rpush mylist a
521 $r rpush mylist b
522 $r rpush mylist c
523 set l1 [$r lrange mylist 0 -1]
524 set v [$r rpoplpush mylist mylist]
525 set l2 [$r lrange mylist 0 -1]
526 list $l1 $v $l2
527 } {{a b c} c {c a b}}
528
529 test {RPOPLPUSH target list already exists} {
530 $r del mylist
531 $r del newlist
532 $r rpush mylist a
533 $r rpush mylist b
534 $r rpush mylist c
535 $r rpush mylist d
536 $r rpush newlist x
537 set v1 [$r rpoplpush mylist newlist]
538 set v2 [$r rpoplpush mylist newlist]
539 set l1 [$r lrange mylist 0 -1]
540 set l2 [$r lrange newlist 0 -1]
541 list $v1 $v2 $l1 $l2
542 } {d c {a b} {c d x}}
543
b9bb7ba2 544 test {RPOPLPUSH against non existing key} {
545 $r del mylist
546 $r del newlist
547 set v1 [$r rpoplpush mylist newlist]
548 list $v1 [$r exists mylist] [$r exists newlist]
549 } {{} 0 0}
550
551 test {RPOPLPUSH against non list src key} {
552 $r del mylist
553 $r del newlist
554 $r set mylist x
555 catch {$r rpoplpush mylist newlist} err
556 list [$r type mylist] [$r exists newlist] [string range $err 0 2]
557 } {string 0 ERR}
558
559 test {RPOPLPUSH against non list dst key} {
560 $r del mylist
561 $r del newlist
562 $r rpush mylist a
563 $r rpush mylist b
564 $r rpush mylist c
565 $r rpush mylist d
566 $r set newlist x
567 catch {$r rpoplpush mylist newlist} err
568 list [$r lrange mylist 0 -1] [$r type newlist] [string range $err 0 2]
569 } {{a b c d} string ERR}
570
fdcaae84 571 test {RPOPLPUSH against non existing src key} {
572 $r del mylist
573 $r del newlist
574 $r rpoplpush mylist newlist
575 } {}
576
ed9b544e 577 test {RENAME basic usage} {
43c9dc7b 578 $r set mykey hello
579 $r rename mykey mykey1
580 $r rename mykey1 mykey2
581 $r get mykey2
ed9b544e 582 } {hello}
583
584 test {RENAME source key should no longer exist} {
43c9dc7b 585 $r exists mykey
ed9b544e 586 } {0}
587
588 test {RENAME against already existing key} {
43c9dc7b 589 $r set mykey a
590 $r set mykey2 b
591 $r rename mykey2 mykey
592 set res [$r get mykey]
593 append res [$r exists mykey2]
ed9b544e 594 } {b0}
595
596 test {RENAMENX basic usage} {
43c9dc7b 597 $r del mykey
598 $r del mykey2
599 $r set mykey foobar
600 $r renamenx mykey mykey2
601 set res [$r get mykey2]
602 append res [$r exists mykey]
ed9b544e 603 } {foobar0}
604
605 test {RENAMENX against already existing key} {
43c9dc7b 606 $r set mykey foo
607 $r set mykey2 bar
608 $r renamenx mykey mykey2
ed9b544e 609 } {0}
610
611 test {RENAMENX against already existing key (2)} {
43c9dc7b 612 set res [$r get mykey]
613 append res [$r get mykey2]
ed9b544e 614 } {foobar}
615
616 test {RENAME against non existing source key} {
43c9dc7b 617 catch {$r rename nokey foobar} err
618 format $err
c937aa89 619 } {ERR*}
ed9b544e 620
621 test {RENAME where source and dest key is the same} {
43c9dc7b 622 catch {$r rename mykey mykey} err
623 format $err
c937aa89 624 } {ERR*}
ed9b544e 625
626 test {DEL all keys again (DB 0)} {
43c9dc7b 627 foreach key [$r keys *] {
628 $r del $key
ed9b544e 629 }
43c9dc7b 630 $r dbsize
ed9b544e 631 } {0}
632
633 test {DEL all keys again (DB 1)} {
eea4baf7 634 $r select 10
43c9dc7b 635 foreach key [$r keys *] {
636 $r del $key
ed9b544e 637 }
43c9dc7b 638 set res [$r dbsize]
eea4baf7 639 $r select 9
ed9b544e 640 format $res
641 } {0}
642
643 test {MOVE basic usage} {
43c9dc7b 644 $r set mykey foobar
eea4baf7 645 $r move mykey 10
ed9b544e 646 set res {}
43c9dc7b 647 lappend res [$r exists mykey]
648 lappend res [$r dbsize]
eea4baf7 649 $r select 10
43c9dc7b 650 lappend res [$r get mykey]
651 lappend res [$r dbsize]
eea4baf7 652 $r select 9
ed9b544e 653 format $res
654 } [list 0 0 foobar 1]
655
656 test {MOVE against key existing in the target DB} {
43c9dc7b 657 $r set mykey hello
eea4baf7 658 $r move mykey 10
ed9b544e 659 } {0}
660
661 test {SET/GET keys in different DBs} {
43c9dc7b 662 $r set a hello
663 $r set b world
eea4baf7 664 $r select 10
43c9dc7b 665 $r set a foo
666 $r set b bared
eea4baf7 667 $r select 9
ed9b544e 668 set res {}
43c9dc7b 669 lappend res [$r get a]
670 lappend res [$r get b]
eea4baf7 671 $r select 10
43c9dc7b 672 lappend res [$r get a]
673 lappend res [$r get b]
eea4baf7 674 $r select 9
ed9b544e 675 format $res
676 } {hello world foo bared}
677
678 test {Basic LPOP/RPOP} {
43c9dc7b 679 $r del mylist
680 $r rpush mylist 1
681 $r rpush mylist 2
682 $r lpush mylist 0
683 list [$r lpop mylist] [$r rpop mylist] [$r lpop mylist] [$r llen mylist]
ed9b544e 684 } [list 0 2 1 0]
685
686 test {LPOP/RPOP against empty list} {
43c9dc7b 687 $r lpop mylist
ed9b544e 688 } {}
689
690 test {LPOP against non list value} {
43c9dc7b 691 $r set notalist foo
692 catch {$r lpop notalist} err
693 format $err
c937aa89 694 } {ERR*kind*}
ed9b544e 695
696 test {Mass LPUSH/LPOP} {
697 set sum 0
698 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 699 $r lpush mylist $i
ed9b544e 700 incr sum $i
701 }
702 set sum2 0
703 for {set i 0} {$i < 500} {incr i} {
43c9dc7b 704 incr sum2 [$r lpop mylist]
705 incr sum2 [$r rpop mylist]
ed9b544e 706 }
707 expr $sum == $sum2
708 } {1}
709
710 test {LRANGE basics} {
711 for {set i 0} {$i < 10} {incr i} {
43c9dc7b 712 $r rpush mylist $i
ed9b544e 713 }
43c9dc7b 714 list [$r lrange mylist 1 -2] \
715 [$r lrange mylist -3 -1] \
716 [$r lrange mylist 4 4]
ed9b544e 717 } {{1 2 3 4 5 6 7 8} {7 8 9} 4}
718
719 test {LRANGE inverted indexes} {
43c9dc7b 720 $r lrange mylist 6 2
ed9b544e 721 } {}
722
723 test {LRANGE out of range indexes including the full list} {
43c9dc7b 724 $r lrange mylist -1000 1000
ed9b544e 725 } {0 1 2 3 4 5 6 7 8 9}
726
727 test {LRANGE against non existing key} {
43c9dc7b 728 $r lrange nosuchkey 0 1
ed9b544e 729 } {}
730
731 test {LTRIM basics} {
43c9dc7b 732 $r del mylist
ed9b544e 733 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 734 $r lpush mylist $i
735 $r ltrim mylist 0 4
ed9b544e 736 }
43c9dc7b 737 $r lrange mylist 0 -1
ed9b544e 738 } {99 98 97 96 95}
739
c632369b 740 test {LTRIM stress testing} {
741 set mylist {}
742 set err {}
743 for {set i 0} {$i < 20} {incr i} {
744 lappend mylist $i
745 }
746
747 for {set j 0} {$j < 100} {incr j} {
748 # Fill the list
749 $r del mylist
750 for {set i 0} {$i < 20} {incr i} {
751 $r rpush mylist $i
752 }
753 # Trim at random
754 set a [randomInt 20]
755 set b [randomInt 20]
756 $r ltrim mylist $a $b
757 if {[$r lrange mylist 0 -1] ne [lrange $mylist $a $b]} {
758 set err "[$r lrange mylist 0 -1] != [lrange $mylist $a $b]"
759 break
760 }
761 }
762 set _ $err
763 } {}
764
ed9b544e 765 test {LSET} {
c632369b 766 $r del mylist
767 foreach x {99 98 97 96 95} {
768 $r rpush mylist $x
769 }
43c9dc7b 770 $r lset mylist 1 foo
771 $r lset mylist -1 bar
772 $r lrange mylist 0 -1
ed9b544e 773 } {99 foo 97 96 bar}
774
775 test {LSET out of range index} {
43c9dc7b 776 catch {$r lset mylist 10 foo} err
777 format $err
c937aa89 778 } {ERR*range*}
ed9b544e 779
780 test {LSET against non existing key} {
43c9dc7b 781 catch {$r lset nosuchkey 10 foo} err
782 format $err
c937aa89 783 } {ERR*key*}
ed9b544e 784
785 test {LSET against non list value} {
43c9dc7b 786 $r set nolist foobar
787 catch {$r lset nolist 0 foo} err
788 format $err
c937aa89 789 } {ERR*value*}
ed9b544e 790
791 test {SADD, SCARD, SISMEMBER, SMEMBERS basics} {
43c9dc7b 792 $r sadd myset foo
793 $r sadd myset bar
794 list [$r scard myset] [$r sismember myset foo] \
795 [$r sismember myset bar] [$r sismember myset bla] \
796 [lsort [$r smembers myset]]
ed9b544e 797 } {2 1 1 0 {bar foo}}
798
799 test {SADD adding the same element multiple times} {
43c9dc7b 800 $r sadd myset foo
801 $r sadd myset foo
802 $r sadd myset foo
803 $r scard myset
ed9b544e 804 } {2}
805
806 test {SADD against non set} {
43c9dc7b 807 catch {$r sadd mylist foo} err
808 format $err
c937aa89 809 } {ERR*kind*}
ed9b544e 810
811 test {SREM basics} {
43c9dc7b 812 $r sadd myset ciao
813 $r srem myset foo
814 lsort [$r smembers myset]
ed9b544e 815 } {bar ciao}
816
817 test {Mass SADD and SINTER with two sets} {
818 for {set i 0} {$i < 1000} {incr i} {
43c9dc7b 819 $r sadd set1 $i
820 $r sadd set2 [expr $i+995]
ed9b544e 821 }
43c9dc7b 822 lsort [$r sinter set1 set2]
ed9b544e 823 } {995 996 997 998 999}
40d224a9 824
825 test {SUNION with two sets} {
826 lsort [$r sunion set1 set2]
827 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
ed9b544e 828
829 test {SINTERSTORE with two sets} {
43c9dc7b 830 $r sinterstore setres set1 set2
831 lsort [$r smembers setres]
ed9b544e 832 } {995 996 997 998 999}
833
210e29f7 834 test {SINTERSTORE with two sets, after a DEBUG RELOAD} {
835 $r debug reload
836 $r sinterstore setres set1 set2
837 lsort [$r smembers setres]
838 } {995 996 997 998 999}
839
40d224a9 840 test {SUNIONSTORE with two sets} {
841 $r sunionstore setres set1 set2
842 lsort [$r smembers setres]
843 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
844
f4d9b3c6 845 test {SUNIONSTORE against non existing keys} {
846 $r set setres xxx
847 list [$r sunionstore setres foo111 bar222] [$r exists xxx]
848 } {0 0}
849
ed9b544e 850 test {SINTER against three sets} {
43c9dc7b 851 $r sadd set3 999
852 $r sadd set3 995
853 $r sadd set3 1000
854 $r sadd set3 2000
855 lsort [$r sinter set1 set2 set3]
ed9b544e 856 } {995 999}
857
858 test {SINTERSTORE with three sets} {
43c9dc7b 859 $r sinterstore setres set1 set2 set3
860 lsort [$r smembers setres]
ed9b544e 861 } {995 999}
40d224a9 862
863 test {SUNION with non existing keys} {
864 lsort [$r sunion nokey1 set1 set2 nokey2]
865 } [lsort -uniq "[$r smembers set1] [$r smembers set2]"]
866
f4f56e1d 867 test {SDIFF with two sets} {
868 for {set i 5} {$i < 1000} {incr i} {
869 $r sadd set4 $i
870 }
871 lsort [$r sdiff set1 set4]
872 } {0 1 2 3 4}
873
874 test {SDIFF with three sets} {
875 $r sadd set5 0
876 lsort [$r sdiff set1 set4 set5]
877 } {1 2 3 4}
878
879 test {SDIFFSTORE with three sets} {
880 $r sdiffstore sres set1 set4 set5
881 lsort [$r smembers sres]
882 } {1 2 3 4}
883
12fea928 884 test {SPOP basics} {
885 $r del myset
886 $r sadd myset 1
887 $r sadd myset 2
888 $r sadd myset 3
889 list [lsort [list [$r spop myset] [$r spop myset] [$r spop myset]]] [$r scard myset]
890 } {{1 2 3} 0}
891
ed9b544e 892 test {SAVE - make sure there are all the types as values} {
bbf44ecf 893 # Wait for a background saving in progress to terminate
0d36ded0 894 waitForBgsave $r
43c9dc7b 895 $r lpush mysavelist hello
896 $r lpush mysavelist world
897 $r set myemptykey {}
898 $r set mynormalkey {blablablba}
d7f43c08 899 $r zadd mytestzset a 10
900 $r zadd mytestzset b 20
901 $r zadd mytestzset c 30
43c9dc7b 902 $r save
c937aa89 903 } {OK}
6cbfd2b3 904
905 test {SRANDMEMBER} {
906 $r del myset
907 $r sadd myset a
908 $r sadd myset b
909 $r sadd myset c
910 unset -nocomplain myset
911 array set myset {}
912 for {set i 0} {$i < 100} {incr i} {
913 set myset([$r srandmember myset]) 1
914 }
915 lsort [array names myset]
916 } {a b c}
ed9b544e 917
98e1bb81 918 test {Create a random list and a random set} {
ed9b544e 919 set tosort {}
920 array set seenrand {}
921 for {set i 0} {$i < 10000} {incr i} {
922 while 1 {
923 # Make sure all the weights are different because
924 # Redis does not use a stable sort but Tcl does.
98e1bb81 925 randpath {
926 set rint [expr int(rand()*1000000)]
927 } {
928 set rint [expr rand()]
929 }
43c9dc7b 930 if {![info exists seenrand($rint)]} break
ed9b544e 931 }
43c9dc7b 932 set seenrand($rint) x
933 $r lpush tosort $i
98e1bb81 934 $r sadd tosort-set $i
43c9dc7b 935 $r set weight_$i $rint
936 lappend tosort [list $i $rint]
ed9b544e 937 }
938 set sorted [lsort -index 1 -real $tosort]
939 set res {}
940 for {set i 0} {$i < 10000} {incr i} {
941 lappend res [lindex $sorted $i 0]
942 }
943 format {}
944 } {}
945
946 test {SORT with BY against the newly created list} {
43c9dc7b 947 $r sort tosort {BY weight_*}
ed9b544e 948 } $res
949
98e1bb81 950 test {the same SORT with BY, but against the newly created set} {
951 $r sort tosort-set {BY weight_*}
952 } $res
953
5384a2d8 954 test {SORT with BY and STORE against the newly created list} {
955 $r sort tosort {BY weight_*} store sort-res
956 $r lrange sort-res 0 -1
957 } $res
958
ed9b544e 959 test {SORT direct, numeric, against the newly created list} {
43c9dc7b 960 $r sort tosort
ed9b544e 961 } [lsort -integer $res]
962
963 test {SORT decreasing sort} {
43c9dc7b 964 $r sort tosort {DESC}
ed9b544e 965 } [lsort -decreasing -integer $res]
966
967 test {SORT speed, sorting 10000 elements list using BY, 100 times} {
968 set start [clock clicks -milliseconds]
969 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 970 set sorted [$r sort tosort {BY weight_* LIMIT 0 10}]
ed9b544e 971 }
972 set elapsed [expr [clock clicks -milliseconds]-$start]
973 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
974 flush stdout
975 format {}
976 } {}
c8c72447 977
ed9b544e 978 test {SORT speed, sorting 10000 elements list directly, 100 times} {
979 set start [clock clicks -milliseconds]
980 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 981 set sorted [$r sort tosort {LIMIT 0 10}]
ed9b544e 982 }
983 set elapsed [expr [clock clicks -milliseconds]-$start]
984 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
985 flush stdout
986 format {}
987 } {}
988
989 test {SORT speed, pseudo-sorting 10000 elements list, BY <const>, 100 times} {
990 set start [clock clicks -milliseconds]
991 for {set i 0} {$i < 100} {incr i} {
43c9dc7b 992 set sorted [$r sort tosort {BY nokey LIMIT 0 10}]
ed9b544e 993 }
994 set elapsed [expr [clock clicks -milliseconds]-$start]
995 puts -nonewline "\n Average time to sort: [expr double($elapsed)/100] milliseconds "
996 flush stdout
997 format {}
998 } {}
999
1000 test {SORT regression for issue #19, sorting floats} {
43c9dc7b 1001 $r flushdb
ed9b544e 1002 foreach x {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15} {
43c9dc7b 1003 $r lpush mylist $x
ed9b544e 1004 }
43c9dc7b 1005 $r sort mylist
ed9b544e 1006 } [lsort -real {1.1 5.10 3.10 7.44 2.1 5.75 6.12 0.25 1.15}]
1007
28173a49 1008 test {SORT with GET #} {
1009 $r del mylist
1010 $r lpush mylist 1
1011 $r lpush mylist 2
1012 $r lpush mylist 3
1013 $r mset weight_1 10 weight_2 5 weight_3 30
1014 $r sort mylist BY weight_* GET #
1015 } {2 1 3}
1016
d922ae65 1017 test {SORT with constant GET} {
1018 $r sort mylist GET foo
1019 } {{} {} {}}
1020
ed9b544e 1021 test {LREM, remove all the occurrences} {
eea4baf7 1022 $r flushdb
43c9dc7b 1023 $r rpush mylist foo
1024 $r rpush mylist bar
1025 $r rpush mylist foobar
1026 $r rpush mylist foobared
1027 $r rpush mylist zap
1028 $r rpush mylist bar
1029 $r rpush mylist test
1030 $r rpush mylist foo
1031 set res [$r lrem mylist 0 bar]
1032 list [$r lrange mylist 0 -1] $res
ed9b544e 1033 } {{foo foobar foobared zap test foo} 2}
1034
1035 test {LREM, remove the first occurrence} {
43c9dc7b 1036 set res [$r lrem mylist 1 foo]
1037 list [$r lrange mylist 0 -1] $res
ed9b544e 1038 } {{foobar foobared zap test foo} 1}
1039
1040 test {LREM, remove non existing element} {
43c9dc7b 1041 set res [$r lrem mylist 1 nosuchelement]
1042 list [$r lrange mylist 0 -1] $res
ed9b544e 1043 } {{foobar foobared zap test foo} 0}
1044
1045 test {LREM, starting from tail with negative count} {
eea4baf7 1046 $r flushdb
43c9dc7b 1047 $r rpush mylist foo
1048 $r rpush mylist bar
1049 $r rpush mylist foobar
1050 $r rpush mylist foobared
1051 $r rpush mylist zap
1052 $r rpush mylist bar
1053 $r rpush mylist test
1054 $r rpush mylist foo
1055 $r rpush mylist foo
1056 set res [$r lrem mylist -1 bar]
1057 list [$r lrange mylist 0 -1] $res
ed9b544e 1058 } {{foo bar foobar foobared zap test foo foo} 1}
1059
1060 test {LREM, starting from tail with negative count (2)} {
43c9dc7b 1061 set res [$r lrem mylist -2 foo]
1062 list [$r lrange mylist 0 -1] $res
ed9b544e 1063 } {{foo bar foobar foobared zap test} 2}
1064
724a51b1 1065 test {LREM, deleting objects that may be encoded as integers} {
1066 $r lpush myotherlist 1
1067 $r lpush myotherlist 2
1068 $r lpush myotherlist 3
1069 $r lrem myotherlist 1 2
1070 $r llen myotherlist
1071 } {2}
1072
5b19bd72 1073 test {MGET} {
eea4baf7 1074 $r flushdb
43c9dc7b 1075 $r set foo BAR
1076 $r set bar FOO
1077 $r mget foo bar
5b19bd72 1078 } {BAR FOO}
1079
1080 test {MGET against non existing key} {
43c9dc7b 1081 $r mget foo baazz bar
5b19bd72 1082 } {BAR {} FOO}
1083
1084 test {MGET against non-string key} {
43c9dc7b 1085 $r sadd myset ciao
1086 $r sadd myset bau
1087 $r mget foo baazz bar myset
5b19bd72 1088 } {BAR {} FOO {}}
1089
ce7bef07 1090 test {RANDOMKEY} {
eea4baf7 1091 $r flushdb
ce7bef07 1092 $r set foo x
1093 $r set bar y
1094 set foo_seen 0
1095 set bar_seen 0
1096 for {set i 0} {$i < 100} {incr i} {
1097 set rkey [$r randomkey]
1098 if {$rkey eq {foo}} {
1099 set foo_seen 1
1100 }
1101 if {$rkey eq {bar}} {
1102 set bar_seen 1
1103 }
1104 }
1105 list $foo_seen $bar_seen
1106 } {1 1}
1107
1108 test {RANDOMKEY against empty DB} {
eea4baf7 1109 $r flushdb
ce7bef07 1110 $r randomkey
1111 } {}
1112
f5785ae9 1113 test {RANDOMKEY regression 1} {
eea4baf7 1114 $r flushdb
f5785ae9 1115 $r set x 10
1116 $r del x
1117 $r randomkey
1118 } {}
1119
7ac6d461 1120 test {GETSET (set new value)} {
1121 list [$r getset foo xyz] [$r get foo]
1122 } {{} xyz}
1123
1124 test {GETSET (replace old value)} {
1125 $r set foo bar
1126 list [$r getset foo xyz] [$r get foo]
1127 } {bar xyz}
1128
0eeb2a4b 1129 test {SMOVE basics} {
1130 $r sadd myset1 a
1131 $r sadd myset1 b
1132 $r sadd myset1 c
1133 $r sadd myset2 x
1134 $r sadd myset2 y
1135 $r sadd myset2 z
1136 $r smove myset1 myset2 a
1137 list [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1138 } {{a x y z} {b c}}
1139
1140 test {SMOVE non existing key} {
1141 list [$r smove myset1 myset2 foo] [lsort [$r smembers myset2]] [lsort [$r smembers myset1]]
1142 } {0 {a x y z} {b c}}
1143
1144 test {SMOVE non existing src set} {
1145 list [$r smove noset myset2 foo] [lsort [$r smembers myset2]]
1146 } {0 {a x y z}}
1147
1148 test {SMOVE non existing dst set} {
1149 list [$r smove myset2 myset3 y] [lsort [$r smembers myset2]] [lsort [$r smembers myset3]]
1150 } {1 {a x z} y}
1151
1152 test {SMOVE wrong src key type} {
1153 $r set x 10
1154 catch {$r smove x myset2 foo} err
1155 format $err
1156 } {ERR*}
1157
1158 test {SMOVE wrong dst key type} {
1159 $r set x 10
1160 catch {$r smove myset2 x foo} err
1161 format $err
1162 } {ERR*}
1163
f69f2cba 1164 test {MSET base case} {
1165 $r mset x 10 y "foo bar" z "x x x x x x x\n\n\r\n"
1166 $r mget x y z
1167 } [list 10 {foo bar} "x x x x x x x\n\n\r\n"]
1168
1169 test {MSET wrong number of args} {
1170 catch {$r mset x 10 y "foo bar" z} err
1171 format $err
1172 } {*wrong number*}
1173
1174 test {MSETNX with already existent key} {
1175 list [$r msetnx x1 xxx y2 yyy x 20] [$r exists x1] [$r exists y2]
1176 } {0 0 0}
1177
1178 test {MSETNX with not existing keys} {
1179 list [$r msetnx x1 xxx y2 yyy] [$r get x1] [$r get y2]
1180 } {1 xxx yyy}
1181
5acdc75d 1182 test {MSETNX should remove all the volatile keys even on failure} {
1183 $r mset x 1 y 2 z 3
1184 $r expire y 10000
1185 $r expire z 10000
1186 list [$r msetnx x A y B z C] [$r mget x y z]
1187 } {0 {1 {} {}}}
1188
d7f43c08 1189 test {ZSET basic ZADD and score update} {
1190 $r zadd ztmp 10 x
1191 $r zadd ztmp 20 y
1192 $r zadd ztmp 30 z
1193 set aux1 [$r zrange ztmp 0 -1]
1194 $r zadd ztmp 1 y
1195 set aux2 [$r zrange ztmp 0 -1]
1196 list $aux1 $aux2
1197 } {{x y z} {y x z}}
1198
fc77604c 1199 test {ZCARD basics} {
1200 $r zcard ztmp
1201 } {3}
1202
1203 test {ZCARD non existing key} {
1204 $r zcard ztmp-blabla
1205 } {0}
1206
69d95c3e
PN
1207 test {ZRANK basics} {
1208 $r zadd zranktmp 10 x
1209 $r zadd zranktmp 20 y
1210 $r zadd zranktmp 30 z
1211 list [$r zrank zranktmp x] [$r zrank zranktmp y] [$r zrank zranktmp z]
1212 } {0 1 2}
1213
798d9e55
PN
1214 test {ZREVRANK basics} {
1215 list [$r zrevrank zranktmp x] [$r zrevrank zranktmp y] [$r zrevrank zranktmp z]
1216 } {2 1 0}
1217
69d95c3e
PN
1218 test {ZRANK - after deletion} {
1219 $r zrem zranktmp y
1220 list [$r zrank zranktmp x] [$r zrank zranktmp z]
1221 } {0 1}
1222
d7f43c08 1223 test {ZSCORE} {
eaa256ad 1224 set aux {}
1225 set err {}
1226 for {set i 0} {$i < 1000} {incr i} {
1227 set score [expr rand()]
1228 lappend aux $score
1229 $r zadd zscoretest $score $i
1230 }
1231 for {set i 0} {$i < 1000} {incr i} {
1232 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1233 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1234 break
1235 }
1236 }
1237 set _ $err
1238 } {}
d7f43c08 1239
210e29f7 1240 test {ZSCORE after a DEBUG RELOAD} {
1241 set aux {}
1242 set err {}
1243 $r del zscoretest
1244 for {set i 0} {$i < 1000} {incr i} {
1245 set score [expr rand()]
1246 lappend aux $score
1247 $r zadd zscoretest $score $i
1248 }
1249 $r debug reload
1250 for {set i 0} {$i < 1000} {incr i} {
1251 if {[$r zscore zscoretest $i] != [lindex $aux $i]} {
1252 set err "Expected score was [lindex $aux $i] but got [$r zscore zscoretest $i] for element $i"
1253 break
1254 }
1255 }
1256 set _ $err
1257 } {}
1258
ac945e2d 1259 test {ZRANGE and ZREVRANGE basics} {
04c71068 1260 list [$r zrange ztmp 0 -1] [$r zrevrange ztmp 0 -1] \
1261 [$r zrange ztmp 1 -1] [$r zrevrange ztmp 1 -1]
1262 } {{y x z} {z x y} {x z} {x y}}
d7f43c08 1263
81d45645 1264 test {ZRANGE WITHSCORES} {
1265 $r zrange ztmp 0 -1 withscores
1266 } {y 1 x 10 z 30}
1267
d7f43c08 1268 test {ZSETs stress tester - sorting is working well?} {
1269 set delta 0
1270 for {set test 0} {$test < 2} {incr test} {
1271 unset -nocomplain auxarray
1272 array set auxarray {}
1273 set auxlist {}
1274 $r del myzset
1275 for {set i 0} {$i < 1000} {incr i} {
1276 if {$test == 0} {
1277 set score [expr rand()]
1278 } else {
1279 set score [expr int(rand()*10)]
1280 }
1281 set auxarray($i) $score
1282 $r zadd myzset $score $i
1283 # Random update
1284 if {[expr rand()] < .2} {
1285 set j [expr int(rand()*1000)]
1286 if {$test == 0} {
1287 set score [expr rand()]
1288 } else {
1289 set score [expr int(rand()*10)]
1290 }
1291 set auxarray($j) $score
1292 $r zadd myzset $score $j
1293 }
1294 }
1295 foreach {item score} [array get auxarray] {
1296 lappend auxlist [list $score $item]
1297 }
1298 set sorted [lsort -command zlistAlikeSort $auxlist]
1299 set auxlist {}
1300 foreach x $sorted {
1301 lappend auxlist [lindex $x 1]
1302 }
1303 set fromredis [$r zrange myzset 0 -1]
1304 set delta 0
1305 for {set i 0} {$i < [llength $fromredis]} {incr i} {
1306 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
1307 incr delta
1308 }
1309 }
1310 }
1311 format $delta
1312 } {0}
1313
28173a49 1314 test {ZINCRBY - can create a new sorted set} {
1315 $r del zset
1316 $r zincrby zset 1 foo
1317 list [$r zrange zset 0 -1] [$r zscore zset foo]
1318 } {foo 1}
1319
1320 test {ZINCRBY - increment and decrement} {
1321 $r zincrby zset 2 foo
1322 $r zincrby zset 1 bar
1323 set v1 [$r zrange zset 0 -1]
1324 $r zincrby zset 10 bar
1325 $r zincrby zset -5 foo
1326 $r zincrby zset -5 bar
1327 set v2 [$r zrange zset 0 -1]
1328 list $v1 $v2 [$r zscore zset foo] [$r zscore zset bar]
1329 } {{bar foo} {foo bar} -2 6}
1330
223a0591 1331 test {ZRANGEBYSCORE and ZCOUNT basics} {
c74e7c77 1332 $r del zset
1333 $r zadd zset 1 a
1334 $r zadd zset 2 b
1335 $r zadd zset 3 c
1336 $r zadd zset 4 d
1337 $r zadd zset 5 e
223a0591 1338 list [$r zrangebyscore zset 2 4] [$r zrangebyscore zset (2 (4] \
1339 [$r zcount zset 2 4] [$r zcount zset (2 (4]
1340 } {{b c d} c 3 1}
c74e7c77 1341
0500ef27
SH
1342 test {ZRANGEBYSCORE withscores} {
1343 $r del zset
1344 $r zadd zset 1 a
1345 $r zadd zset 2 b
1346 $r zadd zset 3 c
1347 $r zadd zset 4 d
1348 $r zadd zset 5 e
1349 $r zrangebyscore zset 2 4 withscores
1350 } {b 2 c 3 d 4}
1351
5b1207c6 1352 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
1353 set err {}
1354 $r del zset
1355 for {set i 0} {$i < 1000} {incr i} {
1356 $r zadd zset [expr rand()] $i
1357 }
1358 for {set i 0} {$i < 100} {incr i} {
1359 set min [expr rand()]
1360 set max [expr rand()]
1361 if {$min > $max} {
1362 set aux $min
1363 set min $max
1364 set max $aux
1365 }
1366 set low [$r zrangebyscore zset -inf $min]
1367 set ok [$r zrangebyscore zset $min $max]
1368 set high [$r zrangebyscore zset $max +inf]
223a0591 1369 set lowx [$r zrangebyscore zset -inf ($min]
1370 set okx [$r zrangebyscore zset ($min ($max]
1371 set highx [$r zrangebyscore zset ($max +inf]
1372
1373 if {[$r zcount zset -inf $min] != [llength $low]} {
1374 append err "Error, len does not match zcount\n"
1375 }
1376 if {[$r zcount zset $min $max] != [llength $ok]} {
1377 append err "Error, len does not match zcount\n"
1378 }
1379 if {[$r zcount zset $max +inf] != [llength $high]} {
1380 append err "Error, len does not match zcount\n"
1381 }
1382 if {[$r zcount zset -inf ($min] != [llength $lowx]} {
1383 append err "Error, len does not match zcount\n"
1384 }
1385 if {[$r zcount zset ($min ($max] != [llength $okx]} {
1386 append err "Error, len does not match zcount\n"
1387 }
1388 if {[$r zcount zset ($max +inf] != [llength $highx]} {
1389 append err "Error, len does not match zcount\n"
1390 }
1391
5b1207c6 1392 foreach x $low {
1393 set score [$r zscore zset $x]
1394 if {$score > $min} {
1395 append err "Error, score for $x is $score > $min\n"
1396 }
1397 }
223a0591 1398 foreach x $lowx {
1399 set score [$r zscore zset $x]
1400 if {$score >= $min} {
1401 append err "Error, score for $x is $score >= $min\n"
1402 }
1403 }
5b1207c6 1404 foreach x $ok {
1405 set score [$r zscore zset $x]
1406 if {$score < $min || $score > $max} {
1407 append err "Error, score for $x is $score outside $min-$max range\n"
1408 }
1409 }
223a0591 1410 foreach x $okx {
1411 set score [$r zscore zset $x]
1412 if {$score <= $min || $score >= $max} {
1413 append err "Error, score for $x is $score outside $min-$max open range\n"
1414 }
1415 }
5b1207c6 1416 foreach x $high {
1417 set score [$r zscore zset $x]
1418 if {$score < $max} {
1419 append err "Error, score for $x is $score < $max\n"
1420 }
1421 }
223a0591 1422 foreach x $highx {
1423 set score [$r zscore zset $x]
1424 if {$score <= $max} {
1425 append err "Error, score for $x is $score <= $max\n"
1426 }
1427 }
5b1207c6 1428 }
1429 set _ $err
1430 } {}
1431
0b13687c 1432 test {ZRANGEBYSCORE with LIMIT} {
1433 $r del zset
1434 $r zadd zset 1 a
1435 $r zadd zset 2 b
1436 $r zadd zset 3 c
1437 $r zadd zset 4 d
1438 $r zadd zset 5 e
1439 list \
1440 [$r zrangebyscore zset 0 10 LIMIT 0 2] \
1441 [$r zrangebyscore zset 0 10 LIMIT 2 3] \
1442 [$r zrangebyscore zset 0 10 LIMIT 2 10] \
1443 [$r zrangebyscore zset 0 10 LIMIT 20 10]
1444 } {{a b} {c d e} {c d e} {}}
1445
0500ef27
SH
1446 test {ZRANGEBYSCORE with LIMIT and withscores} {
1447 $r del zset
1448 $r zadd zset 10 a
1449 $r zadd zset 20 b
1450 $r zadd zset 30 c
1451 $r zadd zset 40 d
1452 $r zadd zset 50 e
1453 $r zrangebyscore zset 20 50 LIMIT 2 3 withscores
1454 } {d 40 e 50}
1455
9212eafd 1456 test {ZREMRANGEBYSCORE basics} {
9c21a518 1457 $r del zset
1458 $r zadd zset 1 a
1459 $r zadd zset 2 b
1460 $r zadd zset 3 c
1461 $r zadd zset 4 d
1462 $r zadd zset 5 e
1463 list [$r zremrangebyscore zset 2 4] [$r zrange zset 0 -1]
1464 } {3 {a e}}
1465
9212eafd 1466 test {ZREMRANGEBYSCORE from -inf to +inf} {
9c21a518 1467 $r del zset
1468 $r zadd zset 1 a
1469 $r zadd zset 2 b
1470 $r zadd zset 3 c
1471 $r zadd zset 4 d
1472 $r zadd zset 5 e
1473 list [$r zremrangebyscore zset -inf +inf] [$r zrange zset 0 -1]
1474 } {5 {}}
1475
9212eafd
PN
1476 test {ZREMRANGEBYRANK basics} {
1477 $r del zset
1478 $r zadd zset 1 a
1479 $r zadd zset 2 b
1480 $r zadd zset 3 c
1481 $r zadd zset 4 d
1482 $r zadd zset 5 e
1483 list [$r zremrangebyrank zset 1 3] [$r zrange zset 0 -1]
1484 } {3 {a e}}
1485
2830ca53 1486 test {ZUNION basics} {
b287c9bb
PN
1487 $r del zseta zsetb zsetc
1488 $r zadd zseta 1 a
1489 $r zadd zseta 2 b
1490 $r zadd zseta 3 c
1491 $r zadd zsetb 1 b
1492 $r zadd zsetb 2 c
1493 $r zadd zsetb 3 d
2830ca53 1494 list [$r zunion zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
b287c9bb
PN
1495 } {4 {a 1 b 3 d 3 c 5}}
1496
2830ca53
PN
1497 test {ZUNION with weights} {
1498 list [$r zunion zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
b287c9bb
PN
1499 } {4 {a 2 b 7 d 9 c 12}}
1500
d2764cd6
PN
1501 test {ZUNION with AGGREGATE MIN} {
1502 list [$r zunion zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1503 } {4 {a 1 b 1 c 2 d 3}}
1504
1505 test {ZUNION with AGGREGATE MAX} {
1506 list [$r zunion zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1507 } {4 {a 1 b 2 c 3 d 3}}
1508
2830ca53
PN
1509 test {ZINTER basics} {
1510 list [$r zinter zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
1511 } {2 {b 3 c 5}}
1512
1513 test {ZINTER with weights} {
1514 list [$r zinter zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
1515 } {2 {b 7 c 12}}
1516
d2764cd6
PN
1517 test {ZINTER with AGGREGATE MIN} {
1518 list [$r zinter zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1519 } {2 {b 1 c 2}}
1520
1521 test {ZINTER with AGGREGATE MAX} {
1522 list [$r zinter zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1523 } {2 {b 2 c 3}}
1524
0b13687c 1525 test {SORT against sorted sets} {
1526 $r del zset
1527 $r zadd zset 1 a
1528 $r zadd zset 5 b
1529 $r zadd zset 2 c
1530 $r zadd zset 10 d
1531 $r zadd zset 3 e
1532 $r sort zset alpha desc
1533 } {e d c b a}
1534
c74e7c77 1535 test {Sorted sets +inf and -inf handling} {
1536 $r del zset
1537 $r zadd zset -100 a
1538 $r zadd zset 200 b
1539 $r zadd zset -300 c
1540 $r zadd zset 1000000 d
1541 $r zadd zset +inf max
1542 $r zadd zset -inf min
1543 $r zrange zset 0 -1
1544 } {min c a b d max}
1545
164ee595 1546 test {HSET/HLEN - Small hash creation} {
1547 array set smallhash {}
1548 for {set i 0} {$i < 8} {incr i} {
1549 set key [randstring 0 8 alpha]
1550 set val [randstring 0 8 alpha]
1551 if {[info exists smallhash($key)]} {
1552 incr i -1
1553 continue
1554 }
1555 $r hset smallhash $key $val
1556 set smallhash($key) $val
1557 }
1558 list [$r hlen smallhash]
1559 } {8}
1560
1561 test {Is the small hash encoded with a zipmap?} {
1562 $r debug object smallhash
1563 } {*zipmap*}
1564
1565 test {HSET/HLEN - Big hash creation} {
1566 array set bighash {}
1567 for {set i 0} {$i < 1024} {incr i} {
1568 set key [randstring 0 8 alpha]
1569 set val [randstring 0 8 alpha]
1570 if {[info exists bighash($key)]} {
1571 incr i -1
1572 continue
1573 }
1574 $r hset bighash $key $val
1575 set bighash($key) $val
1576 }
1577 list [$r hlen bighash]
1578 } {1024}
1579
1580 test {Is the big hash encoded with a zipmap?} {
1581 $r debug object bighash
1582 } {*hashtable*}
1583
1584 test {HGET against the small hash} {
1585 set err {}
1586 foreach k [array names smallhash *] {
1587 if {$smallhash($k) ne [$r hget smallhash $k]} {
1588 set err "$smallhash($k) != [$r hget smallhash $k]"
1589 break
1590 }
1591 }
1592 set _ $err
1593 } {}
1594
1595 test {HGET against the big hash} {
1596 set err {}
1597 foreach k [array names bighash *] {
1598 if {$bighash($k) ne [$r hget bighash $k]} {
1599 set err "$bighash($k) != [$r hget bighash $k]"
1600 break
1601 }
1602 }
1603 set _ $err
1604 } {}
1605
516977de 1606 test {HSET in update and insert mode} {
b1d9c91c 1607 set rv {}
1608 set k [lindex [array names smallhash *] 0]
516977de 1609 lappend rv [$r hset smallhash $k newval1]
11d9d1e3 1610 set smallhash($k) newval1
516977de 1611 lappend rv [$r hget smallhash $k]
b1d9c91c 1612 lappend rv [$r hset smallhash __foobar123__ newval]
1613 set k [lindex [array names bighash *] 0]
516977de 1614 lappend rv [$r hset bighash $k newval2]
11d9d1e3 1615 set bighash($k) newval2
516977de 1616 lappend rv [$r hget bighash $k]
b1d9c91c 1617 lappend rv [$r hset bighash __foobar123__ newval]
1618 lappend rv [$r hdel smallhash __foobar123__]
1619 lappend rv [$r hdel bighash __foobar123__]
1620 set _ $rv
516977de 1621 } {0 newval1 1 0 newval2 1 1 1}
b1d9c91c 1622
1623 test {HGET against non existing key} {
1624 set rv {}
1625 lappend rv [$r hget smallhash __123123123__]
1626 lappend rv [$r hget bighash __123123123__]
1627 set _ $rv
1628 } {{} {}}
1629
1630 test {HKEYS - small hash} {
1631 lsort [$r hkeys smallhash]
1632 } [lsort [array names smallhash *]]
1633
1634 test {HKEYS - big hash} {
1635 lsort [$r hkeys bighash]
1636 } [lsort [array names bighash *]]
1637
11d9d1e3 1638 test {HVALS - small hash} {
1639 set vals {}
1640 foreach {k v} [array get smallhash] {
1641 lappend vals $v
1642 }
1643 set _ [lsort $vals]
1644 } [lsort [$r hvals smallhash]]
1645
1646 test {HVALS - big hash} {
1647 set vals {}
1648 foreach {k v} [array get bighash] {
1649 lappend vals $v
1650 }
1651 set _ [lsort $vals]
1652 } [lsort [$r hvals bighash]]
1653
1654 test {HGETALL - small hash} {
1655 lsort [$r hgetall smallhash]
1656 } [lsort [array get smallhash]]
1657
1658 test {HGETALL - big hash} {
1659 lsort [$r hgetall bighash]
1660 } [lsort [array get bighash]]
1661
1662 test {HDEL and return value} {
1663 set rv {}
1664 lappend rv [$r hdel smallhash nokey]
1665 lappend rv [$r hdel bighash nokey]
1666 set k [lindex [array names smallhash *] 0]
1667 lappend rv [$r hdel smallhash $k]
1668 lappend rv [$r hdel smallhash $k]
1669 lappend rv [$r hget smallhash $k]
a86f14b1 1670 unset smallhash($k)
11d9d1e3 1671 set k [lindex [array names bighash *] 0]
1672 lappend rv [$r hdel bighash $k]
1673 lappend rv [$r hdel bighash $k]
1674 lappend rv [$r hget bighash $k]
a86f14b1 1675 unset bighash($k)
11d9d1e3 1676 set _ $rv
1677 } {0 0 1 0 {} 1 0 {}}
1678
a86f14b1 1679 test {HEXISTS} {
1680 set rv {}
1681 set k [lindex [array names smallhash *] 0]
1682 lappend rv [$r hexists smallhash $k]
1683 lappend rv [$r hexists smallhash nokey]
1684 set k [lindex [array names bighash *] 0]
1685 lappend rv [$r hexists bighash $k]
1686 lappend rv [$r hexists bighash nokey]
1687 } {1 0 1 0}
1688
11d9d1e3 1689 test {Is a zipmap encoded Hash promoted on big payload?} {
1690 $r hset smallhash foo [string repeat a 1024]
1691 $r debug object smallhash
1692 } {*hashtable*}
1693
b1d9c91c 1694 # TODO:
b1d9c91c 1695 # Randomized test, small and big
1696 # .rdb / AOF consistency test should include hashes
1697
b9febaab 1698 test {EXPIRE - don't set timeouts multiple times} {
1699 $r set x foobar
1700 set v1 [$r expire x 5]
1701 set v2 [$r ttl x]
1702 set v3 [$r expire x 10]
1703 set v4 [$r ttl x]
1704 list $v1 $v2 $v3 $v4
1705 } {1 5 0 5}
1706
1707 test {EXPIRE - It should be still possible to read 'x'} {
1708 $r get x
1709 } {foobar}
1710
1711 test {EXPIRE - After 6 seconds the key should no longer be here} {
1712 after 6000
1713 list [$r get x] [$r exists x]
1714 } {{} 0}
1715
1716 test {EXPIRE - Delete on write policy} {
1717 $r del x
1718 $r lpush x foo
1719 $r expire x 1000
1720 $r lpush x bar
1721 $r lrange x 0 -1
1722 } {bar}
1723
5446315f 1724 test {EXPIREAT - Check for EXPIRE alike behavior} {
1725 $r del x
1726 $r set x foo
1727 $r expireat x [expr [clock seconds]+15]
1728 $r ttl x
1729 } {1[345]}
1730
d7f43c08 1731 test {ZSETs skiplist implementation backlink consistency test} {
1732 set diff 0
1733 set elements 10000
1734 for {set j 0} {$j < $elements} {incr j} {
1735 $r zadd myzset [expr rand()] "Element-$j"
1736 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1737 }
1738 set l1 [$r zrange myzset 0 -1]
1739 set l2 [$r zrevrange myzset 0 -1]
1740 for {set j 0} {$j < [llength $l1]} {incr j} {
1741 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1742 incr diff
1743 }
1744 }
1745 format $diff
1746 } {0}
1747
3589e1a7 1748 test {ZSETs ZRANK augmented skip list stress testing} {
1749 set err {}
1750 $r del myzset
1751 for {set k 0} {$k < 10000} {incr k} {
1752 set i [expr {$k%1000}]
1753 if {[expr rand()] < .2} {
1754 $r zrem myzset $i
1755 } else {
1756 set score [expr rand()]
1757 $r zadd myzset $score $i
1758 }
1759 set card [$r zcard myzset]
1760 if {$card > 0} {
1761 set index [randomInt $card]
1762 set ele [lindex [$r zrange myzset $index $index] 0]
1763 set rank [$r zrank myzset $ele]
1764 if {$rank != $index} {
67cac143 1765 set err "$ele RANK is wrong! ($rank != $index)"
3589e1a7 1766 break
1767 }
1768 }
1769 }
1770 set _ $err
1771 } {}
1772
75398fbc 1773 foreach fuzztype {binary alpha compr} {
1774 test "FUZZ stresser with data model $fuzztype" {
1775 set err 0
f69f2cba 1776 for {set i 0} {$i < 10000} {incr i} {
75398fbc 1777 set fuzz [randstring 0 512 $fuzztype]
1778 $r set foo $fuzz
1779 set got [$r get foo]
1780 if {$got ne $fuzz} {
f69f2cba 1781 set err [list $fuzz $got]
75398fbc 1782 break
1783 }
1784 }
f69f2cba 1785 set _ $err
75398fbc 1786 } {0}
1787 }
1788
0d36ded0 1789 test {BGSAVE} {
7c775e09 1790 waitForBgsave $r
0d36ded0 1791 $r flushdb
1792 $r save
1793 $r set x 10
1794 $r bgsave
1795 waitForBgsave $r
1796 $r debug reload
1797 $r get x
1798 } {10}
1799
7c49733c 1800 test {Handle an empty query well} {
1801 set fd [$r channel]
1802 puts -nonewline $fd "\r\n"
1803 flush $fd
1804 $r ping
1805 } {PONG}
1806
1807 test {Negative multi bulk command does not create problems} {
1808 set fd [$r channel]
1809 puts -nonewline $fd "*-10\r\n"
1810 flush $fd
1811 $r ping
1812 } {PONG}
1813
1814 test {Negative multi bulk payload} {
1815 set fd [$r channel]
1816 puts -nonewline $fd "SET x -10\r\n"
1817 flush $fd
1818 gets $fd
1819 } {*invalid bulk*}
1820
1821 test {Too big bulk payload} {
1822 set fd [$r channel]
1823 puts -nonewline $fd "SET x 2000000000\r\n"
1824 flush $fd
1825 gets $fd
1826 } {*invalid bulk*count*}
1827
1828 test {Multi bulk request not followed by bulk args} {
1829 set fd [$r channel]
1830 puts -nonewline $fd "*1\r\nfoo\r\n"
1831 flush $fd
1832 gets $fd
1833 } {*protocol error*}
1834
1835 test {Generic wrong number of args} {
1836 catch {$r ping x y z} err
1837 set _ $err
1838 } {*wrong*arguments*ping*}
1839
1840 test {SELECT an out of range DB} {
1841 catch {$r select 1000000} err
1842 set _ $err
1843 } {*invalid*}
1844
e054afda 1845 if {![catch {package require sha1}]} {
1846 test {Check consistency of different data types after a reload} {
1847 $r flushdb
1848 createComplexDataset $r 10000
1849 set sha1 [datasetDigest $r]
1850 $r debug reload
1851 set sha1_after [datasetDigest $r]
1852 expr {$sha1 eq $sha1_after}
1853 } {1}
e96e4fbf 1854
1855 test {Same dataset digest if saving/reloading as AOF?} {
1856 $r bgrewriteaof
1857 waitForBgrewriteaof $r
1858 $r debug loadaof
1859 set sha1_after [datasetDigest $r]
1860 expr {$sha1 eq $sha1_after}
1861 } {1}
e054afda 1862 }
1863
e96e4fbf 1864 test {EXPIRES after a reload (snapshot + append only file)} {
71c2b467 1865 $r flushdb
1866 $r set x 10
1867 $r expire x 1000
1868 $r save
1869 $r debug reload
1870 set ttl [$r ttl x]
e96e4fbf 1871 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
1872 $r bgrewriteaof
1873 waitForBgrewriteaof $r
1874 set ttl [$r ttl x]
1875 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
1876 list $e1 $e2
1877 } {1 1}
71c2b467 1878
483049a7 1879 test {PIPELINING stresser (also a regression for the old epoll bug)} {
1880 set fd2 [socket 127.0.0.1 6379]
1881 fconfigure $fd2 -encoding binary -translation binary
0447be2e 1882 puts -nonewline $fd2 "SELECT 9\r\n"
1883 flush $fd2
1884 gets $fd2
483049a7 1885
1886 for {set i 0} {$i < 100000} {incr i} {
1887 set q {}
1888 set val "0000${i}0000"
1889 append q "SET key:$i [string length $val]\r\n$val\r\n"
1890 puts -nonewline $fd2 $q
1891 set q {}
1892 append q "GET key:$i\r\n"
1893 puts -nonewline $fd2 $q
1894 }
1895 flush $fd2
1896
1897 for {set i 0} {$i < 100000} {incr i} {
1898 gets $fd2 line
1899 gets $fd2 count
1900 set count [string range $count 1 end]
1901 set val [read $fd2 $count]
1902 read $fd2 2
1903 }
1904 close $fd2
1905 set _ 1
1906 } {1}
1907
4409877e 1908 test {MUTLI / EXEC basics} {
1909 $r del mylist
1910 $r rpush mylist a
1911 $r rpush mylist b
1912 $r rpush mylist c
1913 $r multi
1914 set v1 [$r lrange mylist 0 -1]
1915 set v2 [$r ping]
1916 set v3 [$r exec]
1917 list $v1 $v2 $v3
1918 } {QUEUED QUEUED {{a b c} PONG}}
1919
18b6cb76
DJ
1920 test {DISCARD} {
1921 $r del mylist
1922 $r rpush mylist a
1923 $r rpush mylist b
1924 $r rpush mylist c
1925 $r multi
1926 set v1 [$r del mylist]
1927 set v2 [$r discard]
1928 set v3 [$r lrange mylist 0 -1]
1929 list $v1 $v2 $v3
1930 } {QUEUED OK {a b c}}
1931
3c290b9b 1932 test {APPEND basics} {
1933 list [$r append foo bar] [$r get foo] \
1934 [$r append foo 100] [$r get foo]
1935 } {3 bar 6 bar100}
1936
1937 test {APPEND fuzzing} {
1938 set err {}
1939 foreach type {binary alpha compr} {
1940 set buf {}
1941 $r del x
1942 for {set i 0} {$i < 1000} {incr i} {
1943 set bin [randstring 0 10 $type]
1944 append buf $bin
1945 $r append x $bin
1946 }
1947 if {$buf != [$r get x]} {
1948 set err "Expected '$buf' found '[$r get x]'"
1949 break
1950 }
1951 }
1952 set _ $err
1953 } {}
1954
ed9b544e 1955 # Leave the user with a clean DB before to exit
eea4baf7 1956 test {FLUSHDB} {
1957 set aux {}
1958 $r select 9
1959 $r flushdb
1960 lappend aux [$r dbsize]
1961 $r select 10
1962 $r flushdb
1963 lappend aux [$r dbsize]
1964 } {0 0}
ed9b544e 1965
c28b42ac 1966 test {Perform a final SAVE to leave a clean DB on disk} {
1967 $r save
1968 } {OK}
1969
17511391 1970 catch {
1971 if {[string match {*Darwin*} [exec uname -a]]} {
1972 test {Check for memory leaks} {
1973 exec leaks redis-server
1974 } {*0 leaks*}
1975 }
1976 }
1977
ed9b544e 1978 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
1979 if {$::failed > 0} {
1980 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
1981 }
ed9b544e 1982}
1983
5a6948fb 1984proc stress {} {
43c9dc7b 1985 set r [redis]
eea4baf7 1986 $r select 9
1987 $r flushdb
5a6948fb 1988 while 1 {
1989 set randkey [expr int(rand()*10000)]
1990 set randval [expr int(rand()*10000)]
1991 set randidx0 [expr int(rand()*10)]
1992 set randidx1 [expr int(rand()*10)]
12f9d551 1993 set cmd [expr int(rand()*20)]
43c9dc7b 1994 catch {
1995 if {$cmd == 0} {$r set $randkey $randval}
1996 if {$cmd == 1} {$r get $randkey}
1997 if {$cmd == 2} {$r incr $randkey}
1998 if {$cmd == 3} {$r lpush $randkey $randval}
1999 if {$cmd == 4} {$r rpop $randkey}
2000 if {$cmd == 5} {$r del $randkey}
12f9d551 2001 if {$cmd == 6} {$r llen $randkey}
2002 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
2003 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
2004 if {$cmd == 9} {$r lindex $randkey $randidx0}
2005 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
2006 if {$cmd == 11} {$r sadd $randkey $randval}
2007 if {$cmd == 12} {$r srem $randkey $randval}
2008 if {$cmd == 13} {$r smove $randkey $randval}
2009 if {$cmd == 14} {$r scard $randkey}
2010 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
43c9dc7b 2011 }
5a6948fb 2012 flush stdout
2013 }
eea4baf7 2014 $r flushdb
43c9dc7b 2015 $r close
5a6948fb 2016}
2017
fc77604c 2018# Set a few configuration defaults
2019set ::host 127.0.0.1
2020set ::port 6379
2021set ::stress 0
2022set ::flush 0
2023set ::first 0
2024set ::last 1000000
2025
2026# Parse arguments
2027for {set j 0} {$j < [llength $argv]} {incr j} {
2028 set opt [lindex $argv $j]
2029 set arg [lindex $argv [expr $j+1]]
2030 set lastarg [expr {$arg eq {}}]
2031 if {$opt eq {-h} && !$lastarg} {
2032 set ::host $arg
2033 incr j
2034 } elseif {$opt eq {-p} && !$lastarg} {
2035 set ::port $arg
2036 incr j
2037 } elseif {$opt eq {-stress}} {
2038 set ::stress 1
2039 } elseif {$opt eq {--flush}} {
2040 set ::flush 1
2041 } elseif {$opt eq {--first} && !$lastarg} {
2042 set ::first $arg
2043 incr j
2044 } elseif {$opt eq {--last} && !$lastarg} {
2045 set ::last $arg
2046 incr j
2047 } else {
7c49733c 2048 puts "Wrong argument: $opt"
fc77604c 2049 exit 1
2050 }
2051}
2052
eea4baf7 2053# Before to run the test check if DB 9 and DB 10 are empty
2054set r [redis]
fc77604c 2055
2056if {$::flush} {
2057 $r flushall
2058}
2059
eea4baf7 2060$r select 9
2061set db9size [$r dbsize]
2062$r select 10
2063set db10size [$r dbsize]
2064if {$db9size != 0 || $db10size != 0} {
2065 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
2066 exit 1
2067}
2068$r close
2069unset r
2070unset db9size
2071unset db10size
2072
fc77604c 2073if {$::stress} {
5a6948fb 2074 stress
ed9b544e 2075} else {
fc77604c 2076 main $::host $::port
ed9b544e 2077}