]> git.saurik.com Git - redis.git/blob - tests/unit/type/zset.tcl
761cac49cc77a6553cfb7a0ca829729f125fbf25
[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 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
16 } else {
17 puts "Unknown sorted set encoding"
18 exit
19 }
20
21 test "Check encoding - $encoding" {
22 r del ztmp
23 r zadd ztmp 10 x
24 assert_encoding $encoding ztmp
25 }
26
27 test "ZSET basic ZADD and score update - $encoding" {
28 r del ztmp
29 r zadd ztmp 10 x
30 r zadd ztmp 20 y
31 r zadd ztmp 30 z
32 assert_equal {x y z} [r zrange ztmp 0 -1]
33
34 r zadd ztmp 1 y
35 assert_equal {y x z} [r zrange ztmp 0 -1]
36 }
37
38 test "ZSET element can't be set to NaN with ZADD - $encoding" {
39 assert_error "*not a double*" {r zadd myzset nan abc}
40 }
41
42 test "ZSET element can't be set to NaN with ZINCRBY" {
43 assert_error "*not a double*" {r zadd myzset nan abc}
44 }
45
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}
49 }
50
51 test "ZCARD basics - $encoding" {
52 assert_equal 3 [r zcard ztmp]
53 assert_equal 0 [r zcard zdoesntexist]
54 }
55
56 test "ZREM removes key after last element is removed" {
57 r del ztmp
58 r zadd ztmp 10 x
59 r zadd ztmp 20 y
60
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]
66 }
67
68 test "ZRANGE basics - $encoding" {
69 r del ztmp
70 r zadd ztmp 1 a
71 r zadd ztmp 2 b
72 r zadd ztmp 3 c
73 r zadd ztmp 4 d
74
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]
81
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]
87
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]
93
94 # withscores
95 assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
96 }
97
98 test "ZREVRANGE basics - $encoding" {
99 r del ztmp
100 r zadd ztmp 1 a
101 r zadd ztmp 2 b
102 r zadd ztmp 3 c
103 r zadd ztmp 4 d
104
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]
111
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]
117
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]
123
124 # withscores
125 assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
126 }
127
128 test "ZRANK/ZREVRANK basics - $encoding" {
129 r del zranktmp
130 r zadd zranktmp 10 x
131 r zadd zranktmp 20 y
132 r zadd zranktmp 30 z
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]
141 }
142
143 test "ZRANK - after deletion - $encoding" {
144 r zrem zranktmp y
145 assert_equal 0 [r zrank zranktmp x]
146 assert_equal 1 [r zrank zranktmp z]
147 }
148
149 test "ZINCRBY - can create a new sorted set - $encoding" {
150 r del zset
151 r zincrby zset 1 foo
152 assert_equal {foo} [r zrange zset 0 -1]
153 assert_equal 1 [r zscore zset foo]
154 }
155
156 test "ZINCRBY - increment and decrement - $encoding" {
157 r zincrby zset 2 foo
158 r zincrby zset 1 bar
159 assert_equal {bar foo} [r zrange zset 0 -1]
160
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]
165
166 assert_equal -2 [r zscore zset foo]
167 assert_equal 6 [r zscore zset bar]
168 }
169
170 proc create_default_zset {} {
171 create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g}
172 }
173
174 test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
175 create_default_zset
176
177 # inclusive range
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]
187
188 # exclusive range
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]
198
199 # test empty ranges
200 r zrem zset a
201 r zrem zset g
202
203 # inclusive
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]
209
210 # exclusive
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]
218
219 # empty inner range
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]
224 }
225
226 test "ZRANGEBYSCORE with WITHSCORES" {
227 create_default_zset
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]
230 }
231
232 test "ZRANGEBYSCORE with LIMIT" {
233 create_default_zset
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]
242 }
243
244 test "ZRANGEBYSCORE with LIMIT and WITHSCORES" {
245 create_default_zset
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]
248 }
249
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}
254 }
255
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
261 }
262
263 # inner range
264 assert_equal 3 [remrangebyscore 2 4]
265 assert_equal {a e} [r zrange zset 0 -1]
266
267 # start underflow
268 assert_equal 1 [remrangebyscore -10 1]
269 assert_equal {b c d e} [r zrange zset 0 -1]
270
271 # end overflow
272 assert_equal 1 [remrangebyscore 5 10]
273 assert_equal {a b c d} [r zrange zset 0 -1]
274
275 # switch min and max
276 assert_equal 0 [remrangebyscore 4 2]
277 assert_equal {a b c d e} [r zrange zset 0 -1]
278
279 # -inf to mid
280 assert_equal 3 [remrangebyscore -inf 3]
281 assert_equal {d e} [r zrange zset 0 -1]
282
283 # mid to +inf
284 assert_equal 3 [remrangebyscore 3 +inf]
285 assert_equal {a b} [r zrange zset 0 -1]
286
287 # -inf to +inf
288 assert_equal 5 [remrangebyscore -inf +inf]
289 assert_equal {} [r zrange zset 0 -1]
290
291 # exclusive min
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]
296
297 # exclusive max
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]
302
303 # exclusive min and max
304 assert_equal 3 [remrangebyscore (1 (5]
305 assert_equal {a e} [r zrange zset 0 -1]
306
307 # destroy when empty
308 assert_equal 5 [remrangebyscore 1 5]
309 assert_equal 0 [r exists zset]
310 }
311
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}
316 }
317
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
323 }
324
325 # inner range
326 assert_equal 3 [remrangebyrank 1 3]
327 assert_equal {a e} [r zrange zset 0 -1]
328
329 # start underflow
330 assert_equal 1 [remrangebyrank -10 0]
331 assert_equal {b c d e} [r zrange zset 0 -1]
332
333 # start overflow
334 assert_equal 0 [remrangebyrank 10 -1]
335 assert_equal {a b c d e} [r zrange zset 0 -1]
336
337 # end underflow
338 assert_equal 0 [remrangebyrank 0 -10]
339 assert_equal {a b c d e} [r zrange zset 0 -1]
340
341 # end overflow
342 assert_equal 5 [remrangebyrank 0 10]
343 assert_equal {} [r zrange zset 0 -1]
344
345 # destroy when empty
346 assert_equal 5 [remrangebyrank 0 4]
347 assert_equal 0 [r exists zset]
348 }
349
350 test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
351 r del zseta
352 assert_equal 0 [r zunionstore dst_key 1 zseta]
353 assert_equal 0 [r exists dst_key]
354 }
355
356 test "ZUNIONSTORE with empty set - $encoding" {
357 r del zseta zsetb
358 r zadd zseta 1 a
359 r zadd zseta 2 b
360 r zunionstore zsetc 2 zseta zsetb
361 r zrange zsetc 0 -1 withscores
362 } {a 1 b 2}
363
364 test "ZUNIONSTORE basics - $encoding" {
365 r del zseta zsetb zsetc
366 r zadd zseta 1 a
367 r zadd zseta 2 b
368 r zadd zseta 3 c
369 r zadd zsetb 1 b
370 r zadd zsetb 2 c
371 r zadd zsetb 3 d
372
373 assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
374 assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
375 }
376
377 test "ZUNIONSTORE with weights - $encoding" {
378 assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
379 assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
380 }
381
382 test "ZUNIONSTORE with a regular set and weights - $encoding" {
383 r del seta
384 r sadd seta a
385 r sadd seta b
386 r sadd seta c
387
388 assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
389 assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
390 }
391
392 test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
393 assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
394 assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
395 }
396
397 test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
398 assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
399 assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
400 }
401
402 test "ZINTERSTORE basics - $encoding" {
403 assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
404 assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
405 }
406
407 test "ZINTERSTORE with weights - $encoding" {
408 assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
409 assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
410 }
411
412 test "ZINTERSTORE with a regular set and weights - $encoding" {
413 r del seta
414 r sadd seta a
415 r sadd seta b
416 r sadd seta c
417 assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
418 assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
419 }
420
421 test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
422 assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
423 assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
424 }
425
426 test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
427 assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
428 assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
429 }
430
431 foreach cmd {ZUNIONSTORE ZINTERSTORE} {
432 test "$cmd with +inf/-inf scores - $encoding" {
433 r del zsetinf1 zsetinf2
434
435 r zadd zsetinf1 +inf key
436 r zadd zsetinf2 +inf key
437 r $cmd zsetinf3 2 zsetinf1 zsetinf2
438 assert_equal inf [r zscore zsetinf3 key]
439
440 r zadd zsetinf1 -inf key
441 r zadd zsetinf2 +inf key
442 r $cmd zsetinf3 2 zsetinf1 zsetinf2
443 assert_equal 0 [r zscore zsetinf3 key]
444
445 r zadd zsetinf1 +inf key
446 r zadd zsetinf2 -inf key
447 r $cmd zsetinf3 2 zsetinf1 zsetinf2
448 assert_equal 0 [r zscore zsetinf3 key]
449
450 r zadd zsetinf1 -inf key
451 r zadd zsetinf2 -inf key
452 r $cmd zsetinf3 2 zsetinf1 zsetinf2
453 assert_equal -inf [r zscore zsetinf3 key]
454 }
455
456 test "$cmd with NaN weights $encoding" {
457 r del zsetinf1 zsetinf2
458
459 r zadd zsetinf1 1.0 key
460 r zadd zsetinf2 1.0 key
461 assert_error "*weight value is not a double*" {
462 r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
463 }
464 }
465 }
466 }
467
468 basics ziplist
469 basics skiplist
470
471 test {ZINTERSTORE regression with two sets, intset+hashtable} {
472 r del seta setb setc
473 r sadd set1 a
474 r sadd set2 10
475 r zinterstore set3 2 set1 set2
476 } {0}
477
478 proc stressers {encoding} {
479 if {$encoding == "ziplist"} {
480 # Little extra to allow proper fuzzing in the sorting stresser
481 r config set zset-max-ziplist-entries 256
482 r config set zset-max-ziplist-value 64
483 set elements 128
484 } elseif {$encoding == "skiplist"} {
485 r config set zset-max-ziplist-entries 0
486 r config set zset-max-ziplist-value 0
487 set elements 1000
488 } else {
489 puts "Unknown sorted set encoding"
490 exit
491 }
492
493 test "ZSCORE - $encoding" {
494 r del zscoretest
495 set aux {}
496 for {set i 0} {$i < $elements} {incr i} {
497 set score [expr rand()]
498 lappend aux $score
499 r zadd zscoretest $score $i
500 }
501
502 assert_encoding $encoding zscoretest
503 for {set i 0} {$i < $elements} {incr i} {
504 assert_equal [lindex $aux $i] [r zscore zscoretest $i]
505 }
506 }
507
508 test "ZSCORE after a DEBUG RELOAD - $encoding" {
509 r del zscoretest
510 set aux {}
511 for {set i 0} {$i < $elements} {incr i} {
512 set score [expr rand()]
513 lappend aux $score
514 r zadd zscoretest $score $i
515 }
516
517 r debug reload
518 assert_encoding $encoding zscoretest
519 for {set i 0} {$i < $elements} {incr i} {
520 assert_equal [lindex $aux $i] [r zscore zscoretest $i]
521 }
522 }
523
524 test "ZSET sorting stresser - $encoding" {
525 set delta 0
526 for {set test 0} {$test < 2} {incr test} {
527 unset -nocomplain auxarray
528 array set auxarray {}
529 set auxlist {}
530 r del myzset
531 for {set i 0} {$i < $elements} {incr i} {
532 if {$test == 0} {
533 set score [expr rand()]
534 } else {
535 set score [expr int(rand()*10)]
536 }
537 set auxarray($i) $score
538 r zadd myzset $score $i
539 # Random update
540 if {[expr rand()] < .2} {
541 set j [expr int(rand()*1000)]
542 if {$test == 0} {
543 set score [expr rand()]
544 } else {
545 set score [expr int(rand()*10)]
546 }
547 set auxarray($j) $score
548 r zadd myzset $score $j
549 }
550 }
551 foreach {item score} [array get auxarray] {
552 lappend auxlist [list $score $item]
553 }
554 set sorted [lsort -command zlistAlikeSort $auxlist]
555 set auxlist {}
556 foreach x $sorted {
557 lappend auxlist [lindex $x 1]
558 }
559
560 assert_encoding $encoding myzset
561 set fromredis [r zrange myzset 0 -1]
562 set delta 0
563 for {set i 0} {$i < [llength $fromredis]} {incr i} {
564 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
565 incr delta
566 }
567 }
568 }
569 assert_equal 0 $delta
570 }
571
572 test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
573 set err {}
574 r del zset
575 for {set i 0} {$i < $elements} {incr i} {
576 r zadd zset [expr rand()] $i
577 }
578
579 assert_encoding $encoding zset
580 for {set i 0} {$i < 100} {incr i} {
581 set min [expr rand()]
582 set max [expr rand()]
583 if {$min > $max} {
584 set aux $min
585 set min $max
586 set max $aux
587 }
588 set low [r zrangebyscore zset -inf $min]
589 set ok [r zrangebyscore zset $min $max]
590 set high [r zrangebyscore zset $max +inf]
591 set lowx [r zrangebyscore zset -inf ($min]
592 set okx [r zrangebyscore zset ($min ($max]
593 set highx [r zrangebyscore zset ($max +inf]
594
595 if {[r zcount zset -inf $min] != [llength $low]} {
596 append err "Error, len does not match zcount\n"
597 }
598 if {[r zcount zset $min $max] != [llength $ok]} {
599 append err "Error, len does not match zcount\n"
600 }
601 if {[r zcount zset $max +inf] != [llength $high]} {
602 append err "Error, len does not match zcount\n"
603 }
604 if {[r zcount zset -inf ($min] != [llength $lowx]} {
605 append err "Error, len does not match zcount\n"
606 }
607 if {[r zcount zset ($min ($max] != [llength $okx]} {
608 append err "Error, len does not match zcount\n"
609 }
610 if {[r zcount zset ($max +inf] != [llength $highx]} {
611 append err "Error, len does not match zcount\n"
612 }
613
614 foreach x $low {
615 set score [r zscore zset $x]
616 if {$score > $min} {
617 append err "Error, score for $x is $score > $min\n"
618 }
619 }
620 foreach x $lowx {
621 set score [r zscore zset $x]
622 if {$score >= $min} {
623 append err "Error, score for $x is $score >= $min\n"
624 }
625 }
626 foreach x $ok {
627 set score [r zscore zset $x]
628 if {$score < $min || $score > $max} {
629 append err "Error, score for $x is $score outside $min-$max range\n"
630 }
631 }
632 foreach x $okx {
633 set score [r zscore zset $x]
634 if {$score <= $min || $score >= $max} {
635 append err "Error, score for $x is $score outside $min-$max open range\n"
636 }
637 }
638 foreach x $high {
639 set score [r zscore zset $x]
640 if {$score < $max} {
641 append err "Error, score for $x is $score < $max\n"
642 }
643 }
644 foreach x $highx {
645 set score [r zscore zset $x]
646 if {$score <= $max} {
647 append err "Error, score for $x is $score <= $max\n"
648 }
649 }
650 }
651 assert_equal {} $err
652 }
653
654 test "ZSETs skiplist implementation backlink consistency test - $encoding" {
655 set diff 0
656 for {set j 0} {$j < $elements} {incr j} {
657 r zadd myzset [expr rand()] "Element-$j"
658 r zrem myzset "Element-[expr int(rand()*$elements)]"
659 }
660
661 assert_encoding $encoding myzset
662 set l1 [r zrange myzset 0 -1]
663 set l2 [r zrevrange myzset 0 -1]
664 for {set j 0} {$j < [llength $l1]} {incr j} {
665 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
666 incr diff
667 }
668 }
669 assert_equal 0 $diff
670 }
671
672 test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
673 set err {}
674 r del myzset
675 for {set k 0} {$k < 2000} {incr k} {
676 set i [expr {$k % $elements}]
677 if {[expr rand()] < .2} {
678 r zrem myzset $i
679 } else {
680 set score [expr rand()]
681 r zadd myzset $score $i
682 assert_encoding $encoding myzset
683 }
684
685 set card [r zcard myzset]
686 if {$card > 0} {
687 set index [randomInt $card]
688 set ele [lindex [r zrange myzset $index $index] 0]
689 set rank [r zrank myzset $ele]
690 if {$rank != $index} {
691 set err "$ele RANK is wrong! ($rank != $index)"
692 break
693 }
694 }
695 }
696 assert_equal {} $err
697 }
698 }
699
700 tags {"slow"} {
701 stressers ziplist
702 stressers skiplist
703 }
704 }