zset.tcl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761
  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 a double*" {r zadd myzset nan abc}
  35. }
  36. test "ZSET element can't be set to NaN with ZINCRBY" {
  37. assert_error "*not a double*" {r zadd myzset nan abc}
  38. }
  39. test "ZINCRBY calls leading to NaN result in error" {
  40. r zincrby myzset +inf abc
  41. assert_error "*NaN*" {r zincrby myzset -inf abc}
  42. }
  43. test {ZADD - Variadic version base case} {
  44. r del myzset
  45. list [r zadd myzset 10 a 20 b 30 c] [r zrange myzset 0 -1 withscores]
  46. } {3 {a 10 b 20 c 30}}
  47. test {ZADD - Return value is the number of actually added items} {
  48. list [r zadd myzset 5 x 20 b 30 c] [r zrange myzset 0 -1 withscores]
  49. } {1 {x 5 a 10 b 20 c 30}}
  50. test {ZADD - Variadic version does not add nothing on single parsing err} {
  51. r del myzset
  52. catch {r zadd myzset 10 a 20 b 30.badscore c} e
  53. assert_match {*ERR*not*double*} $e
  54. r exists myzset
  55. } {0}
  56. test {ZADD - Variadic version will raise error on missing arg} {
  57. r del myzset
  58. catch {r zadd myzset 10 a 20 b 30 c 40} e
  59. assert_match {*ERR*syntax*} $e
  60. }
  61. test {ZINCRBY does not work variadic even if shares ZADD implementation} {
  62. r del myzset
  63. catch {r zincrby myzset 10 a 20 b 30 c} e
  64. assert_match {*ERR*wrong*number*arg*} $e
  65. }
  66. test "ZCARD basics - $encoding" {
  67. assert_equal 3 [r zcard ztmp]
  68. assert_equal 0 [r zcard zdoesntexist]
  69. }
  70. test "ZREM removes key after last element is removed" {
  71. r del ztmp
  72. r zadd ztmp 10 x
  73. r zadd ztmp 20 y
  74. assert_equal 1 [r exists ztmp]
  75. assert_equal 0 [r zrem ztmp z]
  76. assert_equal 1 [r zrem ztmp y]
  77. assert_equal 1 [r zrem ztmp x]
  78. assert_equal 0 [r exists ztmp]
  79. }
  80. test "ZREM variadic version" {
  81. r del ztmp
  82. r zadd ztmp 10 a 20 b 30 c
  83. assert_equal 2 [r zrem ztmp x y a b k]
  84. assert_equal 0 [r zrem ztmp foo bar]
  85. assert_equal 1 [r zrem ztmp c]
  86. r exists ztmp
  87. } {0}
  88. test "ZREM variadic version -- remove elements after key deletion" {
  89. r del ztmp
  90. r zadd ztmp 10 a 20 b 30 c
  91. r zrem ztmp a b c d e f g
  92. } {3}
  93. test "ZRANGE basics - $encoding" {
  94. r del ztmp
  95. r zadd ztmp 1 a
  96. r zadd ztmp 2 b
  97. r zadd ztmp 3 c
  98. r zadd ztmp 4 d
  99. assert_equal {a b c d} [r zrange ztmp 0 -1]
  100. assert_equal {a b c} [r zrange ztmp 0 -2]
  101. assert_equal {b c d} [r zrange ztmp 1 -1]
  102. assert_equal {b c} [r zrange ztmp 1 -2]
  103. assert_equal {c d} [r zrange ztmp -2 -1]
  104. assert_equal {c} [r zrange ztmp -2 -2]
  105. # out of range start index
  106. assert_equal {a b c} [r zrange ztmp -5 2]
  107. assert_equal {a b} [r zrange ztmp -5 1]
  108. assert_equal {} [r zrange ztmp 5 -1]
  109. assert_equal {} [r zrange ztmp 5 -2]
  110. # out of range end index
  111. assert_equal {a b c d} [r zrange ztmp 0 5]
  112. assert_equal {b c d} [r zrange ztmp 1 5]
  113. assert_equal {} [r zrange ztmp 0 -5]
  114. assert_equal {} [r zrange ztmp 1 -5]
  115. # withscores
  116. assert_equal {a 1 b 2 c 3 d 4} [r zrange ztmp 0 -1 withscores]
  117. }
  118. test "ZREVRANGE basics - $encoding" {
  119. r del ztmp
  120. r zadd ztmp 1 a
  121. r zadd ztmp 2 b
  122. r zadd ztmp 3 c
  123. r zadd ztmp 4 d
  124. assert_equal {d c b a} [r zrevrange ztmp 0 -1]
  125. assert_equal {d c b} [r zrevrange ztmp 0 -2]
  126. assert_equal {c b a} [r zrevrange ztmp 1 -1]
  127. assert_equal {c b} [r zrevrange ztmp 1 -2]
  128. assert_equal {b a} [r zrevrange ztmp -2 -1]
  129. assert_equal {b} [r zrevrange ztmp -2 -2]
  130. # out of range start index
  131. assert_equal {d c b} [r zrevrange ztmp -5 2]
  132. assert_equal {d c} [r zrevrange ztmp -5 1]
  133. assert_equal {} [r zrevrange ztmp 5 -1]
  134. assert_equal {} [r zrevrange ztmp 5 -2]
  135. # out of range end index
  136. assert_equal {d c b a} [r zrevrange ztmp 0 5]
  137. assert_equal {c b a} [r zrevrange ztmp 1 5]
  138. assert_equal {} [r zrevrange ztmp 0 -5]
  139. assert_equal {} [r zrevrange ztmp 1 -5]
  140. # withscores
  141. assert_equal {d 4 c 3 b 2 a 1} [r zrevrange ztmp 0 -1 withscores]
  142. }
  143. test "ZRANK/ZREVRANK basics - $encoding" {
  144. r del zranktmp
  145. r zadd zranktmp 10 x
  146. r zadd zranktmp 20 y
  147. r zadd zranktmp 30 z
  148. assert_equal 0 [r zrank zranktmp x]
  149. assert_equal 1 [r zrank zranktmp y]
  150. assert_equal 2 [r zrank zranktmp z]
  151. assert_equal "" [r zrank zranktmp foo]
  152. assert_equal 2 [r zrevrank zranktmp x]
  153. assert_equal 1 [r zrevrank zranktmp y]
  154. assert_equal 0 [r zrevrank zranktmp z]
  155. assert_equal "" [r zrevrank zranktmp foo]
  156. }
  157. test "ZRANK - after deletion - $encoding" {
  158. r zrem zranktmp y
  159. assert_equal 0 [r zrank zranktmp x]
  160. assert_equal 1 [r zrank zranktmp z]
  161. }
  162. test "ZINCRBY - can create a new sorted set - $encoding" {
  163. r del zset
  164. r zincrby zset 1 foo
  165. assert_equal {foo} [r zrange zset 0 -1]
  166. assert_equal 1 [r zscore zset foo]
  167. }
  168. test "ZINCRBY - increment and decrement - $encoding" {
  169. r zincrby zset 2 foo
  170. r zincrby zset 1 bar
  171. assert_equal {bar foo} [r zrange zset 0 -1]
  172. r zincrby zset 10 bar
  173. r zincrby zset -5 foo
  174. r zincrby zset -5 bar
  175. assert_equal {foo bar} [r zrange zset 0 -1]
  176. assert_equal -2 [r zscore zset foo]
  177. assert_equal 6 [r zscore zset bar]
  178. }
  179. proc create_default_zset {} {
  180. create_zset zset {-inf a 1 b 2 c 3 d 4 e 5 f +inf g}
  181. }
  182. test "ZRANGEBYSCORE/ZREVRANGEBYSCORE/ZCOUNT basics" {
  183. create_default_zset
  184. # inclusive range
  185. assert_equal {a b c} [r zrangebyscore zset -inf 2]
  186. assert_equal {b c d} [r zrangebyscore zset 0 3]
  187. assert_equal {d e f} [r zrangebyscore zset 3 6]
  188. assert_equal {e f g} [r zrangebyscore zset 4 +inf]
  189. assert_equal {c b a} [r zrevrangebyscore zset 2 -inf]
  190. assert_equal {d c b} [r zrevrangebyscore zset 3 0]
  191. assert_equal {f e d} [r zrevrangebyscore zset 6 3]
  192. assert_equal {g f e} [r zrevrangebyscore zset +inf 4]
  193. assert_equal 3 [r zcount zset 0 3]
  194. # exclusive range
  195. assert_equal {b} [r zrangebyscore zset (-inf (2]
  196. assert_equal {b c} [r zrangebyscore zset (0 (3]
  197. assert_equal {e f} [r zrangebyscore zset (3 (6]
  198. assert_equal {f} [r zrangebyscore zset (4 (+inf]
  199. assert_equal {b} [r zrevrangebyscore zset (2 (-inf]
  200. assert_equal {c b} [r zrevrangebyscore zset (3 (0]
  201. assert_equal {f e} [r zrevrangebyscore zset (6 (3]
  202. assert_equal {f} [r zrevrangebyscore zset (+inf (4]
  203. assert_equal 2 [r zcount zset (0 (3]
  204. # test empty ranges
  205. r zrem zset a
  206. r zrem zset g
  207. # inclusive
  208. assert_equal {} [r zrangebyscore zset 4 2]
  209. assert_equal {} [r zrangebyscore zset 6 +inf]
  210. assert_equal {} [r zrangebyscore zset -inf -6]
  211. assert_equal {} [r zrevrangebyscore zset +inf 6]
  212. assert_equal {} [r zrevrangebyscore zset -6 -inf]
  213. # exclusive
  214. assert_equal {} [r zrangebyscore zset (4 (2]
  215. assert_equal {} [r zrangebyscore zset 2 (2]
  216. assert_equal {} [r zrangebyscore zset (2 2]
  217. assert_equal {} [r zrangebyscore zset (6 (+inf]
  218. assert_equal {} [r zrangebyscore zset (-inf (-6]
  219. assert_equal {} [r zrevrangebyscore zset (+inf (6]
  220. assert_equal {} [r zrevrangebyscore zset (-6 (-inf]
  221. # empty inner range
  222. assert_equal {} [r zrangebyscore zset 2.4 2.6]
  223. assert_equal {} [r zrangebyscore zset (2.4 2.6]
  224. assert_equal {} [r zrangebyscore zset 2.4 (2.6]
  225. assert_equal {} [r zrangebyscore zset (2.4 (2.6]
  226. }
  227. test "ZRANGEBYSCORE with WITHSCORES" {
  228. create_default_zset
  229. assert_equal {b 1 c 2 d 3} [r zrangebyscore zset 0 3 withscores]
  230. assert_equal {d 3 c 2 b 1} [r zrevrangebyscore zset 3 0 withscores]
  231. }
  232. test "ZRANGEBYSCORE with LIMIT" {
  233. create_default_zset
  234. assert_equal {b c} [r zrangebyscore zset 0 10 LIMIT 0 2]
  235. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 3]
  236. assert_equal {d e f} [r zrangebyscore zset 0 10 LIMIT 2 10]
  237. assert_equal {} [r zrangebyscore zset 0 10 LIMIT 20 10]
  238. assert_equal {f e} [r zrevrangebyscore zset 10 0 LIMIT 0 2]
  239. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 3]
  240. assert_equal {d c b} [r zrevrangebyscore zset 10 0 LIMIT 2 10]
  241. assert_equal {} [r zrevrangebyscore zset 10 0 LIMIT 20 10]
  242. }
  243. test "ZRANGEBYSCORE with LIMIT and WITHSCORES" {
  244. create_default_zset
  245. assert_equal {e 4 f 5} [r zrangebyscore zset 2 5 LIMIT 2 3 WITHSCORES]
  246. assert_equal {d 3 c 2} [r zrevrangebyscore zset 5 2 LIMIT 2 3 WITHSCORES]
  247. }
  248. test "ZRANGEBYSCORE with non-value min or max" {
  249. assert_error "*not a double*" {r zrangebyscore fooz str 1}
  250. assert_error "*not a double*" {r zrangebyscore fooz 1 str}
  251. assert_error "*not a double*" {r zrangebyscore fooz 1 NaN}
  252. }
  253. test "ZREMRANGEBYSCORE basics" {
  254. proc remrangebyscore {min max} {
  255. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  256. assert_equal 1 [r exists zset]
  257. r zremrangebyscore zset $min $max
  258. }
  259. # inner range
  260. assert_equal 3 [remrangebyscore 2 4]
  261. assert_equal {a e} [r zrange zset 0 -1]
  262. # start underflow
  263. assert_equal 1 [remrangebyscore -10 1]
  264. assert_equal {b c d e} [r zrange zset 0 -1]
  265. # end overflow
  266. assert_equal 1 [remrangebyscore 5 10]
  267. assert_equal {a b c d} [r zrange zset 0 -1]
  268. # switch min and max
  269. assert_equal 0 [remrangebyscore 4 2]
  270. assert_equal {a b c d e} [r zrange zset 0 -1]
  271. # -inf to mid
  272. assert_equal 3 [remrangebyscore -inf 3]
  273. assert_equal {d e} [r zrange zset 0 -1]
  274. # mid to +inf
  275. assert_equal 3 [remrangebyscore 3 +inf]
  276. assert_equal {a b} [r zrange zset 0 -1]
  277. # -inf to +inf
  278. assert_equal 5 [remrangebyscore -inf +inf]
  279. assert_equal {} [r zrange zset 0 -1]
  280. # exclusive min
  281. assert_equal 4 [remrangebyscore (1 5]
  282. assert_equal {a} [r zrange zset 0 -1]
  283. assert_equal 3 [remrangebyscore (2 5]
  284. assert_equal {a b} [r zrange zset 0 -1]
  285. # exclusive max
  286. assert_equal 4 [remrangebyscore 1 (5]
  287. assert_equal {e} [r zrange zset 0 -1]
  288. assert_equal 3 [remrangebyscore 1 (4]
  289. assert_equal {d e} [r zrange zset 0 -1]
  290. # exclusive min and max
  291. assert_equal 3 [remrangebyscore (1 (5]
  292. assert_equal {a e} [r zrange zset 0 -1]
  293. # destroy when empty
  294. assert_equal 5 [remrangebyscore 1 5]
  295. assert_equal 0 [r exists zset]
  296. }
  297. test "ZREMRANGEBYSCORE with non-value min or max" {
  298. assert_error "*not a double*" {r zremrangebyscore fooz str 1}
  299. assert_error "*not a double*" {r zremrangebyscore fooz 1 str}
  300. assert_error "*not a double*" {r zremrangebyscore fooz 1 NaN}
  301. }
  302. test "ZREMRANGEBYRANK basics" {
  303. proc remrangebyrank {min max} {
  304. create_zset zset {1 a 2 b 3 c 4 d 5 e}
  305. assert_equal 1 [r exists zset]
  306. r zremrangebyrank zset $min $max
  307. }
  308. # inner range
  309. assert_equal 3 [remrangebyrank 1 3]
  310. assert_equal {a e} [r zrange zset 0 -1]
  311. # start underflow
  312. assert_equal 1 [remrangebyrank -10 0]
  313. assert_equal {b c d e} [r zrange zset 0 -1]
  314. # start overflow
  315. assert_equal 0 [remrangebyrank 10 -1]
  316. assert_equal {a b c d e} [r zrange zset 0 -1]
  317. # end underflow
  318. assert_equal 0 [remrangebyrank 0 -10]
  319. assert_equal {a b c d e} [r zrange zset 0 -1]
  320. # end overflow
  321. assert_equal 5 [remrangebyrank 0 10]
  322. assert_equal {} [r zrange zset 0 -1]
  323. # destroy when empty
  324. assert_equal 5 [remrangebyrank 0 4]
  325. assert_equal 0 [r exists zset]
  326. }
  327. test "ZUNIONSTORE against non-existing key doesn't set destination - $encoding" {
  328. r del zseta
  329. assert_equal 0 [r zunionstore dst_key 1 zseta]
  330. assert_equal 0 [r exists dst_key]
  331. }
  332. test "ZUNIONSTORE with empty set - $encoding" {
  333. r del zseta zsetb
  334. r zadd zseta 1 a
  335. r zadd zseta 2 b
  336. r zunionstore zsetc 2 zseta zsetb
  337. r zrange zsetc 0 -1 withscores
  338. } {a 1 b 2}
  339. test "ZUNIONSTORE basics - $encoding" {
  340. r del zseta zsetb zsetc
  341. r zadd zseta 1 a
  342. r zadd zseta 2 b
  343. r zadd zseta 3 c
  344. r zadd zsetb 1 b
  345. r zadd zsetb 2 c
  346. r zadd zsetb 3 d
  347. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb]
  348. assert_equal {a 1 b 3 d 3 c 5} [r zrange zsetc 0 -1 withscores]
  349. }
  350. test "ZUNIONSTORE with weights - $encoding" {
  351. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb weights 2 3]
  352. assert_equal {a 2 b 7 d 9 c 12} [r zrange zsetc 0 -1 withscores]
  353. }
  354. test "ZUNIONSTORE with a regular set and weights - $encoding" {
  355. r del seta
  356. r sadd seta a
  357. r sadd seta b
  358. r sadd seta c
  359. assert_equal 4 [r zunionstore zsetc 2 seta zsetb weights 2 3]
  360. assert_equal {a 2 b 5 c 8 d 9} [r zrange zsetc 0 -1 withscores]
  361. }
  362. test "ZUNIONSTORE with AGGREGATE MIN - $encoding" {
  363. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate min]
  364. assert_equal {a 1 b 1 c 2 d 3} [r zrange zsetc 0 -1 withscores]
  365. }
  366. test "ZUNIONSTORE with AGGREGATE MAX - $encoding" {
  367. assert_equal 4 [r zunionstore zsetc 2 zseta zsetb aggregate max]
  368. assert_equal {a 1 b 2 c 3 d 3} [r zrange zsetc 0 -1 withscores]
  369. }
  370. test "ZINTERSTORE basics - $encoding" {
  371. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb]
  372. assert_equal {b 3 c 5} [r zrange zsetc 0 -1 withscores]
  373. }
  374. test "ZINTERSTORE with weights - $encoding" {
  375. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb weights 2 3]
  376. assert_equal {b 7 c 12} [r zrange zsetc 0 -1 withscores]
  377. }
  378. test "ZINTERSTORE with a regular set and weights - $encoding" {
  379. r del seta
  380. r sadd seta a
  381. r sadd seta b
  382. r sadd seta c
  383. assert_equal 2 [r zinterstore zsetc 2 seta zsetb weights 2 3]
  384. assert_equal {b 5 c 8} [r zrange zsetc 0 -1 withscores]
  385. }
  386. test "ZINTERSTORE with AGGREGATE MIN - $encoding" {
  387. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate min]
  388. assert_equal {b 1 c 2} [r zrange zsetc 0 -1 withscores]
  389. }
  390. test "ZINTERSTORE with AGGREGATE MAX - $encoding" {
  391. assert_equal 2 [r zinterstore zsetc 2 zseta zsetb aggregate max]
  392. assert_equal {b 2 c 3} [r zrange zsetc 0 -1 withscores]
  393. }
  394. foreach cmd {ZUNIONSTORE ZINTERSTORE} {
  395. test "$cmd with +inf/-inf scores - $encoding" {
  396. r del zsetinf1 zsetinf2
  397. r zadd zsetinf1 +inf key
  398. r zadd zsetinf2 +inf key
  399. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  400. assert_equal inf [r zscore zsetinf3 key]
  401. r zadd zsetinf1 -inf key
  402. r zadd zsetinf2 +inf key
  403. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  404. assert_equal 0 [r zscore zsetinf3 key]
  405. r zadd zsetinf1 +inf key
  406. r zadd zsetinf2 -inf key
  407. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  408. assert_equal 0 [r zscore zsetinf3 key]
  409. r zadd zsetinf1 -inf key
  410. r zadd zsetinf2 -inf key
  411. r $cmd zsetinf3 2 zsetinf1 zsetinf2
  412. assert_equal -inf [r zscore zsetinf3 key]
  413. }
  414. test "$cmd with NaN weights $encoding" {
  415. r del zsetinf1 zsetinf2
  416. r zadd zsetinf1 1.0 key
  417. r zadd zsetinf2 1.0 key
  418. assert_error "*weight value is not a double*" {
  419. r $cmd zsetinf3 2 zsetinf1 zsetinf2 weights nan nan
  420. }
  421. }
  422. }
  423. }
  424. basics ziplist
  425. basics skiplist
  426. test {ZINTERSTORE regression with two sets, intset+hashtable} {
  427. r del seta setb setc
  428. r sadd set1 a
  429. r sadd set2 10
  430. r zinterstore set3 2 set1 set2
  431. } {0}
  432. test {ZUNIONSTORE regression, should not create NaN in scores} {
  433. r zadd z -inf neginf
  434. r zunionstore out 1 z weights 0
  435. r zrange out 0 -1 withscores
  436. } {neginf 0}
  437. test {ZINTERSTORE #516 regression, mixed sets and ziplist zsets} {
  438. r sadd one 100 101 102 103
  439. r sadd two 100 200 201 202
  440. r zadd three 1 500 1 501 1 502 1 503 1 100
  441. r zinterstore to_here 3 one two three WEIGHTS 0 0 1
  442. r zrange to_here 0 -1
  443. } {100}
  444. proc stressers {encoding} {
  445. if {$encoding == "ziplist"} {
  446. # Little extra to allow proper fuzzing in the sorting stresser
  447. r config set zset-max-ziplist-entries 256
  448. r config set zset-max-ziplist-value 64
  449. set elements 128
  450. } elseif {$encoding == "skiplist"} {
  451. r config set zset-max-ziplist-entries 0
  452. r config set zset-max-ziplist-value 0
  453. if {$::accurate} {set elements 1000} else {set elements 100}
  454. } else {
  455. puts "Unknown sorted set encoding"
  456. exit
  457. }
  458. test "ZSCORE - $encoding" {
  459. r del zscoretest
  460. set aux {}
  461. for {set i 0} {$i < $elements} {incr i} {
  462. set score [expr rand()]
  463. lappend aux $score
  464. r zadd zscoretest $score $i
  465. }
  466. assert_encoding $encoding zscoretest
  467. for {set i 0} {$i < $elements} {incr i} {
  468. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  469. }
  470. }
  471. test "ZSCORE after a DEBUG RELOAD - $encoding" {
  472. r del zscoretest
  473. set aux {}
  474. for {set i 0} {$i < $elements} {incr i} {
  475. set score [expr rand()]
  476. lappend aux $score
  477. r zadd zscoretest $score $i
  478. }
  479. r debug reload
  480. assert_encoding $encoding zscoretest
  481. for {set i 0} {$i < $elements} {incr i} {
  482. assert_equal [lindex $aux $i] [r zscore zscoretest $i]
  483. }
  484. }
  485. test "ZSET sorting stresser - $encoding" {
  486. set delta 0
  487. for {set test 0} {$test < 2} {incr test} {
  488. unset -nocomplain auxarray
  489. array set auxarray {}
  490. set auxlist {}
  491. r del myzset
  492. for {set i 0} {$i < $elements} {incr i} {
  493. if {$test == 0} {
  494. set score [expr rand()]
  495. } else {
  496. set score [expr int(rand()*10)]
  497. }
  498. set auxarray($i) $score
  499. r zadd myzset $score $i
  500. # Random update
  501. if {[expr rand()] < .2} {
  502. set j [expr int(rand()*1000)]
  503. if {$test == 0} {
  504. set score [expr rand()]
  505. } else {
  506. set score [expr int(rand()*10)]
  507. }
  508. set auxarray($j) $score
  509. r zadd myzset $score $j
  510. }
  511. }
  512. foreach {item score} [array get auxarray] {
  513. lappend auxlist [list $score $item]
  514. }
  515. set sorted [lsort -command zlistAlikeSort $auxlist]
  516. set auxlist {}
  517. foreach x $sorted {
  518. lappend auxlist [lindex $x 1]
  519. }
  520. assert_encoding $encoding myzset
  521. set fromredis [r zrange myzset 0 -1]
  522. set delta 0
  523. for {set i 0} {$i < [llength $fromredis]} {incr i} {
  524. if {[lindex $fromredis $i] != [lindex $auxlist $i]} {
  525. incr delta
  526. }
  527. }
  528. }
  529. assert_equal 0 $delta
  530. }
  531. test "ZRANGEBYSCORE fuzzy test, 100 ranges in $elements element sorted set - $encoding" {
  532. set err {}
  533. r del zset
  534. for {set i 0} {$i < $elements} {incr i} {
  535. r zadd zset [expr rand()] $i
  536. }
  537. assert_encoding $encoding zset
  538. for {set i 0} {$i < 100} {incr i} {
  539. set min [expr rand()]
  540. set max [expr rand()]
  541. if {$min > $max} {
  542. set aux $min
  543. set min $max
  544. set max $aux
  545. }
  546. set low [r zrangebyscore zset -inf $min]
  547. set ok [r zrangebyscore zset $min $max]
  548. set high [r zrangebyscore zset $max +inf]
  549. set lowx [r zrangebyscore zset -inf ($min]
  550. set okx [r zrangebyscore zset ($min ($max]
  551. set highx [r zrangebyscore zset ($max +inf]
  552. if {[r zcount zset -inf $min] != [llength $low]} {
  553. append err "Error, len does not match zcount\n"
  554. }
  555. if {[r zcount zset $min $max] != [llength $ok]} {
  556. append err "Error, len does not match zcount\n"
  557. }
  558. if {[r zcount zset $max +inf] != [llength $high]} {
  559. append err "Error, len does not match zcount\n"
  560. }
  561. if {[r zcount zset -inf ($min] != [llength $lowx]} {
  562. append err "Error, len does not match zcount\n"
  563. }
  564. if {[r zcount zset ($min ($max] != [llength $okx]} {
  565. append err "Error, len does not match zcount\n"
  566. }
  567. if {[r zcount zset ($max +inf] != [llength $highx]} {
  568. append err "Error, len does not match zcount\n"
  569. }
  570. foreach x $low {
  571. set score [r zscore zset $x]
  572. if {$score > $min} {
  573. append err "Error, score for $x is $score > $min\n"
  574. }
  575. }
  576. foreach x $lowx {
  577. set score [r zscore zset $x]
  578. if {$score >= $min} {
  579. append err "Error, score for $x is $score >= $min\n"
  580. }
  581. }
  582. foreach x $ok {
  583. set score [r zscore zset $x]
  584. if {$score < $min || $score > $max} {
  585. append err "Error, score for $x is $score outside $min-$max range\n"
  586. }
  587. }
  588. foreach x $okx {
  589. set score [r zscore zset $x]
  590. if {$score <= $min || $score >= $max} {
  591. append err "Error, score for $x is $score outside $min-$max open range\n"
  592. }
  593. }
  594. foreach x $high {
  595. set score [r zscore zset $x]
  596. if {$score < $max} {
  597. append err "Error, score for $x is $score < $max\n"
  598. }
  599. }
  600. foreach x $highx {
  601. set score [r zscore zset $x]
  602. if {$score <= $max} {
  603. append err "Error, score for $x is $score <= $max\n"
  604. }
  605. }
  606. }
  607. assert_equal {} $err
  608. }
  609. test "ZSETs skiplist implementation backlink consistency test - $encoding" {
  610. set diff 0
  611. for {set j 0} {$j < $elements} {incr j} {
  612. r zadd myzset [expr rand()] "Element-$j"
  613. r zrem myzset "Element-[expr int(rand()*$elements)]"
  614. }
  615. assert_encoding $encoding myzset
  616. set l1 [r zrange myzset 0 -1]
  617. set l2 [r zrevrange myzset 0 -1]
  618. for {set j 0} {$j < [llength $l1]} {incr j} {
  619. if {[lindex $l1 $j] ne [lindex $l2 end-$j]} {
  620. incr diff
  621. }
  622. }
  623. assert_equal 0 $diff
  624. }
  625. test "ZSETs ZRANK augmented skip list stress testing - $encoding" {
  626. set err {}
  627. r del myzset
  628. for {set k 0} {$k < 2000} {incr k} {
  629. set i [expr {$k % $elements}]
  630. if {[expr rand()] < .2} {
  631. r zrem myzset $i
  632. } else {
  633. set score [expr rand()]
  634. r zadd myzset $score $i
  635. assert_encoding $encoding myzset
  636. }
  637. set card [r zcard myzset]
  638. if {$card > 0} {
  639. set index [randomInt $card]
  640. set ele [lindex [r zrange myzset $index $index] 0]
  641. set rank [r zrank myzset $ele]
  642. if {$rank != $index} {
  643. set err "$ele RANK is wrong! ($rank != $index)"
  644. break
  645. }
  646. }
  647. }
  648. assert_equal {} $err
  649. }
  650. }
  651. tags {"slow"} {
  652. stressers ziplist
  653. stressers skiplist
  654. }
  655. }