mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
Issue #13355: Raise ValueError on random.triangular call with invalid params.
Initial patch by Yuriy Senko.
This commit is contained in:
parent
7b2c8bb833
commit
a2dfc35a13
4 changed files with 45 additions and 1 deletions
|
@ -46,6 +46,36 @@ class TestBasicOps(unittest.TestCase):
|
|||
self.assertRaises(TypeError, self.gen.seed, 1, 2, 3, 4)
|
||||
self.assertRaises(TypeError, type(self.gen), [])
|
||||
|
||||
def test_triangular(self):
|
||||
# Check that triangular() correctly handles bad input. See issue 13355.
|
||||
|
||||
# mode > high.
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(mode=2)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=1, high=10, mode=11)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=1, high=1, mode=11)
|
||||
|
||||
# mode < low.
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(mode=-1)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=1, high=10, mode=0)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=1, high=1, mode=0)
|
||||
|
||||
# low > high
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=5, high=2)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=5, high=2, mode=1)
|
||||
with self.assertRaises(ValueError):
|
||||
random.triangular(low=-2, high=-5)
|
||||
|
||||
self.assertEqual(random.triangular(low=10, high=10), 10)
|
||||
self.assertEqual(random.triangular(low=10, high=10, mode=10), 10)
|
||||
|
||||
def test_choice(self):
|
||||
choice = self.gen.choice
|
||||
with self.assertRaises(IndexError):
|
||||
|
@ -489,7 +519,7 @@ class TestDistributions(unittest.TestCase):
|
|||
for variate, args, expected in [
|
||||
(g.uniform, (10.0, 10.0), 10.0),
|
||||
(g.triangular, (10.0, 10.0), 10.0),
|
||||
#(g.triangular, (10.0, 10.0, 10.0), 10.0),
|
||||
(g.triangular, (10.0, 10.0, 10.0), 10.0),
|
||||
(g.expovariate, (float('inf'),), 0.0),
|
||||
(g.vonmisesvariate, (3.0, float('inf')), 3.0),
|
||||
(g.gauss, (10.0, 0.0), 10.0),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue