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