summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_float.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-12-02 17:58:24 (GMT)
committerEric Smith <eric@trueblade.com>2009-12-02 17:58:24 (GMT)
commit8a10ecc051b0932a8386050675862e98db5a5691 (patch)
tree79d1c20595fbfb000abb47c114bac2e813534f15 /Lib/test/test_float.py
parentbb21e50cddb3ead00a3adb5e7a8cc007178dcefb (diff)
downloadcpython-8a10ecc051b0932a8386050675862e98db5a5691.zip
cpython-8a10ecc051b0932a8386050675862e98db5a5691.tar.gz
cpython-8a10ecc051b0932a8386050675862e98db5a5691.tar.bz2
Merged revisions 76632 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r76632 | eric.smith | 2009-12-02 12:43:06 -0500 (Wed, 02 Dec 2009) | 1 line Issue #4482: Add tests for special float value formatting. ........
Diffstat (limited to 'Lib/test/test_float.py')
-rw-r--r--Lib/test/test_float.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index ebb666b..0f984a5 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -495,6 +495,41 @@ class RoundTestCase(unittest.TestCase):
self.assertEqual(float(format(x, '.3f')), round(x, 3))
+ @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
+ "test requires IEEE 754 doubles")
+ def test_format_specials(self):
+ # Test formatting of nans and infs.
+
+ def test(fmt, value, expected):
+ # Test with both % and format().
+ self.assertEqual(fmt % value, expected, fmt)
+ if not '#' in fmt:
+ # Until issue 7094 is implemented, format() for floats doesn't
+ # support '#' formatting
+ fmt = fmt[1:] # strip off the %
+ self.assertEqual(format(value, fmt), expected, fmt)
+
+ for fmt in ['%e', '%f', '%g', '%.0e', '%.6f', '%.20g',
+ '%#e', '%#f', '%#g', '%#.20e', '%#.15f', '%#.3g']:
+ pfmt = '%+' + fmt[1:]
+ sfmt = '% ' + fmt[1:]
+ test(fmt, INF, 'inf')
+ test(fmt, -INF, '-inf')
+ test(fmt, NAN, 'nan')
+ test(fmt, -NAN, 'nan')
+ # When asking for a sign, it's always provided. nans are
+ # always positive.
+ test(pfmt, INF, '+inf')
+ test(pfmt, -INF, '-inf')
+ test(pfmt, NAN, '+nan')
+ test(pfmt, -NAN, '+nan')
+ # When using ' ' for a sign code, only infs can be negative.
+ # Others have a space.
+ test(sfmt, INF, ' inf')
+ test(sfmt, -INF, '-inf')
+ test(sfmt, NAN, ' nan')
+ test(sfmt, -NAN, ' nan')
+
# Beginning with Python 2.6 float has cross platform compatible
# ways to create and represent inf and nan