diff options
author | Raymond Hettinger <python@rcn.com> | 2004-07-09 14:26:18 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2004-07-09 14:26:18 (GMT) |
commit | 5833587d14ed6107ac411ec1c7689fd4ac3b6838 (patch) | |
tree | 628f6449b2174b9c352025aadc3ab38d2a54eb7b /Lib/test/test_random.py | |
parent | d87ac8f24da5dce860b559b69e6af59edd2c3eec (diff) | |
download | cpython-5833587d14ed6107ac411ec1c7689fd4ac3b6838.zip cpython-5833587d14ed6107ac411ec1c7689fd4ac3b6838.tar.gz cpython-5833587d14ed6107ac411ec1c7689fd4ac3b6838.tar.bz2 |
Add some tests for corner cases.
Diffstat (limited to 'Lib/test/test_random.py')
-rw-r--r-- | Lib/test/test_random.py | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index 8d005a2..ead0dca 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -39,6 +39,8 @@ class TestBasicOps(unittest.TestCase): self.gen.seed(arg) for arg in [range(3), dict(one=1)]: self.assertRaises(TypeError, self.gen.seed, arg) + self.assertRaises(TypeError, self.gen.seed, 1, 2) + self.assertRaises(TypeError, type(self.gen), []) def test_jumpahead(self): self.gen.seed() @@ -122,6 +124,9 @@ class TestBasicOps(unittest.TestCase): class WichmannHill_TestBasicOps(TestBasicOps): gen = random.WichmannHill() + def test_setstate_first_arg(self): + self.assertRaises(ValueError, self.gen.setstate, (2, None, None)) + def test_strong_jumpahead(self): # tests that jumpahead(n) semantics correspond to n calls to random() N = 1000 @@ -162,6 +167,19 @@ class WichmannHill_TestBasicOps(TestBasicOps): class MersenneTwister_TestBasicOps(TestBasicOps): gen = random.Random() + def test_setstate_first_arg(self): + self.assertRaises(ValueError, self.gen.setstate, (1, None, None)) + + def test_setstate_middle_arg(self): + # Wrong type, s/b tuple + self.assertRaises(TypeError, self.gen.setstate, (2, None, None)) + # Wrong length, s/b 625 + self.assertRaises(ValueError, self.gen.setstate, (2, (1,2,3), None)) + # Wrong type, s/b tuple of 625 ints + self.assertRaises(TypeError, self.gen.setstate, (2, ('a',)*625, None)) + # Last element s/b an int also + self.assertRaises(TypeError, self.gen.setstate, (2, (0,)*624+('a',), None)) + def test_referenceImplementation(self): # Compare the python implementation with results from the original # code. Create 2000 53-bit precision random floats. Compare only @@ -210,7 +228,6 @@ class MersenneTwister_TestBasicOps(TestBasicOps): 0x11388382c15694L, 0x02dad977c9e1feL, 0x191d96d4d334c6L] - self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96)) actual = self.randomlist(2000)[-10:] for a, e in zip(actual, expected): @@ -274,6 +291,13 @@ class MersenneTwister_TestBasicOps(TestBasicOps): cum |= getbits(span) self.assertEqual(cum, 2**span-1) + # Verify argument checking + self.assertRaises(TypeError, self.gen.getrandbits) + self.assertRaises(TypeError, self.gen.getrandbits, 'a') + self.assertRaises(TypeError, self.gen.getrandbits, 1, 2) + self.assertRaises(ValueError, self.gen.getrandbits, 0) + self.assertRaises(ValueError, self.gen.getrandbits, -1) + def test_randbelow_logic(self, _log=log, int=int): # check bitcount transition points: 2**i and 2**(i+1)-1 # show that: k = int(1.001 + _log(n, 2)) |