[Python-checkins] python/dist/src/Lib/test test_random.py,1.6,1.7

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Fri, 17 Jan 2003 09:23:26 -0800


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1:/tmp/cvs-serv5751/test

Modified Files:
	test_random.py 
Log Message:
* Migrate sample distribution test from random.py to test_random.py.
* Use Sets module to more clearly articulate a couple of tests.


Index: test_random.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_random.py,v
retrieving revision 1.6
retrieving revision 1.7
diff -C2 -d -r1.6 -r1.7
*** test_random.py	5 Jan 2003 09:20:06 -0000	1.6
--- test_random.py	17 Jan 2003 17:23:23 -0000	1.7
***************
*** 5,8 ****
--- 5,9 ----
  import time
  from math import log, exp, sqrt, pi
+ from sets import Set
  from test import test_support
  
***************
*** 62,70 ****
              s = self.gen.sample(population, k)
              self.assertEqual(len(s), k)
!             uniq = dict.fromkeys(s)
              self.assertEqual(len(uniq), k)
!             self.failIf(None in uniq)
          self.assertEqual(self.gen.sample([], 0), [])  # test edge case N==k==0
  
      def test_gauss(self):
          # Ensure that the seed() method initializes all the hidden state.  In
--- 63,89 ----
              s = self.gen.sample(population, k)
              self.assertEqual(len(s), k)
!             uniq = Set(s)
              self.assertEqual(len(uniq), k)
!             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
***************
*** 251,257 ****
      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)
  
  def test_main():
--- 270,274 ----
      def test__all__(self):
          # tests validity but not completeness of the __all__ list
!         self.failUnless(Set(random.__all__) <= Set(dir(random)))
  
  def test_main():