]> git.saurik.com Git - redis.git/blobdiff - tests/unit/type/zset.tcl
check if the list encoding needs to be changed on LPUSHX, RPUSHX, LINSERT
[redis.git] / tests / unit / type / zset.tcl
index cbadb198a96ee286437f3bd883b21ade34a872b9..604597839a0efce0023546f1012f214dd3cf5676 100644 (file)
@@ -1,4 +1,4 @@
-start_server default.conf {} {
+start_server {tags {"zset"}} {
     test {ZSET basic ZADD and score update} {
         r zadd ztmp 10 x
         r zadd ztmp 20 y
@@ -162,85 +162,87 @@ start_server default.conf {} {
         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
@@ -296,12 +298,12 @@ start_server default.conf {} {
         list [r zremrangebyrank zset 1 3] [r zrange zset 0 -1]
     } {3 {a e}}
 
-    test {ZUNION against non-existing key doesn't set destination} {
+    test {ZUNIONSTORE against non-existing key doesn't set destination} {
       r del zseta
-      list [r zunion dst_key 1 zseta] [r exists dst_key]
+      list [r zunionstore dst_key 1 zseta] [r exists dst_key]
     } {0 0}
 
-    test {ZUNION basics} {
+    test {ZUNIONSTORE basics} {
         r del zseta zsetb zsetc
         r zadd zseta 1 a
         r zadd zseta 2 b
@@ -309,76 +311,113 @@ start_server default.conf {} {
         r zadd zsetb 1 b
         r zadd zsetb 2 c
         r zadd zsetb 3 d
-        list [r zunion zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
+        list [r zunionstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
     } {4 {a 1 b 3 d 3 c 5}}
 
-    test {ZUNION with weights} {
-        list [r zunion zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
+    test {ZUNIONSTORE with weights} {
+        list [r zunionstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
     } {4 {a 2 b 7 d 9 c 12}}
 
-    test {ZUNION with AGGREGATE MIN} {
-        list [r zunion zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
+       test {ZUNIONSTORE with a regular set and weights} {
+               r del seta
+               r sadd seta a
+               r sadd seta b
+               r sadd seta c
+        list [r zunionstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
+       } {4 {a 2 b 5 c 8 d 9}}
+
+    test {ZUNIONSTORE with AGGREGATE MIN} {
+        list [r zunionstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
     } {4 {a 1 b 1 c 2 d 3}}
 
-    test {ZUNION with AGGREGATE MAX} {
-        list [r zunion zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
+    test {ZUNIONSTORE with AGGREGATE MAX} {
+        list [r zunionstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
     } {4 {a 1 b 2 c 3 d 3}}
 
-    test {ZINTER basics} {
-        list [r zinter zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
+    test {ZINTERSTORE basics} {
+        list [r zinterstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
     } {2 {b 3 c 5}}
 
-    test {ZINTER with weights} {
-        list [r zinter zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
+    test {ZINTERSTORE with weights} {
+        list [r zinterstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
     } {2 {b 7 c 12}}
 
-    test {ZINTER with AGGREGATE MIN} {
-        list [r zinter zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
+       test {ZINTERSTORE with a regular set and weights} {
+               r del seta
+               r sadd seta a
+               r sadd seta b
+               r sadd seta c
+        list [r zinterstore zsetc 2 seta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
+       } {2 {b 5 c 8}}
+
+    test {ZINTERSTORE with AGGREGATE MIN} {
+        list [r zinterstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
     } {2 {b 1 c 2}}
 
-    test {ZINTER with AGGREGATE MAX} {
-        list [r zinter zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
+    test {ZINTERSTORE with AGGREGATE MAX} {
+        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)]"
-        }
-        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
+    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)]"
             }
-        }
-        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 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 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
+            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
-    } {}
+            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 ZINCRBY} {
+        set e {}
+        catch {r zincrby myzset nan abc} e
+        set _ $e
+    } {*Not A Number*}
+
+    test {ZINCRBY calls leading to Nan are refused} {
+        set e {}
+        r zincrby myzset +inf abc
+        catch {r zincrby myzset -inf abc} e
+        set _ $e
+    } {*Not A Number*}
 }