X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/9ec4ea20a71f075a7fc9a2e1d613c339353ee553..c0de45924c4033a8650b627e75b7fd6396c52187:/tests/unit/type/zset.tcl diff --git a/tests/unit/type/zset.tcl b/tests/unit/type/zset.tcl index 31604412..806f4c88 100644 --- a/tests/unit/type/zset.tcl +++ b/tests/unit/type/zset.tcl @@ -10,7 +10,7 @@ start_server {tags {"zset"}} { if {$encoding == "ziplist"} { r config set zset-max-ziplist-entries 128 r config set zset-max-ziplist-value 64 - } elseif {$encoding == "raw"} { + } elseif {$encoding == "skiplist"} { r config set zset-max-ziplist-entries 0 r config set zset-max-ziplist-value 0 } else { @@ -36,11 +36,11 @@ start_server {tags {"zset"}} { } test "ZSET element can't be set to NaN with ZADD - $encoding" { - assert_error "*not a double*" {r zadd myzset nan abc} + assert_error "*not*float*" {r zadd myzset nan abc} } test "ZSET element can't be set to NaN with ZINCRBY" { - assert_error "*not a double*" {r zadd myzset nan abc} + assert_error "*not*float*" {r zadd myzset nan abc} } test "ZINCRBY calls leading to NaN result in error" { @@ -48,11 +48,66 @@ start_server {tags {"zset"}} { assert_error "*NaN*" {r zincrby myzset -inf abc} } + test {ZADD - Variadic version base case} { + r del myzset + list [r zadd myzset 10 a 20 b 30 c] [r zrange myzset 0 -1 withscores] + } {3 {a 10 b 20 c 30}} + + test {ZADD - Return value is the number of actually added items} { + list [r zadd myzset 5 x 20 b 30 c] [r zrange myzset 0 -1 withscores] + } {1 {x 5 a 10 b 20 c 30}} + + test {ZADD - Variadic version does not add nothing on single parsing err} { + r del myzset + catch {r zadd myzset 10 a 20 b 30.badscore c} e + assert_match {*ERR*not*float*} $e + r exists myzset + } {0} + + test {ZADD - Variadic version will raise error on missing arg} { + r del myzset + catch {r zadd myzset 10 a 20 b 30 c 40} e + assert_match {*ERR*syntax*} $e + } + + test {ZINCRBY does not work variadic even if shares ZADD implementation} { + r del myzset + catch {r zincrby myzset 10 a 20 b 30 c} e + assert_match {*ERR*wrong*number*arg*} $e + } + test "ZCARD basics - $encoding" { assert_equal 3 [r zcard ztmp] assert_equal 0 [r zcard zdoesntexist] } + test "ZREM removes key after last element is removed" { + r del ztmp + r zadd ztmp 10 x + r zadd ztmp 20 y + + assert_equal 1 [r exists ztmp] + assert_equal 0 [r zrem ztmp z] + assert_equal 1 [r zrem ztmp y] + assert_equal 1 [r zrem ztmp x] + assert_equal 0 [r exists ztmp] + } + + test "ZREM variadic version" { + r del ztmp + r zadd ztmp 10 a 20 b 30 c + assert_equal 2 [r zrem ztmp x y a b k] + assert_equal 0 [r zrem ztmp foo bar] + assert_equal 1 [r zrem ztmp c] + r exists ztmp + } {0} + + test "ZREM variadic version -- remove elements after key deletion" { + r del ztmp + r zadd ztmp 10 a 20 b 30 c + r zrem ztmp a b c d e f g + } {3} + test "ZRANGE basics - $encoding" { r del ztmp r zadd ztmp 1 a @@ -236,14 +291,15 @@ start_server {tags {"zset"}} { } test "ZRANGEBYSCORE with non-value min or max" { - assert_error "*not a double*" {r zrangebyscore fooz str 1} - assert_error "*not a double*" {r zrangebyscore fooz 1 str} - assert_error "*not a double*" {r zrangebyscore fooz 1 NaN} + assert_error "*not*float*" {r zrangebyscore fooz str 1} + assert_error "*not*float*" {r zrangebyscore fooz 1 str} + assert_error "*not*float*" {r zrangebyscore fooz 1 NaN} } test "ZREMRANGEBYSCORE basics" { proc remrangebyscore {min max} { create_zset zset {1 a 2 b 3 c 4 d 5 e} + assert_equal 1 [r exists zset] r zremrangebyscore zset $min $max } @@ -290,17 +346,22 @@ start_server {tags {"zset"}} { # exclusive min and max assert_equal 3 [remrangebyscore (1 (5] assert_equal {a e} [r zrange zset 0 -1] + + # destroy when empty + assert_equal 5 [remrangebyscore 1 5] + assert_equal 0 [r exists zset] } test "ZREMRANGEBYSCORE with non-value min or max" { - assert_error "*not a double*" {r zremrangebyscore fooz str 1} - assert_error "*not a double*" {r zremrangebyscore fooz 1 str} - assert_error "*not a double*" {r zremrangebyscore fooz 1 NaN} + assert_error "*not*float*" {r zremrangebyscore fooz str 1} + assert_error "*not*float*" {r zremrangebyscore fooz 1 str} + assert_error "*not*float*" {r zremrangebyscore fooz 1 NaN} } test "ZREMRANGEBYRANK basics" { proc remrangebyrank {min max} { create_zset zset {1 a 2 b 3 c 4 d 5 e} + assert_equal 1 [r exists zset] r zremrangebyrank zset $min $max } @@ -323,6 +384,10 @@ start_server {tags {"zset"}} { # end overflow assert_equal 5 [remrangebyrank 0 10] assert_equal {} [r zrange zset 0 -1] + + # destroy when empty + assert_equal 5 [remrangebyrank 0 4] + assert_equal 0 [r exists zset] } test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" { @@ -331,6 +396,14 @@ start_server {tags {"zset"}} { assert_equal 0 [r exists dst_key] } + test "ZUNIONSTORE with empty set - $encoding" { + r del zseta zsetb + r zadd zseta 1 a + r zadd zseta 2 b + r zunionstore zsetc 2 zseta zsetb + r zrange zsetc 0 -1 withscores + } {a 1 b 2} + test "ZUNIONSTORE basics - $encoding" { r del zseta zsetb zsetc r zadd zseta 1 a @@ -428,7 +501,7 @@ start_server {tags {"zset"}} { r zadd zsetinf1 1.0 key r zadd zsetinf2 1.0 key - assert_error "*weight value is not a double*" { + assert_error "*weight*not*float*" { r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan } } @@ -436,7 +509,28 @@ start_server {tags {"zset"}} { } basics ziplist - basics raw + basics skiplist + + test {ZINTERSTORE regression with two sets, intset+hashtable} { + r del seta setb setc + r sadd set1 a + r sadd set2 10 + r zinterstore set3 2 set1 set2 + } {0} + + test {ZUNIONSTORE regression, should not create NaN in scores} { + r zadd z -inf neginf + r zunionstore out 1 z weights 0 + r zrange out 0 -1 withscores + } {neginf 0} + + test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} { + r sadd one 100 101 102 103 + r sadd two 100 200 201 202 + r zadd three 1 500 1 501 1 502 1 503 1 100 + r zinterstore to_here 3 one two three WEIGHTS 0 0 1 + r zrange to_here 0 -1 + } {100} proc stressers {encoding} { if {$encoding == "ziplist"} { @@ -444,10 +538,10 @@ start_server {tags {"zset"}} { r config set zset-max-ziplist-entries 256 r config set zset-max-ziplist-value 64 set elements 128 - } elseif {$encoding == "raw"} { + } elseif {$encoding == "skiplist"} { r config set zset-max-ziplist-entries 0 r config set zset-max-ziplist-value 0 - set elements 1000 + if {$::accurate} {set elements 1000} else {set elements 100} } else { puts "Unknown sorted set encoding" exit @@ -662,6 +756,6 @@ start_server {tags {"zset"}} { tags {"slow"} { stressers ziplist - stressers raw + stressers skiplist } }