]> granicus.if.org Git - python/commitdiff
Issue #18844: Add more tests
authorRaymond Hettinger <python@rcn.com>
Fri, 14 Oct 2016 05:19:38 +0000 (01:19 -0400)
committerRaymond Hettinger <python@rcn.com>
Fri, 14 Oct 2016 05:19:38 +0000 (01:19 -0400)
Lib/test/test_random.py

index 840f3e7ce81b6147709d78e37612ee1ae578d2b7..4d5a8749c76c05a993239cd284a7767fa6bc94da 100644 (file)
@@ -205,6 +205,20 @@ class TestBasicOps:
         ]:
             self.assertTrue(set(choices(data, cum_weights=weights, k=5)) <= set(data))
 
+        # Test weight focused on a single element of the population
+        self.assertEqual(choices('abcd', [1, 0, 0, 0]), ['a'])
+        self.assertEqual(choices('abcd', [0, 1, 0, 0]), ['b'])
+        self.assertEqual(choices('abcd', [0, 0, 1, 0]), ['c'])
+        self.assertEqual(choices('abcd', [0, 0, 0, 1]), ['d'])
+
+        # Test consistency with random.choice() for empty population
+        with self.assertRaises(IndexError):
+            choices([], k=1)
+        with self.assertRaises(IndexError):
+            choices([], weights=[], k=1)
+        with self.assertRaises(IndexError):
+            choices([], cum_weights=[], k=5)
+
     def test_gauss(self):
         # Ensure that the seed() method initializes all the hidden state.  In
         # particular, through 2.2.1 it failed to reset a piece of state used