summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2003-01-17 17:23:23 (GMT)
committerRaymond Hettinger <python@rcn.com>2003-01-17 17:23:23 (GMT)
commit7b0cf76b72a499c692e444c75b4833475fa56db0 (patch)
treea83f3077501ecc47ac016d2f9fd5fc1ffdb8825d /Lib/test
parentee1bded0466f4b215f39cb37c53a00d65e2a0bf0 (diff)
downloadcpython-7b0cf76b72a499c692e444c75b4833475fa56db0.zip
cpython-7b0cf76b72a499c692e444c75b4833475fa56db0.tar.gz
cpython-7b0cf76b72a499c692e444c75b4833475fa56db0.tar.bz2
* Migrate sample distribution test from random.py to test_random.py.
* Use Sets module to more clearly articulate a couple of tests.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_random.py27
1 files changed, 22 insertions, 5 deletions
diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py
index 1f6abfe..cc9b44e 100644
--- a/Lib/test/test_random.py
+++ b/Lib/test/test_random.py
@@ -4,6 +4,7 @@ import unittest
import random
import time
from math import log, exp, sqrt, pi
+from sets import Set
from test import test_support
class TestBasicOps(unittest.TestCase):
@@ -61,11 +62,29 @@ class TestBasicOps(unittest.TestCase):
for k in xrange(N+1):
s = self.gen.sample(population, k)
self.assertEqual(len(s), k)
- uniq = dict.fromkeys(s)
+ uniq = Set(s)
self.assertEqual(len(uniq), k)
- self.failIf(None in uniq)
+ self.failUnless(uniq <= Set(population))
self.assertEqual(self.gen.sample([], 0), []) # test edge case N==k==0
+ def test_sample_distribution(self):
+ # For the entire allowable range of 0 <= k <= N, validate that
+ # sample generates all possible permutations
+ n = 5
+ pop = range(n)
+ trials = 10000 # large num prevents false negatives without slowing normal case
+ def factorial(n):
+ return n==0 and 1 or n * factorial(n-1)
+ for k in xrange(n):
+ expected = factorial(n) / factorial(n-k)
+ perms = {}
+ for i in xrange(trials):
+ perms[tuple(self.gen.sample(pop, k))] = None
+ if len(perms) == expected:
+ break
+ else:
+ self.fail()
+
def test_gauss(self):
# Ensure that the seed() method initializes all the hidden state. In
# particular, through 2.2.1 it failed to reset a piece of state used
@@ -250,9 +269,7 @@ class TestModule(unittest.TestCase):
def test__all__(self):
# tests validity but not completeness of the __all__ list
- defined = dict.fromkeys(dir(random))
- for entry in random.__all__:
- self.failUnless(entry in defined)
+ self.failUnless(Set(random.__all__) <= Set(dir(random)))
def test_main():
suite = unittest.TestSuite()