Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_complex.py @ 58765:2ba7677bfa95 legacy-trunk
testCopysign was defined twice in test_math; combine the definitions
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Sat, 06 Feb 2010 23:11:25 +0000 |
| parents | 930aeded900d |
| children | 3a7132f65623 |
| rev | line source |
|---|---|
|
30071
735ec0869492
Remove useless import.
Walter Dörwald <walter@livinglogic.de>
parents:
30070
diff
changeset
|
1 import unittest, os |
|
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 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
4 import warnings |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
5 warnings.filterwarnings( |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
6 "ignore", |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
7 category=DeprecationWarning, |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
8 message=".*complex divmod.*are deprecated" |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
9 ) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
10 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
11 from random import random |
| 53338 | 12 from math import atan2, isnan, copysign |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
13 |
|
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
|
14 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
|
15 NAN = float("nan") |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
16 # 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
|
17 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
18 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
|
19 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
20 def assertAlmostEqual(self, a, b): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
21 if isinstance(a, complex): |
|
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.real, b.real) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
24 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
|
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.real, b) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
27 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
28 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
29 if isinstance(b, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
30 unittest.TestCase.assertAlmostEqual(self, a, b.real) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
31 unittest.TestCase.assertAlmostEqual(self, 0., b.imag) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
32 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
33 unittest.TestCase.assertAlmostEqual(self, a, b) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
35 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
|
36 """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
|
37 # put the one with larger magnitude second |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
38 if abs(x) > abs(y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
39 x, y = y, x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
40 if y == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
41 return abs(x) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
42 if x == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
43 return abs(y) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
44 # check that relative difference < eps |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
45 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
|
46 |
| 53338 | 47 def assertFloatsAreIdentical(self, x, y): |
| 48 """assert that floats x and y are identical, in the sense that: | |
| 49 (1) both x and y are nans, or | |
| 50 (2) both x and y are infinities, with the same sign, or | |
| 51 (3) both x and y are zeros, with the same sign, or | |
| 52 (4) x and y are both finite and nonzero, and x == y | |
| 53 | |
| 54 """ | |
| 55 msg = 'floats {!r} and {!r} are not identical' | |
| 56 | |
| 57 if isnan(x) or isnan(y): | |
| 58 if isnan(x) and isnan(y): | |
| 59 return | |
| 60 elif x == y: | |
| 61 if x != 0.0: | |
| 62 return | |
| 63 # both zero; check that signs match | |
| 64 elif copysign(1.0, x) == copysign(1.0, y): | |
| 65 return | |
| 66 else: | |
| 67 msg += ': zeros have different signs' | |
| 68 self.fail(msg.format(x, y)) | |
| 69 | |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
70 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
|
71 """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
|
72 self.assertCloseAbs(x.real, y.real, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
73 self.assertCloseAbs(x.imag, y.imag, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
74 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
75 def assertIs(self, a, b): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
76 self.assertTrue(a is b) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
77 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
78 def check_div(self, x, y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
79 """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
|
80 z = x * y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
81 if x != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
82 q = z / x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
83 self.assertClose(q, y) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
84 q = z.__div__(x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
85 self.assertClose(q, y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
86 q = z.__truediv__(x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
87 self.assertClose(q, y) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
88 if y != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
89 q = z / y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
90 self.assertClose(q, x) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
91 q = z.__div__(y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
92 self.assertClose(q, x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
93 q = z.__truediv__(y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
94 self.assertClose(q, x) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
95 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
96 def test_div(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
97 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
|
98 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
|
99 for x in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
100 for y in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
101 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
|
102 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
103 # 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
|
104 # nonsense errors for these (overflows and underflows). |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
105 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
|
106 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
|
107 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
108 # Just for fun. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
109 for i in xrange(100): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
110 self.check_div(complex(random(), random()), |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
111 complex(random(), random())) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
112 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
113 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
|
114 # FIXME: The following currently crashes on Alpha |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
115 # 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
|
116 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
117 def test_truediv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
118 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
|
119 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
|
120 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
121 def test_floordiv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
122 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
|
123 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
|
124 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
125 def test_coerce(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
126 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
|
127 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
128 def test_richcompare(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
129 self.assertRaises(OverflowError, complex.__eq__, 1+1j, 1L<<10000) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
130 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
|
131 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
|
132 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
|
133 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
|
134 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
|
135 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
|
136 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
|
137 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
|
138 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
|
139 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
140 def test_mod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
141 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
|
142 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
143 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
144 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
145 a % 0 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
146 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
147 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
148 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
149 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
|
150 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
151 def test_divmod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
152 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
|
153 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
154 def test_pow(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
155 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
|
156 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
|
157 self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
158 self.assertAlmostEqual(pow(1j, -1), 1/1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
159 self.assertAlmostEqual(pow(1j, 200), 1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
160 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
|
161 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
162 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
163 self.assertEqual(a ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
164 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
|
165 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
166 self.assertEqual(3j ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
167 self.assertEqual(3j ** 0, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
168 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
169 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
170 0j ** a |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
171 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
172 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
173 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
174 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
|
175 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
176 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
177 0j ** (3-2j) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
178 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
179 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
180 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
181 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
|
182 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
183 # 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
|
184 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
|
185 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
|
186 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
|
187 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
188 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
|
189 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
190 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
191 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
|
192 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
193 def test_boolcontext(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
194 for i in xrange(100): |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
195 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
|
196 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
|
197 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
198 def test_conjugate(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
199 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
|
200 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
201 def test_constructor(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
202 class OS: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
203 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
204 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
205 class NS(object): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
206 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
207 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
208 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
|
209 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
|
210 self.assertRaises(TypeError, complex, OS(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
211 self.assertRaises(TypeError, complex, NS(None)) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
212 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
213 self.assertAlmostEqual(complex("1+10j"), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
214 self.assertAlmostEqual(complex(10), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
215 self.assertAlmostEqual(complex(10.0), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
216 self.assertAlmostEqual(complex(10L), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
217 self.assertAlmostEqual(complex(10+0j), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
218 self.assertAlmostEqual(complex(1,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
219 self.assertAlmostEqual(complex(1,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
220 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
|
221 self.assertAlmostEqual(complex(1L,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
222 self.assertAlmostEqual(complex(1L,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 self.assertAlmostEqual(complex(314), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
230 self.assertAlmostEqual(complex(314L), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
231 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
|
232 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
|
233 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
|
234 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
|
235 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
|
236 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
|
237 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
|
238 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
|
239 self.assertAlmostEqual(complex("1"), 1+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
240 self.assertAlmostEqual(complex("1j"), 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
241 self.assertAlmostEqual(complex(), 0) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
242 self.assertAlmostEqual(complex("-1"), -1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
243 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
|
244 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
|
245 self.assertAlmostEqual(complex("(1.3+2.2j)"), 1.3+2.2j) |
| 53338 | 246 self.assertAlmostEqual(complex("3.14+1J"), 3.14+1j) |
| 247 self.assertAlmostEqual(complex(" ( +3.14-6J )"), 3.14-6j) | |
| 248 self.assertAlmostEqual(complex(" ( +3.14-J )"), 3.14-1j) | |
| 249 self.assertAlmostEqual(complex(" ( +3.14+j )"), 3.14+1j) | |
| 250 self.assertAlmostEqual(complex("J"), 1j) | |
| 251 self.assertAlmostEqual(complex("( j )"), 1j) | |
| 252 self.assertAlmostEqual(complex("+J"), 1j) | |
| 253 self.assertAlmostEqual(complex("( -j)"), -1j) | |
| 254 self.assertAlmostEqual(complex('1e-500'), 0.0 + 0.0j) | |
| 255 self.assertAlmostEqual(complex('-1e-500j'), 0.0 - 0.0j) | |
| 256 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
|
257 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
258 class complex2(complex): pass |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
259 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
|
260 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
|
261 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
|
262 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
|
263 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
|
264 |
|
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
|
265 # 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
|
266 # 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
|
267 # 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
|
268 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
|
269 """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
|
270 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
|
271 |
|
60e6eaa378d9
Patch # 1507 by Mark Dickinson. Make complex(x, -0) retain the sign of
Guido van Rossum <guido@python.org>
parents:
41093
diff
changeset
|
272 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
|
273 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
|
274 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
|
275 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
|
276 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
277 c = 3.14 + 1j |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
54227
diff
changeset
|
278 self.assertTrue(complex(c) is c) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
279 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
|
280 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
281 self.assertRaises(TypeError, complex, "1", "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
282 self.assertRaises(TypeError, complex, 1, "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
283 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
284 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
285 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
|
286 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
287 # 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
|
288 # Fixed in 2.3. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
289 self.assertRaises(ValueError, complex, '1+1j\0j') |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
290 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
291 self.assertRaises(TypeError, int, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
292 self.assertRaises(TypeError, long, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
293 self.assertRaises(TypeError, float, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
294 self.assertRaises(ValueError, complex, "") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
295 self.assertRaises(TypeError, complex, None) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
296 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
|
297 self.assertRaises(ValueError, complex, "3\09") |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
298 self.assertRaises(TypeError, complex, "1", "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
299 self.assertRaises(TypeError, complex, "1", 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
300 self.assertRaises(TypeError, complex, 1, "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
301 self.assertRaises(ValueError, complex, "1+") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
302 self.assertRaises(ValueError, complex, "1+1j+1j") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
303 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
|
304 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
|
305 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
|
306 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
|
307 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
|
308 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
309 self.assertRaises(ValueError, complex, unicode("x")) |
| 53338 | 310 self.assertRaises(ValueError, complex, "1j+2") |
| 311 self.assertRaises(ValueError, complex, "1e1ej") | |
| 312 self.assertRaises(ValueError, complex, "1e++1ej") | |
| 313 self.assertRaises(ValueError, complex, ")1+2j(") | |
| 314 # the following three are accepted by Python 2.6 | |
| 315 self.assertRaises(ValueError, complex, "1..1j") | |
| 316 self.assertRaises(ValueError, complex, "1.11.1j") | |
| 317 self.assertRaises(ValueError, complex, "1e1.1j") | |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
318 |
|
56771
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
319 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
|
320 # 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
|
321 self.assertEqual(type(complex(unicode("1"*500))), complex) |
|
0b1a088012c7
Issue #7117: Use PyOS_string_to_double instead of PyOS_ascii_strtod in
Mark Dickinson <dickinsm@gmail.com>
parents:
55002
diff
changeset
|
322 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
323 class EvilExc(Exception): |
|
29668
36a17904a3ed
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29532
diff
changeset
|
324 pass |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
325 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
326 class evilcomplex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
327 def __complex__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
328 raise EvilExc |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
329 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
330 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
|
331 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
332 class float2: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
333 def __init__(self, value): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
334 self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
335 def __float__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
336 return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
337 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
338 self.assertAlmostEqual(complex(float2(42.)), 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
339 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
|
340 self.assertRaises(TypeError, complex, float2(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
341 |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
342 class complex0(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
343 """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
|
344 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
345 return 42j |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
346 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
347 class complex1(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
348 """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
|
349 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
|
350 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
|
351 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
352 return self |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
353 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
354 class complex2(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
355 """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
|
356 complex is returned""" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
357 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
358 return None |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
359 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
360 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
|
361 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
|
362 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
|
363 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
364 def test_hash(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
365 for x in xrange(-30, 30): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
366 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
|
367 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
|
368 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
|
369 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
370 def test_abs(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
371 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
|
372 for num in nums: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
373 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
|
374 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
375 def test_repr(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
376 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
|
377 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
|
378 |
|
35586
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
379 self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)') |
|
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
380 |
|
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
|
381 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
|
382 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
|
383 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
|
384 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
|
385 |
| 53338 | 386 self.assertEqual(repr(complex(1., INF)), "(1+infj)") |
| 387 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
|
388 self.assertEqual(repr(complex(INF, 1)), "(inf+1j)") |
| 53338 | 389 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
|
390 self.assertEqual(repr(complex(NAN, 1)), "(nan+1j)") |
| 53338 | 391 self.assertEqual(repr(complex(1, NAN)), "(1+nanj)") |
| 392 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
|
393 |
| 53338 | 394 self.assertEqual(repr(complex(0, INF)), "infj") |
| 395 self.assertEqual(repr(complex(0, -INF)), "-infj") | |
| 396 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
|
397 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
398 def test_neg(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
399 self.assertEqual(-(1+6j), -1-6j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
400 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
401 def test_file(self): |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
402 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
403 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
404 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
405 fo = None |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
406 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
407 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
|
408 print >>fo, a, b |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
409 fo.close() |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
410 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
|
411 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
|
412 finally: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
413 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
|
414 fo.close() |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
415 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
416 os.remove(test_support.TESTFN) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
417 except (OSError, IOError): |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
418 pass |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
419 |
|
47471
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
420 def test_getnewargs(self): |
|
71bed208fe50
Fixed complex.__getnewargs__() to not emit another complex object.
Alexandre Vassalotti <alexandre@peadrop.com>
parents:
45357
diff
changeset
|
421 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
|
422 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
|
423 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
|
424 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
|
425 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
|
426 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
|
427 |
|
45076
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
428 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
|
429 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
|
430 # 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
|
431 z1, z2 = 0j, -0j |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
432 self.assertEquals(atan2(z1.imag, -1.), atan2(0., -1.)) |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
433 self.assertEquals(atan2(z2.imag, -1.), atan2(-0., -1.)) |
|
3f16ac915214
Issue #1678380. Fix a bug that identifies 0j and -0j when they appear
Mark Dickinson <dickinsm@gmail.com>
parents:
44037
diff
changeset
|
434 |
| 53338 | 435 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
| 436 "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
|
437 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
|
438 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
|
439 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
|
440 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
|
441 |
|
d2ccd89e8e6b
Issue #5829: don't raise OverflowError for complex('1e500'). Backport of r72803.
Mark Dickinson <dickinsm@gmail.com>
parents:
53802
diff
changeset
|
442 @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
|
443 "test requires IEEE 754 doubles") |
| 53338 | 444 def test_repr_roundtrip(self): |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
445 vals = [0.0, 1e-500, 1e-315, 1e-200, 0.0123, 3.1415, 1e50, INF, NAN] |
| 53338 | 446 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
|
447 |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
448 # 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
|
449 # numbers involving an infinity, nan, or negative zero |
| 53338 | 450 for x in vals: |
| 451 for y in vals: | |
| 452 z = complex(x, y) | |
| 453 roundtrip = complex(repr(z)) | |
| 454 self.assertFloatsAreIdentical(z.real, roundtrip.real) | |
| 455 self.assertFloatsAreIdentical(z.imag, roundtrip.imag) | |
| 456 | |
|
53376
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
457 # 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
|
458 # 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
|
459 inf, nan = float('inf'), float('nan') |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
460 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
|
461 for x in vals: |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
462 for y in vals: |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
463 z = complex(x, y) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
464 roundtrip = eval(repr(z)) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
465 # 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
|
466 self.assertFloatsAreIdentical(0.0 + z.real, |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
467 0.0 + roundtrip.real) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
468 self.assertFloatsAreIdentical(0.0 + z.imag, |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
469 0.0 + roundtrip.imag) |
|
5fd0da2a9778
Fix typo in complex parsing code; expand tests.
Mark Dickinson <dickinsm@gmail.com>
parents:
53338
diff
changeset
|
470 |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
471 def test_format(self): |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
472 # empty format string is same as str() |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
473 self.assertEqual(format(1+3j, ''), str(1+3j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
474 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
|
475 self.assertEqual(format(3j, ''), str(3j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
476 self.assertEqual(format(3.2j, ''), str(3.2j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
477 self.assertEqual(format(3+0j, ''), str(3+0j)) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
478 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
|
479 |
|
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
|
480 # 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
|
481 # 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
|
482 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
|
483 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
|
484 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
|
485 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
|
486 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
|
487 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
|
488 self.assertEqual(format(z, '10'), 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
|
489 |
|
53629
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
490 self.assertEqual(format(1+3j, 'g'), '1+3j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
491 self.assertEqual(format(3j, 'g'), '0+3j') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
492 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
|
493 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
494 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
|
495 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
|
496 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
|
497 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
|
498 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
|
499 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
|
500 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
|
501 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
502 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
|
503 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
|
504 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
|
505 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
|
506 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
|
507 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
|
508 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
|
509 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
510 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
|
511 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
|
512 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
|
513 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
|
514 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
|
515 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
|
516 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
|
517 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
|
518 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
519 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
|
520 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
|
521 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
|
522 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
|
523 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
|
524 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
|
525 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
526 # alternate is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
527 self.assertRaises(ValueError, (1.5+0.5j).__format__, '#f') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
528 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
529 # zero padding is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
530 self.assertRaises(ValueError, (1.5+0.5j).__format__, '010f') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
531 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
532 # '=' alignment is invalid |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
533 self.assertRaises(ValueError, (1.5+3j).__format__, '=20') |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
534 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
535 # integer presentation types are an error |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
536 for t in 'bcdoxX': |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
537 self.assertRaises(ValueError, (1.5+0.5j).__format__, t) |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
538 |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
539 # make sure everything works in ''.format() |
|
34e7c1b14495
Issue #1588: Add complex.__format__.
Eric Smith <eric@trueblade.com>
parents:
53376
diff
changeset
|
540 self.assertEqual('*{0:.3f}*'.format(3.14159+2.71828j), '*3.142+2.718j*') |
| 53338 | 541 |
|
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
|
542 # 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
|
543 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
|
544 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
|
545 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
|
546 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
|
547 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
|
548 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
|
549 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
|
550 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
|
551 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
|
552 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
|
553 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
|
554 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
|
555 |
|
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
|
556 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
|
557 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
|
558 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
|
559 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
|
560 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
561 def test_main(): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
562 test_support.run_unittest(ComplexTest) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
563 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
564 if __name__ == "__main__": |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
565 test_main() |
