]> git.saurik.com Git - redis.git/blob - tests/unit/type/zset.tcl
Merge http://github.com/ngmoco/redis
[redis.git] / tests / unit / type / zset.tcl
1 start_server default.conf {} {
2 test {ZSET basic ZADD and score update} {
3 r zadd ztmp 10 x
4 r zadd ztmp 20 y
5 r zadd ztmp 30 z
6 set aux1 [r zrange ztmp 0 -1]
7 r zadd ztmp 1 y
8 set aux2 [r zrange ztmp 0 -1]
9 list $aux1 $aux2
10 } {{x y z} {y x z}}
11
12 test {ZCARD basics} {
13 r zcard ztmp
14 } {3}
15
16 test {ZCARD non existing key} {
17 r zcard ztmp-blabla
18 } {0}
19
20 test {ZRANK basics} {
21 r zadd zranktmp 10 x
22 r zadd zranktmp 20 y
23 r zadd zranktmp 30 z
24 list [r zrank zranktmp x] [r zrank zranktmp y] [r zrank zranktmp z]
25 } {0 1 2}
26
27 test {ZREVRANK basics} {
28 list [r zrevrank zranktmp x] [r zrevrank zranktmp y] [r zrevrank zranktmp z]
29 } {2 1 0}
30
31 test {ZRANK - after deletion} {
32 r zrem zranktmp y
33 list [r zrank zranktmp x] [r zrank zranktmp z]
34 } {0 1}
35
36 test {ZSCORE} {
37 set aux {}
38 set err {}
39 for {set i 0} {$i < 1000} {incr i} {
40 set score [expr rand()]
41 lappend aux $score
42 r zadd zscoretest $score $i
43 }
44 for {set i 0} {$i < 1000} {incr i} {
45 if {[r zscore zscoretest $i] != [lindex $aux $i]} {
46 set err "Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
47 break
48 }
49 }
50 set _ $err
51 } {}
52
53 test {ZSCORE after a DEBUG RELOAD} {
54 set aux {}
55 set err {}
56 r del zscoretest
57 for {set i 0} {$i < 1000} {incr i} {
58 set score [expr rand()]
59 lappend aux $score
60 r zadd zscoretest $score $i
61 }
62 r debug reload
63 for {set i 0} {$i < 1000} {incr i} {
64 if {[r zscore zscoretest $i] != [lindex $aux $i]} {
65 set err "Expected score was [lindex $aux $i] but got [r zscore zscoretest $i] for element $i"
66 break
67 }
68 }
69 set _ $err
70 } {}
71
72 test {ZRANGE and ZREVRANGE basics} {
73 list [r zrange ztmp 0 -1] [r zrevrange ztmp 0 -1] \
74 [r zrange ztmp 1 -1] [r zrevrange ztmp 1 -1]
75 } {{y x z} {z x y} {x z} {x y}}
76
77 test {ZRANGE WITHSCORES} {
78 r zrange ztmp 0 -1 withscores
79 } {y 1 x 10 z 30}
80
81 test {ZSETs stress tester - sorting is working well?} {
82 set delta 0
83 for {set test 0} {$test < 2} {incr test} {
84 unset -nocomplain auxarray
85 array set auxarray {}
86 set auxlist {}
87 r del myzset
88 for {set i 0} {$i < 1000} {incr i} {
89 if {$test == 0} {
90 set score [expr rand()]
91 } else {
92 set score [expr int(rand()*10)]
93 }
94 set auxarray($i) $score
95 r zadd myzset $score $i
96 # Random update
97 if {[expr rand()] < .2} {
98 set j [expr int(rand()*1000)]
99 if {$test == 0} {
100 set score [expr rand()]
101 } else {
102 set score [expr int(rand()*10)]
103 }
104 set auxarray($j) $score
105 r zadd myzset $score $j
106 }
107 }
108 foreach {item score} [array get auxarray] {
109 lappend auxlist [list $score $item]
110 }
111 set sorted [lsort -command zlistAlikeSort $auxlist]
112 set auxlist {}
113 foreach x $sorted {
114 lappend auxlist [lindex $x 1]
115 }
116 set fromredis [r zrange myzset 0 -1]
117 set delta 0
118 for {set i 0} {$i < [llength $fromredis]} {incr i} {
119 if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
120 incr delta
121 }
122 }
123 }
124 format $delta
125 } {0}
126
127 test {ZINCRBY - can create a new sorted set} {
128 r del zset
129 r zincrby zset 1 foo
130 list [r zrange zset 0 -1] [r zscore zset foo]
131 } {foo 1}
132
133 test {ZINCRBY - increment and decrement} {
134 r zincrby zset 2 foo
135 r zincrby zset 1 bar
136 set v1 [r zrange zset 0 -1]
137 r zincrby zset 10 bar
138 r zincrby zset -5 foo
139 r zincrby zset -5 bar
140 set v2 [r zrange zset 0 -1]
141 list $v1 $v2 [r zscore zset foo] [r zscore zset bar]
142 } {{bar foo} {foo bar} -2 6}
143
144 test {ZRANGEBYSCORE and ZCOUNT basics} {
145 r del zset
146 r zadd zset 1 a
147 r zadd zset 2 b
148 r zadd zset 3 c
149 r zadd zset 4 d
150 r zadd zset 5 e
151 list [r zrangebyscore zset 2 4] [r zrangebyscore zset (2 (4] \
152 [r zcount zset 2 4] [r zcount zset (2 (4]
153 } {{b c d} c 3 1}
154
155 test {ZRANGEBYSCORE withscores} {
156 r del zset
157 r zadd zset 1 a
158 r zadd zset 2 b
159 r zadd zset 3 c
160 r zadd zset 4 d
161 r zadd zset 5 e
162 r zrangebyscore zset 2 4 withscores
163 } {b 2 c 3 d 4}
164
165 test {ZRANGEBYSCORE fuzzy test, 100 ranges in 1000 elements sorted set} {
166 set err {}
167 r del zset
168 for {set i 0} {$i < 1000} {incr i} {
169 r zadd zset [expr rand()] $i
170 }
171 for {set i 0} {$i < 100} {incr i} {
172 set min [expr rand()]
173 set max [expr rand()]
174 if {$min > $max} {
175 set aux $min
176 set min $max
177 set max $aux
178 }
179 set low [r zrangebyscore zset -inf $min]
180 set ok [r zrangebyscore zset $min $max]
181 set high [r zrangebyscore zset $max +inf]
182 set lowx [r zrangebyscore zset -inf ($min]
183 set okx [r zrangebyscore zset ($min ($max]
184 set highx [r zrangebyscore zset ($max +inf]
185
186 if {[r zcount zset -inf $min] != [llength $low]} {
187 append err "Error, len does not match zcount\n"
188 }
189 if {[r zcount zset $min $max] != [llength $ok]} {
190 append err "Error, len does not match zcount\n"
191 }
192 if {[r zcount zset $max +inf] != [llength $high]} {
193 append err "Error, len does not match zcount\n"
194 }
195 if {[r zcount zset -inf ($min] != [llength $lowx]} {
196 append err "Error, len does not match zcount\n"
197 }
198 if {[r zcount zset ($min ($max] != [llength $okx]} {
199 append err "Error, len does not match zcount\n"
200 }
201 if {[r zcount zset ($max +inf] != [llength $highx]} {
202 append err "Error, len does not match zcount\n"
203 }
204
205 foreach x $low {
206 set score [r zscore zset $x]
207 if {$score > $min} {
208 append err "Error, score for $x is $score > $min\n"
209 }
210 }
211 foreach x $lowx {
212 set score [r zscore zset $x]
213 if {$score >= $min} {
214 append err "Error, score for $x is $score >= $min\n"
215 }
216 }
217 foreach x $ok {
218 set score [r zscore zset $x]
219 if {$score < $min || $score > $max} {
220 append err "Error, score for $x is $score outside $min-$max range\n"
221 }
222 }
223 foreach x $okx {
224 set score [r zscore zset $x]
225 if {$score <= $min || $score >= $max} {
226 append err "Error, score for $x is $score outside $min-$max open range\n"
227 }
228 }
229 foreach x $high {
230 set score [r zscore zset $x]
231 if {$score < $max} {
232 append err "Error, score for $x is $score < $max\n"
233 }
234 }
235 foreach x $highx {
236 set score [r zscore zset $x]
237 if {$score <= $max} {
238 append err "Error, score for $x is $score <= $max\n"
239 }
240 }
241 }
242 set _ $err
243 } {}
244
245 test {ZRANGEBYSCORE with LIMIT} {
246 r del zset
247 r zadd zset 1 a
248 r zadd zset 2 b
249 r zadd zset 3 c
250 r zadd zset 4 d
251 r zadd zset 5 e
252 list \
253 [r zrangebyscore zset 0 10 LIMIT 0 2] \
254 [r zrangebyscore zset 0 10 LIMIT 2 3] \
255 [r zrangebyscore zset 0 10 LIMIT 2 10] \
256 [r zrangebyscore zset 0 10 LIMIT 20 10]
257 } {{a b} {c d e} {c d e} {}}
258
259 test {ZRANGEBYSCORE with LIMIT and withscores} {
260 r del zset
261 r zadd zset 10 a
262 r zadd zset 20 b
263 r zadd zset 30 c
264 r zadd zset 40 d
265 r zadd zset 50 e
266 r zrangebyscore zset 20 50 LIMIT 2 3 withscores
267 } {d 40 e 50}
268
269 test {ZREMRANGEBYSCORE basics} {
270 r del zset
271 r zadd zset 1 a
272 r zadd zset 2 b
273 r zadd zset 3 c
274 r zadd zset 4 d
275 r zadd zset 5 e
276 list [r zremrangebyscore zset 2 4] [r zrange zset 0 -1]
277 } {3 {a e}}
278
279 test {ZREMRANGEBYSCORE from -inf to +inf} {
280 r del zset
281 r zadd zset 1 a
282 r zadd zset 2 b
283 r zadd zset 3 c
284 r zadd zset 4 d
285 r zadd zset 5 e
286 list [r zremrangebyscore zset -inf +inf] [r zrange zset 0 -1]
287 } {5 {}}
288
289 test {ZREMRANGEBYRANK basics} {
290 r del zset
291 r zadd zset 1 a
292 r zadd zset 2 b
293 r zadd zset 3 c
294 r zadd zset 4 d
295 r zadd zset 5 e
296 list [r zremrangebyrank zset 1 3] [r zrange zset 0 -1]
297 } {3 {a e}}
298
299 test {ZUNIONSTORE against non-existing key doesn't set destination} {
300 r del zseta
301 list [r zunionstore dst_key 1 zseta] [r exists dst_key]
302 } {0 0}
303
304 test {ZUNIONSTORE basics} {
305 r del zseta zsetb zsetc
306 r zadd zseta 1 a
307 r zadd zseta 2 b
308 r zadd zseta 3 c
309 r zadd zsetb 1 b
310 r zadd zsetb 2 c
311 r zadd zsetb 3 d
312 list [r zunionstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
313 } {4 {a 1 b 3 d 3 c 5}}
314
315 test {ZUNIONSTORE with weights} {
316 list [r zunionstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
317 } {4 {a 2 b 7 d 9 c 12}}
318
319 test {ZUNIONSTORE with AGGREGATE MIN} {
320 list [r zunionstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
321 } {4 {a 1 b 1 c 2 d 3}}
322
323 test {ZUNIONSTORE with AGGREGATE MAX} {
324 list [r zunionstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
325 } {4 {a 1 b 2 c 3 d 3}}
326
327 test {ZINTERSTORE basics} {
328 list [r zinterstore zsetc 2 zseta zsetb] [r zrange zsetc 0 -1 withscores]
329 } {2 {b 3 c 5}}
330
331 test {ZINTERSTORE with weights} {
332 list [r zinterstore zsetc 2 zseta zsetb weights 2 3] [r zrange zsetc 0 -1 withscores]
333 } {2 {b 7 c 12}}
334
335 test {ZINTERSTORE with AGGREGATE MIN} {
336 list [r zinterstore zsetc 2 zseta zsetb aggregate min] [r zrange zsetc 0 -1 withscores]
337 } {2 {b 1 c 2}}
338
339 test {ZINTERSTORE with AGGREGATE MAX} {
340 list [r zinterstore zsetc 2 zseta zsetb aggregate max] [r zrange zsetc 0 -1 withscores]
341 } {2 {b 2 c 3}}
342
343 test {ZSETs skiplist implementation backlink consistency test} {
344 set diff 0
345 set elements 10000
346 for {set j 0} {$j < $elements} {incr j} {
347 r zadd myzset [expr rand()] "Element-$j"
348 r zrem myzset "Element-[expr int(rand()*$elements)]"
349 }
350 set l1 [r zrange myzset 0 -1]
351 set l2 [r zrevrange myzset 0 -1]
352 for {set j 0} {$j < [llength $l1]} {incr j} {
353 if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
354 incr diff
355 }
356 }
357 format $diff
358 } {0}
359
360 test {ZSETs ZRANK augmented skip list stress testing} {
361 set err {}
362 r del myzset
363 for {set k 0} {$k < 10000} {incr k} {
364 set i [expr {$k%1000}]
365 if {[expr rand()] < .2} {
366 r zrem myzset $i
367 } else {
368 set score [expr rand()]
369 r zadd myzset $score $i
370 }
371 set card [r zcard myzset]
372 if {$card > 0} {
373 set index [randomInt $card]
374 set ele [lindex [r zrange myzset $index $index] 0]
375 set rank [r zrank myzset $ele]
376 if {$rank != $index} {
377 set err "$ele RANK is wrong! ($rank != $index)"
378 break
379 }
380 }
381 }
382 set _ $err
383 } {}
384 }