zset.tcl 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  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/ZLEXCOUNT 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 "ZLEXCOUNT advanced" {
  358. create_default_lex_zset
  359. assert_equal 9 [r zlexcount zset - +]
  360. assert_equal 0 [r zlexcount zset + -]
  361. assert_equal 0 [r zlexcount zset + \[c]
  362. assert_equal 0 [r zlexcount zset \[c -]
  363. assert_equal 8 [r zlexcount zset \[bar +]
  364. assert_equal 5 [r zlexcount zset \[bar \[foo]
  365. assert_equal 4 [r zlexcount zset \[bar (foo]
  366. assert_equal 4 [r zlexcount zset (bar \[foo]
  367. assert_equal 3 [r zlexcount zset (bar (foo]
  368. assert_equal 5 [r zlexcount zset - (foo]
  369. assert_equal 1 [r zlexcount zset (maxstring +]
  370. }
  371. test "ZRANGEBYSLEX with LIMIT" {
  372. create_default_lex_zset
  373. assert_equal {alpha bar} [r zrangebylex zset - \[cool LIMIT 0 2]
  374. assert_equal {bar cool} [r zrangebylex zset - \[cool LIMIT 1 2]
  375. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 0 0]
  376. assert_equal {} [r zrangebylex zset \[bar \[down LIMIT 2 0]
  377. assert_equal {bar} [r zrangebylex zset \[bar \[down LIMIT 0 1]
  378. assert_equal {cool} [r zrangebylex zset \[bar \[down LIMIT 1 1]
  379. assert_equal {bar cool down} [r zrangebylex zset \[bar \[down LIMIT 0 100]
  380. assert_equal {omega hill great foo elephant} [r zrevrangebylex zset + \[d LIMIT 0 5]
  381. assert_equal {omega hill great foo} [r zrevrangebylex zset + \[d LIMIT 0 4]
  382. }
  383. test "ZRANGEBYLEX with invalid lex range specifiers" {
  384. assert_error "*not*string*" {r zrangebylex fooz foo bar}
  385. assert_error "*not*string*" {r zrangebylex fooz \[foo bar}
  386. assert_error "*not*string*" {r zrangebylex fooz foo \[bar}
  387. assert_error "*not*string*" {r zrangebylex fooz +x \[bar}
  388. assert_error "*not*string*" {r zrangebylex fooz -x \[bar}
  389. }
  390. test "ZREMRANGEBYSCORE basics" {
  391. proc remrangebyscore {min max} {
  392. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  393. assert_equal 1 [r exists zset]
  394. r zremrangebyscore zset $min $max
  395. }
  396. # inner range
  397. assert_equal 3 [remrangebyscore 2 4]
  398. assert_equal {a e} [r zrange zset 0 -1]
  399. # start underflow
  400. assert_equal 1 [remrangebyscore -10 1]
  401. assert_equal {b c d e} [r zrange zset 0 -1]
  402. # end overflow
  403. assert_equal 1 [remrangebyscore 5 10]
  404. assert_equal {a b c d} [r zrange zset 0 -1]
  405. # switch min and max
  406. assert_equal 0 [remrangebyscore 4 2]
  407. assert_equal {a b c d e} [r zrange zset 0 -1]
  408. # -inf to mid
  409. assert_equal 3 [remrangebyscore -inf 3]
  410. assert_equal {d e} [r zrange zset 0 -1]
  411. # mid to +inf
  412. assert_equal 3 [remrangebyscore 3 +inf]
  413. assert_equal {a b} [r zrange zset 0 -1]
  414. # -inf to +inf
  415. assert_equal 5 [remrangebyscore -inf +inf]
  416. assert_equal {} [r zrange zset 0 -1]
  417. # exclusive min
  418. assert_equal 4 [remrangebyscore (1 5]
  419. assert_equal {a} [r zrange zset 0 -1]
  420. assert_equal 3 [remrangebyscore (2 5]
  421. assert_equal {a b} [r zrange zset 0 -1]
  422. # exclusive max
  423. assert_equal 4 [remrangebyscore 1 (5]
  424. assert_equal {e} [r zrange zset 0 -1]
  425. assert_equal 3 [remrangebyscore 1 (4]
  426. assert_equal {d e} [r zrange zset 0 -1]
  427. # exclusive min and max
  428. assert_equal 3 [remrangebyscore (1 (5]
  429. assert_equal {a e} [r zrange zset 0 -1]
  430. # destroy when empty
  431. assert_equal 5 [remrangebyscore 1 5]
  432. assert_equal 0 [r exists zset]
  433. }
  434. test "ZREMRANGEBYSCORE with non-value min or max" {
  435. assert_error "*not*float*" {r zremrangebyscore fooz str 1}
  436. assert_error "*not*float*" {r zremrangebyscore fooz 1 str}
  437. assert_error "*not*float*" {r zremrangebyscore fooz 1 NaN}
  438. }
  439. test "ZREMRANGEBYRANK basics" {
  440. proc remrangebyrank {min max} {
  441. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  442. assert_equal 1 [r exists zset]
  443. r zremrangebyrank zset $min $max
  444. }
  445. # inner range
  446. assert_equal 3 [remrangebyrank 1 3]
  447. assert_equal {a e} [r zrange zset 0 -1]
  448. # start underflow
  449. assert_equal 1 [remrangebyrank -10 0]
  450. assert_equal {b c d e} [r zrange zset 0 -1]
  451. # start overflow
  452. assert_equal 0 [remrangebyrank 10 -1]
  453. assert_equal {a b c d e} [r zrange zset 0 -1]
  454. # end underflow
  455. assert_equal 0 [remrangebyrank 0 -10]
  456. assert_equal {a b c d e} [r zrange zset 0 -1]
  457. # end overflow
  458. assert_equal 5 [remrangebyrank 0 10]
  459. assert_equal {} [r zrange zset 0 -1]
  460. # destroy when empty
  461. assert_equal 5 [remrangebyrank 0 4]
  462. assert_equal 0 [r exists zset]
  463. }
  464. test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
  465. r del zseta
  466. assert_equal 0 [r zunionstore dst_key 1 zseta]
  467. assert_equal 0 [r exists dst_key]
  468. }
  469. test "ZUNIONSTORE with empty set - $encoding" {
  470. r del zseta zsetb
  471. r zadd zseta 1 a
  472. r zadd zseta 2 b
  473. r zunionstore zsetc 2 zseta zsetb
  474. r zrange zsetc 0 -1 withscores
  475. } {a 1 b 2}
  476. test "ZUNIONSTORE basics - $encoding" {
  477. r del zseta zsetb zsetc
  478. r zadd zseta 1 a
  479. r zadd zseta 2 b
  480. r zadd zseta 3 c
  481. r zadd zsetb 1 b
  482. r zadd zsetb 2 c
  483. r zadd zsetb 3 d
  484. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
  485. assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
  486. }
  487. test "ZUNIONSTORE with weights - $encoding" {
  488. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
  489. assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
  490. }
  491. test "ZUNIONSTORE with a regular set and weights - $encoding" {
  492. r del seta
  493. r sadd seta a
  494. r sadd seta b
  495. r sadd seta c
  496. assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
  497. assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
  498. }
  499. test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
  500. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
  501. assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
  502. }
  503. test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
  504. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
  505. assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
  506. }
  507. test "ZINTERSTORE basics - $encoding" {
  508. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
  509. assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
  510. }
  511. test "ZINTERSTORE with weights - $encoding" {
  512. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
  513. assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
  514. }
  515. test "ZINTERSTORE with a regular set and weights - $encoding" {
  516. r del seta
  517. r sadd seta a
  518. r sadd seta b
  519. r sadd seta c
  520. assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
  521. assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
  522. }
  523. test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
  524. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
  525. assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
  526. }
  527. test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
  528. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
  529. assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
  530. }
  531. foreach cmd {ZUNIONSTORE ZINTERSTORE} {
  532. test "$cmd with +inf/-inf scores - $encoding" {
  533. r del zsetinf1 zsetinf2
  534. r zadd zsetinf1 +inf key
  535. r zadd zsetinf2 +inf key
  536. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  537. assert_equal inf [r zscore zsetinf3 key]
  538. r zadd zsetinf1 -inf key
  539. r zadd zsetinf2 +inf key
  540. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  541. assert_equal 0 [r zscore zsetinf3 key]
  542. r zadd zsetinf1 +inf key
  543. r zadd zsetinf2 -inf key
  544. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  545. assert_equal 0 [r zscore zsetinf3 key]
  546. r zadd zsetinf1 -inf key
  547. r zadd zsetinf2 -inf key
  548. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  549. assert_equal -inf [r zscore zsetinf3 key]
  550. }
  551. test "$cmd with NaN weights $encoding" {
  552. r del zsetinf1 zsetinf2
  553. r zadd zsetinf1 1.0 key
  554. r zadd zsetinf2 1.0 key
  555. assert_error "*weight*not*float*" {
  556. r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
  557. }
  558. }
  559. }
  560. }
  561. basics ziplist
  562. basics skiplist
  563. test {ZINTERSTORE regression with two sets, intset+hashtable} {
  564. r del seta setb setc
  565. r sadd set1 a
  566. r sadd set2 10
  567. r zinterstore set3 2 set1 set2
  568. } {0}
  569. test {ZUNIONSTORE regression, should not create NaN in scores} {
  570. r zadd z -inf neginf
  571. r zunionstore out 1 z weights 0
  572. r zrange out 0 -1 withscores
  573. } {neginf 0}
  574. test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} {
  575. r sadd one 100 101 102 103
  576. r sadd two 100 200 201 202
  577. r zadd three 1 500 1 501 1 502 1 503 1 100
  578. r zinterstore to_here 3 one two three WEIGHTS 0 0 1
  579. r zrange to_here 0 -1
  580. } {100}
  581. test {ZUNIONSTORE result is sorted} {
  582. # Create two sets with common and not common elements, perform
  583. # the UNION, check that elements are still sorted.
  584. r del one two dest
  585. set cmd1 [list r zadd one]
  586. set cmd2 [list r zadd two]
  587. for {set j 0} {$j < 1000} {incr j} {
  588. lappend cmd1 [expr rand()] [randomInt 1000]
  589. lappend cmd2 [expr rand()] [randomInt 1000]
  590. }
  591. {*}$cmd1
  592. {*}$cmd2
  593. assert {[r zcard one] > 100}
  594. assert {[r zcard two] > 100}
  595. r zunionstore dest 2 one two
  596. set oldscore 0
  597. foreach {ele score} [r zrange dest 0 -1 withscores] {
  598. assert {$score >= $oldscore}
  599. set oldscore $score
  600. }
  601. }
  602. test "ZSET commands don't accept the empty strings as valid score" {
  603. assert_error "*not*float*" {r zadd myzset "" abc}
  604. }
  605. proc stressers {encoding} {
  606. if {$encoding == "ziplist"} {
  607. # Little extra to allow proper fuzzing in the sorting stresser
  608. r config set zset-max-ziplist-entries 256
  609. r config set zset-max-ziplist-value 64
  610. set elements 128
  611. } elseif {$encoding == "skiplist"} {
  612. r config set zset-max-ziplist-entries 0
  613. r config set zset-max-ziplist-value 0
  614. if {$::accurate} {set elements 1000} else {set elements 100}
  615. } else {
  616. puts "Unknown sorted set encoding"
  617. exit
  618. }
  619. test "ZSCORE - $encoding" {
  620. r del zscoretest
  621. set aux {}
  622. for {set i 0} {$i < $elements} {incr i} {
  623. set score [expr rand()]
  624. lappend aux $score
  625. r zadd zscoretest $score $i
  626. }
  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 "ZSCORE after a DEBUG RELOAD - $encoding" {
  633. r del zscoretest
  634. set aux {}
  635. for {set i 0} {$i < $elements} {incr i} {
  636. set score [expr rand()]
  637. lappend aux $score
  638. r zadd zscoretest $score $i
  639. }
  640. r debug reload
  641. assert_encoding $encoding zscoretest
  642. for {set i 0} {$i < $elements} {incr i} {
  643. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  644. }
  645. }
  646. test "ZSET sorting stresser - $encoding" {
  647. set delta 0
  648. for {set test 0} {$test < 2} {incr test} {
  649. unset -nocomplain auxarray
  650. array set auxarray {}
  651. set auxlist {}
  652. r del myzset
  653. for {set i 0} {$i < $elements} {incr i} {
  654. if {$test == 0} {
  655. set score [expr rand()]
  656. } else {
  657. set score [expr int(rand()*10)]
  658. }
  659. set auxarray($i) $score
  660. r zadd myzset $score $i
  661. # Random update
  662. if {[expr rand()] < .2} {
  663. set j [expr int(rand()*1000)]
  664. if {$test == 0} {
  665. set score [expr rand()]
  666. } else {
  667. set score [expr int(rand()*10)]
  668. }
  669. set auxarray($j) $score
  670. r zadd myzset $score $j
  671. }
  672. }
  673. foreach {item score} [array get auxarray] {
  674. lappend auxlist [list $score $item]
  675. }
  676. set sorted [lsort -command zlistAlikeSort $auxlist]
  677. set auxlist {}
  678. foreach x $sorted {
  679. lappend auxlist [lindex $x 1]
  680. }
  681. assert_encoding $encoding myzset
  682. set fromredis [r zrange myzset 0 -1]
  683. set delta 0
  684. for {set i 0} {$i < [llength $fromredis]} {incr i} {
  685. if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
  686. incr delta
  687. }
  688. }
  689. }
  690. assert_equal 0 $delta
  691. }
  692. test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  693. set err {}
  694. r del zset
  695. for {set i 0} {$i < $elements} {incr i} {
  696. r zadd zset [expr rand()] $i
  697. }
  698. assert_encoding $encoding zset
  699. for {set i 0} {$i < 100} {incr i} {
  700. set min [expr rand()]
  701. set max [expr rand()]
  702. if {$min > $max} {
  703. set aux $min
  704. set min $max
  705. set max $aux
  706. }
  707. set low [r zrangebyscore zset -inf $min]
  708. set ok [r zrangebyscore zset $min $max]
  709. set high [r zrangebyscore zset $max +inf]
  710. set lowx [r zrangebyscore zset -inf ($min]
  711. set okx [r zrangebyscore zset ($min ($max]
  712. set highx [r zrangebyscore zset ($max +inf]
  713. if {[r zcount zset -inf $min] != [llength $low]} {
  714. append err "Error, len does not match zcount\n"
  715. }
  716. if {[r zcount zset $min $max] != [llength $ok]} {
  717. append err "Error, len does not match zcount\n"
  718. }
  719. if {[r zcount zset $max +inf] != [llength $high]} {
  720. append err "Error, len does not match zcount\n"
  721. }
  722. if {[r zcount zset -inf ($min] != [llength $lowx]} {
  723. append err "Error, len does not match zcount\n"
  724. }
  725. if {[r zcount zset ($min ($max] != [llength $okx]} {
  726. append err "Error, len does not match zcount\n"
  727. }
  728. if {[r zcount zset ($max +inf] != [llength $highx]} {
  729. append err "Error, len does not match zcount\n"
  730. }
  731. foreach x $low {
  732. set score [r zscore zset $x]
  733. if {$score > $min} {
  734. append err "Error, score for $x is $score > $min\n"
  735. }
  736. }
  737. foreach x $lowx {
  738. set score [r zscore zset $x]
  739. if {$score >= $min} {
  740. append err "Error, score for $x is $score >= $min\n"
  741. }
  742. }
  743. foreach x $ok {
  744. set score [r zscore zset $x]
  745. if {$score < $min || $score > $max} {
  746. append err "Error, score for $x is $score outside $min-$max range\n"
  747. }
  748. }
  749. foreach x $okx {
  750. set score [r zscore zset $x]
  751. if {$score <= $min || $score >= $max} {
  752. append err "Error, score for $x is $score outside $min-$max open range\n"
  753. }
  754. }
  755. foreach x $high {
  756. set score [r zscore zset $x]
  757. if {$score < $max} {
  758. append err "Error, score for $x is $score < $max\n"
  759. }
  760. }
  761. foreach x $highx {
  762. set score [r zscore zset $x]
  763. if {$score <= $max} {
  764. append err "Error, score for $x is $score <= $max\n"
  765. }
  766. }
  767. }
  768. assert_equal {} $err
  769. }
  770. test "ZRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  771. set lexset {}
  772. r del zset
  773. for {set j 0} {$j < $elements} {incr j} {
  774. set e [randstring 0 30 alpha]
  775. lappend lexset $e
  776. r zadd zset 0 $e
  777. }
  778. set lexset [lsort -unique $lexset]
  779. for {set j 0} {$j < 100} {incr j} {
  780. set min [randstring 0 30 alpha]
  781. set max [randstring 0 30 alpha]
  782. set mininc [randomInt 2]
  783. set maxinc [randomInt 2]
  784. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  785. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  786. set rev [randomInt 2]
  787. if {$rev} {
  788. set cmd zrevrangebylex
  789. } else {
  790. set cmd zrangebylex
  791. }
  792. # Make sure data is the same in both sides
  793. assert {[r zrange zset 0 -1] eq $lexset}
  794. # Get the Redis output
  795. set output [r $cmd zset $cmin $cmax]
  796. if {$rev} {
  797. set outlen [r zlexcount zset $cmax $cmin]
  798. } else {
  799. set outlen [r zlexcount zset $cmin $cmax]
  800. }
  801. # Compute the same output via Tcl
  802. set o {}
  803. set copy $lexset
  804. if {(!$rev && [string compare $min $max] > 0) ||
  805. ($rev && [string compare $max $min] > 0)} {
  806. # Empty output when ranges are inverted.
  807. } else {
  808. if {$rev} {
  809. # Invert the Tcl array using Redis itself.
  810. set copy [r zrevrange zset 0 -1]
  811. # Invert min / max as well
  812. lassign [list $min $max $mininc $maxinc] \
  813. max min maxinc mininc
  814. }
  815. foreach e $copy {
  816. set mincmp [string compare $e $min]
  817. set maxcmp [string compare $e $max]
  818. if {
  819. ($mininc && $mincmp >= 0 || !$mininc && $mincmp > 0)
  820. &&
  821. ($maxinc && $maxcmp <= 0 || !$maxinc && $maxcmp < 0)
  822. } {
  823. lappend o $e
  824. }
  825. }
  826. }
  827. assert {$o eq $output}
  828. assert {$outlen eq [llength $output]}
  829. }
  830. }
  831. test "ZREMRANGEBYLEX fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  832. set lexset {}
  833. r del zset zsetcopy
  834. for {set j 0} {$j < $elements} {incr j} {
  835. set e [randstring 0 30 alpha]
  836. lappend lexset $e
  837. r zadd zset 0 $e
  838. }
  839. set lexset [lsort -unique $lexset]
  840. for {set j 0} {$j < 100} {incr j} {
  841. # Copy...
  842. r zunionstore zsetcopy 1 zset
  843. set lexsetcopy $lexset
  844. set min [randstring 0 30 alpha]
  845. set max [randstring 0 30 alpha]
  846. set mininc [randomInt 2]
  847. set maxinc [randomInt 2]
  848. if {$mininc} {set cmin "\[$min"} else {set cmin "($min"}
  849. if {$maxinc} {set cmax "\[$max"} else {set cmax "($max"}
  850. # Make sure data is the same in both sides
  851. assert {[r zrange zset 0 -1] eq $lexset}
  852. # Get the range we are going to remove
  853. set torem [r zrangebylex zset $cmin $cmax]
  854. set toremlen [r zlexcount zset $cmin $cmax]
  855. r zremrangebylex zsetcopy $cmin $cmax
  856. set output [r zrange zsetcopy 0 -1]
  857. # Remove the range with Tcl from the original list
  858. if {$toremlen} {
  859. set first [lsearch -exact $lexsetcopy [lindex $torem 0]]
  860. set last [expr {$first+$toremlen-1}]
  861. set lexsetcopy [lreplace $lexsetcopy $first $last]
  862. }
  863. assert {$lexsetcopy eq $output}
  864. }
  865. }
  866. test "ZSETs skiplist implementation backlink consistency test - $encoding" {
  867. set diff 0
  868. for {set j 0} {$j < $elements} {incr j} {
  869. r zadd myzset [expr rand()] "Element-$j"
  870. r zrem myzset "Element-[expr int(rand()*$elements)]"
  871. }
  872. assert_encoding $encoding myzset
  873. set l1 [r zrange myzset 0 -1]
  874. set l2 [r zrevrange myzset 0 -1]
  875. for {set j 0} {$j < [llength $l1]} {incr j} {
  876. if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
  877. incr diff
  878. }
  879. }
  880. assert_equal 0 $diff
  881. }
  882. test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
  883. set err {}
  884. r del myzset
  885. for {set k 0} {$k < 2000} {incr k} {
  886. set i [expr {$k % $elements}]
  887. if {[expr rand()] < .2} {
  888. r zrem myzset $i
  889. } else {
  890. set score [expr rand()]
  891. r zadd myzset $score $i
  892. assert_encoding $encoding myzset
  893. }
  894. set card [r zcard myzset]
  895. if {$card > 0} {
  896. set index [randomInt $card]
  897. set ele [lindex [r zrange myzset $index $index] 0]
  898. set rank [r zrank myzset $ele]
  899. if {$rank != $index} {
  900. set err "$ele RANK is wrong! ($rank != $index)"
  901. break
  902. }
  903. }
  904. }
  905. assert_equal {} $err
  906. }
  907. }
  908. tags {"slow"} {
  909. stressers ziplist
  910. stressers skiplist
  911. }
  912. }