set.tcl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. start_server {
  2. tags {"set"}
  3. overrides {
  4. "set-max-intset-entries" 512
  5. }
  6. } {
  7. proc create_set {key entries} {
  8. r del $key
  9. foreach entry $entries { r sadd $key $entry }
  10. }
  11. test {SADD, SCARD, SISMEMBER, SMEMBERS basics - regular set} {
  12. create_set myset {foo}
  13. assert_encoding hashtable myset
  14. assert_equal 1 [r sadd myset bar]
  15. assert_equal 0 [r sadd myset bar]
  16. assert_equal 2 [r scard myset]
  17. assert_equal 1 [r sismember myset foo]
  18. assert_equal 1 [r sismember myset bar]
  19. assert_equal 0 [r sismember myset bla]
  20. assert_equal {bar foo} [lsort [r smembers myset]]
  21. }
  22. test {SADD, SCARD, SISMEMBER, SMEMBERS basics - intset} {
  23. create_set myset {17}
  24. assert_encoding intset myset
  25. assert_equal 1 [r sadd myset 16]
  26. assert_equal 0 [r sadd myset 16]
  27. assert_equal 2 [r scard myset]
  28. assert_equal 1 [r sismember myset 16]
  29. assert_equal 1 [r sismember myset 17]
  30. assert_equal 0 [r sismember myset 18]
  31. assert_equal {16 17} [lsort [r smembers myset]]
  32. }
  33. test {SADD against non set} {
  34. r lpush mylist foo
  35. assert_error WRONGTYPE* {r sadd mylist bar}
  36. }
  37. test "SADD a non-integer against an intset" {
  38. create_set myset {1 2 3}
  39. assert_encoding intset myset
  40. assert_equal 1 [r sadd myset a]
  41. assert_encoding hashtable myset
  42. }
  43. test "SADD an integer larger than 64 bits" {
  44. create_set myset {213244124402402314402033402}
  45. assert_encoding hashtable myset
  46. assert_equal 1 [r sismember myset 213244124402402314402033402]
  47. }
  48. test "SADD overflows the maximum allowed integers in an intset" {
  49. r del myset
  50. for {set i 0} {$i < 512} {incr i} { r sadd myset $i }
  51. assert_encoding intset myset
  52. assert_equal 1 [r sadd myset 512]
  53. assert_encoding hashtable myset
  54. }
  55. test {Variadic SADD} {
  56. r del myset
  57. assert_equal 3 [r sadd myset a b c]
  58. assert_equal 2 [r sadd myset A a b c B]
  59. assert_equal [lsort {A a b c B}] [lsort [r smembers myset]]
  60. }
  61. test "Set encoding after DEBUG RELOAD" {
  62. r del myintset myhashset mylargeintset
  63. for {set i 0} {$i < 100} {incr i} { r sadd myintset $i }
  64. for {set i 0} {$i < 1280} {incr i} { r sadd mylargeintset $i }
  65. for {set i 0} {$i < 256} {incr i} { r sadd myhashset [format "i%03d" $i] }
  66. assert_encoding intset myintset
  67. assert_encoding hashtable mylargeintset
  68. assert_encoding hashtable myhashset
  69. r debug reload
  70. assert_encoding intset myintset
  71. assert_encoding hashtable mylargeintset
  72. assert_encoding hashtable myhashset
  73. }
  74. test {SREM basics - regular set} {
  75. create_set myset {foo bar ciao}
  76. assert_encoding hashtable myset
  77. assert_equal 0 [r srem myset qux]
  78. assert_equal 1 [r srem myset foo]
  79. assert_equal {bar ciao} [lsort [r smembers myset]]
  80. }
  81. test {SREM basics - intset} {
  82. create_set myset {3 4 5}
  83. assert_encoding intset myset
  84. assert_equal 0 [r srem myset 6]
  85. assert_equal 1 [r srem myset 4]
  86. assert_equal {3 5} [lsort [r smembers myset]]
  87. }
  88. test {SREM with multiple arguments} {
  89. r del myset
  90. r sadd myset a b c d
  91. assert_equal 0 [r srem myset k k k]
  92. assert_equal 2 [r srem myset b d x y]
  93. lsort [r smembers myset]
  94. } {a c}
  95. test {SREM variadic version with more args needed to destroy the key} {
  96. r del myset
  97. r sadd myset 1 2 3
  98. r srem myset 1 2 3 4 5 6 7 8
  99. } {3}
  100. foreach {type} {hashtable intset} {
  101. for {set i 1} {$i <= 5} {incr i} {
  102. r del [format "set%d" $i]
  103. }
  104. for {set i 0} {$i < 200} {incr i} {
  105. r sadd set1 $i
  106. r sadd set2 [expr $i+195]
  107. }
  108. foreach i {199 195 1000 2000} {
  109. r sadd set3 $i
  110. }
  111. for {set i 5} {$i < 200} {incr i} {
  112. r sadd set4 $i
  113. }
  114. r sadd set5 0
  115. # To make sure the sets are encoded as the type we are testing -- also
  116. # when the VM is enabled and the values may be swapped in and out
  117. # while the tests are running -- an extra element is added to every
  118. # set that determines its encoding.
  119. set large 200
  120. if {$type eq "hashtable"} {
  121. set large foo
  122. }
  123. for {set i 1} {$i <= 5} {incr i} {
  124. r sadd [format "set%d" $i] $large
  125. }
  126. test "Generated sets must be encoded as $type" {
  127. for {set i 1} {$i <= 5} {incr i} {
  128. assert_encoding $type [format "set%d" $i]
  129. }
  130. }
  131. test "SINTER with two sets - $type" {
  132. assert_equal [list 195 196 197 198 199 $large] [lsort [r sinter set1 set2]]
  133. }
  134. test "SINTERSTORE with two sets - $type" {
  135. r sinterstore setres set1 set2
  136. assert_encoding $type setres
  137. assert_equal [list 195 196 197 198 199 $large] [lsort [r smembers setres]]
  138. }
  139. test "SINTERSTORE with two sets, after a DEBUG RELOAD - $type" {
  140. r debug reload
  141. r sinterstore setres set1 set2
  142. assert_encoding $type setres
  143. assert_equal [list 195 196 197 198 199 $large] [lsort [r smembers setres]]
  144. }
  145. test "SUNION with two sets - $type" {
  146. set expected [lsort -uniq "[r smembers set1] [r smembers set2]"]
  147. assert_equal $expected [lsort [r sunion set1 set2]]
  148. }
  149. test "SUNIONSTORE with two sets - $type" {
  150. r sunionstore setres set1 set2
  151. assert_encoding $type setres
  152. set expected [lsort -uniq "[r smembers set1] [r smembers set2]"]
  153. assert_equal $expected [lsort [r smembers setres]]
  154. }
  155. test "SINTER against three sets - $type" {
  156. assert_equal [list 195 199 $large] [lsort [r sinter set1 set2 set3]]
  157. }
  158. test "SINTERSTORE with three sets - $type" {
  159. r sinterstore setres set1 set2 set3
  160. assert_equal [list 195 199 $large] [lsort [r smembers setres]]
  161. }
  162. test "SUNION with non existing keys - $type" {
  163. set expected [lsort -uniq "[r smembers set1] [r smembers set2]"]
  164. assert_equal $expected [lsort [r sunion nokey1 set1 set2 nokey2]]
  165. }
  166. test "SDIFF with two sets - $type" {
  167. assert_equal {0 1 2 3 4} [lsort [r sdiff set1 set4]]
  168. }
  169. test "SDIFF with three sets - $type" {
  170. assert_equal {1 2 3 4} [lsort [r sdiff set1 set4 set5]]
  171. }
  172. test "SDIFFSTORE with three sets - $type" {
  173. r sdiffstore setres set1 set4 set5
  174. # When we start with intsets, we should always end with intsets.
  175. if {$type eq {intset}} {
  176. assert_encoding intset setres
  177. }
  178. assert_equal {1 2 3 4} [lsort [r smembers setres]]
  179. }
  180. }
  181. test "SDIFF with first set empty" {
  182. r del set1 set2 set3
  183. r sadd set2 1 2 3 4
  184. r sadd set3 a b c d
  185. r sdiff set1 set2 set3
  186. } {}
  187. test "SDIFF with same set two times" {
  188. r del set1
  189. r sadd set1 a b c 1 2 3 4 5 6
  190. r sdiff set1 set1
  191. } {}
  192. test "SDIFF fuzzing" {
  193. for {set j 0} {$j < 100} {incr j} {
  194. unset -nocomplain s
  195. array set s {}
  196. set args {}
  197. set num_sets [expr {[randomInt 10]+1}]
  198. for {set i 0} {$i < $num_sets} {incr i} {
  199. set num_elements [randomInt 100]
  200. r del set_$i
  201. lappend args set_$i
  202. while {$num_elements} {
  203. set ele [randomValue]
  204. r sadd set_$i $ele
  205. if {$i == 0} {
  206. set s($ele) x
  207. } else {
  208. unset -nocomplain s($ele)
  209. }
  210. incr num_elements -1
  211. }
  212. }
  213. set result [lsort [r sdiff {*}$args]]
  214. assert_equal $result [lsort [array names s]]
  215. }
  216. }
  217. test "SINTER against non-set should throw error" {
  218. r set key1 x
  219. assert_error "WRONGTYPE*" {r sinter key1 noset}
  220. }
  221. test "SUNION against non-set should throw error" {
  222. r set key1 x
  223. assert_error "WRONGTYPE*" {r sunion key1 noset}
  224. }
  225. test "SINTER should handle non existing key as empty" {
  226. r del set1 set2 set3
  227. r sadd set1 a b c
  228. r sadd set2 b c d
  229. r sinter set1 set2 set3
  230. } {}
  231. test "SINTER with same integer elements but different encoding" {
  232. r del set1 set2
  233. r sadd set1 1 2 3
  234. r sadd set2 1 2 3 a
  235. r srem set2 a
  236. assert_encoding intset set1
  237. assert_encoding hashtable set2
  238. lsort [r sinter set1 set2]
  239. } {1 2 3}
  240. test "SINTERSTORE against non existing keys should delete dstkey" {
  241. r set setres xxx
  242. assert_equal 0 [r sinterstore setres foo111 bar222]
  243. assert_equal 0 [r exists setres]
  244. }
  245. test "SUNIONSTORE against non existing keys should delete dstkey" {
  246. r set setres xxx
  247. assert_equal 0 [r sunionstore setres foo111 bar222]
  248. assert_equal 0 [r exists setres]
  249. }
  250. foreach {type contents} {hashtable {a b c} intset {1 2 3}} {
  251. test "SPOP basics - $type" {
  252. create_set myset $contents
  253. assert_encoding $type myset
  254. assert_equal $contents [lsort [list [r spop myset] [r spop myset] [r spop myset]]]
  255. assert_equal 0 [r scard myset]
  256. }
  257. test "SRANDMEMBER - $type" {
  258. create_set myset $contents
  259. unset -nocomplain myset
  260. array set myset {}
  261. for {set i 0} {$i < 100} {incr i} {
  262. set myset([r srandmember myset]) 1
  263. }
  264. assert_equal $contents [lsort [array names myset]]
  265. }
  266. }
  267. test "SRANDMEMBER with <count> against non existing key" {
  268. r srandmember nonexisting_key 100
  269. } {}
  270. foreach {type contents} {
  271. hashtable {
  272. 1 5 10 50 125 50000 33959417 4775547 65434162
  273. 12098459 427716 483706 2726473884 72615637475
  274. MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA
  275. SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN
  276. SANDRA DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH
  277. KIMBERLY DEBORAH JESSICA SHIRLEY CYNTHIA ANGELA MELISSA
  278. BRENDA AMY ANNA REBECCA VIRGINIA KATHLEEN
  279. }
  280. intset {
  281. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  282. 20 21 22 23 24 25 26 27 28 29
  283. 30 31 32 33 34 35 36 37 38 39
  284. 40 41 42 43 44 45 46 47 48 49
  285. }
  286. } {
  287. test "SRANDMEMBER with <count> - $type" {
  288. create_set myset $contents
  289. unset -nocomplain myset
  290. array set myset {}
  291. foreach ele [r smembers myset] {
  292. set myset($ele) 1
  293. }
  294. assert_equal [lsort $contents] [lsort [array names myset]]
  295. # Make sure that a count of 0 is handled correctly.
  296. assert_equal [r srandmember myset 0] {}
  297. # We'll stress different parts of the code, see the implementation
  298. # of SRANDMEMBER for more information, but basically there are
  299. # four different code paths.
  300. #
  301. # PATH 1: Use negative count.
  302. #
  303. # 1) Check that it returns repeated elements.
  304. set res [r srandmember myset -100]
  305. assert_equal [llength $res] 100
  306. # 2) Check that all the elements actually belong to the
  307. # original set.
  308. foreach ele $res {
  309. assert {[info exists myset($ele)]}
  310. }
  311. # 3) Check that eventually all the elements are returned.
  312. unset -nocomplain auxset
  313. set iterations 1000
  314. while {$iterations != 0} {
  315. incr iterations -1
  316. set res [r srandmember myset -10]
  317. foreach ele $res {
  318. set auxset($ele) 1
  319. }
  320. if {[lsort [array names myset]] eq
  321. [lsort [array names auxset]]} {
  322. break;
  323. }
  324. }
  325. assert {$iterations != 0}
  326. # PATH 2: positive count (unique behavior) with requested size
  327. # equal or greater than set size.
  328. foreach size {50 100} {
  329. set res [r srandmember myset $size]
  330. assert_equal [llength $res] 50
  331. assert_equal [lsort $res] [lsort [array names myset]]
  332. }
  333. # PATH 3: Ask almost as elements as there are in the set.
  334. # In this case the implementation will duplicate the original
  335. # set and will remove random elements up to the requested size.
  336. #
  337. # PATH 4: Ask a number of elements definitely smaller than
  338. # the set size.
  339. #
  340. # We can test both the code paths just changing the size but
  341. # using the same code.
  342. foreach size {45 5} {
  343. set res [r srandmember myset $size]
  344. assert_equal [llength $res] $size
  345. # 1) Check that all the elements actually belong to the
  346. # original set.
  347. foreach ele $res {
  348. assert {[info exists myset($ele)]}
  349. }
  350. # 2) Check that eventually all the elements are returned.
  351. unset -nocomplain auxset
  352. set iterations 1000
  353. while {$iterations != 0} {
  354. incr iterations -1
  355. set res [r srandmember myset -10]
  356. foreach ele $res {
  357. set auxset($ele) 1
  358. }
  359. if {[lsort [array names myset]] eq
  360. [lsort [array names auxset]]} {
  361. break;
  362. }
  363. }
  364. assert {$iterations != 0}
  365. }
  366. }
  367. }
  368. proc setup_move {} {
  369. r del myset3 myset4
  370. create_set myset1 {1 a b}
  371. create_set myset2 {2 3 4}
  372. assert_encoding hashtable myset1
  373. assert_encoding intset myset2
  374. }
  375. test "SMOVE basics - from regular set to intset" {
  376. # move a non-integer element to an intset should convert encoding
  377. setup_move
  378. assert_equal 1 [r smove myset1 myset2 a]
  379. assert_equal {1 b} [lsort [r smembers myset1]]
  380. assert_equal {2 3 4 a} [lsort [r smembers myset2]]
  381. assert_encoding hashtable myset2
  382. # move an integer element should not convert the encoding
  383. setup_move
  384. assert_equal 1 [r smove myset1 myset2 1]
  385. assert_equal {a b} [lsort [r smembers myset1]]
  386. assert_equal {1 2 3 4} [lsort [r smembers myset2]]
  387. assert_encoding intset myset2
  388. }
  389. test "SMOVE basics - from intset to regular set" {
  390. setup_move
  391. assert_equal 1 [r smove myset2 myset1 2]
  392. assert_equal {1 2 a b} [lsort [r smembers myset1]]
  393. assert_equal {3 4} [lsort [r smembers myset2]]
  394. }
  395. test "SMOVE non existing key" {
  396. setup_move
  397. assert_equal 0 [r smove myset1 myset2 foo]
  398. assert_equal {1 a b} [lsort [r smembers myset1]]
  399. assert_equal {2 3 4} [lsort [r smembers myset2]]
  400. }
  401. test "SMOVE non existing src set" {
  402. setup_move
  403. assert_equal 0 [r smove noset myset2 foo]
  404. assert_equal {2 3 4} [lsort [r smembers myset2]]
  405. }
  406. test "SMOVE from regular set to non existing destination set" {
  407. setup_move
  408. assert_equal 1 [r smove myset1 myset3 a]
  409. assert_equal {1 b} [lsort [r smembers myset1]]
  410. assert_equal {a} [lsort [r smembers myset3]]
  411. assert_encoding hashtable myset3
  412. }
  413. test "SMOVE from intset to non existing destination set" {
  414. setup_move
  415. assert_equal 1 [r smove myset2 myset3 2]
  416. assert_equal {3 4} [lsort [r smembers myset2]]
  417. assert_equal {2} [lsort [r smembers myset3]]
  418. assert_encoding intset myset3
  419. }
  420. test "SMOVE wrong src key type" {
  421. r set x 10
  422. assert_error "WRONGTYPE*" {r smove x myset2 foo}
  423. }
  424. test "SMOVE wrong dst key type" {
  425. r set x 10
  426. assert_error "WRONGTYPE*" {r smove myset2 x foo}
  427. }
  428. test "SMOVE with identical source and destination" {
  429. r del set
  430. r sadd set a b c
  431. r smove set set b
  432. lsort [r smembers set]
  433. } {a b c}
  434. tags {slow} {
  435. test {intsets implementation stress testing} {
  436. for {set j 0} {$j < 20} {incr j} {
  437. unset -nocomplain s
  438. array set s {}
  439. r del s
  440. set len [randomInt 1024]
  441. for {set i 0} {$i < $len} {incr i} {
  442. randpath {
  443. set data [randomInt 65536]
  444. } {
  445. set data [randomInt 4294967296]
  446. } {
  447. set data [randomInt 18446744073709551616]
  448. }
  449. set s($data) {}
  450. r sadd s $data
  451. }
  452. assert_equal [lsort [r smembers s]] [lsort [array names s]]
  453. set len [array size s]
  454. for {set i 0} {$i < $len} {incr i} {
  455. set e [r spop s]
  456. if {![info exists s($e)]} {
  457. puts "Can't find '$e' on local array"
  458. puts "Local array: [lsort [r smembers s]]"
  459. puts "Remote array: [lsort [array names s]]"
  460. error "exception"
  461. }
  462. array unset s $e
  463. }
  464. assert_equal [r scard s] 0
  465. assert_equal [array size s] 0
  466. }
  467. }
  468. }
  469. }