summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorEric Smith <eric@trueblade.com>2009-04-30 01:00:33 (GMT)
committerEric Smith <eric@trueblade.com>2009-04-30 01:00:33 (GMT)
commit58a42244cfefc96b7adada5344b231ff2f32a86a (patch)
treea2a90372b3bef2cc1c06bb9f07b880f2c3997731 /Lib/test
parent738a41dd85f55f49508be62a135f9e65b5c92255 (diff)
downloadcpython-58a42244cfefc96b7adada5344b231ff2f32a86a.zip
cpython-58a42244cfefc96b7adada5344b231ff2f32a86a.tar.gz
cpython-58a42244cfefc96b7adada5344b231ff2f32a86a.tar.bz2
Issue #1588: Add complex.__format__.
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_complex.py61
1 files changed, 60 insertions, 1 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py
index 87e2584..ac19353 100644
--- a/Lib/test/test_complex.py
+++ b/Lib/test/test_complex.py
@@ -436,7 +436,66 @@ class ComplexTest(unittest.TestCase):
self.assertFloatsAreIdentical(0.0 + z.imag,
0.0 + roundtrip.imag)
-
+ def test_format(self):
+ # empty format string is same as str()
+ self.assertEqual(format(1+3j, ''), str(1+3j))
+ self.assertEqual(format(1.5+3.5j, ''), str(1.5+3.5j))
+ self.assertEqual(format(3j, ''), str(3j))
+ self.assertEqual(format(3.2j, ''), str(3.2j))
+ self.assertEqual(format(3+0j, ''), str(3+0j))
+ self.assertEqual(format(3.2+0j, ''), str(3.2+0j))
+
+ self.assertEqual(format(1+3j, 'g'), '1+3j')
+ self.assertEqual(format(3j, 'g'), '0+3j')
+ self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')
+
+ self.assertEqual(format(1.5+3.5j, '+g'), '+1.5+3.5j')
+ self.assertEqual(format(1.5-3.5j, '+g'), '+1.5-3.5j')
+ self.assertEqual(format(1.5-3.5j, '-g'), '1.5-3.5j')
+ self.assertEqual(format(1.5+3.5j, ' g'), ' 1.5+3.5j')
+ self.assertEqual(format(1.5-3.5j, ' g'), ' 1.5-3.5j')
+ self.assertEqual(format(-1.5+3.5j, ' g'), '-1.5+3.5j')
+ self.assertEqual(format(-1.5-3.5j, ' g'), '-1.5-3.5j')
+
+ self.assertEqual(format(-1.5-3.5e-20j, 'g'), '-1.5-3.5e-20j')
+ self.assertEqual(format(-1.5-3.5j, 'f'), '-1.500000-3.500000j')
+ self.assertEqual(format(-1.5-3.5j, 'F'), '-1.500000-3.500000j')
+ self.assertEqual(format(-1.5-3.5j, 'e'), '-1.500000e+00-3.500000e+00j')
+ self.assertEqual(format(-1.5-3.5j, '.2e'), '-1.50e+00-3.50e+00j')
+ self.assertEqual(format(-1.5-3.5j, '.2E'), '-1.50E+00-3.50E+00j')
+ self.assertEqual(format(-1.5e10-3.5e5j, '.2G'), '-1.5E+10-3.5E+05j')
+
+ self.assertEqual(format(1.5+3j, '<20g'), '1.5+3j ')
+ self.assertEqual(format(1.5+3j, '*<20g'), '1.5+3j**************')
+ self.assertEqual(format(1.5+3j, '>20g'), ' 1.5+3j')
+ self.assertEqual(format(1.5+3j, '^20g'), ' 1.5+3j ')
+ self.assertEqual(format(1.5+3j, '<20'), '(1.5+3j) ')
+ self.assertEqual(format(1.5+3j, '>20'), ' (1.5+3j)')
+ self.assertEqual(format(1.5+3j, '^20'), ' (1.5+3j) ')
+ self.assertEqual(format(1.123-3.123j, '^20.2'), ' (1.1-3.1j) ')
+
+ self.assertEqual(format(1.5+3j, '<20.2f'), '1.50+3.00j ')
+ self.assertEqual(format(1.5e20+3j, '<20.2f'), '150000000000000000000.00+3.00j')
+ self.assertEqual(format(1.5e20+3j, '>40.2f'), ' 150000000000000000000.00+3.00j')
+ self.assertEqual(format(1.5e20+3j, '^40,.2f'), ' 150,000,000,000,000,000,000.00+3.00j ')
+ self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ')
+ self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j')
+
+ # alternate is invalid
+ self.assertRaises(ValueError, (1.5+0.5j).__format__, '#f')
+
+ # zero padding is invalid
+ self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f')
+
+ # '=' alignment is invalid
+ self.assertRaises(ValueError, (1.5+3j).__format__, '=20')
+
+ # integer presentation types are an error
+ for t in 'bcdoxX':
+ self.assertRaises(ValueError, (1.5+0.5j).__format__, t)
+
+ # make sure everything works in ''.format()
+ self.assertEqual('*{0:.3f}*'.format(3.14159+2.71828j), '*3.142+2.718j*')
def test_main():
support.run_unittest(ComplexTest)