[Python-checkins] r71644 - in python/branches/py3k-short-float-repr/Lib/test: formatfloat_testcases.txt test_float.py

mark.dickinson python-checkins at python.org
Thu Apr 16 14:07:33 CEST 2009


Author: mark.dickinson
Date: Thu Apr 16 14:07:32 2009
New Revision: 71644

Log:
Adapt float formatting tests to deal with possibliity that
we're not using the short float repr.


Modified:
   python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt
   python/branches/py3k-short-float-repr/Lib/test/test_float.py

Modified: python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt
==============================================================================
--- python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt	(original)
+++ python/branches/py3k-short-float-repr/Lib/test/formatfloat_testcases.txt	Thu Apr 16 14:07:32 2009
@@ -314,47 +314,8 @@
 %#.5g 234.56 -> 234.56
 %#.6g 234.56 -> 234.560
 
--- repr formatting.  Should always include a decimal point (and at
--- least one digit after the point) or an exponent.  Produces the shortest
--- string that rounds correctly.
-
-%r 0 -> 0.0
-%r 1 -> 1.0
-
--- check short repr
-%r 0.01 -> 0.01
-%r 0.02 -> 0.02
-%r 0.03 -> 0.03
-%r 0.04 -> 0.04
-%r 0.05 -> 0.05
-%r 1.23456789 -> 1.23456789
-%r 10 -> 10.0
-%r 100 -> 100.0
-
--- values >= 1e16 get an exponent
-%r 1e15 -> 1000000000000000.0
-%r 0.999999999999e16 -> 9999999999990000.0
-%r 1e16 -> 1e+16
-%r 1.000000000001e16 -> 1.000000000001e+16
-%r 1e17 -> 1e+17
-
--- as do values < 1e-4
-%r 1e-3 -> 0.001
-%r 1.001e-4 -> 0.0001001
-%r 1.000000000001e-4 -> 0.0001000000000001
-%r 1e-4 -> 0.0001
-%r 0.999999999999e-4 -> 9.99999999999e-05
-%r 0.999e-4 -> 9.99e-05
-%r 1e-5 -> 1e-05
-
--- some values that are designed to fail if the FPU rounding
--- precision is 64-bits.
-%r 8.72293771110361e+25 -> 8.72293771110361e+25
-%r 7.47005307342313e+26 -> 7.47005307342313e+26
-%r 2.86438000439698e+28 -> 2.86438000439698e+28
-%r 8.89142905246179e+28 -> 8.89142905246179e+28
-%r 3.08578087079232e+35 -> 3.08578087079232e+35
-
+-- for repr formatting see the separate test_short_repr test in
+-- test_float.py.  Not all platforms use short repr for floats.
 
 -- str formatting.  Result always includes decimal point and at
 -- least one digit after the point, or an exponent.

Modified: python/branches/py3k-short-float-repr/Lib/test/test_float.py
==============================================================================
--- python/branches/py3k-short-float-repr/Lib/test/test_float.py	(original)
+++ python/branches/py3k-short-float-repr/Lib/test/test_float.py	Thu Apr 16 14:07:32 2009
@@ -1,6 +1,7 @@
 
 import unittest, struct
 import os
+import sys
 from test import support
 import math
 from math import isinf, isnan, copysign, ldexp
@@ -14,6 +15,9 @@
 test_dir = os.path.dirname(__file__) or os.curdir
 format_testfile = os.path.join(test_dir, 'formatfloat_testcases.txt')
 
+# determine whether we're using short float repr or not
+short_float_repr = getattr(sys, 'float_repr_style', 'none') == 'short'
+
 class GeneralFloatCases(unittest.TestCase):
 
     def test_float(self):
@@ -333,6 +337,56 @@
             self.assertEqual(fmt % float(arg), rhs)
             self.assertEqual(fmt % -float(arg), '-' + rhs)
 
+    @unittest.skipUnless(getattr(sys, 'float_repr_style', '') == 'short',
+                         "applies only when using short float repr style")
+    def test_short_repr(self):
+        # test short float repr introduced in Python 3.1.  One aspect
+        # of this repr is that we get some degree of str -> float ->
+        # str roundtripping.  In particular, for any numeric string
+        # containing 15 or fewer significant digits, those exact same
+        # digits (modulo trailing zeros) should appear in the output.
+        # No more repr(0.03) -> "0.029999999999999999"!
+
+        test_strings = [
+            # output always includes *either* a decimal point and at
+            # least one digit after that point, or an exponent.
+            '0.0',
+            '1.0',
+            '0.01',
+            '0.02',
+            '0.03',
+            '0.04',
+            '0.05',
+            '1.23456789',
+            '10.0',
+            '100.0',
+            # values >= 1e16 get an exponent...
+            '1000000000000000.0',
+            '9999999999999990.0',
+            '1e+16',
+            '1e+17',
+            # ... and so do values < 1e-4
+            '0.001',
+            '0.001001',
+            '0.00010000000000001',
+            '0.0001',
+            '9.999999999999e-05',
+            '1e-05',
+            # values designed to provoke failure if the FPU rounding
+            # precision isn't set correctly
+            '8.72293771110361e+25',
+            '7.47005307342313e+26',
+            '2.86438000439698e+28',
+            '8.89142905246179e+28',
+            '3.08578087079232e+35',
+            ]
+
+        for s in test_strings:
+            negs = '-'+s
+            self.assertEqual(s, repr(float(s)))
+            self.assertEqual(negs, repr(float(negs)))
+
+
 
 # Beginning with Python 2.6 float has cross platform compatible
 # ways to create and represent inf and nan


More information about the Python-checkins mailing list