Skip to content

Commit 0ee3f05

Browse files
committed
Test for SRANDMEMBER with <count>.
1 parent 578c945 commit 0ee3f05

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

Diff for: tests/unit/type/set.tcl

+112
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,118 @@ start_server {
271271
}
272272
}
273273

274+
test "SRANDMEMBER with <count> against non existing key" {
275+
r srandmember nonexisting_key 100
276+
} {}
277+
278+
foreach {type contents} {
279+
hashtable {
280+
1 5 10 50 125 50000 33959417 4775547 65434162
281+
12098459 427716 483706 2726473884 72615637475
282+
MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA
283+
SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN
284+
SANDRA DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH
285+
KIMBERLY DEBORAH JESSICA SHIRLEY CYNTHIA ANGELA MELISSA
286+
BRENDA AMY ANNA REBECCA VIRGINIA KATHLEEN
287+
}
288+
intset {
289+
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
290+
20 21 22 23 24 25 26 27 28 29
291+
30 31 32 33 34 35 36 37 38 39
292+
40 41 42 43 44 45 46 47 48 49
293+
}
294+
} {
295+
test "SRANDMEMBER with <count> - $type" {
296+
create_set myset $contents
297+
unset -nocomplain myset
298+
array set myset {}
299+
foreach ele [r smembers myset] {
300+
set myset($ele) 1
301+
}
302+
assert_equal [lsort $contents] [lsort [array names myset]]
303+
304+
# Make sure that a count of 0 is handled correctly.
305+
assert_equal [r srandmember myset 0] {}
306+
307+
# We'll stress different parts of the code, see the implementation
308+
# of SRANDMEMBER for more information, but basically there are
309+
# four different code paths.
310+
#
311+
# PATH 1: Use negative count.
312+
#
313+
# 1) Check that it returns repeated elements.
314+
set res [r srandmember myset -100]
315+
assert_equal [llength $res] 100
316+
317+
# 2) Check that all the elements actually belong to the
318+
# original set.
319+
foreach ele $res {
320+
assert {[info exists myset($ele)]}
321+
}
322+
323+
# 3) Check that eventually all the elements are returned.
324+
unset -nocomplain auxset
325+
set iterations 1000
326+
while {$iterations != 0} {
327+
incr iterations -1
328+
set res [r srandmember myset -10]
329+
foreach ele $res {
330+
set auxset($ele) 1
331+
}
332+
if {[lsort [array names myset]] eq
333+
[lsort [array names auxset]]} {
334+
break;
335+
}
336+
}
337+
assert {$iterations != 0}
338+
339+
# PATH 2: positive count (unique behavior) with requested size
340+
# equal or greater than set size.
341+
foreach size {50 100} {
342+
set res [r srandmember myset $size]
343+
assert_equal [llength $res] 50
344+
assert_equal [lsort $res] [lsort [array names myset]]
345+
}
346+
347+
# PATH 3: Ask almost as elements as there are in the set.
348+
# In this case the implementation will duplicate the original
349+
# set and will remove random elements up to the requested size.
350+
#
351+
# PATH 4: Ask a number of elements definitely smaller than
352+
# the set size.
353+
#
354+
# We can test both the code paths just changing the size but
355+
# using the same code.
356+
357+
foreach size {45 5} {
358+
set res [r srandmember myset $size]
359+
assert_equal [llength $res] $size
360+
361+
# 1) Check that all the elements actually belong to the
362+
# original set.
363+
foreach ele $res {
364+
assert {[info exists myset($ele)]}
365+
}
366+
367+
# 2) Check that eventually all the elements are returned.
368+
unset -nocomplain auxset
369+
set iterations 1000
370+
while {$iterations != 0} {
371+
incr iterations -1
372+
set res [r srandmember myset -10]
373+
foreach ele $res {
374+
set auxset($ele) 1
375+
}
376+
if {[lsort [array names myset]] eq
377+
[lsort [array names auxset]]} {
378+
break;
379+
}
380+
}
381+
assert {$iterations != 0}
382+
}
383+
}
384+
}
385+
274386
proc setup_move {} {
275387
r del myset3 myset4
276388
create_set myset1 {1 a b}

0 commit comments

Comments
 (0)