annotate Lib/test/test_complex.py @ 87690:66c1c9f32567 default tip

Add use_weakref option to functools.lru_cache The use_weakref option makes lru_cache create weak references to the function arguments when creating the cache key, which prevents them from staying in the memory forever even after all other references to them are gone. This is a proof of concept for http://bugs.python.org/issue19859
author Radomir Dopieralski <devel@sheep.art.pl>
date Tue, 03 Dec 2013 16:02:48 +0100
parents a73c47c1d374
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59563
diff changeset
1 import unittest
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45371
diff changeset
2 from test import support
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
3
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
4 from random import random
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
5 from math import atan2, isnan, copysign
59563
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
6 import operator
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
7
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45082
diff changeset
8 INF = float("inf")
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45082
diff changeset
9 NAN = float("nan")
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
10 # These tests ensure that complex math does the right thing
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
11
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
12 class ComplexTest(unittest.TestCase):
21401
6da6091a31ee Add a comment explaining what these tests are for, and where to look for
Fred Drake <fdrake@acm.org>
parents: 19791
diff changeset
13
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
14 def assertAlmostEqual(self, a, b):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
15 if isinstance(a, complex):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
16 if isinstance(b, complex):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
17 unittest.TestCase.assertAlmostEqual(self, a.real, b.real)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
18 unittest.TestCase.assertAlmostEqual(self, a.imag, b.imag)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
19 else:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
20 unittest.TestCase.assertAlmostEqual(self, a.real, b)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
21 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
22 else:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
23 if isinstance(b, complex):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
24 unittest.TestCase.assertAlmostEqual(self, a, b.real)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
25 unittest.TestCase.assertAlmostEqual(self, 0., b.imag)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
26 else:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
27 unittest.TestCase.assertAlmostEqual(self, a, b)
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
28
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
29 def assertCloseAbs(self, x, y, eps=1e-9):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
30 """Return true iff floats x and y "are close\""""
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
31 # put the one with larger magnitude second
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
32 if abs(x) > abs(y):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
33 x, y = y, x
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
34 if y == 0:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
35 return abs(x) < eps
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
36 if x == 0:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
37 return abs(y) < eps
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
38 # check that relative difference < eps
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54225
diff changeset
39 self.assertTrue(abs((x-y)/y) < eps)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
40
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
41 def assertFloatsAreIdentical(self, x, y):
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
42 """assert that floats x and y are identical, in the sense that:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
43 (1) both x and y are nans, or
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
44 (2) both x and y are infinities, with the same sign, or
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
45 (3) both x and y are zeros, with the same sign, or
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
46 (4) x and y are both finite and nonzero, and x == y
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
47
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
48 """
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
49 msg = 'floats {!r} and {!r} are not identical'
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
50
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
51 if isnan(x) or isnan(y):
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
52 if isnan(x) and isnan(y):
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
53 return
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
54 elif x == y:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
55 if x != 0.0:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
56 return
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
57 # both zero; check that signs match
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
58 elif copysign(1.0, x) == copysign(1.0, y):
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
59 return
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
60 else:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
61 msg += ': zeros have different signs'
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
62 self.fail(msg.format(x, y))
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
63
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
64 def assertClose(self, x, y, eps=1e-9):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
65 """Return true iff complexes x and y "are close\""""
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
66 self.assertCloseAbs(x.real, y.real, eps)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
67 self.assertCloseAbs(x.imag, y.imag, eps)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
68
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
69 def check_div(self, x, y):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
70 """Compute complex z=x*y, and check that z/x==y and z/y==x."""
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
71 z = x * y
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
72 if x != 0:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
73 q = z / x
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
74 self.assertClose(q, y)
30070
061d86fc11bb Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents: 29867
diff changeset
75 q = z.__truediv__(x)
061d86fc11bb Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents: 29867
diff changeset
76 self.assertClose(q, y)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
77 if y != 0:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
78 q = z / y
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
79 self.assertClose(q, x)
30070
061d86fc11bb Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents: 29867
diff changeset
80 q = z.__truediv__(y)
061d86fc11bb Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents: 29867
diff changeset
81 self.assertClose(q, x)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
82
37191
4bfe8afe8f65 Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents: 35586
diff changeset
83 def test_truediv(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
84 simple_real = [float(i) for i in range(-5, 6)]
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
85 simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
86 for x in simple_complex:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
87 for y in simple_complex:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
88 self.check_div(x, y)
19791
c97dda238b36 Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents: 19790
diff changeset
89
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
90 # A naive complex division algorithm (such as in 2.0) is very prone to
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
91 # nonsense errors for these (overflows and underflows).
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
92 self.check_div(complex(1e200, 1e200), 1+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
93 self.check_div(complex(1e-200, 1e-200), 1+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
94
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
95 # Just for fun.
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
96 for i in range(100):
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
97 self.check_div(complex(random(), random()),
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
98 complex(random(), random()))
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
99
37191
4bfe8afe8f65 Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents: 35586
diff changeset
100 self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
101 # FIXME: The following currently crashes on Alpha
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
102 # self.assertRaises(OverflowError, pow, 1e200+1j, 1e200+1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
103
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
104 self.assertAlmostEqual(complex.__truediv__(2+0j, 1+1j), 1-1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
105 self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
106
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
107 def test_floordiv(self):
42435
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
108 self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 1.5+0j)
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
109 self.assertRaises(TypeError, complex.__floordiv__, 3+0j, 0+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
110
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
111 def test_richcompare(self):
61787
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
112 self.assertIs(complex.__eq__(1+1j, 1<<10000), False)
59563
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
113 self.assertIs(complex.__lt__(1+1j, None), NotImplemented)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
114 self.assertIs(complex.__eq__(1+1j, 1+1j), True)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
115 self.assertIs(complex.__eq__(1+1j, 2+2j), False)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
116 self.assertIs(complex.__ne__(1+1j, 1+1j), False)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
117 self.assertIs(complex.__ne__(1+1j, 2+2j), True)
61787
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
118 for i in range(1, 100):
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
119 f = i / 100.0
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
120 self.assertIs(complex.__eq__(f+0j, f), True)
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
121 self.assertIs(complex.__ne__(f+0j, f), False)
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
122 self.assertIs(complex.__eq__(complex(f, f), f), False)
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
123 self.assertIs(complex.__ne__(complex(f, f), f), True)
59563
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
124 self.assertIs(complex.__lt__(1+1j, 2+2j), NotImplemented)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
125 self.assertIs(complex.__le__(1+1j, 2+2j), NotImplemented)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
126 self.assertIs(complex.__gt__(1+1j, 2+2j), NotImplemented)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
127 self.assertIs(complex.__ge__(1+1j, 2+2j), NotImplemented)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
128 self.assertRaises(TypeError, operator.lt, 1+1j, 2+2j)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
129 self.assertRaises(TypeError, operator.le, 1+1j, 2+2j)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
130 self.assertRaises(TypeError, operator.gt, 1+1j, 2+2j)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
131 self.assertRaises(TypeError, operator.ge, 1+1j, 2+2j)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
132 self.assertIs(operator.eq(1+1j, 1+1j), True)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
133 self.assertIs(operator.eq(1+1j, 2+2j), False)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
134 self.assertIs(operator.ne(1+1j, 1+1j), False)
f65977dcf21a Issue #7845: Make 1j.__le__(2j) return NotImplemented rather than raising TypeError.
Mark Dickinson <dickinsm@gmail.com>
parents: 59027
diff changeset
135 self.assertIs(operator.ne(1+1j, 2+2j), True)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
136
61787
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
137 def test_richcompare_boundaries(self):
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
138 def check(n, deltas, is_equal, imag = 0.0):
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
139 for delta in deltas:
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
140 i = n + delta
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
141 z = complex(i, imag)
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
142 self.assertIs(complex.__eq__(z, i), is_equal(delta))
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
143 self.assertIs(complex.__ne__(z, i), not is_equal(delta))
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
144 # For IEEE-754 doubles the following should hold:
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
145 # x in [2 ** (52 + i), 2 ** (53 + i + 1)] -> x mod 2 ** i == 0
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
146 # where the interval is representable, of course.
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
147 for i in range(1, 10):
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
148 pow = 52 + i
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
149 mult = 2 ** i
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
150 check(2 ** pow, range(1, 101), lambda delta: delta % mult == 0)
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
151 check(2 ** pow, range(1, 101), lambda delta: False, float(i))
d6016fa58df6 Issue #8748: Fix two issues with comparisons between complex and integer
Mark Dickinson <dickinsm@gmail.com>
parents: 60174
diff changeset
152 check(2 ** 53, range(-100, 0), lambda delta: True)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
153
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
154 def test_mod(self):
42435
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
155 # % is no longer supported on complex numbers
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
156 self.assertRaises(TypeError, (1+1j).__mod__, 0+0j)
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
157 self.assertRaises(TypeError, lambda: (3.33+4.43j) % 0)
43284
1f9eee08ab35 Check in some documentation tweaks for PEP 3141, add some tests, and implement
Jeffrey Yasskin <jyasskin@gmail.com>
parents: 42911
diff changeset
158 self.assertRaises(TypeError, (1+1j).__mod__, 4.3j)
29867
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
159
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
160 def test_divmod(self):
42435
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
161 self.assertRaises(TypeError, divmod, 1+1j, 1+0j)
f6cc76e4ca25 Make test_complex pass again now that floordiv and mod are illegal.
Guido van Rossum <guido@python.org>
parents: 41904
diff changeset
162 self.assertRaises(TypeError, divmod, 1+1j, 0+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
163
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
164 def test_pow(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
165 self.assertAlmostEqual(pow(1+1j, 0+0j), 1.0)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
166 self.assertAlmostEqual(pow(0+0j, 2+0j), 0.0)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
167 self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
168 self.assertAlmostEqual(pow(1j, -1), 1/1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
169 self.assertAlmostEqual(pow(1j, 200), 1)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
170 self.assertRaises(ValueError, pow, 1+1j, 1+1j, 1+1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
171
29867
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
172 a = 3.33+4.43j
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
173 self.assertEqual(a ** 0j, 1)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
174 self.assertEqual(a ** 0.+0.j, 1)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
175
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
176 self.assertEqual(3j ** 0j, 1)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
177 self.assertEqual(3j ** 0, 1)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
178
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
179 try:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
180 0j ** a
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
181 except ZeroDivisionError:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
182 pass
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
183 else:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
184 self.fail("should fail 0.0 to negative or complex power")
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
185
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
186 try:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
187 0j ** (3-2j)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
188 except ZeroDivisionError:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
189 pass
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
190 else:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
191 self.fail("should fail 0.0 to negative or complex power")
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
192
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
193 # The following is used to exercise certain code paths
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
194 self.assertEqual(a ** 105, a ** 105)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
195 self.assertEqual(a ** -105, a ** -105)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
196 self.assertEqual(a ** -30, a ** -30)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
197
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
198 self.assertEqual(0.0j ** 0, 1)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
199
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
200 b = 5.1+2.3j
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
201 self.assertRaises(ValueError, pow, a, b, 0)
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
202
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
203 def test_boolcontext(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
204 for i in range(100):
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54225
diff changeset
205 self.assertTrue(complex(random() + 1e-6, random() + 1e-6))
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54225
diff changeset
206 self.assertTrue(not complex(0.0, 0.0))
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
207
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
208 def test_conjugate(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
209 self.assertClose(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
210
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
211 def test_constructor(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
212 class OS:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
213 def __init__(self, value): self.value = value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
214 def __complex__(self): return self.value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
215 class NS(object):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
216 def __init__(self, value): self.value = value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
217 def __complex__(self): return self.value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
218 self.assertEqual(complex(OS(1+10j)), 1+10j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
219 self.assertEqual(complex(NS(1+10j)), 1+10j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
220 self.assertRaises(TypeError, complex, OS(None))
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
221 self.assertRaises(TypeError, complex, NS(None))
66673
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66444
diff changeset
222 self.assertRaises(TypeError, complex, {})
80426
399e59ad0a70 Issue #16290: __complex__ must now always return an instance of complex.
Mark Dickinson <mdickinson@enthought.com>
parents: 68257
diff changeset
223 self.assertRaises(TypeError, complex, NS(1.5))
399e59ad0a70 Issue #16290: __complex__ must now always return an instance of complex.
Mark Dickinson <mdickinson@enthought.com>
parents: 68257
diff changeset
224 self.assertRaises(TypeError, complex, NS(1))
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
225
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
226 self.assertAlmostEqual(complex("1+10j"), 1+10j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
227 self.assertAlmostEqual(complex(10), 10+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
228 self.assertAlmostEqual(complex(10.0), 10+0j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
229 self.assertAlmostEqual(complex(10), 10+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
230 self.assertAlmostEqual(complex(10+0j), 10+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
231 self.assertAlmostEqual(complex(1,10), 1+10j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
232 self.assertAlmostEqual(complex(1,10), 1+10j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
233 self.assertAlmostEqual(complex(1,10.0), 1+10j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
234 self.assertAlmostEqual(complex(1,10), 1+10j)
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
235 self.assertAlmostEqual(complex(1,10), 1+10j)
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
236 self.assertAlmostEqual(complex(1,10.0), 1+10j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
237 self.assertAlmostEqual(complex(1.0,10), 1+10j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
238 self.assertAlmostEqual(complex(1.0,10), 1+10j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
239 self.assertAlmostEqual(complex(1.0,10.0), 1+10j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
240 self.assertAlmostEqual(complex(3.14+0j), 3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
241 self.assertAlmostEqual(complex(3.14), 3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
242 self.assertAlmostEqual(complex(314), 314.0+0j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
243 self.assertAlmostEqual(complex(314), 314.0+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
244 self.assertAlmostEqual(complex(3.14+0j, 0j), 3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
245 self.assertAlmostEqual(complex(3.14, 0.0), 3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
246 self.assertAlmostEqual(complex(314, 0), 314.0+0j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
247 self.assertAlmostEqual(complex(314, 0), 314.0+0j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
248 self.assertAlmostEqual(complex(0j, 3.14j), -3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
249 self.assertAlmostEqual(complex(0.0, 3.14j), -3.14+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
250 self.assertAlmostEqual(complex(0j, 3.14), 3.14j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
251 self.assertAlmostEqual(complex(0.0, 3.14), 3.14j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
252 self.assertAlmostEqual(complex("1"), 1+0j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
253 self.assertAlmostEqual(complex("1j"), 1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
254 self.assertAlmostEqual(complex(), 0)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
255 self.assertAlmostEqual(complex("-1"), -1)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
256 self.assertAlmostEqual(complex("+1"), +1)
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
257 self.assertAlmostEqual(complex("(1+2j)"), 1+2j)
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
258 self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j)
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
259 self.assertAlmostEqual(complex("3.14+1J"), 3.14+1j)
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
260 self.assertAlmostEqual(complex(" ( +3.14-6J )"), 3.14-6j)
53343
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
261 self.assertAlmostEqual(complex(" ( +3.14-J )"), 3.14-1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
262 self.assertAlmostEqual(complex(" ( +3.14+j )"), 3.14+1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
263 self.assertAlmostEqual(complex("J"), 1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
264 self.assertAlmostEqual(complex("( j )"), 1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
265 self.assertAlmostEqual(complex("+J"), 1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
266 self.assertAlmostEqual(complex("( -j)"), -1j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
267 self.assertAlmostEqual(complex('1e-500'), 0.0 + 0.0j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
268 self.assertAlmostEqual(complex('-1e-500j'), 0.0 - 0.0j)
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
269 self.assertAlmostEqual(complex('-1e-500+1e-500j'), -0.0 + 0.0j)
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
270
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
271 class complex2(complex): pass
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
272 self.assertAlmostEqual(complex(complex2(1+1j)), 1+1j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
273 self.assertAlmostEqual(complex(real=17, imag=23), 17+23j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
274 self.assertAlmostEqual(complex(real=17+23j), 17+23j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
275 self.assertAlmostEqual(complex(real=17+23j, imag=23), 17+46j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
276 self.assertAlmostEqual(complex(real=1+2j, imag=3+4j), -3+5j)
21593
ff70e5634ae9 SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21401
diff changeset
277
44045
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
278 # check that the sign of a zero in the real or imaginary part
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
279 # is preserved when constructing from two floats. (These checks
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
280 # are harmless on systems without support for signed zeros.)
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
281 def split_zeros(x):
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
282 """Function that produces different results for 0. and -0."""
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
283 return atan2(x, -1.)
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
284
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
285 self.assertEqual(split_zeros(complex(1., 0.).imag), split_zeros(0.))
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
286 self.assertEqual(split_zeros(complex(1., -0.).imag), split_zeros(-0.))
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
287 self.assertEqual(split_zeros(complex(0., 1.).real), split_zeros(0.))
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
288 self.assertEqual(split_zeros(complex(-0., 1.).real), split_zeros(-0.))
530fb9852c50 Merged revisions 59202-59211 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 43284
diff changeset
289
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
290 c = 3.14 + 1j
55593
0bb232078b8c Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents: 54225
diff changeset
291 self.assertTrue(complex(c) is c)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
292 del c
21593
ff70e5634ae9 SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21401
diff changeset
293
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
294 self.assertRaises(TypeError, complex, "1", "1")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
295 self.assertRaises(TypeError, complex, 1, "1")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
296
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
297 # SF bug 543840: complex(string) accepts strings with \0
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
298 # Fixed in 2.3.
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
299 self.assertRaises(ValueError, complex, '1+1j\0j')
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
300
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
301 self.assertRaises(TypeError, int, 5+3j)
40644
1c26505c1de6 Rip out 'long' and 'L'-suffixed integer literals.
Guido van Rossum <guido@python.org>
parents: 39603
diff changeset
302 self.assertRaises(TypeError, int, 5+3j)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
303 self.assertRaises(TypeError, float, 5+3j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
304 self.assertRaises(ValueError, complex, "")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
305 self.assertRaises(TypeError, complex, None)
86992
a73c47c1d374 #17080: improve error message of float/complex when the wrong type is passed.
Ezio Melotti <ezio.melotti@gmail.com>
parents: 85836
diff changeset
306 self.assertRaisesRegex(TypeError, "not 'NoneType'", complex, None)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
307 self.assertRaises(ValueError, complex, "\0")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
308 self.assertRaises(ValueError, complex, "3\09")
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
309 self.assertRaises(TypeError, complex, "1", "2")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
310 self.assertRaises(TypeError, complex, "1", 42)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
311 self.assertRaises(TypeError, complex, 1, "2")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
312 self.assertRaises(ValueError, complex, "1+")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
313 self.assertRaises(ValueError, complex, "1+1j+1j")
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
314 self.assertRaises(ValueError, complex, "--")
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
315 self.assertRaises(ValueError, complex, "(1+2j")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
316 self.assertRaises(ValueError, complex, "1+2j)")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
317 self.assertRaises(ValueError, complex, "1+(2j)")
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
318 self.assertRaises(ValueError, complex, "(1+2j)123")
41821
ae691227fd34 Remove have_unicode checks and merge those tests into the
Walter Dörwald <walter@livinglogic.de>
parents: 41642
diff changeset
319 self.assertRaises(ValueError, complex, "x")
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
320 self.assertRaises(ValueError, complex, "1j+2")
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
321 self.assertRaises(ValueError, complex, "1e1ej")
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
322 self.assertRaises(ValueError, complex, "1e++1ej")
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
323 self.assertRaises(ValueError, complex, ")1+2j(")
53343
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
324 # the following three are accepted by Python 2.6
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
325 self.assertRaises(ValueError, complex, "1..1j")
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
326 self.assertRaises(ValueError, complex, "1.11.1j")
06db1d9d255f Make sure that complex parsing code and corresponding tests
Mark Dickinson <dickinsm@gmail.com>
parents: 53332
diff changeset
327 self.assertRaises(ValueError, complex, "1e1.1j")
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
328
56769
4cbea9e78356 Remove length limitation on string arguments to complex()
Mark Dickinson <dickinsm@gmail.com>
parents: 55593
diff changeset
329 # check that complex accepts long unicode strings
4cbea9e78356 Remove length limitation on string arguments to complex()
Mark Dickinson <dickinsm@gmail.com>
parents: 55593
diff changeset
330 self.assertEqual(type(complex("1"*500)), complex)
66673
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66444
diff changeset
331 # check whitespace processing
7b6af56ee74d Issue #10557: Fixed error messages from float() and other numeric
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents: 66444
diff changeset
332 self.assertEqual(complex('\N{EM SPACE}(\N{EN SPACE}1+1j ) '), 1+1j)
56769
4cbea9e78356 Remove length limitation on string arguments to complex()
Mark Dickinson <dickinsm@gmail.com>
parents: 55593
diff changeset
333
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
334 class EvilExc(Exception):
29668
36a17904a3ed Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 29532
diff changeset
335 pass
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
336
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
337 class evilcomplex:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
338 def __complex__(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
339 raise EvilExc
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
340
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
341 self.assertRaises(EvilExc, complex, evilcomplex())
21593
ff70e5634ae9 SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21401
diff changeset
342
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
343 class float2:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
344 def __init__(self, value):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
345 self.value = value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
346 def __float__(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
347 return self.value
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
348
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
349 self.assertAlmostEqual(complex(float2(42.)), 42)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
350 self.assertAlmostEqual(complex(real=float2(17.), imag=float2(23.)), 17+23j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
351 self.assertRaises(TypeError, complex, float2(None))
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
352
34931
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
353 class complex0(complex):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
354 """Test usage of __complex__() when inheriting from 'complex'"""
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
355 def __complex__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
356 return 42j
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
357
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
358 class complex1(complex):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
359 """Test usage of __complex__() with a __new__() method"""
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
360 def __new__(self, value=0j):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
361 return complex.__new__(self, 2*value)
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
362 def __complex__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
363 return self
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
364
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
365 class complex2(complex):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
366 """Make sure that __complex__() calls fail if anything other than a
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
367 complex is returned"""
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
368 def __complex__(self):
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
369 return None
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
370
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
371 self.assertAlmostEqual(complex(complex0(1j)), 42j)
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
372 self.assertAlmostEqual(complex(complex1(1j)), 2j)
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
373 self.assertRaises(TypeError, complex, complex2(1j))
9a1b1e1ae653 Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents: 33233
diff changeset
374
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
375 def test_hash(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
376 for x in range(-30, 30):
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
377 self.assertEqual(hash(x), hash(complex(x, 0)))
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
378 x /= 3.0 # now check against floating point
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
379 self.assertEqual(hash(x), hash(complex(x, 0.)))
21593
ff70e5634ae9 SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents: 21401
diff changeset
380
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
381 def test_abs(self):
41642
a6eb6acfe04a Merged revisions 55007-55179 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41575
diff changeset
382 nums = [complex(x/3., y/7.) for x in range(-9,9) for y in range(-9,9)]
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
383 for num in nums:
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
384 self.assertAlmostEqual((num.real**2 + num.imag**2) ** 0.5, abs(num))
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
385
66705
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
386 def test_repr_str(self):
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
387 def test(v, expected, test_fn=self.assertEqual):
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
388 test_fn(repr(v), expected)
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
389 test_fn(str(v), expected)
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
390
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
391 test(1+6j, '(1+6j)')
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
392 test(1-6j, '(1-6j)')
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
393
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
394 test(-(1+0j), '(-1+-0j)', test_fn=self.assertNotEqual)
17066
2c1be7924b04 SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff changeset
395
66705
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
396 test(complex(1., INF), "(1+infj)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
397 test(complex(1., -INF), "(1-infj)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
398 test(complex(INF, 1), "(inf+1j)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
399 test(complex(-INF, INF), "(-inf+infj)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
400 test(complex(NAN, 1), "(nan+1j)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
401 test(complex(1, NAN), "(1+nanj)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
402 test(complex(NAN, NAN), "(nan+nanj)")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
403
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
404 test(complex(0, INF), "infj")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
405 test(complex(0, -INF), "-infj")
fd638ca74634 Removed static function complex_format, moved it into complex_repr. Modified tests to check both str and repr, which are the same for complex.
Eric Smith <eric@trueblade.com>
parents: 66673
diff changeset
406 test(complex(0, NAN), "nanj")
35586
e6119d577cd3 Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents: 34931
diff changeset
407
41554
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
408 self.assertEqual(1-6j,complex(repr(1-6j)))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
409 self.assertEqual(1+6j,complex(repr(1+6j)))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
410 self.assertEqual(-6j,complex(repr(-6j)))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
411 self.assertEqual(6j,complex(repr(6j)))
06145fbc7ab9 Merged revisions 53952-54987 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 40775
diff changeset
412
66710
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
413 @support.requires_IEEE_754
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
414 def test_negative_zero_repr_str(self):
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
415 def test(v, expected, test_fn=self.assertEqual):
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
416 test_fn(repr(v), expected)
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
417 test_fn(str(v), expected)
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45082
diff changeset
418
66710
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
419 test(complex(0., 1.), "1j")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
420 test(complex(-0., 1.), "(-0+1j)")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
421 test(complex(0., -1.), "-1j")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
422 test(complex(-0., -1.), "(-0-1j)")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
423
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
424 test(complex(0., 0.), "0j")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
425 test(complex(0., -0.), "-0j")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
426 test(complex(-0., 0.), "(-0+0j)")
b55f3d639cd8 Issue 10625: Add tests for negative zeros in complex str and repr.
Eric Smith <eric@trueblade.com>
parents: 66709
diff changeset
427 test(complex(-0., -0.), "(-0-0j)")
45371
7c4c46342137 Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60845 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 45082
diff changeset
428
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
429 def test_neg(self):
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
430 self.assertEqual(-(1+6j), -1-6j)
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
431
29867
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
432 def test_file(self):
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
433 a = 3.33+4.43j
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
434 b = 5.1+2.3j
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
435
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
436 fo = None
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
437 try:
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45371
diff changeset
438 fo = open(support.TESTFN, "w")
40775
4cdf21a0dbbc Fix most trivially-findable print statements.
Guido van Rossum <guido@python.org>
parents: 40644
diff changeset
439 print(a, b, file=fo)
29867
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
440 fo.close()
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45371
diff changeset
441 fo = open(support.TESTFN, "r")
42911
cb28e14ee808 Changes in anticipation of stricter str vs. bytes enforcement.
Guido van Rossum <guido@python.org>
parents: 42902
diff changeset
442 self.assertEqual(fo.read(), ("%s %s\n" % (a, b)))
29867
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
443 finally:
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
444 if (fo is not None) and (not fo.closed):
199cd78d6d78 Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents: 29668
diff changeset
445 fo.close()
60174
255a401c19b0 (partially)
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59563
diff changeset
446 support.unlink(support.TESTFN)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
447
47555
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
448 def test_getnewargs(self):
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
449 self.assertEqual((1+2j).__getnewargs__(), (1.0, 2.0))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
450 self.assertEqual((1-2j).__getnewargs__(), (1.0, -2.0))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
451 self.assertEqual((2j).__getnewargs__(), (0.0, 2.0))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
452 self.assertEqual((-0j).__getnewargs__(), (0.0, -0.0))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
453 self.assertEqual(complex(0, INF).__getnewargs__(), (0.0, INF))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
454 self.assertEqual(complex(INF, 0).__getnewargs__(), (INF, 0.0))
7d824fd131ad Merged revisions 63829-63831,63858,63865,63879,63882,63948,63970-63972,63976,63989,64014-64015,64021-64022,64063-64065,64067 via svnmerge from
Georg Brandl <georg@python.org>
parents: 47201
diff changeset
455
66709
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
456 @support.requires_IEEE_754
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
457 def test_plus_minus_0j(self):
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
458 # test that -0j and 0j literals are not identified
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
459 z1, z2 = 0j, -0j
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
460 self.assertEqual(atan2(z1.imag, -1.), atan2(0., -1.))
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
461 self.assertEqual(atan2(z2.imag, -1.), atan2(-0., -1.))
45082
d7540031b959 Merged revisions 60475-60479,60481-60488 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents: 44045
diff changeset
462
66709
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
463 @support.requires_IEEE_754
62648
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
464 def test_negated_imaginary_literal(self):
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
465 z0 = -0j
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
466 z1 = -7j
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
467 z2 = -1e1000j
62649
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
468 # Note: In versions of Python < 3.2, a negated imaginary literal
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
469 # accidentally ended up with real part 0.0 instead of -0.0, thanks to a
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
470 # modification during CST -> AST translation (see issue #9011). That's
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
471 # fixed in Python 3.2.
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
472 self.assertFloatsAreIdentical(z0.real, -0.0)
62648
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
473 self.assertFloatsAreIdentical(z0.imag, -0.0)
62649
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
474 self.assertFloatsAreIdentical(z1.real, -0.0)
62648
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
475 self.assertFloatsAreIdentical(z1.imag, -7.0)
62649
2bc54da4b82c Issue #9011: Tests for Python 3.2's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 61787
diff changeset
476 self.assertFloatsAreIdentical(z2.real, -0.0)
62648
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
477 self.assertFloatsAreIdentical(z2.imag, -INF)
e6f1b3d1040a Tests for Python 3.1's treatment of negated imaginary literals.
Mark Dickinson <dickinsm@gmail.com>
parents: 59028
diff changeset
478
66709
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
479 @support.requires_IEEE_754
54225
670fa0c0ff9a Issue #5829: complex('1e500') shouldn't raise OverflowError
Mark Dickinson <dickinsm@gmail.com>
parents: 53849
diff changeset
480 def test_overflow(self):
670fa0c0ff9a Issue #5829: complex('1e500') shouldn't raise OverflowError
Mark Dickinson <dickinsm@gmail.com>
parents: 53849
diff changeset
481 self.assertEqual(complex("1e500"), complex(INF, 0.0))
670fa0c0ff9a Issue #5829: complex('1e500') shouldn't raise OverflowError
Mark Dickinson <dickinsm@gmail.com>
parents: 53849
diff changeset
482 self.assertEqual(complex("-1e500j"), complex(0.0, -INF))
670fa0c0ff9a Issue #5829: complex('1e500') shouldn't raise OverflowError
Mark Dickinson <dickinsm@gmail.com>
parents: 53849
diff changeset
483 self.assertEqual(complex("-1e500+1.8e308j"), complex(-INF, INF))
670fa0c0ff9a Issue #5829: complex('1e500') shouldn't raise OverflowError
Mark Dickinson <dickinsm@gmail.com>
parents: 53849
diff changeset
484
66709
f2a4ade36a1e Issue #10624: Use support.requires_IEEE_754 in all appropriate tests.
Eric Smith <eric@trueblade.com>
parents: 66705
diff changeset
485 @support.requires_IEEE_754
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
486 def test_repr_roundtrip(self):
53377
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
487 vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN]
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
488 vals += [-v for v in vals]
53377
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
489
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
490 # complex(repr(z)) should recover z exactly, even for complex
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
491 # numbers involving an infinity, nan, or negative zero
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
492 for x in vals:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
493 for y in vals:
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
494 z = complex(x, y)
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
495 roundtrip = complex(repr(z))
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
496 self.assertFloatsAreIdentical(z.real, roundtrip.real)
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
497 self.assertFloatsAreIdentical(z.imag, roundtrip.imag)
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
498
53377
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
499 # if we predefine some constants, then eval(repr(z)) should
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
500 # also work, except that it might change the sign of zeros
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
501 inf, nan = float('inf'), float('nan')
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
502 infj, nanj = complex(0.0, inf), complex(0.0, nan)
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
503 for x in vals:
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
504 for y in vals:
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
505 z = complex(x, y)
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
506 roundtrip = eval(repr(z))
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
507 # adding 0.0 has no effect beside changing -0.0 to 0.0
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
508 self.assertFloatsAreIdentical(0.0 + z.real,
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
509 0.0 + roundtrip.real)
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
510 self.assertFloatsAreIdentical(0.0 + z.imag,
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
511 0.0 + roundtrip.imag)
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
512
53632
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
513 def test_format(self):
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
514 # empty format string is same as str()
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
515 self.assertEqual(format(1+3j, ''), str(1+3j))
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
516 self.assertEqual(format(1.5+3.5j, ''), str(1.5+3.5j))
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
517 self.assertEqual(format(3j, ''), str(3j))
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
518 self.assertEqual(format(3.2j, ''), str(3.2j))
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
519 self.assertEqual(format(3+0j, ''), str(3+0j))
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
520 self.assertEqual(format(3.2+0j, ''), str(3.2+0j))
53377
a34d945c07ed Merged revisions 71869 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 53343
diff changeset
521
53787
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
522 # empty presentation type should still be analogous to str,
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
523 # even when format string is nonempty (issue #5920).
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
524 self.assertEqual(format(3.2+0j, '-'), str(3.2+0j))
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
525 self.assertEqual(format(3.2+0j, '<'), str(3.2+0j))
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
526 z = 4/7. - 100j/7.
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
527 self.assertEqual(format(z, ''), str(z))
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
528 self.assertEqual(format(z, '-'), str(z))
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
529 self.assertEqual(format(z, '<'), str(z))
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
530 self.assertEqual(format(z, '10'), str(z))
63417
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
531 z = complex(0.0, 3.0)
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
532 self.assertEqual(format(z, ''), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
533 self.assertEqual(format(z, '-'), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
534 self.assertEqual(format(z, '<'), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
535 self.assertEqual(format(z, '2'), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
536 z = complex(-0.0, 2.0)
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
537 self.assertEqual(format(z, ''), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
538 self.assertEqual(format(z, '-'), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
539 self.assertEqual(format(z, '<'), str(z))
398fa7b7753a Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents: 62648
diff changeset
540 self.assertEqual(format(z, '3'), str(z))
53787
c6376d3fa968 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.
Eric Smith <eric@trueblade.com>
parents: 53632
diff changeset
541
53632
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
542 self.assertEqual(format(1+3j, 'g'), '1+3j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
543 self.assertEqual(format(3j, 'g'), '0+3j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
544 self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
545
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
546 self.assertEqual(format(1.5+3.5j, '+g'), '+1.5+3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
547 self.assertEqual(format(1.5-3.5j, '+g'), '+1.5-3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
548 self.assertEqual(format(1.5-3.5j, '-g'), '1.5-3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
549 self.assertEqual(format(1.5+3.5j, ' g'), ' 1.5+3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
550 self.assertEqual(format(1.5-3.5j, ' g'), ' 1.5-3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
551 self.assertEqual(format(-1.5+3.5j, ' g'), '-1.5+3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
552 self.assertEqual(format(-1.5-3.5j, ' g'), '-1.5-3.5j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
553
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
554 self.assertEqual(format(-1.5-3.5e-20j, 'g'), '-1.5-3.5e-20j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
555 self.assertEqual(format(-1.5-3.5j, 'f'), '-1.500000-3.500000j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
556 self.assertEqual(format(-1.5-3.5j, 'F'), '-1.500000-3.500000j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
557 self.assertEqual(format(-1.5-3.5j, 'e'), '-1.500000e+00-3.500000e+00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
558 self.assertEqual(format(-1.5-3.5j, '.2e'), '-1.50e+00-3.50e+00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
559 self.assertEqual(format(-1.5-3.5j, '.2E'), '-1.50E+00-3.50E+00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
560 self.assertEqual(format(-1.5e10-3.5e5j, '.2G'), '-1.5E+10-3.5E+05j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
561
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
562 self.assertEqual(format(1.5+3j, '<20g'), '1.5+3j ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
563 self.assertEqual(format(1.5+3j, '*<20g'), '1.5+3j**************')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
564 self.assertEqual(format(1.5+3j, '>20g'), ' 1.5+3j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
565 self.assertEqual(format(1.5+3j, '^20g'), ' 1.5+3j ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
566 self.assertEqual(format(1.5+3j, '<20'), '(1.5+3j) ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
567 self.assertEqual(format(1.5+3j, '>20'), ' (1.5+3j)')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
568 self.assertEqual(format(1.5+3j, '^20'), ' (1.5+3j) ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
569 self.assertEqual(format(1.123-3.123j, '^20.2'), ' (1.1-3.1j) ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
570
59028
73cd83706bc4 Merged revisions 78333 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 55593
diff changeset
571 self.assertEqual(format(1.5+3j, '20.2f'), ' 1.50+3.00j')
73cd83706bc4 Merged revisions 78333 via svnmerge from
Eric Smith <eric@trueblade.com>
parents: 55593
diff changeset
572 self.assertEqual(format(1.5+3j, '>20.2f'), ' 1.50+3.00j')
53632
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
573 self.assertEqual(format(1.5+3j, '<20.2f'), '1.50+3.00j ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
574 self.assertEqual(format(1.5e20+3j, '<20.2f'), '150000000000000000000.00+3.00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
575 self.assertEqual(format(1.5e20+3j, '>40.2f'), ' 150000000000000000000.00+3.00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
576 self.assertEqual(format(1.5e20+3j, '^40,.2f'), ' 150,000,000,000,000,000,000.00+3.00j ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
577 self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
578 self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
579
66444
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
580 # Issue 7094: Alternate formatting (specified by #)
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
581 self.assertEqual(format(1+1j, '.0e'), '1e+00+1e+00j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
582 self.assertEqual(format(1+1j, '#.0e'), '1.e+00+1.e+00j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
583 self.assertEqual(format(1+1j, '.0f'), '1+1j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
584 self.assertEqual(format(1+1j, '#.0f'), '1.+1.j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
585 self.assertEqual(format(1.1+1.1j, 'g'), '1.1+1.1j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
586 self.assertEqual(format(1.1+1.1j, '#g'), '1.10000+1.10000j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
587
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
588 # Alternate doesn't make a difference for these, they format the same with or without it
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
589 self.assertEqual(format(1+1j, '.1e'), '1.0e+00+1.0e+00j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
590 self.assertEqual(format(1+1j, '#.1e'), '1.0e+00+1.0e+00j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
591 self.assertEqual(format(1+1j, '.1f'), '1.0+1.0j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
592 self.assertEqual(format(1+1j, '#.1f'), '1.0+1.0j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
593
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
594 # Misc. other alternate tests
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
595 self.assertEqual(format((-1.5+0.5j), '#f'), '-1.500000+0.500000j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
596 self.assertEqual(format((-1.5+0.5j), '#.0f'), '-2.+0.j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
597 self.assertEqual(format((-1.5+0.5j), '#e'), '-1.500000e+00+5.000000e-01j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
598 self.assertEqual(format((-1.5+0.5j), '#.0e'), '-2.e+00+5.e-01j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
599 self.assertEqual(format((-1.5+0.5j), '#g'), '-1.50000+0.500000j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
600 self.assertEqual(format((-1.5+0.5j), '.0g'), '-2+0.5j')
eded691497b7 Issue #7094: Add alternate ('#') flag to __format__ methods for float, complex and Decimal. Allows greater control over when decimal points appear. Added to make transitioning from %-formatting easier. '#g' still has a problem with Decimal which I'll fix soon.
Eric Smith <eric@trueblade.com>
parents: 66310
diff changeset
601 self.assertEqual(format((-1.5+0.5j), '#.0g'), '-2.+0.5j')
53632
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
602
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
603 # zero padding is invalid
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
604 self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
605
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
606 # '=' alignment is invalid
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
607 self.assertRaises(ValueError, (1.5+3j).__format__, '=20')
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
608
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
609 # integer presentation types are an error
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
610 for t in 'bcdoxX':
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
611 self.assertRaises(ValueError, (1.5+0.5j).__format__, t)
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
612
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
613 # make sure everything works in ''.format()
2cde0d003ffc Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents: 53377
diff changeset
614 self.assertEqual('*{0:.3f}*'.format(3.14159+2.71828j), '*3.142+2.718j*')
53332
330115137414 Issue #5816: Simplify code for parsing and printing of complex numbers.
Mark Dickinson <dickinsm@gmail.com>
parents: 47555
diff changeset
615
53849
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
616 # issue 3382
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
617 self.assertEqual(format(complex(NAN, NAN), 'f'), 'nan+nanj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
618 self.assertEqual(format(complex(1, NAN), 'f'), '1.000000+nanj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
619 self.assertEqual(format(complex(NAN, 1), 'f'), 'nan+1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
620 self.assertEqual(format(complex(NAN, -1), 'f'), 'nan-1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
621 self.assertEqual(format(complex(NAN, NAN), 'F'), 'NAN+NANj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
622 self.assertEqual(format(complex(1, NAN), 'F'), '1.000000+NANj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
623 self.assertEqual(format(complex(NAN, 1), 'F'), 'NAN+1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
624 self.assertEqual(format(complex(NAN, -1), 'F'), 'NAN-1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
625 self.assertEqual(format(complex(INF, INF), 'f'), 'inf+infj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
626 self.assertEqual(format(complex(1, INF), 'f'), '1.000000+infj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
627 self.assertEqual(format(complex(INF, 1), 'f'), 'inf+1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
628 self.assertEqual(format(complex(INF, -1), 'f'), 'inf-1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
629 self.assertEqual(format(complex(INF, INF), 'F'), 'INF+INFj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
630 self.assertEqual(format(complex(1, INF), 'F'), '1.000000+INFj')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
631 self.assertEqual(format(complex(INF, 1), 'F'), 'INF+1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
632 self.assertEqual(format(complex(INF, -1), 'F'), 'INF-1.000000j')
04bdc944718a Issue #3382. float 'F' formatting no longer maps to 'f'. This only affects nan and inf.
Eric Smith <eric@trueblade.com>
parents: 53787
diff changeset
633
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
634 def test_main():
47201
9c930a32cebb #2621 rename test.test_support to test.support
Benjamin Peterson <benjamin@python.org>
parents: 45371
diff changeset
635 support.run_unittest(ComplexTest)
29532
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
636
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
637 if __name__ == "__main__":
2fbd256a1db2 Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents: 24442
diff changeset
638 test_main()