mirror of
https://github.com/python/cpython.git
synced 2025-08-22 09:45:06 +00:00
[3.12] gh-130285: Fix handling of zero or empty counts in random.sample() (gh-130291) (gh-130417)
This commit is contained in:
parent
91e5e246b3
commit
8db2fa2575
3 changed files with 21 additions and 5 deletions
|
@ -224,8 +224,6 @@ class TestBasicOps:
|
|||
sample(['red', 'green', 'blue'], counts=10, k=10) # counts not iterable
|
||||
with self.assertRaises(ValueError):
|
||||
sample(['red', 'green', 'blue'], counts=[-3, -7, -8], k=2) # counts are negative
|
||||
with self.assertRaises(ValueError):
|
||||
sample(['red', 'green', 'blue'], counts=[0, 0, 0], k=2) # counts are zero
|
||||
with self.assertRaises(ValueError):
|
||||
sample(['red', 'green'], counts=[10, 10], k=21) # population too small
|
||||
with self.assertRaises(ValueError):
|
||||
|
@ -233,6 +231,20 @@ class TestBasicOps:
|
|||
with self.assertRaises(ValueError):
|
||||
sample(['red', 'green', 'blue'], counts=[1, 2, 3, 4], k=2) # too many counts
|
||||
|
||||
# Cases with zero counts match equivalents without counts (see gh-130285)
|
||||
self.assertEqual(
|
||||
sample('abc', k=0, counts=[0, 0, 0]),
|
||||
sample([], k=0),
|
||||
)
|
||||
self.assertEqual(
|
||||
sample([], 0, counts=[]),
|
||||
sample([], 0),
|
||||
)
|
||||
with self.assertRaises(ValueError):
|
||||
sample([], 1, counts=[])
|
||||
with self.assertRaises(ValueError):
|
||||
sample('x', 1, counts=[0])
|
||||
|
||||
def test_choices(self):
|
||||
choices = self.gen.choices
|
||||
data = ['red', 'green', 'blue', 'yellow']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue