]> git.saurik.com Git - redis.git/blob - tests/unit/type/zset.tcl
fix behavior for out-of-range negative end index on ZREMRANGEBYRANK
[redis.git] / tests / unit / type / zset.tcl
1 start_server {tags {"zset"}} {
2 proc create_zset {key items} {
3 r del $key
4 foreach {score entry} $items {
5 r zadd $key $score $entry
6 }
7 }
8
9 test {ZSET basic ZADD and score update} {
10 r zadd ztmp 10 x
11 r zadd ztmp 20 y
12 r zadd ztmp 30 z
13 set aux1 [r zrange ztmp 0 -1]
14 r zadd ztmp 1 y
15 set aux2 [r zrange ztmp 0 -1]
16 list $aux1 $aux2
17 } {{x y z} {y x z}}
18
19 test {ZCARD basics} {
20 r zcard ztmp
21 } {3}
22
23 test {ZCARD non existing key} {
24 r zcard ztmp-blabla
25 } {0}
26
27 test "ZRANGE basics" {
28 r del ztmp
29 r zadd ztmp 1 a
30 r zadd ztmp 2 b
31 r zadd ztmp 3 c
32 r zadd ztmp 4 d
33
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]
40
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]
46
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]
52
53 # withscores
54 assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
55 }
56
57 test "ZREVRANGE basics" {
58 r del ztmp
59 r zadd ztmp 1 a
60 r zadd ztmp 2 b
61 r zadd ztmp 3 c
62 r zadd ztmp 4 d
63
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]
70
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]
76
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]
82
83 # withscores
84 assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
85 }
86
87 test {ZRANK basics} {
88 r zadd zranktmp 10 x
89 r zadd zranktmp 20 y
90 r zadd zranktmp 30 z
91 list [r zrank zranktmp x] [r zrank zranktmp y] [r zrank zranktmp z]
92 } {0 1 2}
93
94 test {ZREVRANK basics} {
95 list [r zrevrank zranktmp x] [r zrevrank zranktmp y] [r zrevrank zranktmp z]
96 } {2 1 0}
97
98 test {ZRANK - after deletion} {
99 r zrem zranktmp y
100 list [r zrank zranktmp x] [r zrank zranktmp z]
101 } {0 1}
102
103 test {ZSCORE} {
104 set aux {}
105 set err {}
106 for {set i 0} {$i < 1000} {incr i} {
107 set score [expr rand()]
108 lappend aux $score
109 r zadd zscoretest $score $i
110 }
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"
114 break
115 }
116 }
117 set _ $err
118 } {}
119
120 test {ZSCORE after a DEBUG RELOAD} {
121 set aux {}
122 set err {}
123 r del zscoretest
124 for {set i 0} {$i < 1000} {incr i} {
125 set score [expr rand()]
126 lappend aux $score
127 r zadd zscoretest $score $i
128 }
129 r debug reload
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"
133 break
134 }
135 }
136 set _ $err
137 } {}
138
139 test {ZSETs stress tester - sorting is working well?} {
140 set delta 0
141 for {set test 0} {$test < 2} {incr test} {
142 unset -nocomplain auxarray
143 array set auxarray {}
144 set auxlist {}
145 r del myzset
146 for {set i 0} {$i < 1000} {incr i} {
147 if {$test == 0} {
148 set score [expr rand()]
149 } else {
150 set score [expr int(rand()*10)]
151 }
152 set auxarray($i) $score
153 r zadd myzset $score $i
154 # Random update
155 if {[expr rand()] < .2} {
156 set j [expr int(rand()*1000)]
157 if {$test == 0} {
158 set score [expr rand()]
159 } else {
160 set score [expr int(rand()*10)]
161 }
162 set auxarray($j) $score
163 r zadd myzset $score $j
164 }
165 }
166 foreach {item score} [array get auxarray] {
167 lappend auxlist [list $score $item]
168 }
169 set sorted [lsort -command zlistAlikeSort $auxlist]
170 set auxlist {}
171 foreach x $sorted {
172 lappend auxlist [lindex $x 1]
173 }
174 set fromredis [r zrange myzset 0 -1]
175 set delta 0
176 for {set i 0} {$i < [llength $fromredis]} {incr i} {
177 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
178 incr delta
179 }
180 }
181 }
182 format $delta
183 } {0}
184
185 test {ZINCRBY - can create a new sorted set} {
186 r del zset
187 r zincrby zset 1 foo
188 list [r zrange zset 0 -1] [r zscore zset foo]
189 } {foo 1}
190
191 test {ZINCRBY - increment and decrement} {
192 r zincrby zset 2 foo
193 r zincrby zset 1 bar
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}
201
202 test {ZRANGEBYSCORE and ZCOUNT basics} {
203 r del zset
204 r zadd zset 1 a
205 r zadd zset 2 b
206 r zadd zset 3 c
207 r zadd zset 4 d
208 r zadd zset 5 e
209 list [r zrangebyscore zset 2 4] [r zrangebyscore zset (2 (4] \
210 [r zcount zset 2 4] [r zcount zset (2 (4]
211 } {{b c d} c 3 1}
212
213 test {ZRANGEBYSCORE withscores} {
214 r del zset
215 r zadd zset 1 a
216 r zadd zset 2 b
217 r zadd zset 3 c
218 r zadd zset 4 d
219 r zadd zset 5 e
220 r zrangebyscore zset 2 4 withscores
221 } {b 2 c 3 d 4}
222
223 tags {"slow"} {
224 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
225 set err {}
226 r del zset
227 for {set i 0} {$i < 1000} {incr i} {
228 r zadd zset [expr rand()] $i
229 }
230 for {set i 0} {$i < 100} {incr i} {
231 set min [expr rand()]
232 set max [expr rand()]
233 if {$min > $max} {
234 set aux $min
235 set min $max
236 set max $aux
237 }
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]
244
245 if {[r zcount zset -inf $min] != [llength $low]} {
246 append err "Error, len does not match zcount\n"
247 }
248 if {[r zcount zset $min $max] != [llength $ok]} {
249 append err "Error, len does not match zcount\n"
250 }
251 if {[r zcount zset $max +inf] != [llength $high]} {
252 append err "Error, len does not match zcount\n"
253 }
254 if {[r zcount zset -inf ($min] != [llength $lowx]} {
255 append err "Error, len does not match zcount\n"
256 }
257 if {[r zcount zset ($min ($max] != [llength $okx]} {
258 append err "Error, len does not match zcount\n"
259 }
260 if {[r zcount zset ($max +inf] != [llength $highx]} {
261 append err "Error, len does not match zcount\n"
262 }
263
264 foreach x $low {
265 set score [r zscore zset $x]
266 if {$score > $min} {
267 append err "Error, score for $x is $score > $min\n"
268 }
269 }
270 foreach x $lowx {
271 set score [r zscore zset $x]
272 if {$score >= $min} {
273 append err "Error, score for $x is $score >= $min\n"
274 }
275 }
276 foreach x $ok {
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"
280 }
281 }
282 foreach x $okx {
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"
286 }
287 }
288 foreach x $high {
289 set score [r zscore zset $x]
290 if {$score < $max} {
291 append err "Error, score for $x is $score < $max\n"
292 }
293 }
294 foreach x $highx {
295 set score [r zscore zset $x]
296 if {$score <= $max} {
297 append err "Error, score for $x is $score <= $max\n"
298 }
299 }
300 }
301 set _ $err
302 } {}
303 }
304
305 test {ZRANGEBYSCORE with LIMIT} {
306 r del zset
307 r zadd zset 1 a
308 r zadd zset 2 b
309 r zadd zset 3 c
310 r zadd zset 4 d
311 r zadd zset 5 e
312 list \
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} {}}
318
319 test {ZRANGEBYSCORE with LIMIT and withscores} {
320 r del zset
321 r zadd zset 10 a
322 r zadd zset 20 b
323 r zadd zset 30 c
324 r zadd zset 40 d
325 r zadd zset 50 e
326 r zrangebyscore zset 20 50 LIMIT 2 3 withscores
327 } {d 40 e 50}
328
329 test {ZREMRANGEBYSCORE basics} {
330 r del zset
331 r zadd zset 1 a
332 r zadd zset 2 b
333 r zadd zset 3 c
334 r zadd zset 4 d
335 r zadd zset 5 e
336 list [r zremrangebyscore zset 2 4] [r zrange zset 0 -1]
337 } {3 {a e}}
338
339 test {ZREMRANGEBYSCORE from -inf to +inf} {
340 r del zset
341 r zadd zset 1 a
342 r zadd zset 2 b
343 r zadd zset 3 c
344 r zadd zset 4 d
345 r zadd zset 5 e
346 list [r zremrangebyscore zset -inf +inf] [r zrange zset 0 -1]
347 } {5 {}}
348
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
353 }
354
355 # inner range
356 assert_equal 3 [remrangebyrank 1 3]
357 assert_equal {a e} [r zrange zset 0 -1]
358
359 # start underflow
360 assert_equal 1 [remrangebyrank -10 0]
361 assert_equal {b c d e} [r zrange zset 0 -1]
362
363 # start overflow
364 assert_equal 0 [remrangebyrank 10 -1]
365 assert_equal {a b c d e} [r zrange zset 0 -1]
366
367 # end underflow
368 assert_equal 0 [remrangebyrank 0 -10]
369 assert_equal {a b c d e} [r zrange zset 0 -1]
370
371 # end overflow
372 assert_equal 5 [remrangebyrank 0 10]
373 assert_equal {} [r zrange zset 0 -1]
374 }
375
376 test {ZUNIONSTORE against non-existing key doesn't set destination} {
377 r del zseta
378 list [r zunionstore dst_key 1 zseta] [r exists dst_key]
379 } {0 0}
380
381 test {ZUNIONSTORE basics} {
382 r del zseta zsetb zsetc
383 r zadd zseta 1 a
384 r zadd zseta 2 b
385 r zadd zseta 3 c
386 r zadd zsetb 1 b
387 r zadd zsetb 2 c
388 r zadd zsetb 3 d
389 list [r zunionstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
390 } {4 {a 1 b 3 d 3 c 5}}
391
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}}
395
396 test {ZUNIONSTORE with a regular set and weights} {
397 r del seta
398 r sadd seta a
399 r sadd seta b
400 r sadd seta c
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}}
403
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}}
407
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}}
411
412 test {ZINTERSTORE basics} {
413 list [r zinterstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
414 } {2 {b 3 c 5}}
415
416 test {ZINTERSTORE with weights} {
417 list [r zinterstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
418 } {2 {b 7 c 12}}
419
420 test {ZINTERSTORE with a regular set and weights} {
421 r del seta
422 r sadd seta a
423 r sadd seta b
424 r sadd seta c
425 list [r zinterstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
426 } {2 {b 5 c 8}}
427
428 test {ZINTERSTORE with AGGREGATE MIN} {
429 list [r zinterstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
430 } {2 {b 1 c 2}}
431
432 test {ZINTERSTORE with AGGREGATE MAX} {
433 list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
434 } {2 {b 2 c 3}}
435
436 tags {"slow"} {
437 test {ZSETs skiplist implementation backlink consistency test} {
438 set diff 0
439 set elements 10000
440 for {set j 0} {$j < $elements} {incr j} {
441 r zadd myzset [expr rand()] "Element-$j"
442 r zrem myzset "Element-[expr int(rand()*$elements)]"
443 }
444 set l1 [r zrange myzset 0 -1]
445 set l2 [r zrevrange myzset 0 -1]
446 for {set j 0} {$j < [llength $l1]} {incr j} {
447 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
448 incr diff
449 }
450 }
451 format $diff
452 } {0}
453
454 test {ZSETs ZRANK augmented skip list stress testing} {
455 set err {}
456 r del myzset
457 for {set k 0} {$k < 10000} {incr k} {
458 set i [expr {$k%1000}]
459 if {[expr rand()] < .2} {
460 r zrem myzset $i
461 } else {
462 set score [expr rand()]
463 r zadd myzset $score $i
464 }
465 set card [r zcard myzset]
466 if {$card > 0} {
467 set index [randomInt $card]
468 set ele [lindex [r zrange myzset $index $index] 0]
469 set rank [r zrank myzset $ele]
470 if {$rank != $index} {
471 set err "$ele RANK is wrong! ($rank != $index)"
472 break
473 }
474 }
475 }
476 set _ $err
477 } {}
478 }
479
480 test {ZSET element can't be set to nan with ZADD} {
481 set e {}
482 catch {r zadd myzset nan abc} e
483 set _ $e
484 } {*Not A Number*}
485
486 test {ZSET element can't be set to nan with ZINCRBY} {
487 set e {}
488 catch {r zincrby myzset nan abc} e
489 set _ $e
490 } {*Not A Number*}
491
492 test {ZINCRBY calls leading to Nan are refused} {
493 set e {}
494 r zincrby myzset +inf abc
495 catch {r zincrby myzset -inf abc} e
496 set _ $e
497 } {*Not A Number*}
498 }