Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(14)

Delta Between Two Patch Sets: Lib/test/test_random.py

Issue 602: range: lean and mean (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/branches/py3k/
Left Patch Set: address more concerns Created 4 months, 1 week ago , Downloaded from: http://bugs.python.org/file10183/range_lean_and_mean5.patch
Right Patch Set: __len__ is back! Created 4 months, 1 week ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 import unittest 3 import unittest
4 import random 4 import random
5 import time 5 import time
6 import pickle 6 import pickle
7 import warnings 7 import warnings
8 from math import log, exp, sqrt, pi 8 from math import log, exp, sqrt, pi
9 from test import test_support 9 from test import test_support
10 10
11 class TestBasicOps(unittest.TestCase): 11 class TestBasicOps(unittest.TestCase):
12 # Superclass with tests common to all generators. 12 # Superclass with tests common to all generators.
13 # Subclasses must arrange for self.gen to retrieve the Random instance 13 # Subclasses must arrange for self.gen to retrieve the Random instance
14 # to be tested. 14 # to be tested.
15 15
16 def randomlist(self, n): 16 def randomlist(self, n):
17 """Helper function to make a list of random numbers""" 17 """Helper function to make a list of random numbers"""
18 return [self.gen.random() for i in range(n)] 18 return [self.gen.random() for i in range(n)]
19 19
20 def test_autoseed(self): 20 def test_autoseed(self):
21 self.gen.seed() 21 self.gen.seed()
22 state1 = self.gen.getstate() 22 state1 = self.gen.getstate()
23 time.sleep(0.1) 23 time.sleep(0.1)
24 self.gen.seed() # diffent seeds at different times 24 self.gen.seed() # diffent seeds at different times
25 state2 = self.gen.getstate() 25 state2 = self.gen.getstate()
26 self.assertNotEqual(state1, state2) 26 self.assertNotEqual(state1, state2)
27 27
28 def test_saverestore(self): 28 def test_saverestore(self):
29 N = 1000 29 N = 1000
30 self.gen.seed() 30 self.gen.seed()
31 state = self.gen.getstate() 31 state = self.gen.getstate()
32 randseq = self.randomlist(N) 32 randseq = self.randomlist(N)
33 self.gen.setstate(state) # should regenerate the same sequence 33 self.gen.setstate(state) # should regenerate the same sequence
34 self.assertEqual(randseq, self.randomlist(N)) 34 self.assertEqual(randseq, self.randomlist(N))
35 35
36 def test_seedargs(self): 36 def test_seedargs(self):
37 for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20), 37 for arg in [None, 0, 0, 1, 1, -1, -1, 10**20, -(10**20),
38 3.14, 1+2j, 'a', tuple('abc')]: 38 3.14, 1+2j, 'a', tuple('abc')]:
39 self.gen.seed(arg) 39 self.gen.seed(arg)
40 for arg in [list(range(3)), dict(one=1)]: 40 for arg in [list(range(3)), dict(one=1)]:
41 self.assertRaises(TypeError, self.gen.seed, arg) 41 self.assertRaises(TypeError, self.gen.seed, arg)
42 self.assertRaises(TypeError, self.gen.seed, 1, 2) 42 self.assertRaises(TypeError, self.gen.seed, 1, 2)
43 self.assertRaises(TypeError, type(self.gen), []) 43 self.assertRaises(TypeError, type(self.gen), [])
44 44
45 def test_sample(self): 45 def test_sample(self):
46 # For the entire allowable range of 0 <= k <= N, validate that 46 # For the entire allowable range of 0 <= k <= N, validate that
47 # the sample is of the correct length and contains only unique items 47 # the sample is of the correct length and contains only unique items
48 N = 100 48 N = 100
49 population = list(range(N)) 49 population = list(range(N))
50 for k in range(N+1): 50 for k in range(N+1):
(...skipping 389 matching lines...) Show 10 above Show 10 below
440 s1 += e 440 s1 += e
441 s2 += (e - mu) ** 2 441 s2 += (e - mu) ** 2
442 N = len(y) 442 N = len(y)
443 self.assertAlmostEqual(s1/N, mu, places=2) 443 self.assertAlmostEqual(s1/N, mu, places=2)
444 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2) 444 self.assertAlmostEqual(s2/(N-1), sigmasqrd, places=2)
445 445
446 class TestModule(unittest.TestCase): 446 class TestModule(unittest.TestCase):
447 def testMagicConstants(self): 447 def testMagicConstants(self):
448 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141) 448 self.assertAlmostEqual(random.NV_MAGICCONST, 1.71552776992141)
449 self.assertAlmostEqual(random.TWOPI, 6.28318530718) 449 self.assertAlmostEqual(random.TWOPI, 6.28318530718)
450 self.assertAlmostEqual(random.LOG4, 1.38629436111989) 450 self.assertAlmostEqual(random.LOG4, 1.38629436111989)
451 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627) 451 self.assertAlmostEqual(random.SG_MAGICCONST, 2.50407739677627)
452 452
453 def test__all__(self): 453 def test__all__(self):
454 # tests validity but not completeness of the __all__ list 454 # tests validity but not completeness of the __all__ list
455 self.failUnless(set(random.__all__) <= set(dir(random))) 455 self.failUnless(set(random.__all__) <= set(dir(random)))
456 456
457 def test_random_subclass_with_kwargs(self): 457 def test_random_subclass_with_kwargs(self):
458 # SF bug #1486663 -- this used to erroneously raise a TypeError 458 # SF bug #1486663 -- this used to erroneously raise a TypeError
459 class Subclass(random.Random): 459 class Subclass(random.Random):
460 def __init__(self, newarg=None): 460 def __init__(self, newarg=None):
461 random.Random.__init__(self) 461 random.Random.__init__(self)
462 Subclass(newarg=1) 462 Subclass(newarg=1)
463 463
464 464
465 def test_main(verbose=None): 465 def test_main(verbose=None):
466 testclasses = [MersenneTwister_TestBasicOps, 466 testclasses = [MersenneTwister_TestBasicOps,
467 TestDistributions, 467 TestDistributions,
468 TestModule] 468 TestModule]
469 469
470 try: 470 try:
471 random.SystemRandom().random() 471 random.SystemRandom().random()
472 except NotImplementedError: 472 except NotImplementedError:
473 pass 473 pass
474 else: 474 else:
475 testclasses.append(SystemRandom_TestBasicOps) 475 testclasses.append(SystemRandom_TestBasicOps)
476 476
477 test_support.run_unittest(*testclasses) 477 test_support.run_unittest(*testclasses)
478 478
479 # verify reference counting 479 # verify reference counting
480 import sys 480 import sys
481 if verbose and hasattr(sys, "gettotalrefcount"): 481 if verbose and hasattr(sys, "gettotalrefcount"):
482 counts = [None] * 5 482 counts = [None] * 5
483 for i in range(len(counts)): 483 for i in range(len(counts)):
484 test_support.run_unittest(*testclasses) 484 test_support.run_unittest(*testclasses)
485 counts[i] = sys.gettotalrefcount() 485 counts[i] = sys.gettotalrefcount()
486 print(counts) 486 print(counts)
487 487
488 if __name__ == "__main__": 488 if __name__ == "__main__":
489 test_main(verbose=True) 489 test_main(verbose=True)
LEFTRIGHT

Powered by Google App Engine
This is Rietveld r305