1 start_server
{tags
{"zset"}} {
2 proc create_zset
{key items
} {
4 foreach {score
entry} $items {
5 r zadd
$key $score $entry
9 test
{ZSET basic ZADD and score
update} {
13 set aux1
[r zrange ztmp
0 -1]
15 set aux2
[r zrange ztmp
0 -1]
23 test
{ZCARD non existing key
} {
27 test
"ZRANGE basics" {
34 assert_equal
{a b c d
} [r zrange ztmp
0 -1]
35 assert_equal
{a b c
} [r zrange ztmp
0 -2]
36 assert_equal
{b c d
} [r zrange ztmp
1 -1]
37 assert_equal
{b c
} [r zrange ztmp
1 -2]
38 assert_equal
{c d
} [r zrange ztmp
-2 -1]
39 assert_equal
{c
} [r zrange ztmp
-2 -2]
41 # out of range start index
42 assert_equal
{a b c
} [r zrange ztmp
-5 2]
43 assert_equal
{a b
} [r zrange ztmp
-5 1]
44 assert_equal
{} [r zrange ztmp
5 -1]
45 assert_equal
{} [r zrange ztmp
5 -2]
47 # out of range end index
48 assert_equal
{a b c d
} [r zrange ztmp
0 5]
49 assert_equal
{b c d
} [r zrange ztmp
1 5]
50 assert_equal
{} [r zrange ztmp
0 -5]
51 assert_equal
{} [r zrange ztmp
1 -5]
54 assert_equal
{a
1 b
2 c
3 d
4} [r zrange ztmp
0 -1 withscores
]
57 test
"ZREVRANGE basics" {
64 assert_equal
{d c b a
} [r zrevrange ztmp
0 -1]
65 assert_equal
{d c b
} [r zrevrange ztmp
0 -2]
66 assert_equal
{c b a
} [r zrevrange ztmp
1 -1]
67 assert_equal
{c b
} [r zrevrange ztmp
1 -2]
68 assert_equal
{b a
} [r zrevrange ztmp
-2 -1]
69 assert_equal
{b
} [r zrevrange ztmp
-2 -2]
71 # out of range start index
72 assert_equal
{d c b
} [r zrevrange ztmp
-5 2]
73 assert_equal
{d c
} [r zrevrange ztmp
-5 1]
74 assert_equal
{} [r zrevrange ztmp
5 -1]
75 assert_equal
{} [r zrevrange ztmp
5 -2]
77 # out of range end index
78 assert_equal
{d c b a
} [r zrevrange ztmp
0 5]
79 assert_equal
{c b a
} [r zrevrange ztmp
1 5]
80 assert_equal
{} [r zrevrange ztmp
0 -5]
81 assert_equal
{} [r zrevrange ztmp
1 -5]
84 assert_equal
{d
4 c
3 b
2 a
1} [r zrevrange ztmp
0 -1 withscores
]
91 list [r zrank zranktmp x
] [r zrank zranktmp y
] [r zrank zranktmp z
]
94 test
{ZREVRANK basics
} {
95 list [r zrevrank zranktmp x
] [r zrevrank zranktmp y
] [r zrevrank zranktmp z
]
98 test
{ZRANK
- after deletion
} {
100 list [r zrank zranktmp x
] [r zrank zranktmp z
]
106 for {set i
0} {$i < 1000} {incr i
} {
107 set score
[expr rand
()]
109 r zadd zscoretest
$score $i
111 for {set i
0} {$i < 1000} {incr i
} {
112 if {[r zscore zscoretest
$i] != [lindex $aux $i]} {
113 set err
"Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
120 test
{ZSCORE
after a DEBUG RELOAD
} {
124 for {set i
0} {$i < 1000} {incr i
} {
125 set score
[expr rand
()]
127 r zadd zscoretest
$score $i
130 for {set i
0} {$i < 1000} {incr i
} {
131 if {[r zscore zscoretest
$i] != [lindex $aux $i]} {
132 set err
"Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
139 test
{ZSETs stress tester
- sorting is working well?
} {
141 for {set test
0} {$test < 2} {incr test
} {
142 unset -nocomplain auxarray
143 array set auxarray
{}
146 for {set i
0} {$i < 1000} {incr i
} {
148 set score
[expr rand
()]
150 set score
[expr int
(rand
()*10)]
152 set auxarray
($i) $score
153 r zadd myzset
$score $i
155 if {[expr rand
()] < .2} {
156 set j
[expr int
(rand
()*1000)]
158 set score
[expr rand
()]
160 set score
[expr int
(rand
()*10)]
162 set auxarray
($j) $score
163 r zadd myzset
$score $j
166 foreach {item score
} [array get auxarray
] {
167 lappend auxlist
[list $score $item]
169 set sorted
[lsort -command zlistAlikeSort
$auxlist]
172 lappend auxlist
[lindex $x 1]
174 set fromredis
[r zrange myzset
0 -1]
176 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
177 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
185 test
{ZINCRBY
- can create a new sorted
set} {
188 list [r zrange zset
0 -1] [r zscore zset foo
]
191 test
{ZINCRBY
- increment and decrement
} {
194 set v1
[r zrange zset
0 -1]
195 r zincrby zset
10 bar
196 r zincrby zset
-5 foo
197 r zincrby zset
-5 bar
198 set v2
[r zrange zset
0 -1]
199 list $v1 $v2 [r zscore zset foo
] [r zscore zset bar
]
200 } {{bar foo
} {foo bar
} -2 6}
202 test
{ZRANGEBYSCORE and ZCOUNT basics
} {
209 list [r zrangebyscore zset
2 4] [r zrangebyscore zset
(2 (4] \
210 [r zcount zset
2 4] [r zcount zset
(2 (4]
213 test
{ZRANGEBYSCORE withscores
} {
220 r zrangebyscore zset
2 4 withscores
224 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
227 for {set i
0} {$i < 1000} {incr i
} {
228 r zadd zset
[expr rand
()] $i
230 for {set i
0} {$i < 100} {incr i
} {
231 set min
[expr rand
()]
232 set max
[expr rand
()]
238 set low
[r zrangebyscore zset
-inf $min]
239 set ok
[r zrangebyscore zset
$min $max]
240 set high
[r zrangebyscore zset
$max +inf
]
241 set lowx
[r zrangebyscore zset
-inf ($min]
242 set okx
[r zrangebyscore zset
($min ($max]
243 set highx
[r zrangebyscore zset
($max +inf
]
245 if {[r zcount zset
-inf $min] != [llength $low]} {
246 append err
"Error, len does not match zcount\n"
248 if {[r zcount zset
$min $max] != [llength $ok]} {
249 append err
"Error, len does not match zcount\n"
251 if {[r zcount zset
$max +inf
] != [llength $high]} {
252 append err
"Error, len does not match zcount\n"
254 if {[r zcount zset
-inf ($min] != [llength $lowx]} {
255 append err
"Error, len does not match zcount\n"
257 if {[r zcount zset
($min ($max] != [llength $okx]} {
258 append err
"Error, len does not match zcount\n"
260 if {[r zcount zset
($max +inf
] != [llength $highx]} {
261 append err
"Error, len does not match zcount\n"
265 set score
[r zscore zset
$x]
267 append err
"Error, score for $x is $score > $min\n"
271 set score
[r zscore zset
$x]
272 if {$score >= $min} {
273 append err
"Error, score for $x is $score >= $min\n"
277 set score
[r zscore zset
$x]
278 if {$score < $min ||
$score > $max} {
279 append err
"Error, score for $x is $score outside $min-$max range\n"
283 set score
[r zscore zset
$x]
284 if {$score <= $min ||
$score >= $max} {
285 append err
"Error, score for $x is $score outside $min-$max open range\n"
289 set score
[r zscore zset
$x]
291 append err
"Error, score for $x is $score < $max\n"
295 set score
[r zscore zset
$x]
296 if {$score <= $max} {
297 append err
"Error, score for $x is $score <= $max\n"
305 test
{ZRANGEBYSCORE with LIMIT
} {
313 [r zrangebyscore zset
0 10 LIMIT
0 2] \
314 [r zrangebyscore zset
0 10 LIMIT
2 3] \
315 [r zrangebyscore zset
0 10 LIMIT
2 10] \
316 [r zrangebyscore zset
0 10 LIMIT
20 10]
317 } {{a b
} {c d e
} {c d e
} {}}
319 test
{ZRANGEBYSCORE with LIMIT and withscores
} {
326 r zrangebyscore zset
20 50 LIMIT
2 3 withscores
329 test
{ZREMRANGEBYSCORE basics
} {
336 list [r zremrangebyscore zset
2 4] [r zrange zset
0 -1]
339 test
{ZREMRANGEBYSCORE from
-inf to
+inf
} {
346 list [r zremrangebyscore zset
-inf +inf
] [r zrange zset
0 -1]
349 test
"ZREMRANGEBYRANK basics" {
350 proc remrangebyrank
{min max
} {
351 create_zset zset
{1 a
2 b
3 c
4 d
5 e
}
352 r zremrangebyrank zset
$min $max
356 assert_equal
3 [remrangebyrank
1 3]
357 assert_equal
{a e
} [r zrange zset
0 -1]
360 assert_equal
1 [remrangebyrank
-10 0]
361 assert_equal
{b c d e
} [r zrange zset
0 -1]
364 assert_equal
0 [remrangebyrank
10 -1]
365 assert_equal
{a b c d e
} [r zrange zset
0 -1]
368 assert_equal
0 [remrangebyrank
0 -10]
369 assert_equal
{a b c d e
} [r zrange zset
0 -1]
372 assert_equal
5 [remrangebyrank
0 10]
373 assert_equal
{} [r zrange zset
0 -1]
376 test
{ZUNIONSTORE against non-existing key doesn't
set destination
} {
378 list [r zunionstore dst_key
1 zseta
] [r exists dst_key
]
381 test
{ZUNIONSTORE basics
} {
382 r del zseta zsetb zsetc
389 list [r zunionstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
390 } {4 {a
1 b
3 d
3 c
5}}
392 test
{ZUNIONSTORE with weights
} {
393 list [r zunionstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
394 } {4 {a
2 b
7 d
9 c
12}}
396 test
{ZUNIONSTORE with a regular
set and weights
} {
401 list [r zunionstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
402 } {4 {a
2 b
5 c
8 d
9}}
404 test
{ZUNIONSTORE with AGGREGATE MIN
} {
405 list [r zunionstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
406 } {4 {a
1 b
1 c
2 d
3}}
408 test
{ZUNIONSTORE with AGGREGATE MAX
} {
409 list [r zunionstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
410 } {4 {a
1 b
2 c
3 d
3}}
412 test
{ZINTERSTORE basics
} {
413 list [r zinterstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
416 test
{ZINTERSTORE with weights
} {
417 list [r zinterstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
420 test
{ZINTERSTORE with a regular
set and weights
} {
425 list [r zinterstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
428 test
{ZINTERSTORE with AGGREGATE MIN
} {
429 list [r zinterstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
432 test
{ZINTERSTORE with AGGREGATE MAX
} {
433 list [r zinterstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
436 foreach cmd
{ZUNIONSTORE ZINTERSTORE
} {
437 test
"$cmd with +inf/-inf scores" {
438 r del zsetinf1 zsetinf2
440 r zadd zsetinf1
+inf key
441 r zadd zsetinf2
+inf key
442 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
443 assert_equal inf
[r zscore zsetinf3 key
]
445 r zadd zsetinf1
-inf key
446 r zadd zsetinf2
+inf key
447 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
448 assert_equal
0 [r zscore zsetinf3 key
]
450 r zadd zsetinf1
+inf key
451 r zadd zsetinf2
-inf key
452 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
453 assert_equal
0 [r zscore zsetinf3 key
]
455 r zadd zsetinf1
-inf key
456 r zadd zsetinf2
-inf key
457 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
458 assert_equal
-inf [r zscore zsetinf3 key
]
461 test
"$cmd with NaN weights" {
462 r del zsetinf1 zsetinf2
464 r zadd zsetinf1
1.0 key
465 r zadd zsetinf2
1.0 key
466 assert_error
"*weight value is not a double*" {
467 r
$cmd zsetinf3
2 zsetinf1 zsetinf2 weights nan nan
473 test
{ZSETs skiplist implementation backlink consistency test
} {
476 for {set j
0} {$j < $elements} {incr j
} {
477 r zadd myzset
[expr rand
()] "Element-$j"
478 r zrem myzset
"Element-[expr int(rand()*$elements)]"
480 set l1
[r zrange myzset
0 -1]
481 set l2
[r zrevrange myzset
0 -1]
482 for {set j
0} {$j < [llength $l1]} {incr j
} {
483 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
490 test
{ZSETs ZRANK augmented skip
list stress testing
} {
493 for {set k
0} {$k < 10000} {incr k
} {
494 set i
[expr {$k%1000}]
495 if {[expr rand
()] < .2} {
498 set score
[expr rand
()]
499 r zadd myzset
$score $i
501 set card
[r zcard myzset
]
503 set index
[randomInt
$card]
504 set ele
[lindex [r zrange myzset
$index $index] 0]
505 set rank
[r zrank myzset
$ele]
506 if {$rank != $index} {
507 set err
"$ele RANK is wrong! ($rank != $index)"
516 test
{ZSET element can't be
set to NaN with ZADD
} {
517 assert_error
"*not a double*" {r zadd myzset nan abc
}
520 test
{ZSET element can't be
set to NaN with ZINCRBY
} {
521 assert_error
"*not a double*" {r zadd myzset nan abc
}
524 test
{ZINCRBY calls leading to NaN result in
error} {
525 r zincrby myzset
+inf abc
526 assert_error
"*NaN*" {r zincrby myzset
-inf abc
}