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

Side by Side Diff: Lib/test/test_random.py

Issue 602: range: lean and mean (Closed) SVN Base: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: address more concerns Created 1 year, 7 months ago , Downloaded from: http://bugs.python.org/file10183/range_lean_and_mean5.patch
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
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
(...skipping 28 matching lines...) Expand all
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 = range(N) 49 population = list(range(N))
50 for k in range(N+1): 50 for k in range(N+1):
51 s = self.gen.sample(population, k) 51 s = self.gen.sample(population, k)
52 self.assertEqual(len(s), k) 52 self.assertEqual(len(s), k)
53 uniq = set(s) 53 uniq = set(s)
54 self.assertEqual(len(uniq), k) 54 self.assertEqual(len(uniq), k)
55 self.failUnless(uniq <= set(population)) 55 self.failUnless(uniq <= set(population))
56 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0 56 self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
57 57
58 def test_sample_distribution(self): 58 def test_sample_distribution(self):
59 # For the entire allowable range of 0 <= k <= N, validate that 59 # For the entire allowable range of 0 <= k <= N, validate that
60 # sample generates all possible permutations 60 # sample generates all possible permutations
61 n = 5 61 n = 5
62 pop = range(n) 62 pop = list(range(n))
63 trials = 10000 # large num prevents false negatives without slowing nor mal case 63 trials = 10000 # large num prevents false negatives without slowing nor mal case
64 def factorial(n): 64 def factorial(n):
65 if n == 0: 65 if n == 0:
66 return 1 66 return 1
67 return n * factorial(n - 1) 67 return n * factorial(n - 1)
68 for k in range(n): 68 for k in range(n):
69 expected = factorial(n) // factorial(n-k) 69 expected = factorial(n) // factorial(n-k)
70 perms = {} 70 perms = {}
71 for i in range(trials): 71 for i in range(trials):
72 perms[tuple(self.gen.sample(pop, k))] = None 72 perms[tuple(self.gen.sample(pop, k))] = None
73 if len(perms) == expected: 73 if len(perms) == expected:
74 break 74 break
75 else: 75 else:
76 self.fail() 76 self.fail()
77 77
78 def test_sample_inputs(self): 78 def test_sample_inputs(self):
79 # SF bug #801342 -- population can be any iterable defining __len__() 79 # SF bug #801342 -- population can be any iterable defining __len__()
80 self.gen.sample(set(range(20)), 2) 80 self.gen.sample(set(range(20)), 2)
81 self.gen.sample(range(20), 2) 81 self.gen.sample(list(range(20)), 2)
82 self.gen.sample(range(20), 2) 82 self.gen.sample(list(range(20)), 2)
83 self.gen.sample(str('abcdefghijklmnopqrst'), 2) 83 self.gen.sample(str('abcdefghijklmnopqrst'), 2)
84 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2) 84 self.gen.sample(tuple('abcdefghijklmnopqrst'), 2)
85 85
86 def test_sample_on_dicts(self): 86 def test_sample_on_dicts(self):
87 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2 ) 87 self.assertRaises(TypeError, self.gen.sample, dict.fromkeys('abcdef'), 2 )
88 88
89 def test_gauss(self): 89 def test_gauss(self):
90 # Ensure that the seed() method initializes all the hidden state. In 90 # Ensure that the seed() method initializes all the hidden state. In
91 # particular, through 2.2.1 it failed to reset a piece of state used 91 # particular, through 2.2.1 it failed to reset a piece of state used
92 # by (and only by) the .gauss() method. 92 # by (and only by) the .gauss() method.
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after
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)
OLDNEW

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld r497