Skip to content

Commit 905f09a

Browse files
committed
Fix test to randomize with lambda
1 parent e1e003c commit 905f09a

File tree

1 file changed

+40
-1
lines changed

1 file changed

+40
-1
lines changed

tutorial/tests/test_12_functions_advanced.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,46 @@ def test_randomize_list(
2929
function_to_test: t.Callable,
3030
my_list: list[int],
3131
):
32-
assert function_to_test(my_list) == reference_randomize_list(my_list)
32+
# We need to verify:
33+
# 1. Content is preserved (same elements)
34+
# 2. Randomization occurs (not sorted, not original order)
35+
36+
# Skip randomization tests for trivial lists
37+
if len(my_list) <= 1:
38+
return
39+
40+
# Run the function multiple times to ensure randomization
41+
results = []
42+
for _ in range(3): # Run multiple times
43+
# Use a copy to prevent the function from modifying the original
44+
test_list = my_list.copy()
45+
result = function_to_test(test_list)
46+
47+
# 1. Check the same elements are present
48+
assert set(result) == set(my_list)
49+
# 2. Check the length is preserved
50+
assert len(result) == len(my_list)
51+
52+
results.append(result)
53+
54+
# 3. Check that we get different orderings (randomization)
55+
# For lists with enough elements, very unlikely to get the same result twice
56+
if len(my_list) > 2:
57+
# Check there's at least some variation in results
58+
assert len({tuple(r) for r in results}) > 1, (
59+
"Function doesn't appear to randomize"
60+
)
61+
62+
# Ensure it's not just returning sorted lists
63+
sorted_list = sorted(my_list)
64+
assert not all(r == sorted_list for r in results), (
65+
"Function appears to just sort the list"
66+
)
67+
68+
# Ensure it's not just returning the original list
69+
assert not all(r == my_list for r in results), (
70+
"Function appears to return the original list"
71+
)
3372

3473

3574
#

0 commit comments

Comments
 (0)