]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #13355: Raise ValueError on random.triangular call with invalid params.
authorAndrew Svetlov <andrew.svetlov@gmail.com>
Fri, 12 Apr 2013 20:39:33 +0000 (23:39 +0300)
committerAndrew Svetlov <andrew.svetlov@gmail.com>
Fri, 12 Apr 2013 20:39:33 +0000 (23:39 +0300)
Initial patch by Yuriy Senko.

Lib/random.py
Lib/test/test_random.py
Misc/ACKS
Misc/NEWS

index af04ab207d37c098d274caf852d37bdbcfc28198..8560fb9a25d755f9f92f0838b8f1a38c29d0f92e 100644 (file)
@@ -367,6 +367,16 @@ class Random(_random.Random):
         http://en.wikipedia.org/wiki/Triangular_distribution
 
         """
+        # Sanity check. According to the doc low must be less or equal to
+        # high. And mode should be somewhere between these bounds.
+        if low > high:
+            raise ValueError('high cannot be less then low.')
+        if mode is not None and (mode < low or mode > high):
+            raise ValueError('mode must be between low and high.')
+
+        if high == low:
+            return low
+
         u = self.random()
         c = 0.5 if mode is None else (mode - low) / (high - low)
         if u > c:
index 33164150c3650b8f2aea7d99c0e11475aa6ffdaa..75e1afc8247c2d83acc51de12700778155e62a59 100644 (file)
@@ -43,6 +43,33 @@ class TestBasicOps(unittest.TestCase):
         self.assertRaises(TypeError, self.gen.seed, 1, 2)
         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_jumpahead(self):
         self.gen.seed()
         state1 = self.gen.getstate()
@@ -543,7 +570,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),
index 4de44cc9ae6c0ce5e9084d5d935442a6985e3b7b..9480f2d3d331f6669de7391f44dec5abdba36b74 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -907,6 +907,7 @@ Nick Seidenman
 Žiga Seilnach
 Yury Selivanov
 Fred Sells
+Yuriy Senko
 Jiwon Seo
 Joakim Sernbrant
 Roger Serwy
index 62e809a48c50163773dca3b89b36a4756bfb1224..c4d9635a8b30b92487b54d8ae378d148ebebe1df 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -22,6 +22,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #13355: Raise ValueError on random.triangular call with invalid params.
+  Initial patch by Yuriy Senko.
+
 - Issue #17666: Fix reading gzip files with an extra field.
 
 - Issue #13150, #17512: sysconfig no longer parses the Makefile and config.h