]> git.saurik.com Git - redis.git/blob - tests/unit/type/zset.tcl
Fix case and indent
[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 proc create_default_zset {} {
203 create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g}
204 }
205
206 test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
207 create_default_zset
208
209 # inclusive range
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]
219
220 # exclusive range
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]
230 }
231
232 test "ZRANGEBYSCORE with WITHSCORES" {
233 create_default_zset
234 assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores]
235 assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores]
236 }
237
238 test "ZRANGEBYSCORE with LIMIT" {
239 create_default_zset
240 assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2]
241 assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3]
242 assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10]
243 assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10]
244 assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2]
245 assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3]
246 assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10]
247 assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10]
248 }
249
250 test "ZRANGEBYSCORE with LIMIT and WITHSCORES" {
251 create_default_zset
252 assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES]
253 assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES]
254 }
255
256 test "ZRANGEBYSCORE with non-value min or max" {
257 assert_error "*not a double*" {r zrangebyscore fooz str 1}
258 assert_error "*not a double*" {r zrangebyscore fooz 1 str}
259 assert_error "*not a double*" {r zrangebyscore fooz 1 NaN}
260 }
261
262 tags {"slow"} {
263 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
264 set err {}
265 r del zset
266 for {set i 0} {$i < 1000} {incr i} {
267 r zadd zset [expr rand()] $i
268 }
269 for {set i 0} {$i < 100} {incr i} {
270 set min [expr rand()]
271 set max [expr rand()]
272 if {$min > $max} {
273 set aux $min
274 set min $max
275 set max $aux
276 }
277 set low [r zrangebyscore zset -inf $min]
278 set ok [r zrangebyscore zset $min $max]
279 set high [r zrangebyscore zset $max +inf]
280 set lowx [r zrangebyscore zset -inf ($min]
281 set okx [r zrangebyscore zset ($min ($max]
282 set highx [r zrangebyscore zset ($max +inf]
283
284 if {[r zcount zset -inf $min] != [llength $low]} {
285 append err "Error, len does not match zcount\n"
286 }
287 if {[r zcount zset $min $max] != [llength $ok]} {
288 append err "Error, len does not match zcount\n"
289 }
290 if {[r zcount zset $max +inf] != [llength $high]} {
291 append err "Error, len does not match zcount\n"
292 }
293 if {[r zcount zset -inf ($min] != [llength $lowx]} {
294 append err "Error, len does not match zcount\n"
295 }
296 if {[r zcount zset ($min ($max] != [llength $okx]} {
297 append err "Error, len does not match zcount\n"
298 }
299 if {[r zcount zset ($max +inf] != [llength $highx]} {
300 append err "Error, len does not match zcount\n"
301 }
302
303 foreach x $low {
304 set score [r zscore zset $x]
305 if {$score > $min} {
306 append err "Error, score for $x is $score > $min\n"
307 }
308 }
309 foreach x $lowx {
310 set score [r zscore zset $x]
311 if {$score >= $min} {
312 append err "Error, score for $x is $score >= $min\n"
313 }
314 }
315 foreach x $ok {
316 set score [r zscore zset $x]
317 if {$score < $min || $score > $max} {
318 append err "Error, score for $x is $score outside $min-$max range\n"
319 }
320 }
321 foreach x $okx {
322 set score [r zscore zset $x]
323 if {$score <= $min || $score >= $max} {
324 append err "Error, score for $x is $score outside $min-$max open range\n"
325 }
326 }
327 foreach x $high {
328 set score [r zscore zset $x]
329 if {$score < $max} {
330 append err "Error, score for $x is $score < $max\n"
331 }
332 }
333 foreach x $highx {
334 set score [r zscore zset $x]
335 if {$score <= $max} {
336 append err "Error, score for $x is $score <= $max\n"
337 }
338 }
339 }
340 set _ $err
341 } {}
342 }
343
344 test "ZREMRANGEBYSCORE basics" {
345 proc remrangebyscore {min max} {
346 create_zset zset {1 a 2 b 3 c 4 d 5 e}
347 r zremrangebyscore zset $min $max
348 }
349
350 # inner range
351 assert_equal 3 [remrangebyscore 2 4]
352 assert_equal {a e} [r zrange zset 0 -1]
353
354 # start underflow
355 assert_equal 1 [remrangebyscore -10 1]
356 assert_equal {b c d e} [r zrange zset 0 -1]
357
358 # end overflow
359 assert_equal 1 [remrangebyscore 5 10]
360 assert_equal {a b c d} [r zrange zset 0 -1]
361
362 # switch min and max
363 assert_equal 0 [remrangebyscore 4 2]
364 assert_equal {a b c d e} [r zrange zset 0 -1]
365
366 # -inf to mid
367 assert_equal 3 [remrangebyscore -inf 3]
368 assert_equal {d e} [r zrange zset 0 -1]
369
370 # mid to +inf
371 assert_equal 3 [remrangebyscore 3 +inf]
372 assert_equal {a b} [r zrange zset 0 -1]
373
374 # -inf to +inf
375 assert_equal 5 [remrangebyscore -inf +inf]
376 assert_equal {} [r zrange zset 0 -1]
377
378 # exclusive min
379 assert_equal 4 [remrangebyscore (1 5]
380 assert_equal {a} [r zrange zset 0 -1]
381 assert_equal 3 [remrangebyscore (2 5]
382 assert_equal {a b} [r zrange zset 0 -1]
383
384 # exclusive max
385 assert_equal 4 [remrangebyscore 1 (5]
386 assert_equal {e} [r zrange zset 0 -1]
387 assert_equal 3 [remrangebyscore 1 (4]
388 assert_equal {d e} [r zrange zset 0 -1]
389
390 # exclusive min and max
391 assert_equal 3 [remrangebyscore (1 (5]
392 assert_equal {a e} [r zrange zset 0 -1]
393 }
394
395 test "ZREMRANGEBYSCORE with non-value min or max" {
396 assert_error "*not a double*" {r zremrangebyscore fooz str 1}
397 assert_error "*not a double*" {r zremrangebyscore fooz 1 str}
398 assert_error "*not a double*" {r zremrangebyscore fooz 1 NaN}
399 }
400
401 test "ZREMRANGEBYRANK basics" {
402 proc remrangebyrank {min max} {
403 create_zset zset {1 a 2 b 3 c 4 d 5 e}
404 r zremrangebyrank zset $min $max
405 }
406
407 # inner range
408 assert_equal 3 [remrangebyrank 1 3]
409 assert_equal {a e} [r zrange zset 0 -1]
410
411 # start underflow
412 assert_equal 1 [remrangebyrank -10 0]
413 assert_equal {b c d e} [r zrange zset 0 -1]
414
415 # start overflow
416 assert_equal 0 [remrangebyrank 10 -1]
417 assert_equal {a b c d e} [r zrange zset 0 -1]
418
419 # end underflow
420 assert_equal 0 [remrangebyrank 0 -10]
421 assert_equal {a b c d e} [r zrange zset 0 -1]
422
423 # end overflow
424 assert_equal 5 [remrangebyrank 0 10]
425 assert_equal {} [r zrange zset 0 -1]
426 }
427
428 test {ZUNIONSTORE against non-existing key doesn't set destination} {
429 r del zseta
430 list [r zunionstore dst_key 1 zseta] [r exists dst_key]
431 } {0 0}
432
433 test {ZUNIONSTORE basics} {
434 r del zseta zsetb zsetc
435 r zadd zseta 1 a
436 r zadd zseta 2 b
437 r zadd zseta 3 c
438 r zadd zsetb 1 b
439 r zadd zsetb 2 c
440 r zadd zsetb 3 d
441 list [r zunionstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
442 } {4 {a 1 b 3 d 3 c 5}}
443
444 test {ZUNIONSTORE with weights} {
445 list [r zunionstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
446 } {4 {a 2 b 7 d 9 c 12}}
447
448 test {ZUNIONSTORE with a regular set and weights} {
449 r del seta
450 r sadd seta a
451 r sadd seta b
452 r sadd seta c
453 list [r zunionstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
454 } {4 {a 2 b 5 c 8 d 9}}
455
456 test {ZUNIONSTORE with AGGREGATE MIN} {
457 list [r zunionstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
458 } {4 {a 1 b 1 c 2 d 3}}
459
460 test {ZUNIONSTORE with AGGREGATE MAX} {
461 list [r zunionstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
462 } {4 {a 1 b 2 c 3 d 3}}
463
464 test {ZINTERSTORE basics} {
465 list [r zinterstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
466 } {2 {b 3 c 5}}
467
468 test {ZINTERSTORE with weights} {
469 list [r zinterstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
470 } {2 {b 7 c 12}}
471
472 test {ZINTERSTORE with a regular set and weights} {
473 r del seta
474 r sadd seta a
475 r sadd seta b
476 r sadd seta c
477 list [r zinterstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
478 } {2 {b 5 c 8}}
479
480 test {ZINTERSTORE with AGGREGATE MIN} {
481 list [r zinterstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
482 } {2 {b 1 c 2}}
483
484 test {ZINTERSTORE with AGGREGATE MAX} {
485 list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
486 } {2 {b 2 c 3}}
487
488 foreach cmd {ZUNIONSTORE ZINTERSTORE} {
489 test "$cmd with +inf/-inf scores" {
490 r del zsetinf1 zsetinf2
491
492 r zadd zsetinf1 +inf key
493 r zadd zsetinf2 +inf key
494 r $cmd zsetinf3 2 zsetinf1 zsetinf2
495 assert_equal inf [r zscore zsetinf3 key]
496
497 r zadd zsetinf1 -inf key
498 r zadd zsetinf2 +inf key
499 r $cmd zsetinf3 2 zsetinf1 zsetinf2
500 assert_equal 0 [r zscore zsetinf3 key]
501
502 r zadd zsetinf1 +inf key
503 r zadd zsetinf2 -inf key
504 r $cmd zsetinf3 2 zsetinf1 zsetinf2
505 assert_equal 0 [r zscore zsetinf3 key]
506
507 r zadd zsetinf1 -inf key
508 r zadd zsetinf2 -inf key
509 r $cmd zsetinf3 2 zsetinf1 zsetinf2
510 assert_equal -inf [r zscore zsetinf3 key]
511 }
512
513 test "$cmd with NaN weights" {
514 r del zsetinf1 zsetinf2
515
516 r zadd zsetinf1 1.0 key
517 r zadd zsetinf2 1.0 key
518 assert_error "*weight value is not a double*" {
519 r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
520 }
521 }
522 }
523
524 tags {"slow"} {
525 test {ZSETs skiplist implementation backlink consistency test} {
526 set diff 0
527 set elements 10000
528 for {set j 0} {$j < $elements} {incr j} {
529 r zadd myzset [expr rand()] "Element-$j"
530 r zrem myzset "Element-[expr int(rand()*$elements)]"
531 }
532 set l1 [r zrange myzset 0 -1]
533 set l2 [r zrevrange myzset 0 -1]
534 for {set j 0} {$j < [llength $l1]} {incr j} {
535 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
536 incr diff
537 }
538 }
539 format $diff
540 } {0}
541
542 test {ZSETs ZRANK augmented skip list stress testing} {
543 set err {}
544 r del myzset
545 for {set k 0} {$k < 10000} {incr k} {
546 set i [expr {$k%1000}]
547 if {[expr rand()] < .2} {
548 r zrem myzset $i
549 } else {
550 set score [expr rand()]
551 r zadd myzset $score $i
552 }
553 set card [r zcard myzset]
554 if {$card > 0} {
555 set index [randomInt $card]
556 set ele [lindex [r zrange myzset $index $index] 0]
557 set rank [r zrank myzset $ele]
558 if {$rank != $index} {
559 set err "$ele RANK is wrong! ($rank != $index)"
560 break
561 }
562 }
563 }
564 set _ $err
565 } {}
566 }
567
568 test {ZSET element can't be set to NaN with ZADD} {
569 assert_error "*not a double*" {r zadd myzset nan abc}
570 }
571
572 test {ZSET element can't be set to NaN with ZINCRBY} {
573 assert_error "*not a double*" {r zadd myzset nan abc}
574 }
575
576 test {ZINCRBY calls leading to NaN result in error} {
577 r zincrby myzset +inf abc
578 assert_error "*NaN*" {r zincrby myzset -inf abc}
579 }
580 }