Skip to content

Commit 85ade39

Browse files
committed
Add test cases for zero counts or empty counts
1 parent ff2db57 commit 85ade39

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

Lib/test/test_random.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,27 @@ def test_sample_with_counts(self):
225225
sample(['red', 'green', 'blue'], counts=10, k=10) # counts not iterable
226226
with self.assertRaises(ValueError):
227227
sample(['red', 'green', 'blue'], counts=[-3, -7, -8], k=2) # counts are negative
228-
with self.assertRaises(ValueError):
229-
sample(['red', 'green', 'blue'], counts=[0, 0, 0], k=2) # counts are zero
230228
with self.assertRaises(ValueError):
231229
sample(['red', 'green'], counts=[10, 10], k=21) # population too small
232230
with self.assertRaises(ValueError):
233231
sample(['red', 'green', 'blue'], counts=[1, 2], k=2) # too few counts
234232
with self.assertRaises(ValueError):
235233
sample(['red', 'green', 'blue'], counts=[1, 2, 3, 4], k=2) # too many counts
236234

235+
# Cases with zero counts match equivalents without counts (see gh-130285)
236+
self.assertEqual(
237+
sample('abc', k=0, counts=[0, 0, 0]),
238+
sample([], k=0),
239+
)
240+
self.assertEqual(
241+
sample([], 0, counts=[]),
242+
sample([], 0),
243+
)
244+
with self.assertRaises(ValueError):
245+
sample([], 1, counts=[])
246+
with self.assertRaises(ValueError):
247+
sample('x', 1, counts=[0])
248+
237249
def test_choices(self):
238250
choices = self.gen.choices
239251
data = ['red', 'green', 'blue', 'yellow']

0 commit comments

Comments
 (0)