4 "set-max-intset-entries" 512
7 proc create_set
{key entries
} {
9 foreach entry $entries { r sadd
$key $entry }
12 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
- regular
set} {
13 create_set myset
{foo
}
14 assert_encoding hashtable myset
15 assert_equal
1 [r sadd myset bar
]
16 assert_equal
0 [r sadd myset bar
]
17 assert_equal
2 [r scard myset
]
18 assert_equal
1 [r sismember myset foo
]
19 assert_equal
1 [r sismember myset bar
]
20 assert_equal
0 [r sismember myset bla
]
21 assert_equal
{bar foo
} [lsort [r smembers myset
]]
24 test
{SADD
, SCARD
, SISMEMBER
, SMEMBERS basics
- intset
} {
26 assert_encoding intset myset
27 assert_equal
1 [r sadd myset
16]
28 assert_equal
0 [r sadd myset
16]
29 assert_equal
2 [r scard myset
]
30 assert_equal
1 [r sismember myset
16]
31 assert_equal
1 [r sismember myset
17]
32 assert_equal
0 [r sismember myset
18]
33 assert_equal
{16 17} [lsort [r smembers myset
]]
36 test
{SADD against non
set} {
38 assert_error ERR
*kind
* {r sadd mylist bar
}
41 test
"SADD a non-integer against an intset" {
42 create_set myset
{1 2 3}
43 assert_encoding intset myset
44 assert_equal
1 [r sadd myset a
]
45 assert_encoding hashtable myset
48 test
"SADD an integer larger than 64 bits" {
49 create_set myset
{213244124402402314402033402}
50 assert_encoding hashtable myset
51 assert_equal
1 [r sismember myset
213244124402402314402033402]
54 test
"SADD overflows the maximum allowed integers in an intset" {
56 for {set i
0} {$i < 512} {incr i
} { r sadd myset
$i }
57 assert_encoding intset myset
58 assert_equal
1 [r sadd myset
512]
59 assert_encoding hashtable myset
62 test
{Variadic SADD
} {
64 assert_equal
3 [r sadd myset a b c
]
65 assert_equal
2 [r sadd myset A a b c B
]
66 assert_equal
[lsort {A a b c B
}] [lsort [r smembers myset
]]
69 test
"Set encoding after DEBUG RELOAD" {
70 r del myintset myhashset mylargeintset
71 for {set i
0} {$i < 100} {incr i
} { r sadd myintset
$i }
72 for {set i
0} {$i < 1280} {incr i
} { r sadd mylargeintset
$i }
73 for {set i
0} {$i < 256} {incr i
} { r sadd myhashset
[format "i%03d" $i] }
74 assert_encoding intset myintset
75 assert_encoding hashtable mylargeintset
76 assert_encoding hashtable myhashset
79 assert_encoding intset myintset
80 assert_encoding hashtable mylargeintset
81 assert_encoding hashtable myhashset
84 test
{SREM basics
- regular
set} {
85 create_set myset
{foo bar ciao
}
86 assert_encoding hashtable myset
87 assert_equal
0 [r srem myset qux
]
88 assert_equal
1 [r srem myset foo
]
89 assert_equal
{bar ciao
} [lsort [r smembers myset
]]
92 test
{SREM basics
- intset
} {
93 create_set myset
{3 4 5}
94 assert_encoding intset myset
95 assert_equal
0 [r srem myset
6]
96 assert_equal
1 [r srem myset
4]
97 assert_equal
{3 5} [lsort [r smembers myset
]]
100 test
{SREM with multiple arguments
} {
103 assert_equal
0 [r srem myset k k k
]
104 assert_equal
2 [r srem myset b d x y
]
105 lsort [r smembers myset
]
108 foreach {type
} {hashtable intset
} {
109 for {set i
1} {$i <= 5} {incr i
} {
110 r del
[format "set%d" $i]
112 for {set i
0} {$i < 200} {incr i
} {
114 r sadd set2
[expr $i+195]
116 foreach i
{199 195 1000 2000} {
119 for {set i
5} {$i < 200} {incr i
} {
124 # To make sure the sets are encoded as the type we are testing -- also
125 # when the VM is enabled and the values may be swapped in and out
126 # while the tests are running -- an extra element is added to every
127 # set that determines its encoding.
129 if {$type eq
"hashtable"} {
133 for {set i
1} {$i <= 5} {incr i
} {
134 r sadd
[format "set%d" $i] $large
137 test
"Generated sets must be encoded as $type" {
138 for {set i
1} {$i <= 5} {incr i
} {
139 assert_encoding
$type [format "set%d" $i]
143 test
"SINTER with two sets - $type" {
144 assert_equal
[list 195 196 197 198 199 $large] [lsort [r sinter set1 set2
]]
147 test
"SINTERSTORE with two sets - $type" {
148 r sinterstore setres set1 set2
149 assert_encoding
$type setres
150 assert_equal
[list 195 196 197 198 199 $large] [lsort [r smembers setres
]]
153 test
"SINTERSTORE with two sets, after a DEBUG RELOAD - $type" {
155 r sinterstore setres set1 set2
156 assert_encoding
$type setres
157 assert_equal
[list 195 196 197 198 199 $large] [lsort [r smembers setres
]]
160 test
"SUNION with two sets - $type" {
161 set expected
[lsort -uniq "[r smembers set1] [r smembers set2]"]
162 assert_equal
$expected [lsort [r sunion set1 set2
]]
165 test
"SUNIONSTORE with two sets - $type" {
166 r sunionstore setres set1 set2
167 assert_encoding
$type setres
168 set expected
[lsort -uniq "[r smembers set1] [r smembers set2]"]
169 assert_equal
$expected [lsort [r smembers setres
]]
172 test
"SINTER against three sets - $type" {
173 assert_equal
[list 195 199 $large] [lsort [r sinter set1 set2 set3
]]
176 test
"SINTERSTORE with three sets - $type" {
177 r sinterstore setres set1 set2 set3
178 assert_equal
[list 195 199 $large] [lsort [r smembers setres
]]
181 test
"SUNION with non existing keys - $type" {
182 set expected
[lsort -uniq "[r smembers set1] [r smembers set2]"]
183 assert_equal
$expected [lsort [r sunion nokey1 set1 set2 nokey2
]]
186 test
"SDIFF with two sets - $type" {
187 assert_equal
{0 1 2 3 4} [lsort [r sdiff set1 set4
]]
190 test
"SDIFF with three sets - $type" {
191 assert_equal
{1 2 3 4} [lsort [r sdiff set1 set4 set5
]]
194 test
"SDIFFSTORE with three sets - $type" {
195 r sdiffstore setres set1 set4 set5
196 # The type is determined by type of the first key to diff against.
197 # See the implementation for more information.
198 assert_encoding
$type setres
199 assert_equal
{1 2 3 4} [lsort [r smembers setres
]]
203 test
"SINTER against non-set should throw error" {
205 assert_error
"ERR*wrong kind*" {r sinter key1 noset
}
208 test
"SUNION against non-set should throw error" {
210 assert_error
"ERR*wrong kind*" {r sunion key1 noset
}
213 test
"SINTERSTORE against non existing keys should delete dstkey" {
215 assert_equal
0 [r sinterstore setres foo111 bar222
]
216 assert_equal
0 [r exists setres
]
219 test
"SUNIONSTORE against non existing keys should delete dstkey" {
221 assert_equal
0 [r sunionstore setres foo111 bar222
]
222 assert_equal
0 [r exists setres
]
225 foreach {type contents
} {hashtable
{a b c
} intset
{1 2 3}} {
226 test
"SPOP basics - $type" {
227 create_set myset
$contents
228 assert_encoding
$type myset
229 assert_equal
$contents [lsort [list [r spop myset
] [r spop myset
] [r spop myset
]]]
230 assert_equal
0 [r scard myset
]
233 test
"SRANDMEMBER - $type" {
234 create_set myset
$contents
235 unset -nocomplain myset
237 for {set i
0} {$i < 100} {incr i
} {
238 set myset
([r srandmember myset
]) 1
240 assert_equal
$contents [lsort [array names myset
]]
246 create_set myset1
{1 a b
}
247 create_set myset2
{2 3 4}
248 assert_encoding hashtable myset1
249 assert_encoding intset myset2
252 test
"SMOVE basics - from regular set to intset" {
253 # move a non-integer element to an intset should convert encoding
255 assert_equal
1 [r smove myset1 myset2 a
]
256 assert_equal
{1 b
} [lsort [r smembers myset1
]]
257 assert_equal
{2 3 4 a
} [lsort [r smembers myset2
]]
258 assert_encoding hashtable myset2
260 # move an integer element should not convert the encoding
262 assert_equal
1 [r smove myset1 myset2
1]
263 assert_equal
{a b
} [lsort [r smembers myset1
]]
264 assert_equal
{1 2 3 4} [lsort [r smembers myset2
]]
265 assert_encoding intset myset2
268 test
"SMOVE basics - from intset to regular set" {
270 assert_equal
1 [r smove myset2 myset1
2]
271 assert_equal
{1 2 a b
} [lsort [r smembers myset1
]]
272 assert_equal
{3 4} [lsort [r smembers myset2
]]
275 test
"SMOVE non existing key" {
277 assert_equal
0 [r smove myset1 myset2 foo
]
278 assert_equal
{1 a b
} [lsort [r smembers myset1
]]
279 assert_equal
{2 3 4} [lsort [r smembers myset2
]]
282 test
"SMOVE non existing src set" {
284 assert_equal
0 [r smove noset myset2 foo
]
285 assert_equal
{2 3 4} [lsort [r smembers myset2
]]
288 test
"SMOVE from regular set to non existing destination set" {
290 assert_equal
1 [r smove myset1 myset3 a
]
291 assert_equal
{1 b
} [lsort [r smembers myset1
]]
292 assert_equal
{a
} [lsort [r smembers myset3
]]
293 assert_encoding hashtable myset3
296 test
"SMOVE from intset to non existing destination set" {
298 assert_equal
1 [r smove myset2 myset3
2]
299 assert_equal
{3 4} [lsort [r smembers myset2
]]
300 assert_equal
{2} [lsort [r smembers myset3
]]
301 assert_encoding intset myset3
304 test
"SMOVE wrong src key type" {
306 assert_error
"ERR*wrong kind*" {r smove x myset2 foo
}
309 test
"SMOVE wrong dst key type" {
311 assert_error
"ERR*wrong kind*" {r smove myset2 x foo
}
315 test
{intsets implementation stress testing
} {
316 for {set j
0} {$j < 20} {incr j
} {
320 set len
[randomInt
1024]
321 for {set i
0} {$i < $len} {incr i
} {
323 set data
[randomInt
65536]
325 set data
[randomInt
4294967296]
327 set data
[randomInt
18446744073709551616]
332 assert_equal
[lsort [r smembers s
]] [lsort [array names s
]]
333 set len
[array size s
]
334 for {set i
0} {$i < $len} {incr i
} {
336 if {![info exists s
($e)]} {
337 puts "Can't find '$e' on local array"
338 puts "Local array: [lsort [r smembers s]]"
339 puts "Remote array: [lsort [array names s]]"
344 assert_equal
[r scard s
] 0
345 assert_equal
[array size s
] 0