Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_complex.py @ 80661:94de77bd0b4b 2.7
Issue #16194: document imp.load_dynamic problems
when read several extension modules from the same binary object file.
| author | Andrew Svetlov <andrew.svetlov@gmail.com> |
|---|---|
| date | Thu, 29 Nov 2012 19:51:28 +0200 |
| parents | b36cb4602e21 |
| children |
| rev | line source |
|---|---|
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53396
diff
changeset
|
1 import unittest |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
2 from test import test_support |
|
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 |
| 53338 | 5 from math import atan2, isnan, copysign |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
6 |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
7 INF = float("inf") |
|
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
8 NAN = float("nan") |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
9 # 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
|
10 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
11 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
|
12 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
13 def assertAlmostEqual(self, a, b): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
14 if isinstance(a, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
15 if isinstance(b, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
16 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
|
17 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
|
18 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
19 unittest.TestCase.assertAlmostEqual(self, a.real, b) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
20 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
21 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
22 if isinstance(b, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
23 unittest.TestCase.assertAlmostEqual(self, a, b.real) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
24 unittest.TestCase.assertAlmostEqual(self, 0., b.imag) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
25 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
26 unittest.TestCase.assertAlmostEqual(self, a, b) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
27 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
28 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
|
29 """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
|
30 # put the one with larger magnitude second |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
31 if abs(x) > abs(y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
32 x, y = y, x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
33 if y == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
34 return abs(x) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
35 if x == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
36 return abs(y) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
37 # check that relative difference < eps |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
38 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
|
39 |
| 53338 | 40 def assertFloatsAreIdentical(self, x, y): |
| 41 """assert that floats x and y are identical, in the sense that: | |
| 42 (1) both x and y are nans, or | |
| 43 (2) both x and y are infinities, with the same sign, or | |
| 44 (3) both x and y are zeros, with the same sign, or | |
| 45 (4) x and y are both finite and nonzero, and x == y | |
| 46 | |
| 47 """ | |
| 48 msg = 'floats {!r} and {!r} are not identical' | |
| 49 | |
| 50 if isnan(x) or isnan(y): | |
| 51 if isnan(x) and isnan(y): | |
| 52 return | |
| 53 elif x == y: | |
| 54 if x != 0.0: | |
| 55 return | |
| 56 # both zero; check that signs match | |
| 57 elif copysign(1.0, x) == copysign(1.0, y): | |
| 58 return | |
| 59 else: | |
| 60 msg += ': zeros have different signs' | |
| 61 self.fail(msg.format(x, y)) | |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
62 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
63 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
|
64 """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
|
65 self.assertCloseAbs(x.real, y.real, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
66 self.assertCloseAbs(x.imag, y.imag, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
67 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
68 def check_div(self, x, y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
69 """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
|
70 z = x * y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
71 if x != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
72 q = z / x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
73 self.assertClose(q, y) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
74 q = z.__div__(x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
75 self.assertClose(q, y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
76 q = z.__truediv__(x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
77 self.assertClose(q, y) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
78 if y != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
79 q = z / y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
80 self.assertClose(q, x) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
81 q = z.__div__(y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
82 self.assertClose(q, x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
83 q = z.__truediv__(y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
84 self.assertClose(q, x) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
85 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
86 def test_div(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
87 simple_real = [float(i) for i in xrange(-5, 6)] |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
88 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
|
89 for x in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
90 for y in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
91 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
|
92 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
93 # 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
|
94 # nonsense errors for these (overflows and underflows). |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
95 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
|
96 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
|
97 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
98 # Just for fun. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
99 for i in xrange(100): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
100 self.check_div(complex(random(), random()), |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
101 complex(random(), random())) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
102 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
103 self.assertRaises(ZeroDivisionError, complex.__div__, 1+1j, 0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
104 # FIXME: The following currently crashes on Alpha |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
105 # 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
|
106 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
107 def test_truediv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
108 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
|
109 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
|
110 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
111 def test_floordiv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
112 self.assertAlmostEqual(complex.__floordiv__(3+0j, 1.5+0j), 2) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
113 self.assertRaises(ZeroDivisionError, complex.__floordiv__, 3+0j, 0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
114 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
115 def test_coerce(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
116 self.assertRaises(OverflowError, complex.__coerce__, 1+1j, 1L<<10000) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
117 |
|
61987
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
118 def test_no_implicit_coerce(self): |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
119 # Python 2.7 removed implicit coercion from the complex type |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
120 class A(object): |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
121 def __coerce__(self, other): |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
122 raise RuntimeError |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
123 __hash__ = None |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
124 def __cmp__(self, other): |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
125 return -1 |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
126 |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
127 a = A() |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
128 self.assertRaises(TypeError, lambda: a + 2.0j) |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
129 self.assertTrue(a < 2.0j) |
|
ed79da3a591c
Issue #5211: Complete removal of implicit coercions for the complex
Mark Dickinson <dickinsm@gmail.com>
parents:
60116
diff
changeset
|
130 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
131 def test_richcompare(self): |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
132 self.assertEqual(complex.__eq__(1+1j, 1L<<10000), False) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
133 self.assertEqual(complex.__lt__(1+1j, None), NotImplemented) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
134 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
|
135 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
|
136 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
|
137 self.assertIs(complex.__ne__(1+1j, 2+2j), True) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
138 self.assertRaises(TypeError, complex.__lt__, 1+1j, 2+2j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
139 self.assertRaises(TypeError, complex.__le__, 1+1j, 2+2j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
140 self.assertRaises(TypeError, complex.__gt__, 1+1j, 2+2j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
141 self.assertRaises(TypeError, complex.__ge__, 1+1j, 2+2j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
142 |
|
61991
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
143 def test_richcompare_boundaries(self): |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
144 def check(n, deltas, is_equal, imag = 0.0): |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
145 for delta in deltas: |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
146 i = n + delta |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
147 z = complex(i, imag) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
148 self.assertIs(complex.__eq__(z, i), is_equal(delta)) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
149 self.assertIs(complex.__ne__(z, i), not is_equal(delta)) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
150 # For IEEE-754 doubles the following should hold: |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
151 # x in [2 ** (52 + i), 2 ** (53 + i + 1)] -> x mod 2 ** i == 0 |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
152 # where the interval is representable, of course. |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
153 for i in range(1, 10): |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
154 pow = 52 + i |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
155 mult = 2 ** i |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
156 check(2 ** pow, range(1, 101), lambda delta: delta % mult == 0) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
157 check(2 ** pow, range(1, 101), lambda delta: False, float(i)) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
158 check(2 ** 53, range(-100, 0), lambda delta: True) |
|
3edfad2549c6
Issue #8748: Fix incorrect results from comparisons between an integer
Mark Dickinson <dickinsm@gmail.com>
parents:
61987
diff
changeset
|
159 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
160 def test_mod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
161 self.assertRaises(ZeroDivisionError, (1+1j).__mod__, 0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
162 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
163 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
164 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
165 a % 0 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
166 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
167 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
168 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
169 self.fail("modulo parama can't be 0") |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
170 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
171 def test_divmod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
172 self.assertRaises(ZeroDivisionError, divmod, 1+1j, 0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
173 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
174 def test_pow(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
175 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
|
176 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
|
177 self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
178 self.assertAlmostEqual(pow(1j, -1), 1/1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
179 self.assertAlmostEqual(pow(1j, 200), 1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
180 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
|
181 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
182 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
183 self.assertEqual(a ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
184 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
|
185 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
186 self.assertEqual(3j ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
187 self.assertEqual(3j ** 0, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
188 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
189 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
190 0j ** a |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
191 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
192 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
193 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
194 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
|
195 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
196 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
197 0j ** (3-2j) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
198 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
199 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
200 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
201 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
|
202 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
203 # 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
|
204 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
|
205 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
|
206 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
|
207 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
208 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
|
209 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
210 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
211 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
|
212 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
213 def test_boolcontext(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
214 for i in xrange(100): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
215 self.assertTrue(complex(random() + 1e-6, random() + 1e-6)) |
|
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
216 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
|
217 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
218 def test_conjugate(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
219 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
|
220 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
221 def test_constructor(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
222 class OS: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
223 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
224 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
225 class NS(object): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
226 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
227 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
228 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
|
229 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
|
230 self.assertRaises(TypeError, complex, OS(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
231 self.assertRaises(TypeError, complex, NS(None)) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
232 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
233 self.assertAlmostEqual(complex("1+10j"), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
234 self.assertAlmostEqual(complex(10), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
235 self.assertAlmostEqual(complex(10.0), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
236 self.assertAlmostEqual(complex(10L), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
237 self.assertAlmostEqual(complex(10+0j), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
238 self.assertAlmostEqual(complex(1,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
239 self.assertAlmostEqual(complex(1,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
240 self.assertAlmostEqual(complex(1,10.0), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
241 self.assertAlmostEqual(complex(1L,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
242 self.assertAlmostEqual(complex(1L,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
243 self.assertAlmostEqual(complex(1L,10.0), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
244 self.assertAlmostEqual(complex(1.0,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
245 self.assertAlmostEqual(complex(1.0,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
246 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
|
247 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
|
248 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
|
249 self.assertAlmostEqual(complex(314), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
250 self.assertAlmostEqual(complex(314L), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
251 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
|
252 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
|
253 self.assertAlmostEqual(complex(314, 0), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
254 self.assertAlmostEqual(complex(314L, 0L), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
255 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
|
256 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
|
257 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
|
258 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
|
259 self.assertAlmostEqual(complex("1"), 1+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
260 self.assertAlmostEqual(complex("1j"), 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
261 self.assertAlmostEqual(complex(), 0) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
262 self.assertAlmostEqual(complex("-1"), -1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
263 self.assertAlmostEqual(complex("+1"), +1) |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
264 self.assertAlmostEqual(complex("(1+2j)"), 1+2j) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
265 self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j) |
| 53338 | 266 self.assertAlmostEqual(complex("3.14+1J"), 3.14+1j) |
| 267 self.assertAlmostEqual(complex(" ( +3.14-6J )"), 3.14-6j) | |
| 268 self.assertAlmostEqual(complex(" ( +3.14-J )"), 3.14-1j) | |
| 269 self.assertAlmostEqual(complex(" ( +3.14+j )"), 3.14+1j) | |
| 270 self.assertAlmostEqual(complex("J"), 1j) | |
| 271 self.assertAlmostEqual(complex("( j )"), 1j) | |
| 272 self.assertAlmostEqual(complex("+J"), 1j) | |
| 273 self.assertAlmostEqual(complex("( -j)"), -1j) | |
| 274 self.assertAlmostEqual(complex('1e-500'), 0.0 + 0.0j) | |
| 275 self.assertAlmostEqual(complex('-1e-500j'), 0.0 - 0.0j) | |
| 276 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
|
277 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
278 class complex2(complex): pass |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
279 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
|
280 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
|
281 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
|
282 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
|
283 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
|
284 |
|
44037
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
285 # check that the sign of a zero in the real or imaginary part |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
286 # is preserved when constructing from two floats. (These checks |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
287 # are harmless on systems without support for signed zeros.) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
288 def split_zeros(x): |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
289 """Function that produces different results for 0. and -0.""" |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
290 return atan2(x, -1.) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
291 |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
292 self.assertEqual(split_zeros(complex(1., 0.).imag), split_zeros(0.)) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
293 self.assertEqual(split_zeros(complex(1., -0.).imag), split_zeros(-0.)) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
294 self.assertEqual(split_zeros(complex(0., 1.).real), split_zeros(0.)) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
295 self.assertEqual(split_zeros(complex(-0., 1.).real), split_zeros(-0.)) |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
296 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
297 c = 3.14 + 1j |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
298 self.assertTrue(complex(c) is c) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
299 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
|
300 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
301 self.assertRaises(TypeError, complex, "1", "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
302 self.assertRaises(TypeError, complex, 1, "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
303 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
304 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
305 self.assertEqual(complex(unicode(" 3.14+J ")), 3.14+1j) |
|
21594
18a70a82af15
SF Patch #497487 add test to compare conjugate of a complex number
Neal Norwitz <nnorwitz@gmail.com>
parents:
21593
diff
changeset
|
306 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
307 # 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
|
308 # Fixed in 2.3. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
309 self.assertRaises(ValueError, complex, '1+1j\0j') |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
310 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
311 self.assertRaises(TypeError, int, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
312 self.assertRaises(TypeError, long, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
313 self.assertRaises(TypeError, float, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
314 self.assertRaises(ValueError, complex, "") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
315 self.assertRaises(TypeError, complex, None) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
316 self.assertRaises(ValueError, complex, "\0") |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
317 self.assertRaises(ValueError, complex, "3\09") |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
318 self.assertRaises(TypeError, complex, "1", "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
319 self.assertRaises(TypeError, complex, "1", 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
320 self.assertRaises(TypeError, complex, 1, "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
321 self.assertRaises(ValueError, complex, "1+") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
322 self.assertRaises(ValueError, complex, "1+1j+1j") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
323 self.assertRaises(ValueError, complex, "--") |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
324 self.assertRaises(ValueError, complex, "(1+2j") |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
325 self.assertRaises(ValueError, complex, "1+2j)") |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
326 self.assertRaises(ValueError, complex, "1+(2j)") |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
327 self.assertRaises(ValueError, complex, "(1+2j)123") |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
328 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
329 self.assertRaises(ValueError, complex, unicode("x")) |
| 53338 | 330 self.assertRaises(ValueError, complex, "1j+2") |
| 331 self.assertRaises(ValueError, complex, "1e1ej") | |
| 332 self.assertRaises(ValueError, complex, "1e++1ej") | |
| 333 self.assertRaises(ValueError, complex, ")1+2j(") | |
| 334 # the following three are accepted by Python 2.6 | |
| 335 self.assertRaises(ValueError, complex, "1..1j") | |
| 336 self.assertRaises(ValueError, complex, "1.11.1j") | |
| 337 self.assertRaises(ValueError, complex, "1e1.1j") | |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
338 |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
339 if test_support.have_unicode: |
|
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
340 # check that complex accepts long unicode strings |
|
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
341 self.assertEqual(type(complex(unicode("1"*500))), complex) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
342 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
343 class EvilExc(Exception): |
|
29668
36a17904a3ed
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29532
diff
changeset
|
344 pass |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
345 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
346 class evilcomplex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
347 def __complex__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
348 raise EvilExc |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
349 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
350 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
|
351 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
352 class float2: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
353 def __init__(self, value): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
354 self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
355 def __float__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
356 return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
357 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
358 self.assertAlmostEqual(complex(float2(42.)), 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
359 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
|
360 self.assertRaises(TypeError, complex, float2(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
361 |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
362 class complex0(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
363 """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
|
364 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
365 return 42j |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
366 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
367 class complex1(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
368 """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
|
369 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
|
370 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
|
371 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
372 return self |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
373 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
374 class complex2(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
375 """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
|
376 complex is returned""" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
377 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
378 return None |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
379 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
380 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
|
381 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
|
382 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
|
383 |
|
58978
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
384 def test_subclass(self): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
385 class xcomplex(complex): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
386 def __add__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
387 return xcomplex(complex(self) + other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
388 __radd__ = __add__ |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
389 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
390 def __sub__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
391 return xcomplex(complex(self) + other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
392 __rsub__ = __sub__ |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
393 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
394 def __mul__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
395 return xcomplex(complex(self) * other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
396 __rmul__ = __mul__ |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
397 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
398 def __div__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
399 return xcomplex(complex(self) / other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
400 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
401 def __rdiv__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
402 return xcomplex(other / complex(self)) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
403 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
404 __truediv__ = __div__ |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
405 __rtruediv__ = __rdiv__ |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
406 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
407 def __floordiv__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
408 return xcomplex(complex(self) // other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
409 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
410 def __rfloordiv__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
411 return xcomplex(other // complex(self)) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
412 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
413 def __pow__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
414 return xcomplex(complex(self) ** other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
415 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
416 def __rpow__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
417 return xcomplex(other ** complex(self) ) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
418 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
419 def __mod__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
420 return xcomplex(complex(self) % other) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
421 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
422 def __rmod__(self,other): |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
423 return xcomplex(other % complex(self)) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
424 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
425 infix_binops = ('+', '-', '*', '**', '%', '//', '/') |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
426 xcomplex_values = (xcomplex(1), xcomplex(123.0), |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
427 xcomplex(-10+2j), xcomplex(3+187j), |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
428 xcomplex(3-78j)) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
429 test_values = (1, 123.0, 10-19j, xcomplex(1+2j), |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
430 xcomplex(1+87j), xcomplex(10+90j)) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
431 |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
432 for op in infix_binops: |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
433 for x in xcomplex_values: |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
434 for y in test_values: |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
435 a = 'x %s y' % op |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
436 b = 'y %s x' % op |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
437 self.assertTrue(type(eval(a)) is type(eval(b)) is xcomplex) |
|
c8ea75598fd6
Issue #5211: Fix complex type to avoid implicit calls to
Mark Dickinson <dickinsm@gmail.com>
parents:
58965
diff
changeset
|
438 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
439 def test_hash(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
440 for x in xrange(-30, 30): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
441 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
|
442 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
|
443 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
|
444 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
445 def test_abs(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
446 nums = [complex(x/3., y/7.) for x in xrange(-9,9) for y in xrange(-9,9)] |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
447 for num in nums: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
448 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
|
449 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
450 def test_repr(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
451 self.assertEqual(repr(1+6j), '(1+6j)') |
|
33233
77d3642eb87b
Fix repr for negative imaginary part. Fixes #1013908.
Martin v. Löwis <martin@v.loewis.de>
parents:
30071
diff
changeset
|
452 self.assertEqual(repr(1-6j), '(1-6j)') |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
453 |
|
35586
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
454 self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)') |
|
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
455 |
|
41093
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
456 self.assertEqual(1-6j,complex(repr(1-6j))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
457 self.assertEqual(1+6j,complex(repr(1+6j))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
458 self.assertEqual(-6j,complex(repr(-6j))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
459 self.assertEqual(6j,complex(repr(6j))) |
|
47ac1bfea304
Patch #1491866: change the complex() constructor to allow parthensized forms. This means complex(repr(x)) now works instead of raising a ValueError.
Collin Winter <collinw@gmail.com>
parents:
35586
diff
changeset
|
460 |
| 53338 | 461 self.assertEqual(repr(complex(1., INF)), "(1+infj)") |
| 462 self.assertEqual(repr(complex(1., -INF)), "(1-infj)") | |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
463 self.assertEqual(repr(complex(INF, 1)), "(inf+1j)") |
| 53338 | 464 self.assertEqual(repr(complex(-INF, INF)), "(-inf+infj)") |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
465 self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)") |
| 53338 | 466 self.assertEqual(repr(complex(1, NAN)), "(1+nanj)") |
| 467 self.assertEqual(repr(complex(NAN, NAN)), "(nan+nanj)") | |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
468 |
| 53338 | 469 self.assertEqual(repr(complex(0, INF)), "infj") |
| 470 self.assertEqual(repr(complex(0, -INF)), "-infj") | |
| 471 self.assertEqual(repr(complex(0, NAN)), "nanj") | |
|
45357
a235673b9f8e
Fixed repr() and str() of complex numbers. Complex suffered from the same problem as floats but I forgot to test and fix them.
Christian Heimes <christian@cheimes.de>
parents:
45076
diff
changeset
|
472 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
473 def test_neg(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
474 self.assertEqual(-(1+6j), -1-6j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
475 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
476 def test_file(self): |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
477 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
478 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
479 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
480 fo = None |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
481 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
482 fo = open(test_support.TESTFN, "wb") |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
483 print >>fo, a, b |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
484 fo.close() |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
485 fo = open(test_support.TESTFN, "rb") |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
486 self.assertEqual(fo.read(), "%s %s\n" % (a, b)) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
487 finally: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
488 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
|
489 fo.close() |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53396
diff
changeset
|
490 test_support.unlink(test_support.TESTFN) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
491 |
|
47471
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
492 def test_getnewargs(self): |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
493 self.assertEqual((1+2j).__getnewargs__(), (1.0, 2.0)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
494 self.assertEqual((1-2j).__getnewargs__(), (1.0, -2.0)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
495 self.assertEqual((2j).__getnewargs__(), (0.0, 2.0)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
496 self.assertEqual((-0j).__getnewargs__(), (0.0, -0.0)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
497 self.assertEqual(complex(0, INF).__getnewargs__(), (0.0, INF)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
498 self.assertEqual(complex(INF, 0).__getnewargs__(), (INF, 0.0)) |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
499 |
|
45076
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
500 if float.__getformat__("double").startswith("IEEE"): |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
501 def test_plus_minus_0j(self): |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
502 # test that -0j and 0j literals are not identified |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
503 z1, z2 = 0j, -0j |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63418
diff
changeset
|
504 self.assertEqual(atan2(z1.imag, -1.), atan2(0., -1.)) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
63418
diff
changeset
|
505 self.assertEqual(atan2(z2.imag, -1.), atan2(-0., -1.)) |
|
45076
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
506 |
| 53338 | 507 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
| 508 "test requires IEEE 754 doubles") | |
|
54227
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
509 def test_overflow(self): |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
510 self.assertEqual(complex("1e500"), complex(INF, 0.0)) |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
511 self.assertEqual(complex("-1e500j"), complex(0.0, -INF)) |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
512 self.assertEqual(complex("-1e500+1.8e308j"), complex(-INF, INF)) |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
513 |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
514 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
515 "test requires IEEE 754 doubles") |
| 53338 | 516 def test_repr_roundtrip(self): |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
517 vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN] |
| 53338 | 518 vals += [-v for v in vals] |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
519 |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
520 # complex(repr(z)) should recover z exactly, even for complex |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
521 # numbers involving an infinity, nan, or negative zero |
| 53338 | 522 for x in vals: |
| 523 for y in vals: | |
| 524 z = complex(x, y) | |
| 525 roundtrip = complex(repr(z)) | |
| 526 self.assertFloatsAreIdentical(z.real, roundtrip.real) | |
| 527 self.assertFloatsAreIdentical(z.imag, roundtrip.imag) | |
| 528 | |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
529 # if we predefine some constants, then eval(repr(z)) should |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
530 # also work, except that it might change the sign of zeros |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
531 inf, nan = float('inf'), float('nan') |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
532 infj, nanj = complex(0.0, inf), complex(0.0, nan) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
533 for x in vals: |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
534 for y in vals: |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
535 z = complex(x, y) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
536 roundtrip = eval(repr(z)) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
537 # adding 0.0 has no effect beside changing -0.0 to 0.0 |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
538 self.assertFloatsAreIdentical(0.0 + z.real, |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
539 0.0 + roundtrip.real) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
540 self.assertFloatsAreIdentical(0.0 + z.imag, |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
541 0.0 + roundtrip.imag) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
542 |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
543 def test_format(self): |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
544 # empty format string is same as str() |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
545 self.assertEqual(format(1+3j, ''), str(1+3j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
546 self.assertEqual(format(1.5+3.5j, ''), str(1.5+3.5j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
547 self.assertEqual(format(3j, ''), str(3j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
548 self.assertEqual(format(3.2j, ''), str(3.2j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
549 self.assertEqual(format(3+0j, ''), str(3+0j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
550 self.assertEqual(format(3.2+0j, ''), str(3.2+0j)) |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
551 |
|
53802
1d8f24d40c2c
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:
53629
diff
changeset
|
552 # empty presentation type should still be analogous to str, |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
553 # even when format string is nonempty (issue #5920). |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
554 self.assertEqual(format(3.2+0j, '-'), str(3.2+0j)) |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
555 self.assertEqual(format(3.2+0j, '<'), str(3.2+0j)) |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
556 z = 4/7. - 100j/7. |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
557 self.assertEqual(format(z, ''), str(z)) |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
558 self.assertEqual(format(z, '-'), str(z)) |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
559 self.assertEqual(format(z, '<'), str(z)) |
|
1d8f24d40c2c
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:
53629
diff
changeset
|
560 self.assertEqual(format(z, '10'), str(z)) |
|
63418
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
561 z = complex(0.0, 3.0) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
562 self.assertEqual(format(z, ''), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
563 self.assertEqual(format(z, '-'), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
564 self.assertEqual(format(z, '<'), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
565 self.assertEqual(format(z, '2'), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
566 z = complex(-0.0, 2.0) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
567 self.assertEqual(format(z, ''), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
568 self.assertEqual(format(z, '-'), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
569 self.assertEqual(format(z, '<'), str(z)) |
|
51aad509c20f
Merged revisions 83400 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
61991
diff
changeset
|
570 self.assertEqual(format(z, '3'), str(z)) |
|
53802
1d8f24d40c2c
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:
53629
diff
changeset
|
571 |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
572 self.assertEqual(format(1+3j, 'g'), '1+3j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
573 self.assertEqual(format(3j, 'g'), '0+3j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
574 self.assertEqual(format(1.5+3.5j, 'g'), '1.5+3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
575 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
576 self.assertEqual(format(1.5+3.5j, '+g'), '+1.5+3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
577 self.assertEqual(format(1.5-3.5j, '+g'), '+1.5-3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
578 self.assertEqual(format(1.5-3.5j, '-g'), '1.5-3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
579 self.assertEqual(format(1.5+3.5j, ' g'), ' 1.5+3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
580 self.assertEqual(format(1.5-3.5j, ' g'), ' 1.5-3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
581 self.assertEqual(format(-1.5+3.5j, ' g'), '-1.5+3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
582 self.assertEqual(format(-1.5-3.5j, ' g'), '-1.5-3.5j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
583 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
584 self.assertEqual(format(-1.5-3.5e-20j, 'g'), '-1.5-3.5e-20j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
585 self.assertEqual(format(-1.5-3.5j, 'f'), '-1.500000-3.500000j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
586 self.assertEqual(format(-1.5-3.5j, 'F'), '-1.500000-3.500000j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
587 self.assertEqual(format(-1.5-3.5j, 'e'), '-1.500000e+00-3.500000e+00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
588 self.assertEqual(format(-1.5-3.5j, '.2e'), '-1.50e+00-3.50e+00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
589 self.assertEqual(format(-1.5-3.5j, '.2E'), '-1.50E+00-3.50E+00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
590 self.assertEqual(format(-1.5e10-3.5e5j, '.2G'), '-1.5E+10-3.5E+05j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
591 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
592 self.assertEqual(format(1.5+3j, '<20g'), '1.5+3j ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
593 self.assertEqual(format(1.5+3j, '*<20g'), '1.5+3j**************') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
594 self.assertEqual(format(1.5+3j, '>20g'), ' 1.5+3j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
595 self.assertEqual(format(1.5+3j, '^20g'), ' 1.5+3j ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
596 self.assertEqual(format(1.5+3j, '<20'), '(1.5+3j) ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
597 self.assertEqual(format(1.5+3j, '>20'), ' (1.5+3j)') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
598 self.assertEqual(format(1.5+3j, '^20'), ' (1.5+3j) ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
599 self.assertEqual(format(1.123-3.123j, '^20.2'), ' (1.1-3.1j) ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
600 |
|
59023
ff84e0893cb1
Issue #7988: Fix default alignment to be right aligned for complex.__format__. Now it matches other numeric types.
Eric Smith <eric@trueblade.com>
parents:
58978
diff
changeset
|
601 self.assertEqual(format(1.5+3j, '20.2f'), ' 1.50+3.00j') |
|
ff84e0893cb1
Issue #7988: Fix default alignment to be right aligned for complex.__format__. Now it matches other numeric types.
Eric Smith <eric@trueblade.com>
parents:
58978
diff
changeset
|
602 self.assertEqual(format(1.5+3j, '>20.2f'), ' 1.50+3.00j') |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
603 self.assertEqual(format(1.5+3j, '<20.2f'), '1.50+3.00j ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
604 self.assertEqual(format(1.5e20+3j, '<20.2f'), '150000000000000000000.00+3.00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
605 self.assertEqual(format(1.5e20+3j, '>40.2f'), ' 150000000000000000000.00+3.00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
606 self.assertEqual(format(1.5e20+3j, '^40,.2f'), ' 150,000,000,000,000,000,000.00+3.00j ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
607 self.assertEqual(format(1.5e21+3j, '^40,.2f'), ' 1,500,000,000,000,000,000,000.00+3.00j ') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
608 self.assertEqual(format(1.5e21+3000j, ',.2f'), '1,500,000,000,000,000,000,000.00+3,000.00j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
609 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
610 # alternate is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
611 self.assertRaises(ValueError, (1.5+0.5j).__format__, '#f') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
612 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
613 # zero padding is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
614 self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
615 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
616 # '=' alignment is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
617 self.assertRaises(ValueError, (1.5+3j).__format__, '=20') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
618 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
619 # integer presentation types are an error |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
620 for t in 'bcdoxX': |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
621 self.assertRaises(ValueError, (1.5+0.5j).__format__, t) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
622 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
623 # make sure everything works in ''.format() |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
624 self.assertEqual('*{0:.3f}*'.format(3.14159+2.71828j), '*3.142+2.718j*') |
| 53338 | 625 |
|
57503
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
626 # issue 3382: 'f' and 'F' with inf's and nan's |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
627 self.assertEqual('{0:f}'.format(INF+0j), 'inf+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
628 self.assertEqual('{0:F}'.format(INF+0j), 'INF+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
629 self.assertEqual('{0:f}'.format(-INF+0j), '-inf+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
630 self.assertEqual('{0:F}'.format(-INF+0j), '-INF+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
631 self.assertEqual('{0:f}'.format(complex(INF, INF)), 'inf+infj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
632 self.assertEqual('{0:F}'.format(complex(INF, INF)), 'INF+INFj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
633 self.assertEqual('{0:f}'.format(complex(INF, -INF)), 'inf-infj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
634 self.assertEqual('{0:F}'.format(complex(INF, -INF)), 'INF-INFj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
635 self.assertEqual('{0:f}'.format(complex(-INF, INF)), '-inf+infj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
636 self.assertEqual('{0:F}'.format(complex(-INF, INF)), '-INF+INFj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
637 self.assertEqual('{0:f}'.format(complex(-INF, -INF)), '-inf-infj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
638 self.assertEqual('{0:F}'.format(complex(-INF, -INF)), '-INF-INFj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
639 |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
640 self.assertEqual('{0:f}'.format(complex(NAN, 0)), 'nan+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
641 self.assertEqual('{0:F}'.format(complex(NAN, 0)), 'NAN+0.000000j') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
642 self.assertEqual('{0:f}'.format(complex(NAN, NAN)), 'nan+nanj') |
|
930aeded900d
Issue #3382: Make '%F' and float.__format__('F') convert results to upper case. Much of the patch came from Mark Dickinson.
Eric Smith <eric@trueblade.com>
parents:
56771
diff
changeset
|
643 self.assertEqual('{0:F}'.format(complex(NAN, NAN)), 'NAN+NANj') |
|
45076
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
644 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
645 def test_main(): |
|
63634
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53396
diff
changeset
|
646 with test_support.check_warnings(("complex divmod.., // and % are " |
|
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53396
diff
changeset
|
647 "deprecated", DeprecationWarning)): |
|
762d89faceaa
Merged revisions 79539 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
53396
diff
changeset
|
648 test_support.run_unittest(ComplexTest) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
649 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
650 if __name__ == "__main__": |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
651 test_main() |
