set.tcl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 "SPOP with <count>=1 - $type" {
  258. create_set myset $contents
  259. assert_encoding $type myset
  260. assert_equal $contents [lsort [list [r spop myset 1] [r spop myset 1] [r spop myset 1]]]
  261. assert_equal 0 [r scard myset]
  262. }
  263. test "SRANDMEMBER - $type" {
  264. create_set myset $contents
  265. unset -nocomplain myset
  266. array set myset {}
  267. for {set i 0} {$i < 100} {incr i} {
  268. set myset([r srandmember myset]) 1
  269. }
  270. assert_equal $contents [lsort [array names myset]]
  271. }
  272. }
  273. foreach {type contents} {
  274. hashtable {a b c d e f g h i j k l m n o p q r s t u v w x y z}
  275. intset {1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 3 4 5 6 7 8 9}
  276. } {
  277. test "SPOP with <count>" {
  278. create_set myset $contents
  279. assert_encoding $type myset
  280. assert_equal $contents [lsort [concat [r spop myset 11] [r spop myset 9] [r spop myset 0] [r spop myset 4] [r spop myset 1] [r spop myset 0] [r spop myset 1] [r spop myset 0]]]
  281. assert_equal 0 [r scard myset]
  282. }
  283. }
  284. # As seen in intsetRandomMembers
  285. test "SPOP using integers, testing Knuth's and Floyd's algorithm" {
  286. create_set myset {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  287. assert_encoding intset myset
  288. assert_equal 20 [r scard myset]
  289. r spop myset 1
  290. assert_equal 19 [r scard myset]
  291. r spop myset 2
  292. assert_equal 17 [r scard myset]
  293. r spop myset 3
  294. assert_equal 14 [r scard myset]
  295. r spop myset 10
  296. assert_equal 4 [r scard myset]
  297. r spop myset 10
  298. assert_equal 0 [r scard myset]
  299. r spop myset 1
  300. assert_equal 0 [r scard myset]
  301. } {}
  302. test "SPOP using integers with Knuth's algorithm" {
  303. r spop nonexisting_key 100
  304. } {}
  305. test "SPOP new implementation: code path #1" {
  306. set content {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  307. create_set myset $content
  308. set res [r spop myset 30]
  309. assert {[lsort $content] eq [lsort $res]}
  310. }
  311. test "SPOP new implementation: code path #2" {
  312. set content {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  313. create_set myset $content
  314. set res [r spop myset 2]
  315. assert {[llength $res] == 2}
  316. assert {[r scard myset] == 18}
  317. set union [concat [r smembers myset] $res]
  318. assert {[lsort $union] eq [lsort $content]}
  319. }
  320. test "SPOP new implementation: code path #3" {
  321. set content {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20}
  322. create_set myset $content
  323. set res [r spop myset 18]
  324. assert {[llength $res] == 18}
  325. assert {[r scard myset] == 2}
  326. set union [concat [r smembers myset] $res]
  327. assert {[lsort $union] eq [lsort $content]}
  328. }
  329. test "SRANDMEMBER with <count> against non existing key" {
  330. r srandmember nonexisting_key 100
  331. } {}
  332. foreach {type contents} {
  333. hashtable {
  334. 1 5 10 50 125 50000 33959417 4775547 65434162
  335. 12098459 427716 483706 2726473884 72615637475
  336. MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA
  337. SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN
  338. SANDRA DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH
  339. KIMBERLY DEBORAH JESSICA SHIRLEY CYNTHIA ANGELA MELISSA
  340. BRENDA AMY ANNA REBECCA VIRGINIA KATHLEEN
  341. }
  342. intset {
  343. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  344. 20 21 22 23 24 25 26 27 28 29
  345. 30 31 32 33 34 35 36 37 38 39
  346. 40 41 42 43 44 45 46 47 48 49
  347. }
  348. } {
  349. test "SRANDMEMBER with <count> - $type" {
  350. create_set myset $contents
  351. unset -nocomplain myset
  352. array set myset {}
  353. foreach ele [r smembers myset] {
  354. set myset($ele) 1
  355. }
  356. assert_equal [lsort $contents] [lsort [array names myset]]
  357. # Make sure that a count of 0 is handled correctly.
  358. assert_equal [r srandmember myset 0] {}
  359. # We'll stress different parts of the code, see the implementation
  360. # of SRANDMEMBER for more information, but basically there are
  361. # four different code paths.
  362. #
  363. # PATH 1: Use negative count.
  364. #
  365. # 1) Check that it returns repeated elements.
  366. set res [r srandmember myset -100]
  367. assert_equal [llength $res] 100
  368. # 2) Check that all the elements actually belong to the
  369. # original set.
  370. foreach ele $res {
  371. assert {[info exists myset($ele)]}
  372. }
  373. # 3) Check that eventually all the elements are returned.
  374. unset -nocomplain auxset
  375. set iterations 1000
  376. while {$iterations != 0} {
  377. incr iterations -1
  378. set res [r srandmember myset -10]
  379. foreach ele $res {
  380. set auxset($ele) 1
  381. }
  382. if {[lsort [array names myset]] eq
  383. [lsort [array names auxset]]} {
  384. break;
  385. }
  386. }
  387. assert {$iterations != 0}
  388. # PATH 2: positive count (unique behavior) with requested size
  389. # equal or greater than set size.
  390. foreach size {50 100} {
  391. set res [r srandmember myset $size]
  392. assert_equal [llength $res] 50
  393. assert_equal [lsort $res] [lsort [array names myset]]
  394. }
  395. # PATH 3: Ask almost as elements as there are in the set.
  396. # In this case the implementation will duplicate the original
  397. # set and will remove random elements up to the requested size.
  398. #
  399. # PATH 4: Ask a number of elements definitely smaller than
  400. # the set size.
  401. #
  402. # We can test both the code paths just changing the size but
  403. # using the same code.
  404. foreach size {45 5} {
  405. set res [r srandmember myset $size]
  406. assert_equal [llength $res] $size
  407. # 1) Check that all the elements actually belong to the
  408. # original set.
  409. foreach ele $res {
  410. assert {[info exists myset($ele)]}
  411. }
  412. # 2) Check that eventually all the elements are returned.
  413. unset -nocomplain auxset
  414. set iterations 1000
  415. while {$iterations != 0} {
  416. incr iterations -1
  417. set res [r srandmember myset -10]
  418. foreach ele $res {
  419. set auxset($ele) 1
  420. }
  421. if {[lsort [array names myset]] eq
  422. [lsort [array names auxset]]} {
  423. break;
  424. }
  425. }
  426. assert {$iterations != 0}
  427. }
  428. }
  429. }
  430. proc setup_move {} {
  431. r del myset3 myset4
  432. create_set myset1 {1 a b}
  433. create_set myset2 {2 3 4}
  434. assert_encoding hashtable myset1
  435. assert_encoding intset myset2
  436. }
  437. test "SMOVE basics - from regular set to intset" {
  438. # move a non-integer element to an intset should convert encoding
  439. setup_move
  440. assert_equal 1 [r smove myset1 myset2 a]
  441. assert_equal {1 b} [lsort [r smembers myset1]]
  442. assert_equal {2 3 4 a} [lsort [r smembers myset2]]
  443. assert_encoding hashtable myset2
  444. # move an integer element should not convert the encoding
  445. setup_move
  446. assert_equal 1 [r smove myset1 myset2 1]
  447. assert_equal {a b} [lsort [r smembers myset1]]
  448. assert_equal {1 2 3 4} [lsort [r smembers myset2]]
  449. assert_encoding intset myset2
  450. }
  451. test "SMOVE basics - from intset to regular set" {
  452. setup_move
  453. assert_equal 1 [r smove myset2 myset1 2]
  454. assert_equal {1 2 a b} [lsort [r smembers myset1]]
  455. assert_equal {3 4} [lsort [r smembers myset2]]
  456. }
  457. test "SMOVE non existing key" {
  458. setup_move
  459. assert_equal 0 [r smove myset1 myset2 foo]
  460. assert_equal {1 a b} [lsort [r smembers myset1]]
  461. assert_equal {2 3 4} [lsort [r smembers myset2]]
  462. }
  463. test "SMOVE non existing src set" {
  464. setup_move
  465. assert_equal 0 [r smove noset myset2 foo]
  466. assert_equal {2 3 4} [lsort [r smembers myset2]]
  467. }
  468. test "SMOVE from regular set to non existing destination set" {
  469. setup_move
  470. assert_equal 1 [r smove myset1 myset3 a]
  471. assert_equal {1 b} [lsort [r smembers myset1]]
  472. assert_equal {a} [lsort [r smembers myset3]]
  473. assert_encoding hashtable myset3
  474. }
  475. test "SMOVE from intset to non existing destination set" {
  476. setup_move
  477. assert_equal 1 [r smove myset2 myset3 2]
  478. assert_equal {3 4} [lsort [r smembers myset2]]
  479. assert_equal {2} [lsort [r smembers myset3]]
  480. assert_encoding intset myset3
  481. }
  482. test "SMOVE wrong src key type" {
  483. r set x 10
  484. assert_error "WRONGTYPE*" {r smove x myset2 foo}
  485. }
  486. test "SMOVE wrong dst key type" {
  487. r set x 10
  488. assert_error "WRONGTYPE*" {r smove myset2 x foo}
  489. }
  490. test "SMOVE with identical source and destination" {
  491. r del set
  492. r sadd set a b c
  493. r smove set set b
  494. lsort [r smembers set]
  495. } {a b c}
  496. tags {slow} {
  497. test {intsets implementation stress testing} {
  498. for {set j 0} {$j < 20} {incr j} {
  499. unset -nocomplain s
  500. array set s {}
  501. r del s
  502. set len [randomInt 1024]
  503. for {set i 0} {$i < $len} {incr i} {
  504. randpath {
  505. set data [randomInt 65536]
  506. } {
  507. set data [randomInt 4294967296]
  508. } {
  509. set data [randomInt 18446744073709551616]
  510. }
  511. set s($data) {}
  512. r sadd s $data
  513. }
  514. assert_equal [lsort [r smembers s]] [lsort [array names s]]
  515. set len [array size s]
  516. for {set i 0} {$i < $len} {incr i} {
  517. set e [r spop s]
  518. if {![info exists s($e)]} {
  519. puts "Can't find '$e' on local array"
  520. puts "Local array: [lsort [r smembers s]]"
  521. puts "Remote array: [lsort [array names s]]"
  522. error "exception"
  523. }
  524. array unset s $e
  525. }
  526. assert_equal [r scard s] 0
  527. assert_equal [array size s] 0
  528. }
  529. }
  530. }
  531. }