zset.tcl 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. proc basics {encoding} {
  9. if {$encoding == "ziplist"} {
  10. r config set zset-max-ziplist-entries 128
  11. r config set zset-max-ziplist-value 64
  12. } elseif {$encoding == "skiplist"} {
  13. r config set zset-max-ziplist-entries 0
  14. r config set zset-max-ziplist-value 0
  15. } else {
  16. puts "Unknown sorted set encoding"
  17. exit
  18. }
  19. test "Check encoding - $encoding" {
  20. r del ztmp
  21. r zadd ztmp 10 x
  22. assert_encoding $encoding ztmp
  23. }
  24. test "ZSET basic ZADD and score update - $encoding" {
  25. r del ztmp
  26. r zadd ztmp 10 x
  27. r zadd ztmp 20 y
  28. r zadd ztmp 30 z
  29. assert_equal {x y z} [r zrange ztmp 0 -1]
  30. r zadd ztmp 1 y
  31. assert_equal {y x z} [r zrange ztmp 0 -1]
  32. }
  33. test "ZSET element can't be set to NaN with ZADD - $encoding" {
  34. assert_error "*not*float*" {r zadd myzset nan abc}
  35. }
  36. test "ZSET element can't be set to NaN with ZINCRBY" {
  37. assert_error "*not*float*" {r zadd myzset nan abc}
  38. }
  39. test "ZADD with options syntax error with incomplete pair" {
  40. r del ztmp
  41. catch {r zadd ztmp xx 10 x 20} err
  42. set err
  43. } {ERR*}
  44. test "ZADD XX option without key - $encoding" {
  45. r del ztmp
  46. assert {[r zadd ztmp xx 10 x] == 0}
  47. assert {[r type ztmp] eq {none}}
  48. }
  49. test "ZADD XX existing key - $encoding" {
  50. r del ztmp
  51. r zadd ztmp 10 x
  52. assert {[r zadd ztmp xx 20 y] == 0}
  53. assert {[r zcard ztmp] == 1}
  54. }
  55. test "ZADD XX returns the number of elements actually added" {
  56. r del ztmp
  57. r zadd ztmp 10 x
  58. set retval [r zadd ztmp 10 x 20 y 30 z]
  59. assert {$retval == 2}
  60. }
  61. test "ZADD XX updates existing elements score" {
  62. r del ztmp
  63. r zadd ztmp 10 x 20 y 30 z
  64. r zadd ztmp xx 5 foo 11 x 21 y 40 zap
  65. assert {[r zcard ztmp] == 3}
  66. assert {[r zscore ztmp x] == 11}
  67. assert {[r zscore ztmp y] == 21}
  68. }
  69. test "ZADD XX and NX are not compatible" {
  70. r del ztmp
  71. catch {r zadd ztmp xx nx 10 x} err
  72. set err
  73. } {ERR*}
  74. test "ZADD NX with non exisitng key" {
  75. r del ztmp
  76. r zadd ztmp nx 10 x 20 y 30 z
  77. assert {[r zcard ztmp] == 3}
  78. }
  79. test "ZADD NX only add new elements without updating old ones" {
  80. r del ztmp
  81. r zadd ztmp 10 x 20 y 30 z
  82. assert {[r zadd ztmp nx 11 x 21 y 100 a 200 b] == 2}
  83. assert {[r zscore ztmp x] == 10}
  84. assert {[r zscore ztmp y] == 20}
  85. assert {[r zscore ztmp a] == 100}
  86. assert {[r zscore ztmp b] == 200}
  87. }
  88. test "ZADD INCR works like ZINCRBY" {
  89. r del ztmp
  90. r zadd ztmp 10 x 20 y 30 z
  91. r zadd ztmp INCR 15 x
  92. assert {[r zscore ztmp x] == 25}
  93. }
  94. test "ZADD INCR works with a single score-elemenet pair" {
  95. r del ztmp
  96. r zadd ztmp 10 x 20 y 30 z
  97. catch {r zadd ztmp INCR 15 x 10 y} err
  98. set err
  99. } {ERR*}
  100. test "ZADD CH option changes return value to all changed elements" {
  101. r del ztmp
  102. r zadd ztmp 10 x 20 y 30 z
  103. assert {[r zadd ztmp 11 x 21 y 30 z] == 0}
  104. assert {[r zadd ztmp ch 12 x 22 y 30 z] == 2}
  105. }
  106. test "ZINCRBY calls leading to NaN result in error" {
  107. r zincrby myzset +inf abc
  108. assert_error "*NaN*" {r zincrby myzset -inf abc}
  109. }
  110. test {ZADD - Variadic version base case} {
  111. r del myzset
  112. list [r zadd myzset 10 a 20 b 30 c] [r zrange myzset 0 -1 withscores]
  113. } {3 {a 10 b 20 c 30}}
  114. test {ZADD - Return value is the number of actually added items} {
  115. list [r zadd myzset 5 x 20 b 30 c] [r zrange myzset 0 -1 withscores]
  116. } {1 {x 5 a 10 b 20 c 30}}
  117. test {ZADD - Variadic version does not add nothing on single parsing err} {
  118. r del myzset
  119. catch {r zadd myzset 10 a 20 b 30.badscore c} e
  120. assert_match {*ERR*not*float*} $e
  121. r exists myzset
  122. } {0}
  123. test {ZADD - Variadic version will raise error on missing arg} {
  124. r del myzset
  125. catch {r zadd myzset 10 a 20 b 30 c 40} e
  126. assert_match {*ERR*syntax*} $e
  127. }
  128. test {ZINCRBY does not work variadic even if shares ZADD implementation} {
  129. r del myzset
  130. catch {r zincrby myzset 10 a 20 b 30 c} e
  131. assert_match {*ERR*wrong*number*arg*} $e
  132. }
  133. test "ZCARD basics - $encoding" {
  134. r del ztmp
  135. r zadd ztmp 10 a 20 b 30 c
  136. assert_equal 3 [r zcard ztmp]
  137. assert_equal 0 [r zcard zdoesntexist]
  138. }
  139. test "ZREM removes key after last element is removed" {
  140. r del ztmp
  141. r zadd ztmp 10 x
  142. r zadd ztmp 20 y
  143. assert_equal 1 [r exists ztmp]
  144. assert_equal 0 [r zrem ztmp z]
  145. assert_equal 1 [r zrem ztmp y]
  146. assert_equal 1 [r zrem ztmp x]
  147. assert_equal 0 [r exists ztmp]
  148. }
  149. test "ZREM variadic version" {
  150. r del ztmp
  151. r zadd ztmp 10 a 20 b 30 c
  152. assert_equal 2 [r zrem ztmp x y a b k]
  153. assert_equal 0 [r zrem ztmp foo bar]
  154. assert_equal 1 [r zrem ztmp c]
  155. r exists ztmp
  156. } {0}
  157. test "ZREM variadic version -- remove elements after key deletion" {
  158. r del ztmp
  159. r zadd ztmp 10 a 20 b 30 c
  160. r zrem ztmp a b c d e f g
  161. } {3}
  162. test "ZRANGE basics - $encoding" {
  163. r del ztmp
  164. r zadd ztmp 1 a
  165. r zadd ztmp 2 b
  166. r zadd ztmp 3 c
  167. r zadd ztmp 4 d
  168. assert_equal {a b c d} [r zrange ztmp 0 -1]
  169. assert_equal {a b c} [r zrange ztmp 0 -2]
  170. assert_equal {b c d} [r zrange ztmp 1 -1]
  171. assert_equal {b c} [r zrange ztmp 1 -2]
  172. assert_equal {c d} [r zrange ztmp -2 -1]
  173. assert_equal {c} [r zrange ztmp -2 -2]
  174. # out of range start index
  175. assert_equal {a b c} [r zrange ztmp -5 2]
  176. assert_equal {a b} [r zrange ztmp -5 1]
  177. assert_equal {} [r zrange ztmp 5 -1]
  178. assert_equal {} [r zrange ztmp 5 -2]
  179. # out of range end index
  180. assert_equal {a b c d} [r zrange ztmp 0 5]
  181. assert_equal {b c d} [r zrange ztmp 1 5]
  182. assert_equal {} [r zrange ztmp 0 -5]
  183. assert_equal {} [r zrange ztmp 1 -5]
  184. # withscores
  185. assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
  186. }
  187. test "ZREVRANGE basics - $encoding" {
  188. r del ztmp
  189. r zadd ztmp 1 a
  190. r zadd ztmp 2 b
  191. r zadd ztmp 3 c
  192. r zadd ztmp 4 d
  193. assert_equal {d c b a} [r zrevrange ztmp 0 -1]
  194. assert_equal {d c b} [r zrevrange ztmp 0 -2]
  195. assert_equal {c b a} [r zrevrange ztmp 1 -1]
  196. assert_equal {c b} [r zrevrange ztmp 1 -2]
  197. assert_equal {b a} [r zrevrange ztmp -2 -1]
  198. assert_equal {b} [r zrevrange ztmp -2 -2]
  199. # out of range start index
  200. assert_equal {d c b} [r zrevrange ztmp -5 2]
  201. assert_equal {d c} [r zrevrange ztmp -5 1]
  202. assert_equal {} [r zrevrange ztmp 5 -1]
  203. assert_equal {} [r zrevrange ztmp 5 -2]
  204. # out of range end index
  205. assert_equal {d c b a} [r zrevrange ztmp 0 5]
  206. assert_equal {c b a} [r zrevrange ztmp 1 5]
  207. assert_equal {} [r zrevrange ztmp 0 -5]
  208. assert_equal {} [r zrevrange ztmp 1 -5]
  209. # withscores
  210. assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
  211. }
  212. test "ZRANK/ZREVRANK basics - $encoding" {
  213. r del zranktmp
  214. r zadd zranktmp 10 x
  215. r zadd zranktmp 20 y
  216. r zadd zranktmp 30 z
  217. assert_equal 0 [r zrank zranktmp x]
  218. assert_equal 1 [r zrank zranktmp y]
  219. assert_equal 2 [r zrank zranktmp z]
  220. assert_equal "" [r zrank zranktmp foo]
  221. assert_equal 2 [r zrevrank zranktmp x]
  222. assert_equal 1 [r zrevrank zranktmp y]
  223. assert_equal 0 [r zrevrank zranktmp z]
  224. assert_equal "" [r zrevrank zranktmp foo]
  225. }
  226. test "ZRANK - after deletion - $encoding" {
  227. r zrem zranktmp y
  228. assert_equal 0 [r zrank zranktmp x]
  229. assert_equal 1 [r zrank zranktmp z]
  230. }
  231. test "ZINCRBY - can create a new sorted set - $encoding" {
  232. r del zset
  233. r zincrby zset 1 foo
  234. assert_equal {foo} [r zrange zset 0 -1]
  235. assert_equal 1 [r zscore zset foo]
  236. }
  237. test "ZINCRBY - increment and decrement - $encoding" {
  238. r zincrby zset 2 foo
  239. r zincrby zset 1 bar
  240. assert_equal {bar foo} [r zrange zset 0 -1]
  241. r zincrby zset 10 bar
  242. r zincrby zset -5 foo
  243. r zincrby zset -5 bar
  244. assert_equal {foo bar} [r zrange zset 0 -1]
  245. assert_equal -2 [r zscore zset foo]
  246. assert_equal 6 [r zscore zset bar]
  247. }
  248. test "ZINCRBY return value" {
  249. r del ztmp
  250. set retval [r zincrby ztmp 1.0 x]
  251. assert {$retval == 1.0}
  252. }
  253. proc create_default_zset {} {
  254. create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g}
  255. }
  256. test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
  257. create_default_zset
  258. # inclusive range
  259. assert_equal {a b c} [r zrangebyscore zset -inf 2]
  260. assert_equal {b c d} [r zrangebyscore zset 0 3]
  261. assert_equal {d e f} [r zrangebyscore zset 3 6]
  262. assert_equal {e f g} [r zrangebyscore zset 4 +inf]
  263. assert_equal {c b a} [r zrevrangebyscore zset 2 -inf]
  264. assert_equal {d c b} [r zrevrangebyscore zset 3 0]
  265. assert_equal {f e d} [r zrevrangebyscore zset 6 3]
  266. assert_equal {g f e} [r zrevrangebyscore zset +inf 4]
  267. assert_equal 3 [r zcount zset 0 3]
  268. # exclusive range
  269. assert_equal {b} [r zrangebyscore zset (-inf (2]
  270. assert_equal {b c} [r zrangebyscore zset (0 (3]
  271. assert_equal {e f} [r zrangebyscore zset (3 (6]
  272. assert_equal {f} [r zrangebyscore zset (4 (+inf]
  273. assert_equal {b} [r zrevrangebyscore zset (2 (-inf]
  274. assert_equal {c b} [r zrevrangebyscore zset (3 (0]
  275. assert_equal {f e} [r zrevrangebyscore zset (6 (3]
  276. assert_equal {f} [r zrevrangebyscore zset (+inf (4]
  277. assert_equal 2 [r zcount zset (0 (3]
  278. # test empty ranges
  279. r zrem zset a
  280. r zrem zset g
  281. # inclusive
  282. assert_equal {} [r zrangebyscore zset 4 2]
  283. assert_equal {} [r zrangebyscore zset 6 +inf]
  284. assert_equal {} [r zrangebyscore zset -inf -6]
  285. assert_equal {} [r zrevrangebyscore zset +inf 6]
  286. assert_equal {} [r zrevrangebyscore zset -6 -inf]
  287. # exclusive
  288. assert_equal {} [r zrangebyscore zset (4 (2]
  289. assert_equal {} [r zrangebyscore zset 2 (2]
  290. assert_equal {} [r zrangebyscore zset (2 2]
  291. assert_equal {} [r zrangebyscore zset (6 (+inf]
  292. assert_equal {} [r zrangebyscore zset (-inf (-6]
  293. assert_equal {} [r zrevrangebyscore zset (+inf (6]
  294. assert_equal {} [r zrevrangebyscore zset (-6 (-inf]
  295. # empty inner range
  296. assert_equal {} [r zrangebyscore zset 2.4 2.6]
  297. assert_equal {} [r zrangebyscore zset (2.4 2.6]
  298. assert_equal {} [r zrangebyscore zset 2.4 (2.6]
  299. assert_equal {} [r zrangebyscore zset (2.4 (2.6]
  300. }
  301. test "ZRANGEBYSCORE with WITHSCORES" {
  302. create_default_zset
  303. assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores]
  304. assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores]
  305. }
  306. test "ZRANGEBYSCORE with LIMIT" {
  307. create_default_zset
  308. assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2]
  309. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3]
  310. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10]
  311. assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10]
  312. assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2]
  313. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3]
  314. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10]
  315. assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10]
  316. }
  317. test "ZRANGEBYSCORE with LIMIT and WITHSCORES" {
  318. create_default_zset
  319. assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES]
  320. assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES]
  321. }
  322. test "ZRANGEBYSCORE with non-value min or max" {
  323. assert_error "*not*float*" {r zrangebyscore fooz str 1}
  324. assert_error "*not*float*" {r zrangebyscore fooz 1 str}
  325. assert_error "*not*float*" {r zrangebyscore fooz 1 NaN}
  326. }
  327. proc create_default_lex_zset {} {
  328. create_zset zset {0 alpha 0 bar 0 cool 0 down
  329. 0 elephant 0 foo 0 great 0 hill
  330. 0 omega}
  331. }
  332. test "ZRANGEBYLEX/ZREVRANGEBYLEX/ZCOUNT basics" {
  333. create_default_lex_zset
  334. # inclusive range
  335. assert_equal {alpha bar cool} [r zrangebylex zset - \[cool]
  336. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down]
  337. assert_equal {great hill omega} [r zrangebylex zset \[g +]
  338. assert_equal {cool bar alpha} [r zrevrangebylex zset \[cool -]
  339. assert_equal {down cool bar} [r zrevrangebylex zset \[down \[bar]
  340. assert_equal {omega hill great foo elephant down} [r zrevrangebylex zset + \[d]
  341. assert_equal 3 [r zlexcount zset \[ele \[h]
  342. # exclusive range
  343. assert_equal {alpha bar} [r zrangebylex zset - (cool]
  344. assert_equal {cool} [r zrangebylex zset (bar (down]
  345. assert_equal {hill omega} [r zrangebylex zset (great +]
  346. assert_equal {bar alpha} [r zrevrangebylex zset (cool -]
  347. assert_equal {cool} [r zrevrangebylex zset (down (bar]
  348. assert_equal {omega hill} [r zrevrangebylex zset + (great]
  349. assert_equal 2 [r zlexcount zset (ele (great]
  350. # inclusive and exclusive
  351. assert_equal {} [r zrangebylex zset (az (b]
  352. assert_equal {} [r zrangebylex zset (z +]
  353. assert_equal {} [r zrangebylex zset - \[aaaa]
  354. assert_equal {} [r zrevrangebylex zset \[elez \[elex]
  355. assert_equal {} [r zrevrangebylex zset (hill (omega]
  356. }
  357. test "ZRANGEBYSLEX with LIMIT" {
  358. create_default_lex_zset
  359. assert_equal {alpha bar} [r zrangebylex zset - \[cool LIMIT 0 2]
  360. assert_equal {bar cool} [r zrangebylex zset - \[cool LIMIT 1 2]
  361. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 0 0]
  362. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 2 0]
  363. assert_equal {bar} [r zrangebylex zset \[bar \[down LIMIT 0 1]
  364. assert_equal {cool} [r zrangebylex zset \[bar \[down LIMIT 1 1]
  365. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down LIMIT 0 100]
  366. assert_equal {omega hill great foo elephant} [r zrevrangebylex zset + \[d LIMIT 0 5]
  367. assert_equal {omega hill great foo} [r zrevrangebylex zset + \[d LIMIT 0 4]
  368. }
  369. test "ZRANGEBYLEX with invalid lex range specifiers" {
  370. assert_error "*not*string*" {r zrangebylex fooz foo bar}
  371. assert_error "*not*string*" {r zrangebylex fooz \[foo bar}
  372. assert_error "*not*string*" {r zrangebylex fooz foo \[bar}
  373. assert_error "*not*string*" {r zrangebylex fooz +x \[bar}
  374. assert_error "*not*string*" {r zrangebylex fooz -x \[bar}
  375. }
  376. test "ZREMRANGEBYSCORE basics" {
  377. proc remrangebyscore {min max} {
  378. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  379. assert_equal 1 [r exists zset]
  380. r zremrangebyscore zset $min $max
  381. }
  382. # inner range
  383. assert_equal 3 [remrangebyscore 2 4]
  384. assert_equal {a e} [r zrange zset 0 -1]
  385. # start underflow
  386. assert_equal 1 [remrangebyscore -10 1]
  387. assert_equal {b c d e} [r zrange zset 0 -1]
  388. # end overflow
  389. assert_equal 1 [remrangebyscore 5 10]
  390. assert_equal {a b c d} [r zrange zset 0 -1]
  391. # switch min and max
  392. assert_equal 0 [remrangebyscore 4 2]
  393. assert_equal {a b c d e} [r zrange zset 0 -1]
  394. # -inf to mid
  395. assert_equal 3 [remrangebyscore -inf 3]
  396. assert_equal {d e} [r zrange zset 0 -1]
  397. # mid to +inf
  398. assert_equal 3 [remrangebyscore 3 +inf]
  399. assert_equal {a b} [r zrange zset 0 -1]
  400. # -inf to +inf
  401. assert_equal 5 [remrangebyscore -inf +inf]
  402. assert_equal {} [r zrange zset 0 -1]
  403. # exclusive min
  404. assert_equal 4 [remrangebyscore (1 5]
  405. assert_equal {a} [r zrange zset 0 -1]
  406. assert_equal 3 [remrangebyscore (2 5]
  407. assert_equal {a b} [r zrange zset 0 -1]
  408. # exclusive max
  409. assert_equal 4 [remrangebyscore 1 (5]
  410. assert_equal {e} [r zrange zset 0 -1]
  411. assert_equal 3 [remrangebyscore 1 (4]
  412. assert_equal {d e} [r zrange zset 0 -1]
  413. # exclusive min and max
  414. assert_equal 3 [remrangebyscore (1 (5]
  415. assert_equal {a e} [r zrange zset 0 -1]
  416. # destroy when empty
  417. assert_equal 5 [remrangebyscore 1 5]
  418. assert_equal 0 [r exists zset]
  419. }
  420. test "ZREMRANGEBYSCORE with non-value min or max" {
  421. assert_error "*not*float*" {r zremrangebyscore fooz str 1}
  422. assert_error "*not*float*" {r zremrangebyscore fooz 1 str}
  423. assert_error "*not*float*" {r zremrangebyscore fooz 1 NaN}
  424. }
  425. test "ZREMRANGEBYRANK basics" {
  426. proc remrangebyrank {min max} {
  427. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  428. assert_equal 1 [r exists zset]
  429. r zremrangebyrank zset $min $max
  430. }
  431. # inner range
  432. assert_equal 3 [remrangebyrank 1 3]
  433. assert_equal {a e} [r zrange zset 0 -1]
  434. # start underflow
  435. assert_equal 1 [remrangebyrank -10 0]
  436. assert_equal {b c d e} [r zrange zset 0 -1]
  437. # start overflow
  438. assert_equal 0 [remrangebyrank 10 -1]
  439. assert_equal {a b c d e} [r zrange zset 0 -1]
  440. # end underflow
  441. assert_equal 0 [remrangebyrank 0 -10]
  442. assert_equal {a b c d e} [r zrange zset 0 -1]
  443. # end overflow
  444. assert_equal 5 [remrangebyrank 0 10]
  445. assert_equal {} [r zrange zset 0 -1]
  446. # destroy when empty
  447. assert_equal 5 [remrangebyrank 0 4]
  448. assert_equal 0 [r exists zset]
  449. }
  450. test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
  451. r del zseta
  452. assert_equal 0 [r zunionstore dst_key 1 zseta]
  453. assert_equal 0 [r exists dst_key]
  454. }
  455. test "ZUNIONSTORE with empty set - $encoding" {
  456. r del zseta zsetb
  457. r zadd zseta 1 a
  458. r zadd zseta 2 b
  459. r zunionstore zsetc 2 zseta zsetb
  460. r zrange zsetc 0 -1 withscores
  461. } {a 1 b 2}
  462. test "ZUNIONSTORE basics - $encoding" {
  463. r del zseta zsetb zsetc
  464. r zadd zseta 1 a
  465. r zadd zseta 2 b
  466. r zadd zseta 3 c
  467. r zadd zsetb 1 b
  468. r zadd zsetb 2 c
  469. r zadd zsetb 3 d
  470. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
  471. assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
  472. }
  473. test "ZUNIONSTORE with weights - $encoding" {
  474. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
  475. assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
  476. }
  477. test "ZUNIONSTORE with a regular set and weights - $encoding" {
  478. r del seta
  479. r sadd seta a
  480. r sadd seta b
  481. r sadd seta c
  482. assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
  483. assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
  484. }
  485. test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
  486. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
  487. assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
  488. }
  489. test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
  490. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
  491. assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
  492. }
  493. test "ZINTERSTORE basics - $encoding" {
  494. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
  495. assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
  496. }
  497. test "ZINTERSTORE with weights - $encoding" {
  498. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
  499. assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
  500. }
  501. test "ZINTERSTORE with a regular set and weights - $encoding" {
  502. r del seta
  503. r sadd seta a
  504. r sadd seta b
  505. r sadd seta c
  506. assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
  507. assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
  508. }
  509. test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
  510. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
  511. assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
  512. }
  513. test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
  514. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
  515. assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
  516. }
  517. foreach cmd {ZUNIONSTORE ZINTERSTORE} {
  518. test "$cmd with +inf/-inf scores - $encoding" {
  519. r del zsetinf1 zsetinf2
  520. r zadd zsetinf1 +inf key
  521. r zadd zsetinf2 +inf key
  522. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  523. assert_equal inf [r zscore zsetinf3 key]
  524. r zadd zsetinf1 -inf key
  525. r zadd zsetinf2 +inf key
  526. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  527. assert_equal 0 [r zscore zsetinf3 key]
  528. r zadd zsetinf1 +inf key
  529. r zadd zsetinf2 -inf key
  530. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  531. assert_equal 0 [r zscore zsetinf3 key]
  532. r zadd zsetinf1 -inf key
  533. r zadd zsetinf2 -inf key
  534. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  535. assert_equal -inf [r zscore zsetinf3 key]
  536. }
  537. test "$cmd with NaN weights $encoding" {
  538. r del zsetinf1 zsetinf2
  539. r zadd zsetinf1 1.0 key
  540. r zadd zsetinf2 1.0 key
  541. assert_error "*weight*not*float*" {
  542. r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
  543. }
  544. }
  545. }
  546. }
  547. basics ziplist
  548. basics skiplist
  549. test {ZINTERSTORE regression with two sets, intset+hashtable} {
  550. r del seta setb setc
  551. r sadd set1 a
  552. r sadd set2 10
  553. r zinterstore set3 2 set1 set2
  554. } {0}
  555. test {ZUNIONSTORE regression, should not create NaN in scores} {
  556. r zadd z -inf neginf
  557. r zunionstore out 1 z weights 0
  558. r zrange out 0 -1 withscores
  559. } {neginf 0}
  560. test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} {
  561. r sadd one 100 101 102 103
  562. r sadd two 100 200 201 202
  563. r zadd three 1 500 1 501 1 502 1 503 1 100
  564. r zinterstore to_here 3 one two three WEIGHTS 0 0 1
  565. r zrange to_here 0 -1
  566. } {100}
  567. test {ZUNIONSTORE result is sorted} {
  568. # Create two sets with common and not common elements, perform
  569. # the UNION, check that elements are still sorted.
  570. r del one two dest
  571. set cmd1 [list r zadd one]
  572. set cmd2 [list r zadd two]
  573. for {set j 0} {$j < 1000} {incr j} {
  574. lappend cmd1 [expr rand()] [randomInt 1000]
  575. lappend cmd2 [expr rand()] [randomInt 1000]
  576. }
  577. {*}$cmd1
  578. {*}$cmd2
  579. assert {[r zcard one] > 100}
  580. assert {[r zcard two] > 100}
  581. r zunionstore dest 2 one two
  582. set oldscore 0
  583. foreach {ele score} [r zrange dest 0 -1 withscores] {
  584. assert {$score >= $oldscore}
  585. set oldscore $score
  586. }
  587. }
  588. test "ZSET commands don't accept the empty strings as valid score" {
  589. assert_error "*not*float*" {r zadd myzset "" abc}
  590. }
  591. proc stressers {encoding} {
  592. if {$encoding == "ziplist"} {
  593. # Little extra to allow proper fuzzing in the sorting stresser
  594. r config set zset-max-ziplist-entries 256
  595. r config set zset-max-ziplist-value 64
  596. set elements 128
  597. } elseif {$encoding == "skiplist"} {
  598. r config set zset-max-ziplist-entries 0
  599. r config set zset-max-ziplist-value 0
  600. if {$::accurate} {set elements 1000} else {set elements 100}
  601. } else {
  602. puts "Unknown sorted set encoding"
  603. exit
  604. }
  605. test "ZSCORE - $encoding" {
  606. r del zscoretest
  607. set aux {}
  608. for {set i 0} {$i < $elements} {incr i} {
  609. set score [expr rand()]
  610. lappend aux $score
  611. r zadd zscoretest $score $i
  612. }
  613. assert_encoding $encoding zscoretest
  614. for {set i 0} {$i < $elements} {incr i} {
  615. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  616. }
  617. }
  618. test "ZSCORE after a DEBUG RELOAD - $encoding" {
  619. r del zscoretest
  620. set aux {}
  621. for {set i 0} {$i < $elements} {incr i} {
  622. set score [expr rand()]
  623. lappend aux $score
  624. r zadd zscoretest $score $i
  625. }
  626. r debug reload
  627. assert_encoding $encoding zscoretest
  628. for {set i 0} {$i < $elements} {incr i} {
  629. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  630. }
  631. }
  632. test "ZSET sorting stresser - $encoding" {
  633. set delta 0
  634. for {set test 0} {$test < 2} {incr test} {
  635. unset -nocomplain auxarray
  636. array set auxarray {}
  637. set auxlist {}
  638. r del myzset
  639. for {set i 0} {$i < $elements} {incr i} {
  640. if {$test == 0} {
  641. set score [expr rand()]
  642. } else {
  643. set score [expr int(rand()*10)]
  644. }
  645. set auxarray($i) $score
  646. r zadd myzset $score $i
  647. # Random update
  648. if {[expr rand()] < .2} {
  649. set j [expr int(rand()*1000)]
  650. if {$test == 0} {
  651. set score [expr rand()]
  652. } else {
  653. set score [expr int(rand()*10)]
  654. }
  655. set auxarray($j) $score
  656. r zadd myzset $score $j
  657. }
  658. }
  659. foreach {item score} [array get auxarray] {
  660. lappend auxlist [list $score $item]
  661. }
  662. set sorted [lsort -command zlistAlikeSort $auxlist]
  663. set auxlist {}
  664. foreach x $sorted {
  665. lappend auxlist [lindex $x 1]
  666. }
  667. assert_encoding $encoding myzset
  668. set fromredis [r zrange myzset 0 -1]
  669. set delta 0
  670. for {set i 0} {$i < [llength $fromredis]} {incr i} {
  671. if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
  672. incr delta
  673. }
  674. }
  675. }
  676. assert_equal 0 $delta
  677. }
  678. test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  679. set err {}
  680. r del zset
  681. for {set i 0} {$i < $elements} {incr i} {
  682. r zadd zset [expr rand()] $i
  683. }
  684. assert_encoding $encoding zset
  685. for {set i 0} {$i < 100} {incr i} {
  686. set min [expr rand()]
  687. set max [expr rand()]
  688. if {$min > $max} {
  689. set aux $min
  690. set min $max
  691. set max $aux
  692. }
  693. set low [r zrangebyscore zset -inf $min]
  694. set ok [r zrangebyscore zset $min $max]
  695. set high [r zrangebyscore zset $max +inf]
  696. set lowx [r zrangebyscore zset -inf ($min]
  697. set okx [r zrangebyscore zset ($min ($max]
  698. set highx [r zrangebyscore zset ($max +inf]
  699. if {[r zcount zset -inf $min] != [llength $low]} {
  700. append err "Error, len does not match zcount\n"
  701. }
  702. if {[r zcount zset $min $max] != [llength $ok]} {
  703. append err "Error, len does not match zcount\n"
  704. }
  705. if {[r zcount zset $max +inf] != [llength $high]} {
  706. append err "Error, len does not match zcount\n"
  707. }
  708. if {[r zcount zset -inf ($min] != [llength $lowx]} {
  709. append err "Error, len does not match zcount\n"
  710. }
  711. if {[r zcount zset ($min ($max] != [llength $okx]} {
  712. append err "Error, len does not match zcount\n"
  713. }
  714. if {[r zcount zset ($max +inf] != [llength $highx]} {
  715. append err "Error, len does not match zcount\n"
  716. }
  717. foreach x $low {
  718. set score [r zscore zset $x]
  719. if {$score > $min} {
  720. append err "Error, score for $x is $score > $min\n"
  721. }
  722. }
  723. foreach x $lowx {
  724. set score [r zscore zset $x]
  725. if {$score >= $min} {
  726. append err "Error, score for $x is $score >= $min\n"
  727. }
  728. }
  729. foreach x $ok {
  730. set score [r zscore zset $x]
  731. if {$score < $min || $score > $max} {
  732. append err "Error, score for $x is $score outside $min-$max range\n"
  733. }
  734. }
  735. foreach x $okx {
  736. set score [r zscore zset $x]
  737. if {$score <= $min || $score >= $max} {
  738. append err "Error, score for $x is $score outside $min-$max open range\n"
  739. }
  740. }
  741. foreach x $high {
  742. set score [r zscore zset $x]
  743. if {$score < $max} {
  744. append err "Error, score for $x is $score < $max\n"
  745. }
  746. }
  747. foreach x $highx {
  748. set score [r zscore zset $x]
  749. if {$score <= $max} {
  750. append err "Error, score for $x is $score <= $max\n"
  751. }
  752. }
  753. }
  754. assert_equal {} $err
  755. }
  756. test "ZRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  757. set lexset {}
  758. r del zset
  759. for {set j 0} {$j < $elements} {incr j} {
  760. set e [randstring 0 30 alpha]
  761. lappend lexset $e
  762. r zadd zset 0 $e
  763. }
  764. set lexset [lsort -unique $lexset]
  765. for {set j 0} {$j < 100} {incr j} {
  766. set min [randstring 0 30 alpha]
  767. set max [randstring 0 30 alpha]
  768. set mininc [randomInt 2]
  769. set maxinc [randomInt 2]
  770. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  771. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  772. set rev [randomInt 2]
  773. if {$rev} {
  774. set cmd zrevrangebylex
  775. } else {
  776. set cmd zrangebylex
  777. }
  778. # Make sure data is the same in both sides
  779. assert {[r zrange zset 0 -1] eq $lexset}
  780. # Get the Redis output
  781. set output [r $cmd zset $cmin $cmax]
  782. if {$rev} {
  783. set outlen [r zlexcount zset $cmax $cmin]
  784. } else {
  785. set outlen [r zlexcount zset $cmin $cmax]
  786. }
  787. # Compute the same output via Tcl
  788. set o {}
  789. set copy $lexset
  790. if {(!$rev && [string compare $min $max] > 0) ||
  791. ($rev && [string compare $max $min] > 0)} {
  792. # Empty output when ranges are inverted.
  793. } else {
  794. if {$rev} {
  795. # Invert the Tcl array using Redis itself.
  796. set copy [r zrevrange zset 0 -1]
  797. # Invert min / max as well
  798. lassign [list $min $max $mininc $maxinc] \
  799. max min maxinc mininc
  800. }
  801. foreach e $copy {
  802. set mincmp [string compare $e $min]
  803. set maxcmp [string compare $e $max]
  804. if {
  805. ($mininc && $mincmp >= 0 || !$mininc && $mincmp > 0)
  806. &&
  807. ($maxinc && $maxcmp <= 0 || !$maxinc && $maxcmp < 0)
  808. } {
  809. lappend o $e
  810. }
  811. }
  812. }
  813. assert {$o eq $output}
  814. assert {$outlen eq [llength $output]}
  815. }
  816. }
  817. test "ZREMRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  818. set lexset {}
  819. r del zset zsetcopy
  820. for {set j 0} {$j < $elements} {incr j} {
  821. set e [randstring 0 30 alpha]
  822. lappend lexset $e
  823. r zadd zset 0 $e
  824. }
  825. set lexset [lsort -unique $lexset]
  826. for {set j 0} {$j < 100} {incr j} {
  827. # Copy...
  828. r zunionstore zsetcopy 1 zset
  829. set lexsetcopy $lexset
  830. set min [randstring 0 30 alpha]
  831. set max [randstring 0 30 alpha]
  832. set mininc [randomInt 2]
  833. set maxinc [randomInt 2]
  834. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  835. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  836. # Make sure data is the same in both sides
  837. assert {[r zrange zset 0 -1] eq $lexset}
  838. # Get the range we are going to remove
  839. set torem [r zrangebylex zset $cmin $cmax]
  840. set toremlen [r zlexcount zset $cmin $cmax]
  841. r zremrangebylex zsetcopy $cmin $cmax
  842. set output [r zrange zsetcopy 0 -1]
  843. # Remove the range with Tcl from the original list
  844. if {$toremlen} {
  845. set first [lsearch -exact $lexsetcopy [lindex $torem 0]]
  846. set last [expr {$first+$toremlen-1}]
  847. set lexsetcopy [lreplace $lexsetcopy $first $last]
  848. }
  849. assert {$lexsetcopy eq $output}
  850. }
  851. }
  852. test "ZSETs skiplist implementation backlink consistency test - $encoding" {
  853. set diff 0
  854. for {set j 0} {$j < $elements} {incr j} {
  855. r zadd myzset [expr rand()] "Element-$j"
  856. r zrem myzset "Element-[expr int(rand()*$elements)]"
  857. }
  858. assert_encoding $encoding myzset
  859. set l1 [r zrange myzset 0 -1]
  860. set l2 [r zrevrange myzset 0 -1]
  861. for {set j 0} {$j < [llength $l1]} {incr j} {
  862. if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
  863. incr diff
  864. }
  865. }
  866. assert_equal 0 $diff
  867. }
  868. test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
  869. set err {}
  870. r del myzset
  871. for {set k 0} {$k < 2000} {incr k} {
  872. set i [expr {$k % $elements}]
  873. if {[expr rand()] < .2} {
  874. r zrem myzset $i
  875. } else {
  876. set score [expr rand()]
  877. r zadd myzset $score $i
  878. assert_encoding $encoding myzset
  879. }
  880. set card [r zcard myzset]
  881. if {$card > 0} {
  882. set index [randomInt $card]
  883. set ele [lindex [r zrange myzset $index $index] 0]
  884. set rank [r zrank myzset $ele]
  885. if {$rank != $index} {
  886. set err "$ele RANK is wrong! ($rank != $index)"
  887. break
  888. }
  889. }
  890. }
  891. assert_equal {} $err
  892. }
  893. }
  894. tags {"slow"} {
  895. stressers ziplist
  896. stressers skiplist
  897. }
  898. }