[Python-checkins] python/nondist/sandbox/twister test_twister.py,1.1,1.2

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 18 Dec 2002 10:02:13 -0800


Update of /cvsroot/python/python/nondist/sandbox/twister
In directory sc8-pr-cvs1:/tmp/cvs-serv25267a

Modified Files:
	test_twister.py 
Log Message:
Added helper method call randlist() to be used in place of the method
   formerly included in the C code.

Revised the reference implementation test.  Now uses seeds that affect
the high bits (important because of signed/unsigned distinctions). Also,
included the C code in the comments so that that the reference data
may be more easily re-created.  Also, changed test to compare out to
14 digits.  Avoided the full 15, because some platforms may do weird
things to the last couple of bits.

Changed docstrings into comment blocks so they won't print-out during
the regression test.



Index: test_twister.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/test_twister.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** test_twister.py	17 Dec 2002 00:22:46 -0000	1.1
--- test_twister.py	18 Dec 2002 18:02:11 -0000	1.2
***************
*** 10,13 ****
--- 10,17 ----
      def setUp(self):
          self.gen = MersenneTwister.Random()
+ 
+     def randomlist(self, n):
+         """Helper function to make a list of random numbers"""
+         return [self.gen.random() for i in xrange(n)]
          
      def test_autoseed(self):
***************
*** 23,52 ****
          self.gen.seed()
          state = self.gen.getstate()
!         randseq = self.gen.randomlist(N)
          self.gen.setstate(state)    # should regenerate the same sequence
!         self.assertEqual(randseq, self.gen.randomlist(N))
  
      def test_referenceImplementation(self):
!         """The code from the original source came with a demo.
!            Altering it to create 2000 53-bit precision floats
!            yields a base case that the Python implemenation
!            should match.  Only the last five of entries are
!            checked (if its still on track after 2000 steps,
!            that is a good sign).
!         """
!         testseeds = [int(hex,16) for hex in '0x123 0x234 0x345 0x456'.split()]
!         expected = [0.09857852, 0.93585516, 0.53237051, 0.74474624, 0.07408276]
!         self.gen.seed(*testseeds)
!         actual = self.gen.randomlist(2000)[-5:]
          for a, e in zip(actual, expected):
!             self.assertEqual(round(a-e, 7), 0)
  
      def test_jumpahead(self):
!         """For the MersenneTwister, there is no exact analog to jumpahead(n)
!         where n represents a discrete number of forward steps.  The use case
!         for jumpahead is to create a non-overlapping sequence.  Here, we test
!         that jumpahead(n) at least generates a different sequence from
!         the original and on successive calls.
!         """
          N = 500000
          a = self.gen.random()
--- 27,64 ----
          self.gen.seed()
          state = self.gen.getstate()
!         randseq = self.randomlist(N)
          self.gen.setstate(state)    # should regenerate the same sequence
!         self.assertEqual(randseq, self.randomlist(N))
  
      def test_referenceImplementation(self):
!         ## Compare the python implementation with results from the original
!         ## code.  Create 2000 53-bit precision random floats.  Compare only
!         ## the last ten entries to show that the independent implementations
!         ## are tracking.  Here is the main() function needed to create the
!         ## list of expected random numbers:
!         ##    void main(void){
!         ##         int i;
!         ##         unsigned long init[4]={61731, 24903, 614, 42143}, length=4;
!         ##         init_by_array(init, length);
!         ##         for (i=0; i<2000; i++) {
!         ##           printf("%.15f ", genrand_res53());
!         ##           if (i%5==4) printf("\n");
!         ##         }
!         ##     }
!         expected = [0.458398030737133, 0.860578152019788, 0.928483317267822,
!                     0.359326811197825, 0.081823493762450, 0.143322264701693,
!                     0.084297823823520, 0.538148646718315, 0.089215024911993,
!                     0.784861961053729]
!         self.gen.seed(61731, 24903, 614, 42143)
!         actual = self.randomlist(2000)[-10:]
          for a, e in zip(actual, expected):
!             self.assertEqual(round(a-e, 14), 0)
  
      def test_jumpahead(self):
!         ## For the MersenneTwister, there is no exact analog to jumpahead(n)
!         ## where n represents a discrete number of forward steps.  The use case
!         ## for jumpahead is to create a non-overlapping sequence.  Here, we test
!         ## that jumpahead(n) at least generates a different sequence from
!         ## the original and on successive calls.  
          N = 500000
          a = self.gen.random()