summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorChristian Heimes <christian@cheimes.de>2007-12-18 23:22:54 (GMT)
committerChristian Heimes <christian@cheimes.de>2007-12-18 23:22:54 (GMT)
commit0a8143f6462b491d3f12bfb899efd6e044e350be (patch)
tree855410e5c18dace91bbbb310b5660a40fb5f1eb6 /Lib
parent8777bcae2749099b6ea3ac35f079bfa3df470a78 (diff)
downloadcpython-0a8143f6462b491d3f12bfb899efd6e044e350be.zip
cpython-0a8143f6462b491d3f12bfb899efd6e044e350be.tar.gz
cpython-0a8143f6462b491d3f12bfb899efd6e044e350be.tar.bz2
Applied patch #1635: Float patch for inf and nan on Windows (and other platforms).
The patch unifies float("inf") and repr(float("inf")) on all platforms.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_float.py73
1 files changed, 72 insertions, 1 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index ed5109f..11f1690 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -3,6 +3,12 @@ import unittest, struct
import os
from test import test_support
+def isinf(x):
+ return x * 0.5 == x
+
+def isnan(x):
+ return x != x
+
class FormatFunctionsTestCase(unittest.TestCase):
def setUp(self):
@@ -128,13 +134,78 @@ class ReprTestCase(unittest.TestCase):
self.assertEqual(v, eval(repr(v)))
floats_file.close()
+# Beginning with Python 2.6 float has cross platform compatible
+# ways to create and representate inf and nan
+class InfNanTest(unittest.TestCase):
+ def test_inf_from_str(self):
+ self.assert_(isinf(float("inf")))
+ self.assert_(isinf(float("+inf")))
+ self.assert_(isinf(float("-inf")))
+
+ self.assertEqual(repr(float("inf")), "inf")
+ self.assertEqual(repr(float("+inf")), "inf")
+ self.assertEqual(repr(float("-inf")), "-inf")
+
+ self.assertEqual(repr(float("INF")), "inf")
+ self.assertEqual(repr(float("+Inf")), "inf")
+ self.assertEqual(repr(float("-iNF")), "-inf")
+
+ self.assertEqual(str(float("inf")), "inf")
+ self.assertEqual(str(float("+inf")), "inf")
+ self.assertEqual(str(float("-inf")), "-inf")
+
+ self.assertRaises(ValueError, float, "info")
+ self.assertRaises(ValueError, float, "+info")
+ self.assertRaises(ValueError, float, "-info")
+ self.assertRaises(ValueError, float, "in")
+ self.assertRaises(ValueError, float, "+in")
+ self.assertRaises(ValueError, float, "-in")
+
+ def test_inf_as_str(self):
+ self.assertEqual(repr(1e300 * 1e300), "inf")
+ self.assertEqual(repr(-1e300 * 1e300), "-inf")
+
+ self.assertEqual(str(1e300 * 1e300), "inf")
+ self.assertEqual(str(-1e300 * 1e300), "-inf")
+
+ def test_nan_from_str(self):
+ self.assert_(isnan(float("nan")))
+ self.assert_(isnan(float("+nan")))
+ self.assert_(isnan(float("-nan")))
+
+ self.assertEqual(repr(float("nan")), "nan")
+ self.assertEqual(repr(float("+nan")), "nan")
+ self.assertEqual(repr(float("-nan")), "nan")
+
+ self.assertEqual(repr(float("NAN")), "nan")
+ self.assertEqual(repr(float("+NAn")), "nan")
+ self.assertEqual(repr(float("-NaN")), "nan")
+
+ self.assertEqual(str(float("nan")), "nan")
+ self.assertEqual(str(float("+nan")), "nan")
+ self.assertEqual(str(float("-nan")), "nan")
+
+ self.assertRaises(ValueError, float, "nana")
+ self.assertRaises(ValueError, float, "+nana")
+ self.assertRaises(ValueError, float, "-nana")
+ self.assertRaises(ValueError, float, "na")
+ self.assertRaises(ValueError, float, "+na")
+ self.assertRaises(ValueError, float, "-na")
+
+ def test_nan_as_str(self):
+ self.assertEqual(repr(1e300 * 1e300 * 0), "nan")
+ self.assertEqual(repr(-1e300 * 1e300 * 0), "nan")
+
+ self.assertEqual(str(1e300 * 1e300 * 0), "nan")
+ self.assertEqual(str(-1e300 * 1e300 * 0), "nan")
def test_main():
test_support.run_unittest(
FormatFunctionsTestCase,
UnknownFormatTestCase,
IEEEFormatTestCase,
- #ReprTestCase
+ ReprTestCase,
+ InfNanTest,
)
if __name__ == '__main__':