-start_server {} {
+start_server {tags {"zset"}} {
+ proc create_zset {key items} {
+ r del $key
+ foreach {score entry} $items {
+ r zadd $key $score $entry
+ }
+ }
+
test {ZSET basic ZADD and score update} {
r zadd ztmp 10 x
r zadd ztmp 20 y
r zcard ztmp-blabla
} {0}
+ test "ZRANGE basics" {
+ r del ztmp
+ r zadd ztmp 1 a
+ r zadd ztmp 2 b
+ r zadd ztmp 3 c
+ r zadd ztmp 4 d
+
+ assert_equal {a b c d} [r zrange ztmp 0 -1]
+ assert_equal {a b c} [r zrange ztmp 0 -2]
+ assert_equal {b c d} [r zrange ztmp 1 -1]
+ assert_equal {b c} [r zrange ztmp 1 -2]
+ assert_equal {c d} [r zrange ztmp -2 -1]
+ assert_equal {c} [r zrange ztmp -2 -2]
+
+ # out of range start index
+ assert_equal {a b c} [r zrange ztmp -5 2]
+ assert_equal {a b} [r zrange ztmp -5 1]
+ assert_equal {} [r zrange ztmp 5 -1]
+ assert_equal {} [r zrange ztmp 5 -2]
+
+ # out of range end index
+ assert_equal {a b c d} [r zrange ztmp 0 5]
+ assert_equal {b c d} [r zrange ztmp 1 5]
+ assert_equal {} [r zrange ztmp 0 -5]
+ assert_equal {} [r zrange ztmp 1 -5]
+
+ # withscores
+ assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
+ }
+
+ test "ZREVRANGE basics" {
+ r del ztmp
+ r zadd ztmp 1 a
+ r zadd ztmp 2 b
+ r zadd ztmp 3 c
+ r zadd ztmp 4 d
+
+ assert_equal {d c b a} [r zrevrange ztmp 0 -1]
+ assert_equal {d c b} [r zrevrange ztmp 0 -2]
+ assert_equal {c b a} [r zrevrange ztmp 1 -1]
+ assert_equal {c b} [r zrevrange ztmp 1 -2]
+ assert_equal {b a} [r zrevrange ztmp -2 -1]
+ assert_equal {b} [r zrevrange ztmp -2 -2]
+
+ # out of range start index
+ assert_equal {d c b} [r zrevrange ztmp -5 2]
+ assert_equal {d c} [r zrevrange ztmp -5 1]
+ assert_equal {} [r zrevrange ztmp 5 -1]
+ assert_equal {} [r zrevrange ztmp 5 -2]
+
+ # out of range end index
+ assert_equal {d c b a} [r zrevrange ztmp 0 5]
+ assert_equal {c b a} [r zrevrange ztmp 1 5]
+ assert_equal {} [r zrevrange ztmp 0 -5]
+ assert_equal {} [r zrevrange ztmp 1 -5]
+
+ # withscores
+ assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
+ }
+
test {ZRANK basics} {
r zadd zranktmp 10 x
r zadd zranktmp 20 y
set _ $err
} {}
- test {ZRANGE and ZREVRANGE basics} {
- list [r zrange ztmp 0 -1] [r zrevrange ztmp 0 -1] \
- [r zrange ztmp 1 -1] [r zrevrange ztmp 1 -1]
- } {{y x z} {z x y} {x z} {x y}}
-
- test {ZRANGE WITHSCORES} {
- r zrange ztmp 0 -1 withscores
- } {y 1 x 10 z 30}
-
test {ZSETs stress tester - sorting is working well?} {
set delta 0
for {set test 0} {$test < 2} {incr test} {
r zrangebyscore zset 2 4 withscores
} {b 2 c 3 d 4}
- test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
- set err {}
- r del zset
- for {set i 0} {$i < 1000} {incr i} {
- r zadd zset [expr rand()] $i
- }
- for {set i 0} {$i < 100} {incr i} {
- set min [expr rand()]
- set max [expr rand()]
- if {$min > $max} {
- set aux $min
- set min $max
- set max $aux
- }
- set low [r zrangebyscore zset -inf $min]
- set ok [r zrangebyscore zset $min $max]
- set high [r zrangebyscore zset $max +inf]
- set lowx [r zrangebyscore zset -inf ($min]
- set okx [r zrangebyscore zset ($min ($max]
- set highx [r zrangebyscore zset ($max +inf]
-
- if {[r zcount zset -inf $min] != [llength $low]} {
- append err "Error, len does not match zcount\n"
- }
- if {[r zcount zset $min $max] != [llength $ok]} {
- append err "Error, len does not match zcount\n"
- }
- if {[r zcount zset $max +inf] != [llength $high]} {
- append err "Error, len does not match zcount\n"
- }
- if {[r zcount zset -inf ($min] != [llength $lowx]} {
- append err "Error, len does not match zcount\n"
- }
- if {[r zcount zset ($min ($max] != [llength $okx]} {
- append err "Error, len does not match zcount\n"
- }
- if {[r zcount zset ($max +inf] != [llength $highx]} {
- append err "Error, len does not match zcount\n"
+ tags {"slow"} {
+ test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
+ set err {}
+ r del zset
+ for {set i 0} {$i < 1000} {incr i} {
+ r zadd zset [expr rand()] $i
}
+ for {set i 0} {$i < 100} {incr i} {
+ set min [expr rand()]
+ set max [expr rand()]
+ if {$min > $max} {
+ set aux $min
+ set min $max
+ set max $aux
+ }
+ set low [r zrangebyscore zset -inf $min]
+ set ok [r zrangebyscore zset $min $max]
+ set high [r zrangebyscore zset $max +inf]
+ set lowx [r zrangebyscore zset -inf ($min]
+ set okx [r zrangebyscore zset ($min ($max]
+ set highx [r zrangebyscore zset ($max +inf]
+
+ if {[r zcount zset -inf $min] != [llength $low]} {
+ append err "Error, len does not match zcount\n"
+ }
+ if {[r zcount zset $min $max] != [llength $ok]} {
+ append err "Error, len does not match zcount\n"
+ }
+ if {[r zcount zset $max +inf] != [llength $high]} {
+ append err "Error, len does not match zcount\n"
+ }
+ if {[r zcount zset -inf ($min] != [llength $lowx]} {
+ append err "Error, len does not match zcount\n"
+ }
+ if {[r zcount zset ($min ($max] != [llength $okx]} {
+ append err "Error, len does not match zcount\n"
+ }
+ if {[r zcount zset ($max +inf] != [llength $highx]} {
+ append err "Error, len does not match zcount\n"
+ }
- foreach x $low {
- set score [r zscore zset $x]
- if {$score > $min} {
- append err "Error, score for $x is $score > $min\n"
+ foreach x $low {
+ set score [r zscore zset $x]
+ if {$score > $min} {
+ append err "Error, score for $x is $score > $min\n"
+ }
}
- }
- foreach x $lowx {
- set score [r zscore zset $x]
- if {$score >= $min} {
- append err "Error, score for $x is $score >= $min\n"
+ foreach x $lowx {
+ set score [r zscore zset $x]
+ if {$score >= $min} {
+ append err "Error, score for $x is $score >= $min\n"
+ }
}
- }
- foreach x $ok {
- set score [r zscore zset $x]
- if {$score < $min || $score > $max} {
- append err "Error, score for $x is $score outside $min-$max range\n"
+ foreach x $ok {
+ set score [r zscore zset $x]
+ if {$score < $min || $score > $max} {
+ append err "Error, score for $x is $score outside $min-$max range\n"
+ }
}
- }
- foreach x $okx {
- set score [r zscore zset $x]
- if {$score <= $min || $score >= $max} {
- append err "Error, score for $x is $score outside $min-$max open range\n"
+ foreach x $okx {
+ set score [r zscore zset $x]
+ if {$score <= $min || $score >= $max} {
+ append err "Error, score for $x is $score outside $min-$max open range\n"
+ }
}
- }
- foreach x $high {
- set score [r zscore zset $x]
- if {$score < $max} {
- append err "Error, score for $x is $score < $max\n"
+ foreach x $high {
+ set score [r zscore zset $x]
+ if {$score < $max} {
+ append err "Error, score for $x is $score < $max\n"
+ }
}
- }
- foreach x $highx {
- set score [r zscore zset $x]
- if {$score <= $max} {
- append err "Error, score for $x is $score <= $max\n"
+ foreach x $highx {
+ set score [r zscore zset $x]
+ if {$score <= $max} {
+ append err "Error, score for $x is $score <= $max\n"
+ }
}
}
- }
- set _ $err
- } {}
+ set _ $err
+ } {}
+ }
test {ZRANGEBYSCORE with LIMIT} {
r del zset
list [r zremrangebyscore zset -inf +inf] [r zrange zset 0 -1]
} {5 {}}
- test {ZREMRANGEBYRANK basics} {
- r del zset
- r zadd zset 1 a
- r zadd zset 2 b
- r zadd zset 3 c
- r zadd zset 4 d
- r zadd zset 5 e
- list [r zremrangebyrank zset 1 3] [r zrange zset 0 -1]
- } {3 {a e}}
+ test "ZREMRANGEBYRANK basics" {
+ proc remrangebyrank {min max} {
+ create_zset zset {1 a 2 b 3 c 4 d 5 e}
+ r zremrangebyrank zset $min $max
+ }
+
+ # inner range
+ assert_equal 3 [remrangebyrank 1 3]
+ assert_equal {a e} [r zrange zset 0 -1]
+
+ # start underflow
+ assert_equal 1 [remrangebyrank -10 0]
+ assert_equal {b c d e} [r zrange zset 0 -1]
+
+ # start overflow
+ assert_equal 0 [remrangebyrank 10 -1]
+ assert_equal {a b c d e} [r zrange zset 0 -1]
+
+ # end underflow
+ assert_equal 0 [remrangebyrank 0 -10]
+ assert_equal {a b c d e} [r zrange zset 0 -1]
+
+ # end overflow
+ assert_equal 5 [remrangebyrank 0 10]
+ assert_equal {} [r zrange zset 0 -1]
+ }
test {ZUNIONSTORE against non-existing key doesn't set destination} {
r del zseta
list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
} {2 {b 2 c 3}}
- test {ZSETs skiplist implementation backlink consistency test} {
- set diff 0
- set elements 10000
- for {set j 0} {$j < $elements} {incr j} {
- r zadd myzset [expr rand()] "Element-$j"
- r zrem myzset "Element-[expr int(rand()*$elements)]"
+ foreach cmd {ZUNIONSTORE ZINTERSTORE} {
+ test "$cmd with +inf/-inf scores" {
+ r del zsetinf1 zsetinf2
+
+ r zadd zsetinf1 +inf key
+ r zadd zsetinf2 +inf key
+ r $cmd zsetinf3 2 zsetinf1 zsetinf2
+ assert_equal inf [r zscore zsetinf3 key]
+
+ r zadd zsetinf1 -inf key
+ r zadd zsetinf2 +inf key
+ r $cmd zsetinf3 2 zsetinf1 zsetinf2
+ assert_equal 0 [r zscore zsetinf3 key]
+
+ r zadd zsetinf1 +inf key
+ r zadd zsetinf2 -inf key
+ r $cmd zsetinf3 2 zsetinf1 zsetinf2
+ assert_equal 0 [r zscore zsetinf3 key]
+
+ r zadd zsetinf1 -inf key
+ r zadd zsetinf2 -inf key
+ r $cmd zsetinf3 2 zsetinf1 zsetinf2
+ assert_equal -inf [r zscore zsetinf3 key]
}
- set l1 [r zrange myzset 0 -1]
- set l2 [r zrevrange myzset 0 -1]
- for {set j 0} {$j < [llength $l1]} {incr j} {
- if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
- incr diff
+
+ test "$cmd with NaN weights" {
+ r del zsetinf1 zsetinf2
+
+ r zadd zsetinf1 1.0 key
+ r zadd zsetinf2 1.0 key
+ assert_error "*weight value is not a double*" {
+ r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
}
}
- format $diff
- } {0}
-
- test {ZSETs ZRANK augmented skip list stress testing} {
- set err {}
- r del myzset
- for {set k 0} {$k < 10000} {incr k} {
- set i [expr {$k%1000}]
- if {[expr rand()] < .2} {
- r zrem myzset $i
- } else {
- set score [expr rand()]
- r zadd myzset $score $i
+ }
+
+ tags {"slow"} {
+ test {ZSETs skiplist implementation backlink consistency test} {
+ set diff 0
+ set elements 10000
+ for {set j 0} {$j < $elements} {incr j} {
+ r zadd myzset [expr rand()] "Element-$j"
+ r zrem myzset "Element-[expr int(rand()*$elements)]"
}
- set card [r zcard myzset]
- if {$card > 0} {
- set index [randomInt $card]
- set ele [lindex [r zrange myzset $index $index] 0]
- set rank [r zrank myzset $ele]
- if {$rank != $index} {
- set err "$ele RANK is wrong! ($rank != $index)"
- break
+ set l1 [r zrange myzset 0 -1]
+ set l2 [r zrevrange myzset 0 -1]
+ for {set j 0} {$j < [llength $l1]} {incr j} {
+ if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
+ incr diff
}
}
- }
- set _ $err
- } {}
+ format $diff
+ } {0}
+
+ test {ZSETs ZRANK augmented skip list stress testing} {
+ set err {}
+ r del myzset
+ for {set k 0} {$k < 10000} {incr k} {
+ set i [expr {$k%1000}]
+ if {[expr rand()] < .2} {
+ r zrem myzset $i
+ } else {
+ set score [expr rand()]
+ r zadd myzset $score $i
+ }
+ set card [r zcard myzset]
+ if {$card > 0} {
+ set index [randomInt $card]
+ set ele [lindex [r zrange myzset $index $index] 0]
+ set rank [r zrank myzset $ele]
+ if {$rank != $index} {
+ set err "$ele RANK is wrong! ($rank != $index)"
+ break
+ }
+ }
+ }
+ set _ $err
+ } {}
+ }
- test {ZSET element can't be set to nan with ZADD} {
- set e {}
- catch {r zadd myzset nan abc} e
- set _ $e
- } {*Not A Number*}
+ test {ZSET element can't be set to NaN with ZADD} {
+ assert_error "*not a double*" {r zadd myzset nan abc}
+ }
- test {ZSET element can't be set to nan with ZINCRBY} {
- set e {}
- catch {r zincrby myzset nan abc} e
- set _ $e
- } {*Not A Number*}
+ test {ZSET element can't be set to NaN with ZINCRBY} {
+ assert_error "*not a double*" {r zadd myzset nan abc}
+ }
- test {ZINCRBY calls leading to Nan are refused} {
- set e {}
+ test {ZINCRBY calls leading to NaN result in error} {
r zincrby myzset +inf abc
- catch {r zincrby myzset -inf abc} e
- set _ $e
- } {*Not A Number*}
+ assert_error "*NaN*" {r zincrby myzset -inf abc}
+ }
}