1 start_server
{tags
{"zset"}} {
2 proc create_zset
{key items
} {
4 foreach {score
entry} $items {
5 r zadd
$key $score $entry
9 proc basics
{encoding} {
10 if {$encoding == "ziplist"} {
11 r config
set zset-max-ziplist-entries
128
12 r config
set zset-max-ziplist-value
64
13 } elseif
{$encoding == "skiplist"} {
14 r config
set zset-max-ziplist-entries
0
15 r config
set zset-max-ziplist-value
0
17 puts "Unknown sorted set encoding"
21 test
"Check encoding - $encoding" {
24 assert_encoding
$encoding ztmp
27 test
"ZSET basic ZADD and score update - $encoding" {
32 assert_equal
{x y z
} [r zrange ztmp
0 -1]
35 assert_equal
{y x z
} [r zrange ztmp
0 -1]
38 test
"ZSET element can't be set to NaN with ZADD - $encoding" {
39 assert_error
"*not a double*" {r zadd myzset nan abc
}
42 test
"ZSET element can't be set to NaN with ZINCRBY" {
43 assert_error
"*not a double*" {r zadd myzset nan abc
}
46 test
"ZINCRBY calls leading to NaN result in error" {
47 r zincrby myzset
+inf abc
48 assert_error
"*NaN*" {r zincrby myzset
-inf abc
}
51 test
"ZCARD basics - $encoding" {
52 assert_equal
3 [r zcard ztmp
]
53 assert_equal
0 [r zcard zdoesntexist
]
56 test
"ZREM removes key after last element is removed" {
61 assert_equal
1 [r exists ztmp
]
62 assert_equal
0 [r zrem ztmp z
]
63 assert_equal
1 [r zrem ztmp y
]
64 assert_equal
1 [r zrem ztmp x
]
65 assert_equal
0 [r exists ztmp
]
68 test
"ZRANGE basics - $encoding" {
75 assert_equal
{a b c d
} [r zrange ztmp
0 -1]
76 assert_equal
{a b c
} [r zrange ztmp
0 -2]
77 assert_equal
{b c d
} [r zrange ztmp
1 -1]
78 assert_equal
{b c
} [r zrange ztmp
1 -2]
79 assert_equal
{c d
} [r zrange ztmp
-2 -1]
80 assert_equal
{c
} [r zrange ztmp
-2 -2]
82 # out of range start index
83 assert_equal
{a b c
} [r zrange ztmp
-5 2]
84 assert_equal
{a b
} [r zrange ztmp
-5 1]
85 assert_equal
{} [r zrange ztmp
5 -1]
86 assert_equal
{} [r zrange ztmp
5 -2]
88 # out of range end index
89 assert_equal
{a b c d
} [r zrange ztmp
0 5]
90 assert_equal
{b c d
} [r zrange ztmp
1 5]
91 assert_equal
{} [r zrange ztmp
0 -5]
92 assert_equal
{} [r zrange ztmp
1 -5]
95 assert_equal
{a
1 b
2 c
3 d
4} [r zrange ztmp
0 -1 withscores
]
98 test
"ZREVRANGE basics - $encoding" {
105 assert_equal
{d c b a
} [r zrevrange ztmp
0 -1]
106 assert_equal
{d c b
} [r zrevrange ztmp
0 -2]
107 assert_equal
{c b a
} [r zrevrange ztmp
1 -1]
108 assert_equal
{c b
} [r zrevrange ztmp
1 -2]
109 assert_equal
{b a
} [r zrevrange ztmp
-2 -1]
110 assert_equal
{b
} [r zrevrange ztmp
-2 -2]
112 # out of range start index
113 assert_equal
{d c b
} [r zrevrange ztmp
-5 2]
114 assert_equal
{d c
} [r zrevrange ztmp
-5 1]
115 assert_equal
{} [r zrevrange ztmp
5 -1]
116 assert_equal
{} [r zrevrange ztmp
5 -2]
118 # out of range end index
119 assert_equal
{d c b a
} [r zrevrange ztmp
0 5]
120 assert_equal
{c b a
} [r zrevrange ztmp
1 5]
121 assert_equal
{} [r zrevrange ztmp
0 -5]
122 assert_equal
{} [r zrevrange ztmp
1 -5]
125 assert_equal
{d
4 c
3 b
2 a
1} [r zrevrange ztmp
0 -1 withscores
]
128 test
"ZRANK/ZREVRANK basics - $encoding" {
133 assert_equal
0 [r zrank zranktmp x
]
134 assert_equal
1 [r zrank zranktmp y
]
135 assert_equal
2 [r zrank zranktmp z
]
136 assert_equal
"" [r zrank zranktmp foo
]
137 assert_equal
2 [r zrevrank zranktmp x
]
138 assert_equal
1 [r zrevrank zranktmp y
]
139 assert_equal
0 [r zrevrank zranktmp z
]
140 assert_equal
"" [r zrevrank zranktmp foo
]
143 test
"ZRANK - after deletion - $encoding" {
145 assert_equal
0 [r zrank zranktmp x
]
146 assert_equal
1 [r zrank zranktmp z
]
149 test
"ZINCRBY - can create a new sorted set - $encoding" {
152 assert_equal
{foo
} [r zrange zset
0 -1]
153 assert_equal
1 [r zscore zset foo
]
156 test
"ZINCRBY - increment and decrement - $encoding" {
159 assert_equal
{bar foo
} [r zrange zset
0 -1]
161 r zincrby zset
10 bar
162 r zincrby zset
-5 foo
163 r zincrby zset
-5 bar
164 assert_equal
{foo bar
} [r zrange zset
0 -1]
166 assert_equal
-2 [r zscore zset foo
]
167 assert_equal
6 [r zscore zset bar
]
170 proc create_default_zset
{} {
171 create_zset zset
{-inf a
1 b
2 c
3 d
4 e
5 f
+inf g
}
174 test
"ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
178 assert_equal
{a b c
} [r zrangebyscore zset
-inf 2]
179 assert_equal
{b c d
} [r zrangebyscore zset
0 3]
180 assert_equal
{d e f
} [r zrangebyscore zset
3 6]
181 assert_equal
{e f g
} [r zrangebyscore zset
4 +inf
]
182 assert_equal
{c b a
} [r zrevrangebyscore zset
2 -inf]
183 assert_equal
{d c b
} [r zrevrangebyscore zset
3 0]
184 assert_equal
{f e d
} [r zrevrangebyscore zset
6 3]
185 assert_equal
{g f e
} [r zrevrangebyscore zset
+inf
4]
186 assert_equal
3 [r zcount zset
0 3]
189 assert_equal
{b
} [r zrangebyscore zset
(-inf (2]
190 assert_equal
{b c
} [r zrangebyscore zset
(0 (3]
191 assert_equal
{e f
} [r zrangebyscore zset
(3 (6]
192 assert_equal
{f
} [r zrangebyscore zset
(4 (+inf
]
193 assert_equal
{b
} [r zrevrangebyscore zset
(2 (-inf]
194 assert_equal
{c b
} [r zrevrangebyscore zset
(3 (0]
195 assert_equal
{f e
} [r zrevrangebyscore zset
(6 (3]
196 assert_equal
{f
} [r zrevrangebyscore zset
(+inf
(4]
197 assert_equal
2 [r zcount zset
(0 (3]
204 assert_equal
{} [r zrangebyscore zset
4 2]
205 assert_equal
{} [r zrangebyscore zset
6 +inf
]
206 assert_equal
{} [r zrangebyscore zset
-inf -6]
207 assert_equal
{} [r zrevrangebyscore zset
+inf
6]
208 assert_equal
{} [r zrevrangebyscore zset
-6 -inf]
211 assert_equal
{} [r zrangebyscore zset
(4 (2]
212 assert_equal
{} [r zrangebyscore zset
2 (2]
213 assert_equal
{} [r zrangebyscore zset
(2 2]
214 assert_equal
{} [r zrangebyscore zset
(6 (+inf
]
215 assert_equal
{} [r zrangebyscore zset
(-inf (-6]
216 assert_equal
{} [r zrevrangebyscore zset
(+inf
(6]
217 assert_equal
{} [r zrevrangebyscore zset
(-6 (-inf]
220 assert_equal
{} [r zrangebyscore zset
2.4 2.6]
221 assert_equal
{} [r zrangebyscore zset
(2.4 2.6]
222 assert_equal
{} [r zrangebyscore zset
2.4 (2.6]
223 assert_equal
{} [r zrangebyscore zset
(2.4 (2.6]
226 test
"ZRANGEBYSCORE with WITHSCORES" {
228 assert_equal
{b
1 c
2 d
3} [r zrangebyscore zset
0 3 withscores
]
229 assert_equal
{d
3 c
2 b
1} [r zrevrangebyscore zset
3 0 withscores
]
232 test
"ZRANGEBYSCORE with LIMIT" {
234 assert_equal
{b c
} [r zrangebyscore zset
0 10 LIMIT
0 2]
235 assert_equal
{d e f
} [r zrangebyscore zset
0 10 LIMIT
2 3]
236 assert_equal
{d e f
} [r zrangebyscore zset
0 10 LIMIT
2 10]
237 assert_equal
{} [r zrangebyscore zset
0 10 LIMIT
20 10]
238 assert_equal
{f e
} [r zrevrangebyscore zset
10 0 LIMIT
0 2]
239 assert_equal
{d c b
} [r zrevrangebyscore zset
10 0 LIMIT
2 3]
240 assert_equal
{d c b
} [r zrevrangebyscore zset
10 0 LIMIT
2 10]
241 assert_equal
{} [r zrevrangebyscore zset
10 0 LIMIT
20 10]
244 test
"ZRANGEBYSCORE with LIMIT and WITHSCORES" {
246 assert_equal
{e
4 f
5} [r zrangebyscore zset
2 5 LIMIT
2 3 WITHSCORES
]
247 assert_equal
{d
3 c
2} [r zrevrangebyscore zset
5 2 LIMIT
2 3 WITHSCORES
]
250 test
"ZRANGEBYSCORE with non-value min or max" {
251 assert_error
"*not a double*" {r zrangebyscore fooz str
1}
252 assert_error
"*not a double*" {r zrangebyscore fooz
1 str
}
253 assert_error
"*not a double*" {r zrangebyscore fooz
1 NaN
}
256 test
"ZREMRANGEBYSCORE basics" {
257 proc remrangebyscore
{min max
} {
258 create_zset zset
{1 a
2 b
3 c
4 d
5 e
}
259 assert_equal
1 [r exists zset
]
260 r zremrangebyscore zset
$min $max
264 assert_equal
3 [remrangebyscore
2 4]
265 assert_equal
{a e
} [r zrange zset
0 -1]
268 assert_equal
1 [remrangebyscore
-10 1]
269 assert_equal
{b c d e
} [r zrange zset
0 -1]
272 assert_equal
1 [remrangebyscore
5 10]
273 assert_equal
{a b c d
} [r zrange zset
0 -1]
276 assert_equal
0 [remrangebyscore
4 2]
277 assert_equal
{a b c d e
} [r zrange zset
0 -1]
280 assert_equal
3 [remrangebyscore
-inf 3]
281 assert_equal
{d e
} [r zrange zset
0 -1]
284 assert_equal
3 [remrangebyscore
3 +inf
]
285 assert_equal
{a b
} [r zrange zset
0 -1]
288 assert_equal
5 [remrangebyscore
-inf +inf
]
289 assert_equal
{} [r zrange zset
0 -1]
292 assert_equal
4 [remrangebyscore
(1 5]
293 assert_equal
{a
} [r zrange zset
0 -1]
294 assert_equal
3 [remrangebyscore
(2 5]
295 assert_equal
{a b
} [r zrange zset
0 -1]
298 assert_equal
4 [remrangebyscore
1 (5]
299 assert_equal
{e
} [r zrange zset
0 -1]
300 assert_equal
3 [remrangebyscore
1 (4]
301 assert_equal
{d e
} [r zrange zset
0 -1]
303 # exclusive min and max
304 assert_equal
3 [remrangebyscore
(1 (5]
305 assert_equal
{a e
} [r zrange zset
0 -1]
308 assert_equal
5 [remrangebyscore
1 5]
309 assert_equal
0 [r exists zset
]
312 test
"ZREMRANGEBYSCORE with non-value min or max" {
313 assert_error
"*not a double*" {r zremrangebyscore fooz str
1}
314 assert_error
"*not a double*" {r zremrangebyscore fooz
1 str
}
315 assert_error
"*not a double*" {r zremrangebyscore fooz
1 NaN
}
318 test
"ZREMRANGEBYRANK basics" {
319 proc remrangebyrank
{min max
} {
320 create_zset zset
{1 a
2 b
3 c
4 d
5 e
}
321 assert_equal
1 [r exists zset
]
322 r zremrangebyrank zset
$min $max
326 assert_equal
3 [remrangebyrank
1 3]
327 assert_equal
{a e
} [r zrange zset
0 -1]
330 assert_equal
1 [remrangebyrank
-10 0]
331 assert_equal
{b c d e
} [r zrange zset
0 -1]
334 assert_equal
0 [remrangebyrank
10 -1]
335 assert_equal
{a b c d e
} [r zrange zset
0 -1]
338 assert_equal
0 [remrangebyrank
0 -10]
339 assert_equal
{a b c d e
} [r zrange zset
0 -1]
342 assert_equal
5 [remrangebyrank
0 10]
343 assert_equal
{} [r zrange zset
0 -1]
346 assert_equal
5 [remrangebyrank
0 4]
347 assert_equal
0 [r exists zset
]
350 test
"ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
352 assert_equal
0 [r zunionstore dst_key
1 zseta
]
353 assert_equal
0 [r exists dst_key
]
356 test
"ZUNIONSTORE basics - $encoding" {
357 r del zseta zsetb zsetc
365 assert_equal
4 [r zunionstore zsetc
2 zseta zsetb
]
366 assert_equal
{a
1 b
3 d
3 c
5} [r zrange zsetc
0 -1 withscores
]
369 test
"ZUNIONSTORE with weights - $encoding" {
370 assert_equal
4 [r zunionstore zsetc
2 zseta zsetb weights
2 3]
371 assert_equal
{a
2 b
7 d
9 c
12} [r zrange zsetc
0 -1 withscores
]
374 test
"ZUNIONSTORE with a regular set and weights - $encoding" {
380 assert_equal
4 [r zunionstore zsetc
2 seta zsetb weights
2 3]
381 assert_equal
{a
2 b
5 c
8 d
9} [r zrange zsetc
0 -1 withscores
]
384 test
"ZUNIONSTORE with AGGREGATE MIN - $encoding" {
385 assert_equal
4 [r zunionstore zsetc
2 zseta zsetb aggregate min
]
386 assert_equal
{a
1 b
1 c
2 d
3} [r zrange zsetc
0 -1 withscores
]
389 test
"ZUNIONSTORE with AGGREGATE MAX - $encoding" {
390 assert_equal
4 [r zunionstore zsetc
2 zseta zsetb aggregate max
]
391 assert_equal
{a
1 b
2 c
3 d
3} [r zrange zsetc
0 -1 withscores
]
394 test
"ZINTERSTORE basics - $encoding" {
395 assert_equal
2 [r zinterstore zsetc
2 zseta zsetb
]
396 assert_equal
{b
3 c
5} [r zrange zsetc
0 -1 withscores
]
399 test
"ZINTERSTORE with weights - $encoding" {
400 assert_equal
2 [r zinterstore zsetc
2 zseta zsetb weights
2 3]
401 assert_equal
{b
7 c
12} [r zrange zsetc
0 -1 withscores
]
404 test
"ZINTERSTORE with a regular set and weights - $encoding" {
409 assert_equal
2 [r zinterstore zsetc
2 seta zsetb weights
2 3]
410 assert_equal
{b
5 c
8} [r zrange zsetc
0 -1 withscores
]
413 test
"ZINTERSTORE with AGGREGATE MIN - $encoding" {
414 assert_equal
2 [r zinterstore zsetc
2 zseta zsetb aggregate min
]
415 assert_equal
{b
1 c
2} [r zrange zsetc
0 -1 withscores
]
418 test
"ZINTERSTORE with AGGREGATE MAX - $encoding" {
419 assert_equal
2 [r zinterstore zsetc
2 zseta zsetb aggregate max
]
420 assert_equal
{b
2 c
3} [r zrange zsetc
0 -1 withscores
]
423 foreach cmd
{ZUNIONSTORE ZINTERSTORE
} {
424 test
"$cmd with +inf/-inf scores - $encoding" {
425 r del zsetinf1 zsetinf2
427 r zadd zsetinf1
+inf key
428 r zadd zsetinf2
+inf key
429 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
430 assert_equal inf
[r zscore zsetinf3 key
]
432 r zadd zsetinf1
-inf key
433 r zadd zsetinf2
+inf key
434 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
435 assert_equal
0 [r zscore zsetinf3 key
]
437 r zadd zsetinf1
+inf key
438 r zadd zsetinf2
-inf key
439 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
440 assert_equal
0 [r zscore zsetinf3 key
]
442 r zadd zsetinf1
-inf key
443 r zadd zsetinf2
-inf key
444 r
$cmd zsetinf3
2 zsetinf1 zsetinf2
445 assert_equal
-inf [r zscore zsetinf3 key
]
448 test
"$cmd with NaN weights $encoding" {
449 r del zsetinf1 zsetinf2
451 r zadd zsetinf1
1.0 key
452 r zadd zsetinf2
1.0 key
453 assert_error
"*weight value is not a double*" {
454 r
$cmd zsetinf3
2 zsetinf1 zsetinf2 weights nan nan
463 proc stressers
{encoding} {
464 if {$encoding == "ziplist"} {
465 # Little extra to allow proper fuzzing in the sorting stresser
466 r config
set zset-max-ziplist-entries
256
467 r config
set zset-max-ziplist-value
64
469 } elseif
{$encoding == "skiplist"} {
470 r config
set zset-max-ziplist-entries
0
471 r config
set zset-max-ziplist-value
0
474 puts "Unknown sorted set encoding"
478 test
"ZSCORE - $encoding" {
481 for {set i
0} {$i < $elements} {incr i
} {
482 set score
[expr rand
()]
484 r zadd zscoretest
$score $i
487 assert_encoding
$encoding zscoretest
488 for {set i
0} {$i < $elements} {incr i
} {
489 assert_equal
[lindex $aux $i] [r zscore zscoretest
$i]
493 test
"ZSCORE after a DEBUG RELOAD - $encoding" {
496 for {set i
0} {$i < $elements} {incr i
} {
497 set score
[expr rand
()]
499 r zadd zscoretest
$score $i
503 assert_encoding
$encoding zscoretest
504 for {set i
0} {$i < $elements} {incr i
} {
505 assert_equal
[lindex $aux $i] [r zscore zscoretest
$i]
509 test
"ZSET sorting stresser - $encoding" {
511 for {set test
0} {$test < 2} {incr test
} {
512 unset -nocomplain auxarray
513 array set auxarray
{}
516 for {set i
0} {$i < $elements} {incr i
} {
518 set score
[expr rand
()]
520 set score
[expr int
(rand
()*10)]
522 set auxarray
($i) $score
523 r zadd myzset
$score $i
525 if {[expr rand
()] < .2} {
526 set j
[expr int
(rand
()*1000)]
528 set score
[expr rand
()]
530 set score
[expr int
(rand
()*10)]
532 set auxarray
($j) $score
533 r zadd myzset
$score $j
536 foreach {item score
} [array get auxarray
] {
537 lappend auxlist
[list $score $item]
539 set sorted
[lsort -command zlistAlikeSort
$auxlist]
542 lappend auxlist
[lindex $x 1]
545 assert_encoding
$encoding myzset
546 set fromredis
[r zrange myzset
0 -1]
548 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
549 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
554 assert_equal
0 $delta
557 test
"ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
560 for {set i
0} {$i < $elements} {incr i
} {
561 r zadd zset
[expr rand
()] $i
564 assert_encoding
$encoding zset
565 for {set i
0} {$i < 100} {incr i
} {
566 set min
[expr rand
()]
567 set max
[expr rand
()]
573 set low
[r zrangebyscore zset
-inf $min]
574 set ok
[r zrangebyscore zset
$min $max]
575 set high
[r zrangebyscore zset
$max +inf
]
576 set lowx
[r zrangebyscore zset
-inf ($min]
577 set okx
[r zrangebyscore zset
($min ($max]
578 set highx
[r zrangebyscore zset
($max +inf
]
580 if {[r zcount zset
-inf $min] != [llength $low]} {
581 append err
"Error, len does not match zcount\n"
583 if {[r zcount zset
$min $max] != [llength $ok]} {
584 append err
"Error, len does not match zcount\n"
586 if {[r zcount zset
$max +inf
] != [llength $high]} {
587 append err
"Error, len does not match zcount\n"
589 if {[r zcount zset
-inf ($min] != [llength $lowx]} {
590 append err
"Error, len does not match zcount\n"
592 if {[r zcount zset
($min ($max] != [llength $okx]} {
593 append err
"Error, len does not match zcount\n"
595 if {[r zcount zset
($max +inf
] != [llength $highx]} {
596 append err
"Error, len does not match zcount\n"
600 set score
[r zscore zset
$x]
602 append err
"Error, score for $x is $score > $min\n"
606 set score
[r zscore zset
$x]
607 if {$score >= $min} {
608 append err
"Error, score for $x is $score >= $min\n"
612 set score
[r zscore zset
$x]
613 if {$score < $min ||
$score > $max} {
614 append err
"Error, score for $x is $score outside $min-$max range\n"
618 set score
[r zscore zset
$x]
619 if {$score <= $min ||
$score >= $max} {
620 append err
"Error, score for $x is $score outside $min-$max open range\n"
624 set score
[r zscore zset
$x]
626 append err
"Error, score for $x is $score < $max\n"
630 set score
[r zscore zset
$x]
631 if {$score <= $max} {
632 append err
"Error, score for $x is $score <= $max\n"
639 test
"ZSETs skiplist implementation backlink consistency test - $encoding" {
641 for {set j
0} {$j < $elements} {incr j
} {
642 r zadd myzset
[expr rand
()] "Element-$j"
643 r zrem myzset
"Element-[expr int(rand()*$elements)]"
646 assert_encoding
$encoding myzset
647 set l1
[r zrange myzset
0 -1]
648 set l2
[r zrevrange myzset
0 -1]
649 for {set j
0} {$j < [llength $l1]} {incr j
} {
650 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
657 test
"ZSETs ZRANK augmented skip list stress testing - $encoding" {
660 for {set k
0} {$k < 2000} {incr k
} {
661 set i
[expr {$k % $elements}]
662 if {[expr rand
()] < .2} {
665 set score
[expr rand
()]
666 r zadd myzset
$score $i
667 assert_encoding
$encoding myzset
670 set card
[r zcard myzset
]
672 set index
[randomInt
$card]
673 set ele
[lindex [r zrange myzset
$index $index] 0]
674 set rank
[r zrank myzset
$ele]
675 if {$rank != $index} {
676 set err
"$ele RANK is wrong! ($rank != $index)"