summaryrefslogtreecommitdiff
path: root/Lib/test/test_float.py
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-05-05 18:26:08 +0000
committerEric Smith <eric@trueblade.com>2009-05-05 18:26:08 +0000
commita985a3aee46dfda4b59cf20414bab199ba1b9659 (patch)
treed7c3615c46cb9a17b3c330ab78c41dd6023adf2f /Lib/test/test_float.py
parent929ab934891719ea1561a623ee1b2e502b59e022 (diff)
downloadcpython-git-a985a3aee46dfda4b59cf20414bab199ba1b9659.tar.gz
Issue #5920: Changed format.__float__ and complex.__float__ to use a precision of 12 when using the empty presentation type. This more closely matches str()'s behavior and reduces surprises when adding alignment flags to an empty format string. Patch by Mark Dickinson.
Diffstat (limited to 'Lib/test/test_float.py')
-rw-r--r--Lib/test/test_float.py47
1 files changed, 47 insertions, 0 deletions
diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py
index 971c14f441..1c6c4124c7 100644
--- a/Lib/test/test_float.py
+++ b/Lib/test/test_float.py
@@ -257,6 +257,53 @@ class IEEEFormatTestCase(unittest.TestCase):
self.assertEquals(math.atan2(float('-1e-1000'), -1),
math.atan2(-0.0, -1))
+ def test_format(self):
+ # these should be rewritten to use both format(x, spec) and
+ # x.__format__(spec)
+
+ self.assertEqual(format(0.0, 'f'), '0.000000')
+
+ # the default is 'g', except for empty format spec
+ self.assertEqual(format(0.0, ''), '0.0')
+ self.assertEqual(format(0.01, ''), '0.01')
+ self.assertEqual(format(0.01, 'g'), '0.01')
+
+ # empty presentation type should format in the same way as str
+ # (issue 5920)
+ x = 100/7.
+ self.assertEqual(format(x, ''), str(x))
+ self.assertEqual(format(x, '-'), str(x))
+ self.assertEqual(format(x, '>'), str(x))
+ self.assertEqual(format(x, '2'), str(x))
+
+ self.assertEqual(format(1.0, 'f'), '1.000000')
+
+ self.assertEqual(format(-1.0, 'f'), '-1.000000')
+
+ self.assertEqual(format( 1.0, ' f'), ' 1.000000')
+ self.assertEqual(format(-1.0, ' f'), '-1.000000')
+ self.assertEqual(format( 1.0, '+f'), '+1.000000')
+ self.assertEqual(format(-1.0, '+f'), '-1.000000')
+
+ # % formatting
+ self.assertEqual(format(-1.0, '%'), '-100.000000%')
+
+ # conversion to string should fail
+ self.assertRaises(ValueError, format, 3.0, "s")
+
+ # other format specifiers shouldn't work on floats,
+ # in particular int specifiers
+ for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
+ [chr(x) for x in range(ord('A'), ord('Z')+1)]):
+ if not format_spec in 'eEfFgGn%':
+ self.assertRaises(ValueError, format, 0.0, format_spec)
+ self.assertRaises(ValueError, format, 1.0, format_spec)
+ self.assertRaises(ValueError, format, -1.0, format_spec)
+ self.assertRaises(ValueError, format, 1e100, format_spec)
+ self.assertRaises(ValueError, format, -1e100, format_spec)
+ self.assertRaises(ValueError, format, 1e-100, format_spec)
+ self.assertRaises(ValueError, format, -1e-100, format_spec)
+
@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
"test requires IEEE 754 doubles")
def test_format_testfile(self):