1 start_server
{tags
{"zset"}} {
2 test
{ZSET basic ZADD and score
update} {
6 set aux1
[r zrange ztmp
0 -1]
8 set aux2
[r zrange ztmp
0 -1]
16 test
{ZCARD non existing key
} {
20 test
"ZRANGE basics" {
27 assert_equal
{a b c d
} [r zrange ztmp
0 -1]
28 assert_equal
{a b c
} [r zrange ztmp
0 -2]
29 assert_equal
{b c d
} [r zrange ztmp
1 -1]
30 assert_equal
{b c
} [r zrange ztmp
1 -2]
31 assert_equal
{c d
} [r zrange ztmp
-2 -1]
32 assert_equal
{c
} [r zrange ztmp
-2 -2]
34 # out of range start index
35 assert_equal
{a b c
} [r zrange ztmp
-5 2]
36 assert_equal
{a b
} [r zrange ztmp
-5 1]
37 assert_equal
{} [r zrange ztmp
5 -1]
38 assert_equal
{} [r zrange ztmp
5 -2]
40 # out of range end index
41 assert_equal
{a b c d
} [r zrange ztmp
0 5]
42 assert_equal
{b c d
} [r zrange ztmp
1 5]
43 assert_equal
{} [r zrange ztmp
0 -5]
44 assert_equal
{} [r zrange ztmp
1 -5]
47 assert_equal
{a
1 b
2 c
3 d
4} [r zrange ztmp
0 -1 withscores
]
50 test
"ZREVRANGE basics" {
57 assert_equal
{d c b a
} [r zrevrange ztmp
0 -1]
58 assert_equal
{d c b
} [r zrevrange ztmp
0 -2]
59 assert_equal
{c b a
} [r zrevrange ztmp
1 -1]
60 assert_equal
{c b
} [r zrevrange ztmp
1 -2]
61 assert_equal
{b a
} [r zrevrange ztmp
-2 -1]
62 assert_equal
{b
} [r zrevrange ztmp
-2 -2]
64 # out of range start index
65 assert_equal
{d c b
} [r zrevrange ztmp
-5 2]
66 assert_equal
{d c
} [r zrevrange ztmp
-5 1]
67 assert_equal
{} [r zrevrange ztmp
5 -1]
68 assert_equal
{} [r zrevrange ztmp
5 -2]
70 # out of range end index
71 assert_equal
{d c b a
} [r zrevrange ztmp
0 5]
72 assert_equal
{c b a
} [r zrevrange ztmp
1 5]
73 assert_equal
{} [r zrevrange ztmp
0 -5]
74 assert_equal
{} [r zrevrange ztmp
1 -5]
77 assert_equal
{d
4 c
3 b
2 a
1} [r zrevrange ztmp
0 -1 withscores
]
84 list [r zrank zranktmp x
] [r zrank zranktmp y
] [r zrank zranktmp z
]
87 test
{ZREVRANK basics
} {
88 list [r zrevrank zranktmp x
] [r zrevrank zranktmp y
] [r zrevrank zranktmp z
]
91 test
{ZRANK
- after deletion
} {
93 list [r zrank zranktmp x
] [r zrank zranktmp z
]
99 for {set i
0} {$i < 1000} {incr i
} {
100 set score
[expr rand
()]
102 r zadd zscoretest
$score $i
104 for {set i
0} {$i < 1000} {incr i
} {
105 if {[r zscore zscoretest
$i] != [lindex $aux $i]} {
106 set err
"Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
113 test
{ZSCORE
after a DEBUG RELOAD
} {
117 for {set i
0} {$i < 1000} {incr i
} {
118 set score
[expr rand
()]
120 r zadd zscoretest
$score $i
123 for {set i
0} {$i < 1000} {incr i
} {
124 if {[r zscore zscoretest
$i] != [lindex $aux $i]} {
125 set err
"Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
132 test
{ZSETs stress tester
- sorting is working well?
} {
134 for {set test
0} {$test < 2} {incr test
} {
135 unset -nocomplain auxarray
136 array set auxarray
{}
139 for {set i
0} {$i < 1000} {incr i
} {
141 set score
[expr rand
()]
143 set score
[expr int
(rand
()*10)]
145 set auxarray
($i) $score
146 r zadd myzset
$score $i
148 if {[expr rand
()] < .2} {
149 set j
[expr int
(rand
()*1000)]
151 set score
[expr rand
()]
153 set score
[expr int
(rand
()*10)]
155 set auxarray
($j) $score
156 r zadd myzset
$score $j
159 foreach {item score
} [array get auxarray
] {
160 lappend auxlist
[list $score $item]
162 set sorted
[lsort -command zlistAlikeSort
$auxlist]
165 lappend auxlist
[lindex $x 1]
167 set fromredis
[r zrange myzset
0 -1]
169 for {set i
0} {$i < [llength $fromredis]} {incr i
} {
170 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
178 test
{ZINCRBY
- can create a new sorted
set} {
181 list [r zrange zset
0 -1] [r zscore zset foo
]
184 test
{ZINCRBY
- increment and decrement
} {
187 set v1
[r zrange zset
0 -1]
188 r zincrby zset
10 bar
189 r zincrby zset
-5 foo
190 r zincrby zset
-5 bar
191 set v2
[r zrange zset
0 -1]
192 list $v1 $v2 [r zscore zset foo
] [r zscore zset bar
]
193 } {{bar foo
} {foo bar
} -2 6}
195 test
{ZRANGEBYSCORE and ZCOUNT basics
} {
202 list [r zrangebyscore zset
2 4] [r zrangebyscore zset
(2 (4] \
203 [r zcount zset
2 4] [r zcount zset
(2 (4]
206 test
{ZRANGEBYSCORE withscores
} {
213 r zrangebyscore zset
2 4 withscores
217 test
{ZRANGEBYSCORE fuzzy test
, 100 ranges in
1000 elements sorted
set} {
220 for {set i
0} {$i < 1000} {incr i
} {
221 r zadd zset
[expr rand
()] $i
223 for {set i
0} {$i < 100} {incr i
} {
224 set min
[expr rand
()]
225 set max
[expr rand
()]
231 set low
[r zrangebyscore zset
-inf $min]
232 set ok
[r zrangebyscore zset
$min $max]
233 set high
[r zrangebyscore zset
$max +inf
]
234 set lowx
[r zrangebyscore zset
-inf ($min]
235 set okx
[r zrangebyscore zset
($min ($max]
236 set highx
[r zrangebyscore zset
($max +inf
]
238 if {[r zcount zset
-inf $min] != [llength $low]} {
239 append err
"Error, len does not match zcount\n"
241 if {[r zcount zset
$min $max] != [llength $ok]} {
242 append err
"Error, len does not match zcount\n"
244 if {[r zcount zset
$max +inf
] != [llength $high]} {
245 append err
"Error, len does not match zcount\n"
247 if {[r zcount zset
-inf ($min] != [llength $lowx]} {
248 append err
"Error, len does not match zcount\n"
250 if {[r zcount zset
($min ($max] != [llength $okx]} {
251 append err
"Error, len does not match zcount\n"
253 if {[r zcount zset
($max +inf
] != [llength $highx]} {
254 append err
"Error, len does not match zcount\n"
258 set score
[r zscore zset
$x]
260 append err
"Error, score for $x is $score > $min\n"
264 set score
[r zscore zset
$x]
265 if {$score >= $min} {
266 append err
"Error, score for $x is $score >= $min\n"
270 set score
[r zscore zset
$x]
271 if {$score < $min ||
$score > $max} {
272 append err
"Error, score for $x is $score outside $min-$max range\n"
276 set score
[r zscore zset
$x]
277 if {$score <= $min ||
$score >= $max} {
278 append err
"Error, score for $x is $score outside $min-$max open range\n"
282 set score
[r zscore zset
$x]
284 append err
"Error, score for $x is $score < $max\n"
288 set score
[r zscore zset
$x]
289 if {$score <= $max} {
290 append err
"Error, score for $x is $score <= $max\n"
298 test
{ZRANGEBYSCORE with LIMIT
} {
306 [r zrangebyscore zset
0 10 LIMIT
0 2] \
307 [r zrangebyscore zset
0 10 LIMIT
2 3] \
308 [r zrangebyscore zset
0 10 LIMIT
2 10] \
309 [r zrangebyscore zset
0 10 LIMIT
20 10]
310 } {{a b
} {c d e
} {c d e
} {}}
312 test
{ZRANGEBYSCORE with LIMIT and withscores
} {
319 r zrangebyscore zset
20 50 LIMIT
2 3 withscores
322 test
{ZREMRANGEBYSCORE basics
} {
329 list [r zremrangebyscore zset
2 4] [r zrange zset
0 -1]
332 test
{ZREMRANGEBYSCORE from
-inf to
+inf
} {
339 list [r zremrangebyscore zset
-inf +inf
] [r zrange zset
0 -1]
342 test
{ZREMRANGEBYRANK basics
} {
349 list [r zremrangebyrank zset
1 3] [r zrange zset
0 -1]
352 test
{ZUNIONSTORE against non-existing key doesn't
set destination
} {
354 list [r zunionstore dst_key
1 zseta
] [r exists dst_key
]
357 test
{ZUNIONSTORE basics
} {
358 r del zseta zsetb zsetc
365 list [r zunionstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
366 } {4 {a
1 b
3 d
3 c
5}}
368 test
{ZUNIONSTORE with weights
} {
369 list [r zunionstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
370 } {4 {a
2 b
7 d
9 c
12}}
372 test
{ZUNIONSTORE with a regular
set and weights
} {
377 list [r zunionstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
378 } {4 {a
2 b
5 c
8 d
9}}
380 test
{ZUNIONSTORE with AGGREGATE MIN
} {
381 list [r zunionstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
382 } {4 {a
1 b
1 c
2 d
3}}
384 test
{ZUNIONSTORE with AGGREGATE MAX
} {
385 list [r zunionstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
386 } {4 {a
1 b
2 c
3 d
3}}
388 test
{ZINTERSTORE basics
} {
389 list [r zinterstore zsetc
2 zseta zsetb
] [r zrange zsetc
0 -1 withscores
]
392 test
{ZINTERSTORE with weights
} {
393 list [r zinterstore zsetc
2 zseta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
396 test
{ZINTERSTORE with a regular
set and weights
} {
401 list [r zinterstore zsetc
2 seta zsetb weights
2 3] [r zrange zsetc
0 -1 withscores
]
404 test
{ZINTERSTORE with AGGREGATE MIN
} {
405 list [r zinterstore zsetc
2 zseta zsetb aggregate min
] [r zrange zsetc
0 -1 withscores
]
408 test
{ZINTERSTORE with AGGREGATE MAX
} {
409 list [r zinterstore zsetc
2 zseta zsetb aggregate max
] [r zrange zsetc
0 -1 withscores
]
413 test
{ZSETs skiplist implementation backlink consistency test
} {
416 for {set j
0} {$j < $elements} {incr j
} {
417 r zadd myzset
[expr rand
()] "Element-$j"
418 r zrem myzset
"Element-[expr int(rand()*$elements)]"
420 set l1
[r zrange myzset
0 -1]
421 set l2
[r zrevrange myzset
0 -1]
422 for {set j
0} {$j < [llength $l1]} {incr j
} {
423 if {[lindex $l1 $j] ne
[lindex $l2 end-
$j]} {
430 test
{ZSETs ZRANK augmented skip
list stress testing
} {
433 for {set k
0} {$k < 10000} {incr k
} {
434 set i
[expr {$k%1000}]
435 if {[expr rand
()] < .2} {
438 set score
[expr rand
()]
439 r zadd myzset
$score $i
441 set card
[r zcard myzset
]
443 set index
[randomInt
$card]
444 set ele
[lindex [r zrange myzset
$index $index] 0]
445 set rank
[r zrank myzset
$ele]
446 if {$rank != $index} {
447 set err
"$ele RANK is wrong! ($rank != $index)"
456 test
{ZSET element can't be
set to nan with ZADD
} {
458 catch {r zadd myzset nan abc
} e
462 test
{ZSET element can't be
set to nan with ZINCRBY
} {
464 catch {r zincrby myzset nan abc
} e
468 test
{ZINCRBY calls leading to Nan are refused
} {
470 r zincrby myzset
+inf abc
471 catch {r zincrby myzset
-inf abc
} e