Skip to content

Commit

Permalink
Add test cases for zero counts or empty counts
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Feb 19, 2025
1 parent ff2db57 commit 85ade39
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Lib/test/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,15 +225,27 @@ def test_sample_with_counts(self):
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):
sample(['red', 'green', 'blue'], counts=[1, 2], k=2) # too few counts
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']
Expand Down

0 comments on commit 85ade39

Please sign in to comment.