]> git.saurik.com Git - redis.git/blame - test-redis.tcl
Sharing of small integer objects: may save a lot of memory with datasets having many...
[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
8bca8773
PN
1492 test {ZUNION against non-existing key doesn't set destination} {
1493 $r del zseta
1494 list [$r zunion dst_key 1 zseta] [$r exists dst_key]
1495 } {0 0}
1496
2830ca53 1497 test {ZUNION basics} {
b287c9bb
PN
1498 $r del zseta zsetb zsetc
1499 $r zadd zseta 1 a
1500 $r zadd zseta 2 b
1501 $r zadd zseta 3 c
1502 $r zadd zsetb 1 b
1503 $r zadd zsetb 2 c
1504 $r zadd zsetb 3 d
2830ca53 1505 list [$r zunion zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
b287c9bb
PN
1506 } {4 {a 1 b 3 d 3 c 5}}
1507
2830ca53
PN
1508 test {ZUNION with weights} {
1509 list [$r zunion zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
b287c9bb
PN
1510 } {4 {a 2 b 7 d 9 c 12}}
1511
d2764cd6
PN
1512 test {ZUNION with AGGREGATE MIN} {
1513 list [$r zunion zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1514 } {4 {a 1 b 1 c 2 d 3}}
1515
1516 test {ZUNION with AGGREGATE MAX} {
1517 list [$r zunion zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1518 } {4 {a 1 b 2 c 3 d 3}}
1519
2830ca53
PN
1520 test {ZINTER basics} {
1521 list [$r zinter zsetc 2 zseta zsetb] [$r zrange zsetc 0 -1 withscores]
1522 } {2 {b 3 c 5}}
1523
1524 test {ZINTER with weights} {
1525 list [$r zinter zsetc 2 zseta zsetb weights 2 3] [$r zrange zsetc 0 -1 withscores]
1526 } {2 {b 7 c 12}}
1527
d2764cd6
PN
1528 test {ZINTER with AGGREGATE MIN} {
1529 list [$r zinter zsetc 2 zseta zsetb aggregate min] [$r zrange zsetc 0 -1 withscores]
1530 } {2 {b 1 c 2}}
1531
1532 test {ZINTER with AGGREGATE MAX} {
1533 list [$r zinter zsetc 2 zseta zsetb aggregate max] [$r zrange zsetc 0 -1 withscores]
1534 } {2 {b 2 c 3}}
1535
0b13687c 1536 test {SORT against sorted sets} {
1537 $r del zset
1538 $r zadd zset 1 a
1539 $r zadd zset 5 b
1540 $r zadd zset 2 c
1541 $r zadd zset 10 d
1542 $r zadd zset 3 e
1543 $r sort zset alpha desc
1544 } {e d c b a}
1545
c74e7c77 1546 test {Sorted sets +inf and -inf handling} {
1547 $r del zset
1548 $r zadd zset -100 a
1549 $r zadd zset 200 b
1550 $r zadd zset -300 c
1551 $r zadd zset 1000000 d
1552 $r zadd zset +inf max
1553 $r zadd zset -inf min
1554 $r zrange zset 0 -1
1555 } {min c a b d max}
1556
164ee595 1557 test {HSET/HLEN - Small hash creation} {
1558 array set smallhash {}
1559 for {set i 0} {$i < 8} {incr i} {
1560 set key [randstring 0 8 alpha]
1561 set val [randstring 0 8 alpha]
1562 if {[info exists smallhash($key)]} {
1563 incr i -1
1564 continue
1565 }
1566 $r hset smallhash $key $val
1567 set smallhash($key) $val
1568 }
1569 list [$r hlen smallhash]
1570 } {8}
1571
1572 test {Is the small hash encoded with a zipmap?} {
1573 $r debug object smallhash
1574 } {*zipmap*}
1575
1576 test {HSET/HLEN - Big hash creation} {
1577 array set bighash {}
1578 for {set i 0} {$i < 1024} {incr i} {
1579 set key [randstring 0 8 alpha]
1580 set val [randstring 0 8 alpha]
1581 if {[info exists bighash($key)]} {
1582 incr i -1
1583 continue
1584 }
1585 $r hset bighash $key $val
1586 set bighash($key) $val
1587 }
1588 list [$r hlen bighash]
1589 } {1024}
1590
1591 test {Is the big hash encoded with a zipmap?} {
1592 $r debug object bighash
1593 } {*hashtable*}
1594
1595 test {HGET against the small hash} {
1596 set err {}
1597 foreach k [array names smallhash *] {
1598 if {$smallhash($k) ne [$r hget smallhash $k]} {
1599 set err "$smallhash($k) != [$r hget smallhash $k]"
1600 break
1601 }
1602 }
1603 set _ $err
1604 } {}
1605
1606 test {HGET against the big hash} {
1607 set err {}
1608 foreach k [array names bighash *] {
1609 if {$bighash($k) ne [$r hget bighash $k]} {
1610 set err "$bighash($k) != [$r hget bighash $k]"
1611 break
1612 }
1613 }
1614 set _ $err
1615 } {}
1616
516977de 1617 test {HSET in update and insert mode} {
b1d9c91c 1618 set rv {}
1619 set k [lindex [array names smallhash *] 0]
516977de 1620 lappend rv [$r hset smallhash $k newval1]
11d9d1e3 1621 set smallhash($k) newval1
516977de 1622 lappend rv [$r hget smallhash $k]
b1d9c91c 1623 lappend rv [$r hset smallhash __foobar123__ newval]
1624 set k [lindex [array names bighash *] 0]
516977de 1625 lappend rv [$r hset bighash $k newval2]
11d9d1e3 1626 set bighash($k) newval2
516977de 1627 lappend rv [$r hget bighash $k]
b1d9c91c 1628 lappend rv [$r hset bighash __foobar123__ newval]
1629 lappend rv [$r hdel smallhash __foobar123__]
1630 lappend rv [$r hdel bighash __foobar123__]
1631 set _ $rv
516977de 1632 } {0 newval1 1 0 newval2 1 1 1}
b1d9c91c 1633
1634 test {HGET against non existing key} {
1635 set rv {}
1636 lappend rv [$r hget smallhash __123123123__]
1637 lappend rv [$r hget bighash __123123123__]
1638 set _ $rv
1639 } {{} {}}
1640
1641 test {HKEYS - small hash} {
1642 lsort [$r hkeys smallhash]
1643 } [lsort [array names smallhash *]]
1644
1645 test {HKEYS - big hash} {
1646 lsort [$r hkeys bighash]
1647 } [lsort [array names bighash *]]
1648
11d9d1e3 1649 test {HVALS - small hash} {
1650 set vals {}
1651 foreach {k v} [array get smallhash] {
1652 lappend vals $v
1653 }
1654 set _ [lsort $vals]
1655 } [lsort [$r hvals smallhash]]
1656
1657 test {HVALS - big hash} {
1658 set vals {}
1659 foreach {k v} [array get bighash] {
1660 lappend vals $v
1661 }
1662 set _ [lsort $vals]
1663 } [lsort [$r hvals bighash]]
1664
1665 test {HGETALL - small hash} {
1666 lsort [$r hgetall smallhash]
1667 } [lsort [array get smallhash]]
1668
1669 test {HGETALL - big hash} {
1670 lsort [$r hgetall bighash]
1671 } [lsort [array get bighash]]
1672
1673 test {HDEL and return value} {
1674 set rv {}
1675 lappend rv [$r hdel smallhash nokey]
1676 lappend rv [$r hdel bighash nokey]
1677 set k [lindex [array names smallhash *] 0]
1678 lappend rv [$r hdel smallhash $k]
1679 lappend rv [$r hdel smallhash $k]
1680 lappend rv [$r hget smallhash $k]
a86f14b1 1681 unset smallhash($k)
11d9d1e3 1682 set k [lindex [array names bighash *] 0]
1683 lappend rv [$r hdel bighash $k]
1684 lappend rv [$r hdel bighash $k]
1685 lappend rv [$r hget bighash $k]
a86f14b1 1686 unset bighash($k)
11d9d1e3 1687 set _ $rv
1688 } {0 0 1 0 {} 1 0 {}}
1689
a86f14b1 1690 test {HEXISTS} {
1691 set rv {}
1692 set k [lindex [array names smallhash *] 0]
1693 lappend rv [$r hexists smallhash $k]
1694 lappend rv [$r hexists smallhash nokey]
1695 set k [lindex [array names bighash *] 0]
1696 lappend rv [$r hexists bighash $k]
1697 lappend rv [$r hexists bighash nokey]
1698 } {1 0 1 0}
1699
11d9d1e3 1700 test {Is a zipmap encoded Hash promoted on big payload?} {
1701 $r hset smallhash foo [string repeat a 1024]
1702 $r debug object smallhash
1703 } {*hashtable*}
1704
01426b05
PN
1705 test {HINCRBY against non existing database key} {
1706 $r del htest
1707 list [$r hincrby htest foo 2]
1708 } {2}
1709
1710 test {HINCRBY against non existing hash key} {
1711 set rv {}
1712 $r hdel smallhash tmp
1713 $r hdel bighash tmp
1714 lappend rv [$r hincrby smallhash tmp 2]
1715 lappend rv [$r hget smallhash tmp]
1716 lappend rv [$r hincrby bighash tmp 2]
1717 lappend rv [$r hget bighash tmp]
1718 } {2 2 2 2}
1719
1720 test {HINCRBY against hash key created by hincrby itself} {
1721 set rv {}
1722 lappend rv [$r hincrby smallhash tmp 3]
1723 lappend rv [$r hget smallhash tmp]
1724 lappend rv [$r hincrby bighash tmp 3]
1725 lappend rv [$r hget bighash tmp]
1726 } {5 5 5 5}
1727
1728 test {HINCRBY against hash key originally set with HSET} {
1729 $r hset smallhash tmp 100
1730 $r hset bighash tmp 100
1731 list [$r hincrby smallhash tmp 2] [$r hincrby bighash tmp 2]
1732 } {102 102}
1733
1734 test {HINCRBY over 32bit value} {
1735 $r hset smallhash tmp 17179869184
1736 $r hset bighash tmp 17179869184
1737 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1738 } {17179869185 17179869185}
1739
1740 test {HINCRBY over 32bit value with over 32bit increment} {
1741 $r hset smallhash tmp 17179869184
1742 $r hset bighash tmp 17179869184
1743 list [$r hincrby smallhash tmp 17179869184] [$r hincrby bighash tmp 17179869184]
1744 } {34359738368 34359738368}
1745
1746 test {HINCRBY against key with spaces (no integer encoded)} {
1747 $r hset smallhash tmp " 11 "
1748 $r hset bighash tmp " 11 "
1749 list [$r hincrby smallhash tmp 1] [$r hincrby bighash tmp 1]
1750 } {12 12}
1751
b1d9c91c 1752 # TODO:
b1d9c91c 1753 # Randomized test, small and big
1754 # .rdb / AOF consistency test should include hashes
1755
b9febaab 1756 test {EXPIRE - don't set timeouts multiple times} {
1757 $r set x foobar
1758 set v1 [$r expire x 5]
1759 set v2 [$r ttl x]
1760 set v3 [$r expire x 10]
1761 set v4 [$r ttl x]
1762 list $v1 $v2 $v3 $v4
1763 } {1 5 0 5}
1764
1765 test {EXPIRE - It should be still possible to read 'x'} {
1766 $r get x
1767 } {foobar}
1768
1769 test {EXPIRE - After 6 seconds the key should no longer be here} {
1770 after 6000
1771 list [$r get x] [$r exists x]
1772 } {{} 0}
1773
1774 test {EXPIRE - Delete on write policy} {
1775 $r del x
1776 $r lpush x foo
1777 $r expire x 1000
1778 $r lpush x bar
1779 $r lrange x 0 -1
1780 } {bar}
1781
5446315f 1782 test {EXPIREAT - Check for EXPIRE alike behavior} {
1783 $r del x
1784 $r set x foo
1785 $r expireat x [expr [clock seconds]+15]
1786 $r ttl x
1787 } {1[345]}
1788
d7f43c08 1789 test {ZSETs skiplist implementation backlink consistency test} {
1790 set diff 0
1791 set elements 10000
1792 for {set j 0} {$j < $elements} {incr j} {
1793 $r zadd myzset [expr rand()] "Element-$j"
1794 $r zrem myzset "Element-[expr int(rand()*$elements)]"
1795 }
1796 set l1 [$r zrange myzset 0 -1]
1797 set l2 [$r zrevrange myzset 0 -1]
1798 for {set j 0} {$j < [llength $l1]} {incr j} {
1799 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
1800 incr diff
1801 }
1802 }
1803 format $diff
1804 } {0}
1805
3589e1a7 1806 test {ZSETs ZRANK augmented skip list stress testing} {
1807 set err {}
1808 $r del myzset
1809 for {set k 0} {$k < 10000} {incr k} {
1810 set i [expr {$k%1000}]
1811 if {[expr rand()] < .2} {
1812 $r zrem myzset $i
1813 } else {
1814 set score [expr rand()]
1815 $r zadd myzset $score $i
1816 }
1817 set card [$r zcard myzset]
1818 if {$card > 0} {
1819 set index [randomInt $card]
1820 set ele [lindex [$r zrange myzset $index $index] 0]
1821 set rank [$r zrank myzset $ele]
1822 if {$rank != $index} {
67cac143 1823 set err "$ele RANK is wrong! ($rank != $index)"
3589e1a7 1824 break
1825 }
1826 }
1827 }
1828 set _ $err
1829 } {}
1830
75398fbc 1831 foreach fuzztype {binary alpha compr} {
1832 test "FUZZ stresser with data model $fuzztype" {
1833 set err 0
f69f2cba 1834 for {set i 0} {$i < 10000} {incr i} {
75398fbc 1835 set fuzz [randstring 0 512 $fuzztype]
1836 $r set foo $fuzz
1837 set got [$r get foo]
1838 if {$got ne $fuzz} {
f69f2cba 1839 set err [list $fuzz $got]
75398fbc 1840 break
1841 }
1842 }
f69f2cba 1843 set _ $err
75398fbc 1844 } {0}
1845 }
1846
0d36ded0 1847 test {BGSAVE} {
7c775e09 1848 waitForBgsave $r
0d36ded0 1849 $r flushdb
1850 $r save
1851 $r set x 10
1852 $r bgsave
1853 waitForBgsave $r
1854 $r debug reload
1855 $r get x
1856 } {10}
1857
7c49733c 1858 test {Handle an empty query well} {
1859 set fd [$r channel]
1860 puts -nonewline $fd "\r\n"
1861 flush $fd
1862 $r ping
1863 } {PONG}
1864
1865 test {Negative multi bulk command does not create problems} {
1866 set fd [$r channel]
1867 puts -nonewline $fd "*-10\r\n"
1868 flush $fd
1869 $r ping
1870 } {PONG}
1871
1872 test {Negative multi bulk payload} {
1873 set fd [$r channel]
1874 puts -nonewline $fd "SET x -10\r\n"
1875 flush $fd
1876 gets $fd
1877 } {*invalid bulk*}
1878
1879 test {Too big bulk payload} {
1880 set fd [$r channel]
1881 puts -nonewline $fd "SET x 2000000000\r\n"
1882 flush $fd
1883 gets $fd
1884 } {*invalid bulk*count*}
1885
1886 test {Multi bulk request not followed by bulk args} {
1887 set fd [$r channel]
1888 puts -nonewline $fd "*1\r\nfoo\r\n"
1889 flush $fd
1890 gets $fd
1891 } {*protocol error*}
1892
1893 test {Generic wrong number of args} {
1894 catch {$r ping x y z} err
1895 set _ $err
1896 } {*wrong*arguments*ping*}
1897
1898 test {SELECT an out of range DB} {
1899 catch {$r select 1000000} err
1900 set _ $err
1901 } {*invalid*}
1902
e054afda 1903 if {![catch {package require sha1}]} {
1904 test {Check consistency of different data types after a reload} {
1905 $r flushdb
1906 createComplexDataset $r 10000
1907 set sha1 [datasetDigest $r]
1908 $r debug reload
1909 set sha1_after [datasetDigest $r]
1910 expr {$sha1 eq $sha1_after}
1911 } {1}
e96e4fbf 1912
1913 test {Same dataset digest if saving/reloading as AOF?} {
1914 $r bgrewriteaof
1915 waitForBgrewriteaof $r
1916 $r debug loadaof
1917 set sha1_after [datasetDigest $r]
1918 expr {$sha1 eq $sha1_after}
1919 } {1}
e054afda 1920 }
1921
e96e4fbf 1922 test {EXPIRES after a reload (snapshot + append only file)} {
71c2b467 1923 $r flushdb
1924 $r set x 10
1925 $r expire x 1000
1926 $r save
1927 $r debug reload
1928 set ttl [$r ttl x]
e96e4fbf 1929 set e1 [expr {$ttl > 900 && $ttl <= 1000}]
1930 $r bgrewriteaof
1931 waitForBgrewriteaof $r
1932 set ttl [$r ttl x]
1933 set e2 [expr {$ttl > 900 && $ttl <= 1000}]
1934 list $e1 $e2
1935 } {1 1}
71c2b467 1936
483049a7 1937 test {PIPELINING stresser (also a regression for the old epoll bug)} {
1938 set fd2 [socket 127.0.0.1 6379]
1939 fconfigure $fd2 -encoding binary -translation binary
0447be2e 1940 puts -nonewline $fd2 "SELECT 9\r\n"
1941 flush $fd2
1942 gets $fd2
483049a7 1943
1944 for {set i 0} {$i < 100000} {incr i} {
1945 set q {}
1946 set val "0000${i}0000"
1947 append q "SET key:$i [string length $val]\r\n$val\r\n"
1948 puts -nonewline $fd2 $q
1949 set q {}
1950 append q "GET key:$i\r\n"
1951 puts -nonewline $fd2 $q
1952 }
1953 flush $fd2
1954
1955 for {set i 0} {$i < 100000} {incr i} {
1956 gets $fd2 line
1957 gets $fd2 count
1958 set count [string range $count 1 end]
1959 set val [read $fd2 $count]
1960 read $fd2 2
1961 }
1962 close $fd2
1963 set _ 1
1964 } {1}
1965
4409877e 1966 test {MUTLI / EXEC basics} {
1967 $r del mylist
1968 $r rpush mylist a
1969 $r rpush mylist b
1970 $r rpush mylist c
1971 $r multi
1972 set v1 [$r lrange mylist 0 -1]
1973 set v2 [$r ping]
1974 set v3 [$r exec]
1975 list $v1 $v2 $v3
1976 } {QUEUED QUEUED {{a b c} PONG}}
1977
18b6cb76
DJ
1978 test {DISCARD} {
1979 $r del mylist
1980 $r rpush mylist a
1981 $r rpush mylist b
1982 $r rpush mylist c
1983 $r multi
1984 set v1 [$r del mylist]
1985 set v2 [$r discard]
1986 set v3 [$r lrange mylist 0 -1]
1987 list $v1 $v2 $v3
1988 } {QUEUED OK {a b c}}
1989
3c290b9b 1990 test {APPEND basics} {
1991 list [$r append foo bar] [$r get foo] \
1992 [$r append foo 100] [$r get foo]
1993 } {3 bar 6 bar100}
1994
1995 test {APPEND fuzzing} {
1996 set err {}
1997 foreach type {binary alpha compr} {
1998 set buf {}
1999 $r del x
2000 for {set i 0} {$i < 1000} {incr i} {
2001 set bin [randstring 0 10 $type]
2002 append buf $bin
2003 $r append x $bin
2004 }
2005 if {$buf != [$r get x]} {
2006 set err "Expected '$buf' found '[$r get x]'"
2007 break
2008 }
2009 }
2010 set _ $err
2011 } {}
2012
ed9b544e 2013 # Leave the user with a clean DB before to exit
eea4baf7 2014 test {FLUSHDB} {
2015 set aux {}
2016 $r select 9
2017 $r flushdb
2018 lappend aux [$r dbsize]
2019 $r select 10
2020 $r flushdb
2021 lappend aux [$r dbsize]
2022 } {0 0}
ed9b544e 2023
c28b42ac 2024 test {Perform a final SAVE to leave a clean DB on disk} {
2025 $r save
2026 } {OK}
2027
17511391 2028 catch {
2029 if {[string match {*Darwin*} [exec uname -a]]} {
2030 test {Check for memory leaks} {
2031 exec leaks redis-server
2032 } {*0 leaks*}
2033 }
2034 }
2035
ed9b544e 2036 puts "\n[expr $::passed+$::failed] tests, $::passed passed, $::failed failed"
2037 if {$::failed > 0} {
2038 puts "\n*** WARNING!!! $::failed FAILED TESTS ***\n"
2039 }
ed9b544e 2040}
2041
5a6948fb 2042proc stress {} {
43c9dc7b 2043 set r [redis]
eea4baf7 2044 $r select 9
2045 $r flushdb
5a6948fb 2046 while 1 {
2047 set randkey [expr int(rand()*10000)]
2048 set randval [expr int(rand()*10000)]
2049 set randidx0 [expr int(rand()*10)]
2050 set randidx1 [expr int(rand()*10)]
12f9d551 2051 set cmd [expr int(rand()*20)]
43c9dc7b 2052 catch {
2053 if {$cmd == 0} {$r set $randkey $randval}
2054 if {$cmd == 1} {$r get $randkey}
2055 if {$cmd == 2} {$r incr $randkey}
2056 if {$cmd == 3} {$r lpush $randkey $randval}
2057 if {$cmd == 4} {$r rpop $randkey}
2058 if {$cmd == 5} {$r del $randkey}
12f9d551 2059 if {$cmd == 6} {$r llen $randkey}
2060 if {$cmd == 7} {$r lrange $randkey $randidx0 $randidx1}
2061 if {$cmd == 8} {$r ltrim $randkey $randidx0 $randidx1}
2062 if {$cmd == 9} {$r lindex $randkey $randidx0}
2063 if {$cmd == 10} {$r lset $randkey $randidx0 $randval}
2064 if {$cmd == 11} {$r sadd $randkey $randval}
2065 if {$cmd == 12} {$r srem $randkey $randval}
2066 if {$cmd == 13} {$r smove $randkey $randval}
2067 if {$cmd == 14} {$r scard $randkey}
2068 if {$cmd == 15} {$r expire $randkey [expr $randval%60]}
43c9dc7b 2069 }
5a6948fb 2070 flush stdout
2071 }
eea4baf7 2072 $r flushdb
43c9dc7b 2073 $r close
5a6948fb 2074}
2075
fc77604c 2076# Set a few configuration defaults
2077set ::host 127.0.0.1
2078set ::port 6379
2079set ::stress 0
2080set ::flush 0
2081set ::first 0
2082set ::last 1000000
2083
2084# Parse arguments
2085for {set j 0} {$j < [llength $argv]} {incr j} {
2086 set opt [lindex $argv $j]
2087 set arg [lindex $argv [expr $j+1]]
2088 set lastarg [expr {$arg eq {}}]
2089 if {$opt eq {-h} && !$lastarg} {
2090 set ::host $arg
2091 incr j
2092 } elseif {$opt eq {-p} && !$lastarg} {
2093 set ::port $arg
2094 incr j
2095 } elseif {$opt eq {-stress}} {
2096 set ::stress 1
2097 } elseif {$opt eq {--flush}} {
2098 set ::flush 1
2099 } elseif {$opt eq {--first} && !$lastarg} {
2100 set ::first $arg
2101 incr j
2102 } elseif {$opt eq {--last} && !$lastarg} {
2103 set ::last $arg
2104 incr j
2105 } else {
7c49733c 2106 puts "Wrong argument: $opt"
fc77604c 2107 exit 1
2108 }
2109}
2110
eea4baf7 2111# Before to run the test check if DB 9 and DB 10 are empty
2112set r [redis]
fc77604c 2113
2114if {$::flush} {
2115 $r flushall
2116}
2117
eea4baf7 2118$r select 9
2119set db9size [$r dbsize]
2120$r select 10
2121set db10size [$r dbsize]
2122if {$db9size != 0 || $db10size != 0} {
2123 puts "Can't run the tests against DB 9 and 10: DBs are not empty."
2124 exit 1
2125}
2126$r close
2127unset r
2128unset db9size
2129unset db10size
2130
fc77604c 2131if {$::stress} {
5a6948fb 2132 stress
ed9b544e 2133} else {
fc77604c 2134 main $::host $::port
ed9b544e 2135}