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 proc create_default_zset
{} {
203 create_zset zset
{-inf a
1 b
2 c
3 d
4 e
5 f
+inf g
}
206 test
"ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
210 assert_equal
{a b c
} [r zrangebyscore zset
-inf 2]
211 assert_equal
{b c d
} [r zrangebyscore zset
0 3]
212 assert_equal
{d e f
} [r zrangebyscore zset
3 6]
213 assert_equal
{e f g
} [r zrangebyscore zset
4 +inf
]
214 assert_equal
{c b a
} [r zrevrangebyscore zset
2 -inf]
215 assert_equal
{d c b
} [r zrevrangebyscore zset
3 0]
216 assert_equal
{f e d
} [r zrevrangebyscore zset
6 3]
217 assert_equal
{g f e
} [r zrevrangebyscore zset
+inf
4]
218 assert_equal
3 [r zcount zset
0 3]
221 assert_equal
{b
} [r zrangebyscore zset
(-inf (2]
222 assert_equal
{b c
} [r zrangebyscore zset
(0 (3]
223 assert_equal
{e f
} [r zrangebyscore zset
(3 (6]
224 assert_equal
{f
} [r zrangebyscore zset
(4 (+inf
]
225 assert_equal
{b
} [r zrevrangebyscore zset
(2 (-inf]
226 assert_equal
{c b
} [r zrevrangebyscore zset
(3 (0]
227 assert_equal
{f e
} [r zrevrangebyscore zset
(6 (3]
228 assert_equal
{f
} [r zrevrangebyscore zset
(+inf
(4]
229 assert_equal
2 [r zcount zset
(0 (3]
236 assert_equal
{} [r zrangebyscore zset
4 2]
237 assert_equal
{} [r zrangebyscore zset
6 +inf
]
238 assert_equal
{} [r zrangebyscore zset
-inf -6]
239 assert_equal
{} [r zrevrangebyscore zset
+inf
6]
240 assert_equal
{} [r zrevrangebyscore zset
-6 -inf]
243 assert_equal
{} [r zrangebyscore zset
(4 (2]
244 assert_equal
{} [r zrangebyscore zset
2 (2]
245 assert_equal
{} [r zrangebyscore zset
(2 2]
246 assert_equal
{} [r zrangebyscore zset
(6 (+inf
]
247 assert_equal
{} [r zrangebyscore zset
(-inf (-6]
248 assert_equal
{} [r zrevrangebyscore zset
(+inf
(6]
249 assert_equal
{} [r zrevrangebyscore zset
(-6 (-inf]
252 test
"ZRANGEBYSCORE with WITHSCORES" {
254 assert_equal
{b
1 c
2 d
3} [r zrangebyscore zset
0 3 withscores
]
255 assert_equal
{d
3 c
2 b
1} [r zrevrangebyscore zset
3 0 withscores
]
258 test
"ZRANGEBYSCORE with LIMIT" {
260 assert_equal
{b c
} [r zrangebyscore zset
0 10 LIMIT
0 2]
261 assert_equal
{d e f
} [r zrangebyscore zset
0 10 LIMIT
2 3]
262 assert_equal
{d e f
} [r zrangebyscore zset
0 10 LIMIT
2 10]
263 assert_equal
{} [r zrangebyscore zset
0 10 LIMIT
20 10]
264 assert_equal
{f e
} [r zrevrangebyscore zset
10 0 LIMIT
0 2]
265 assert_equal
{d c b
} [r zrevrangebyscore zset
10 0 LIMIT
2 3]
266 assert_equal
{d c b
} [r zrevrangebyscore zset
10 0 LIMIT
2 10]
267 assert_equal
{} [r zrevrangebyscore zset
10 0 LIMIT
20 10]
270 test
"ZRANGEBYSCORE with LIMIT and WITHSCORES" {
272 assert_equal
{e
4 f
5} [r zrangebyscore zset
2 5 LIMIT
2 3 WITHSCORES
]
273 assert_equal
{d
3 c
2} [r zrevrangebyscore zset
5 2 LIMIT
2 3 WITHSCORES
]
276 test
"ZRANGEBYSCORE with non-value min or max" {
277 assert_error
"*not a double*" {r zrangebyscore fooz str
1}
278 assert_error
"*not a double*" {r zrangebyscore fooz
1 str
}
279 assert_error
"*not a double*" {r zrangebyscore fooz
1 NaN
}
283 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
286 for {set i
0} {$i < 1000} {incr i
} {
287 r zadd zset
[expr rand
()] $i
289 for {set i
0} {$i < 100} {incr i
} {
290 set min
[expr rand
()]
291 set max
[expr rand
()]
297 set low
[r zrangebyscore zset
-inf $min]
298 set ok
[r zrangebyscore zset
$min $max]
299 set high
[r zrangebyscore zset
$max +inf
]
300 set lowx
[r zrangebyscore zset
-inf ($min]
301 set okx
[r zrangebyscore zset
($min ($max]
302 set highx
[r zrangebyscore zset
($max +inf
]
304 if {[r zcount zset
-inf $min] != [llength $low]} {
305 append err
"Error, len does not match zcount\n"
307 if {[r zcount zset
$min $max] != [llength $ok]} {
308 append err
"Error, len does not match zcount\n"
310 if {[r zcount zset
$max +inf
] != [llength $high]} {
311 append err
"Error, len does not match zcount\n"
313 if {[r zcount zset
-inf ($min] != [llength $lowx]} {
314 append err
"Error, len does not match zcount\n"
316 if {[r zcount zset
($min ($max] != [llength $okx]} {
317 append err
"Error, len does not match zcount\n"
319 if {[r zcount zset
($max +inf
] != [llength $highx]} {
320 append err
"Error, len does not match zcount\n"
324 set score
[r zscore zset
$x]
326 append err
"Error, score for $x is $score > $min\n"
330 set score
[r zscore zset
$x]
331 if {$score >= $min} {
332 append err
"Error, score for $x is $score >= $min\n"
336 set score
[r zscore zset
$x]
337 if {$score < $min ||
$score > $max} {
338 append err
"Error, score for $x is $score outside $min-$max range\n"
342 set score
[r zscore zset
$x]
343 if {$score <= $min ||
$score >= $max} {
344 append err
"Error, score for $x is $score outside $min-$max open range\n"
348 set score
[r zscore zset
$x]
350 append err
"Error, score for $x is $score < $max\n"
354 set score
[r zscore zset
$x]
355 if {$score <= $max} {
356 append err
"Error, score for $x is $score <= $max\n"
364 test
"ZREMRANGEBYSCORE basics" {
365 proc remrangebyscore
{min max
} {
366 create_zset zset
{1 a
2 b
3 c
4 d
5 e
}
367 r zremrangebyscore zset
$min $max
371 assert_equal
3 [remrangebyscore
2 4]
372 assert_equal
{a e
} [r zrange zset
0 -1]
375 assert_equal
1 [remrangebyscore
-10 1]
376 assert_equal
{b c d e
} [r zrange zset
0 -1]
379 assert_equal
1 [remrangebyscore
5 10]
380 assert_equal
{a b c d
} [r zrange zset
0 -1]
383 assert_equal
0 [remrangebyscore
4 2]
384 assert_equal
{a b c d e
} [r zrange zset
0 -1]
387 assert_equal
3 [remrangebyscore
-inf 3]
388 assert_equal
{d e
} [r zrange zset
0 -1]
391 assert_equal
3 [remrangebyscore
3 +inf
]
392 assert_equal
{a b
} [r zrange zset
0 -1]
395 assert_equal
5 [remrangebyscore
-inf +inf
]
396 assert_equal
{} [r zrange zset
0 -1]
399 assert_equal
4 [remrangebyscore
(1 5]
400 assert_equal
{a
} [r zrange zset
0 -1]
401 assert_equal
3 [remrangebyscore
(2 5]
402 assert_equal
{a b
} [r zrange zset
0 -1]
405 assert_equal
4 [remrangebyscore
1 (5]
406 assert_equal
{e
} [r zrange zset
0 -1]
407 assert_equal
3 [remrangebyscore
1 (4]
408 assert_equal
{d e
} [r zrange zset
0 -1]
410 # exclusive min and max
411 assert_equal
3 [remrangebyscore
(1 (5]
412 assert_equal
{a e
} [r zrange zset
0 -1]
415 test
"ZREMRANGEBYSCORE with non-value min or max" {
416 assert_error
"*not a double*" {r zremrangebyscore fooz str
1}
417 assert_error
"*not a double*" {r zremrangebyscore fooz
1 str
}
418 assert_error
"*not a double*" {r zremrangebyscore fooz
1 NaN
}
421 test
"ZREMRANGEBYRANK basics" {
422 proc remrangebyrank
{min max
} {
423 create_zset zset
{1 a
2 b
3 c
4 d
5 e
}
424 r zremrangebyrank zset
$min $max
428 assert_equal
3 [remrangebyrank
1 3]
429 assert_equal
{a e
} [r zrange zset
0 -1]
432 assert_equal
1 [remrangebyrank
-10 0]
433 assert_equal
{b c d e
} [r zrange zset
0 -1]
436 assert_equal
0 [remrangebyrank
10 -1]
437 assert_equal
{a b c d e
} [r zrange zset
0 -1]
440 assert_equal
0 [remrangebyrank
0 -10]
441 assert_equal
{a b c d e
} [r zrange zset
0 -1]
444 assert_equal
5 [remrangebyrank
0 10]
445 assert_equal
{} [r zrange zset
0 -1]
448 test
{ZUNIONSTORE against non-existing key doesn't
set destination
} {
450 list [r zunionstore dst_key
1 zseta
] [r exists dst_key
]
453 test
{ZUNIONSTORE basics
} {
454 r del zseta zsetb zsetc
461 list [r zunionstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
462 } {4 {a
1 b
3 d
3 c
5}}
464 test
{ZUNIONSTORE with weights
} {
465 list [r zunionstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
466 } {4 {a
2 b
7 d
9 c
12}}
468 test
{ZUNIONSTORE with a regular
set and weights
} {
473 list [r zunionstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
474 } {4 {a
2 b
5 c
8 d
9}}
476 test
{ZUNIONSTORE with AGGREGATE MIN
} {
477 list [r zunionstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
478 } {4 {a
1 b
1 c
2 d
3}}
480 test
{ZUNIONSTORE with AGGREGATE MAX
} {
481 list [r zunionstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
482 } {4 {a
1 b
2 c
3 d
3}}
484 test
{ZINTERSTORE basics
} {
485 list [r zinterstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
488 test
{ZINTERSTORE with weights
} {
489 list [r zinterstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
492 test
{ZINTERSTORE with a regular
set and weights
} {
497 list [r zinterstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
500 test
{ZINTERSTORE with AGGREGATE MIN
} {
501 list [r zinterstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
504 test
{ZINTERSTORE with AGGREGATE MAX
} {
505 list [r zinterstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
508 foreach cmd
{ZUNIONSTORE ZINTERSTORE
} {
509 test
"$cmd with +inf/-inf scores" {
510 r del zsetinf1 zsetinf2
512 r zadd zsetinf1
+inf key
513 r zadd zsetinf2
+inf key
514 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
515 assert_equal inf
[r zscore zsetinf3 key
]
517 r zadd zsetinf1
-inf key
518 r zadd zsetinf2
+inf key
519 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
520 assert_equal
0 [r zscore zsetinf3 key
]
522 r zadd zsetinf1
+inf key
523 r zadd zsetinf2
-inf key
524 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
525 assert_equal
0 [r zscore zsetinf3 key
]
527 r zadd zsetinf1
-inf key
528 r zadd zsetinf2
-inf key
529 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
530 assert_equal
-inf [r zscore zsetinf3 key
]
533 test
"$cmd with NaN weights" {
534 r del zsetinf1 zsetinf2
536 r zadd zsetinf1
1.0 key
537 r zadd zsetinf2
1.0 key
538 assert_error
"*weight value is not a double*" {
539 r
$cmd zsetinf3
2 zsetinf1 zsetinf2 weights nan nan
545 test
{ZSETs skiplist implementation backlink consistency test
} {
548 for {set j
0} {$j < $elements} {incr j
} {
549 r zadd myzset
[expr rand
()] "Element-$j"
550 r zrem myzset
"Element-[expr int(rand()*$elements)]"
552 set l1
[r zrange myzset
0 -1]
553 set l2
[r zrevrange myzset
0 -1]
554 for {set j
0} {$j < [llength $l1]} {incr j
} {
555 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
562 test
{ZSETs ZRANK augmented skip
list stress testing
} {
565 for {set k
0} {$k < 10000} {incr k
} {
566 set i
[expr {$k%1000}]
567 if {[expr rand
()] < .2} {
570 set score
[expr rand
()]
571 r zadd myzset
$score $i
573 set card
[r zcard myzset
]
575 set index
[randomInt
$card]
576 set ele
[lindex [r zrange myzset
$index $index] 0]
577 set rank
[r zrank myzset
$ele]
578 if {$rank != $index} {
579 set err
"$ele RANK is wrong! ($rank != $index)"
588 test
{ZSET element can't be
set to NaN with ZADD
} {
589 assert_error
"*not a double*" {r zadd myzset nan abc
}
592 test
{ZSET element can't be
set to NaN with ZINCRBY
} {
593 assert_error
"*not a double*" {r zadd myzset nan abc
}
596 test
{ZINCRBY calls leading to NaN result in
error} {
597 r zincrby myzset
+inf abc
598 assert_error
"*NaN*" {r zincrby myzset
-inf abc
}