mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
* Migrate sample distribution test from random.py to test_random.py.
* Use Sets module to more clearly articulate a couple of tests.
This commit is contained in:
parent
ee1bded046
commit
7b0cf76b72
2 changed files with 22 additions and 10 deletions
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue