summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_random.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2012-11-04 01:10:33 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2012-11-04 01:10:33 (GMT)
commit5e3943317d2442b82ac56eafe29cc7d76ac4f0d2 (patch)
tree94c599c6aef09fcee6168a2ad50bd09d933027b0 /Lib/test/test_random.py
parente9d08cf450e342f604fce919c6ae2905d8373881 (diff)
downloadcpython-5e3943317d2442b82ac56eafe29cc7d76ac4f0d2.zip
cpython-5e3943317d2442b82ac56eafe29cc7d76ac4f0d2.tar.gz
cpython-5e3943317d2442b82ac56eafe29cc7d76ac4f0d2.tar.bz2
Issue #15837: add some tests for random.shuffle().
Patch by Alessandro Moura.
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r--Lib/test/test_random.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index b5931ba..a9aec70 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -46,6 +46,39 @@ class TestBasicOps(unittest.TestCase):
self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
self.assertRaises(TypeError, type(self.gen), [])
+ def test_shuffle(self):
+ shuffle = self.gen.shuffle
+ lst = []
+ shuffle(lst)
+ self.assertEqual(lst, [])
+ lst = [37]
+ shuffle(lst)
+ self.assertEqual(lst, [37])
+ seqs = [list(range(n)) for n in range(10)]
+ shuffled_seqs = [list(range(n)) for n in range(10)]
+ for shuffled_seq in shuffled_seqs:
+ shuffle(shuffled_seq)
+ for (seq, shuffled_seq) in zip(seqs, shuffled_seqs):
+ self.assertEqual(len(seq), len(shuffled_seq))
+ self.assertEqual(set(seq), set(shuffled_seq))
+
+ # The above tests all would pass if the shuffle was a
+ # no-op. The following non-deterministic test covers that. It
+ # asserts that the shuffled sequence of 1000 distinct elements
+ # must be different from the original one. Although there is
+ # mathematically a non-zero probability that this could
+ # actually happen in a genuinely random shuffle, it is
+ # completely negligible, given that the number of possible
+ # permutations of 1000 objects is 1000! (factorial of 1000),
+ # which is considerably larger than the number of atoms in the
+ # universe...
+ lst = list(range(1000))
+ shuffled_lst = list(range(1000))
+ shuffle(shuffled_lst)
+ self.assertTrue(lst != shuffled_lst)
+ shuffle(lst)
+ self.assertTrue(lst != shuffled_lst)
+
def test_choice(self):
choice = self.gen.choice
with self.assertRaises(IndexError):