[Python-checkins] python/nondist/sandbox/twister _random.c,1.22,1.23 test_random.py,1.7,1.8

tim_one@users.sourceforge.net tim_one@users.sourceforge.net
Sun, 29 Dec 2002 01:39:16 -0800


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

Modified Files:
	_random.c test_random.py 
Log Message:
test_referenceImplementation():  Use enough decimal digits so that an
IEEE-754 box can reproduce the expected results exactly.

Added new test_strong_referenceImplementation(), which checks the raw
bits for exact equality.  This doesn't depend on 754 arithmetic (the
random() algorithm is exact -- no rounding errors), but does depend on
at least 53 bits of precision in the platform C double.


Index: _random.c
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/_random.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** _random.c	29 Dec 2002 09:18:00 -0000	1.22
--- _random.c	29 Dec 2002 09:39:14 -0000	1.23
***************
*** 231,238 ****
  	}
  
- 	split = PyList_New(0);
- 	if (split == NULL)
- 		goto Done;
- 
  	if (PyInt_Check(arg) || PyLong_Check(arg))
  		n = PyNumber_Absolute(arg);
--- 231,234 ----
***************
*** 245,248 ****
--- 241,249 ----
  	if (n == NULL)
  		goto Done;
+ 
+ 	split = PyList_New(0);
+ 	if (split == NULL)
+ 		goto Done;
+ 
  	masklower = PyLong_FromUnsignedLong(0xffffffffU);
  	if (masklower == NULL)
***************
*** 261,265 ****
  		assert(PyLong_Check(little));
  		err = PyList_Append(split, little);
! 		Py_XDECREF(little);
  		if (err == -1)
  			goto Done;
--- 262,266 ----
  		assert(PyLong_Check(little));
  		err = PyList_Append(split, little);
! 		Py_DECREF(little);
  		if (err == -1)
  			goto Done;

Index: test_random.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/twister/test_random.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** test_random.py	29 Dec 2002 09:05:46 -0000	1.7
--- test_random.py	29 Dec 2002 09:39:14 -0000	1.8
***************
*** 102,113 ****
          #         }
          #     }
!         expected = [0.458398030737133, 0.860578152019788, 0.928483317267822,
!                     0.359326811197825, 0.081823493762450, 0.143322264701693,
!                     0.084297823823520, 0.538148646718315, 0.089215024911993,
!                     0.784861961053729]
          self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96))
          actual = self.randomlist(2000)[-10:]
          for a, e in zip(actual, expected):
              self.assertEqual(round(a-e, 14), 0)
  
  class TestModule(unittest.TestCase):
--- 102,143 ----
          #         }
          #     }
!         expected = [0.45839803073713259,
!                     0.86057815201978782,
!                     0.92848331726782152,
!                     0.35932681119782461,
!                     0.081823493762449573,
!                     0.14332226470169329,
!                     0.084297823823520024,
!                     0.53814864671831453,
!                     0.089215024911993401,
!                     0.78486196105372907]
! 
          self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96))
          actual = self.randomlist(2000)[-10:]
          for a, e in zip(actual, expected):
              self.assertEqual(round(a-e, 14), 0)
+ 
+     def test_strong_referenceImplementation(self):
+         # Like test_referenceImplementation, but checks for exact bit-level
+         # equality.  This should pass on any box where C double contains
+         # at least 53 bits of precision (the underlying algorithm suffers
+         # no rounding errors -- all results are exact).
+         from math import ldexp
+ 
+         expected = [0x0eab3258d2231fL,
+                     0x1b89db315277a5L,
+                     0x1db622a5518016L,
+                     0x0b7f9af0d575bfL,
+                     0x029e4c4db82240L,
+                     0x04961892f5d673L,
+                     0x02b291598e4589L,
+                     0x11388382c15694L,
+                     0x02dad977c9e1feL,
+                     0x191d96d4d334c6L]
+ 
+         self.gen.seed(61731L + (24903L<<32) + (614L<<64) + (42143L<<96))
+         actual = self.randomlist(2000)[-10:]
+         for a, e in zip(actual, expected):
+             self.assertEqual(long(ldexp(a, 53)), e)
  
  class TestModule(unittest.TestCase):