Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_complex.py @ 37850:ebe59cd18273
Use explicit relative import for an, ehm, relative import.
| author | Thomas Wouters <thomas@python.org> |
|---|---|
| date | Tue, 18 Apr 2006 21:41:36 +0000 |
| parents | 4bfe8afe8f65 |
| children | 40bbe669b089 |
| 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 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
12 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
13 # 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
|
14 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
15 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
|
16 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
17 def assertAlmostEqual(self, a, b): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
18 if isinstance(a, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
19 if isinstance(b, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
20 unittest.TestCase.assertAlmostEqual(self, a.real, b.real) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
21 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
|
22 else: |
|
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) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
24 unittest.TestCase.assertAlmostEqual(self, a.imag, 0.) |
|
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 if isinstance(b, complex): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
27 unittest.TestCase.assertAlmostEqual(self, a, b.real) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
28 unittest.TestCase.assertAlmostEqual(self, 0., b.imag) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
29 else: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
30 unittest.TestCase.assertAlmostEqual(self, a, b) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
31 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
32 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
|
33 """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
|
34 # put the one with larger magnitude second |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
35 if abs(x) > abs(y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
36 x, y = y, x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
37 if y == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
38 return abs(x) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
39 if x == 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
40 return abs(y) < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
41 # check that relative difference < eps |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
42 self.assert_(abs((x-y)/y) < eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
43 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
44 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
|
45 """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
|
46 self.assertCloseAbs(x.real, y.real, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
47 self.assertCloseAbs(x.imag, y.imag, eps) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
48 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
49 def assertIs(self, a, b): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
50 self.assert_(a is b) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
51 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
52 def check_div(self, x, y): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
53 """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
|
54 z = x * y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
55 if x != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
56 q = z / x |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
57 self.assertClose(q, y) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
58 q = z.__truediv__(x) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
59 self.assertClose(q, y) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
60 if y != 0: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
61 q = z / y |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
62 self.assertClose(q, x) |
|
30070
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
63 q = z.__truediv__(y) |
|
061d86fc11bb
Check both __div__ and __truediv__ in division tests.
Walter Dörwald <walter@livinglogic.de>
parents:
29867
diff
changeset
|
64 self.assertClose(q, x) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
65 |
|
37191
4bfe8afe8f65
Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents:
35586
diff
changeset
|
66 def test_truediv(self): |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
67 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
|
68 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
|
69 for x in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
70 for y in simple_complex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
71 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
|
72 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
73 # 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
|
74 # nonsense errors for these (overflows and underflows). |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
75 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
|
76 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
|
77 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
78 # Just for fun. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
79 for i in xrange(100): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
80 self.check_div(complex(random(), random()), |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
81 complex(random(), random())) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
82 |
|
37191
4bfe8afe8f65
Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents:
35586
diff
changeset
|
83 self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j) |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
84 # FIXME: The following currently crashes on Alpha |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
85 # 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
|
86 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
87 def test_truediv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
88 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
|
89 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
|
90 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
91 def test_floordiv(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
92 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
|
93 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
|
94 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
95 def test_coerce(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
96 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
|
97 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
98 def test_richcompare(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
99 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
|
100 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
|
101 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
|
102 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
|
103 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
|
104 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
|
105 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
|
106 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
|
107 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
|
108 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
|
109 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
110 def test_mod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
111 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
|
112 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
113 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
114 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
115 a % 0 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
116 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
117 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
118 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
119 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
|
120 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
121 def test_divmod(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
122 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
|
123 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
124 def test_pow(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
125 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
|
126 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
|
127 self.assertRaises(ZeroDivisionError, pow, 0+0j, 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
128 self.assertAlmostEqual(pow(1j, -1), 1/1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
129 self.assertAlmostEqual(pow(1j, 200), 1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
130 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
|
131 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
132 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
133 self.assertEqual(a ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
134 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
|
135 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
136 self.assertEqual(3j ** 0j, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
137 self.assertEqual(3j ** 0, 1) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
138 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
139 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
140 0j ** a |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
141 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
142 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
143 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
144 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
|
145 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
146 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
147 0j ** (3-2j) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
148 except ZeroDivisionError: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
149 pass |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
150 else: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
151 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
|
152 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
153 # 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
|
154 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
|
155 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
|
156 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
|
157 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
158 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
|
159 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
160 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
161 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
|
162 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
163 def test_boolcontext(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
164 for i in xrange(100): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
165 self.assert_(complex(random() + 1e-6, random() + 1e-6)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
166 self.assert_(not complex(0.0, 0.0)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
167 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
168 def test_conjugate(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
169 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
|
170 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
171 def test_constructor(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
172 class OS: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
173 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
174 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
175 class NS(object): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
176 def __init__(self, value): self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
177 def __complex__(self): return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
178 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
|
179 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
|
180 self.assertRaises(TypeError, complex, OS(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
181 self.assertRaises(TypeError, complex, NS(None)) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
182 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
183 self.assertAlmostEqual(complex("1+10j"), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
184 self.assertAlmostEqual(complex(10), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
185 self.assertAlmostEqual(complex(10.0), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
186 self.assertAlmostEqual(complex(10L), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
187 self.assertAlmostEqual(complex(10+0j), 10+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
188 self.assertAlmostEqual(complex(1,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
189 self.assertAlmostEqual(complex(1,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
190 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
|
191 self.assertAlmostEqual(complex(1L,10), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
192 self.assertAlmostEqual(complex(1L,10L), 1+10j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 self.assertAlmostEqual(complex(314), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
200 self.assertAlmostEqual(complex(314L), 314.0+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
201 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
|
202 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
|
203 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
|
204 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
|
205 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
|
206 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
|
207 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
|
208 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
|
209 self.assertAlmostEqual(complex("1"), 1+0j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
210 self.assertAlmostEqual(complex("1j"), 1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
211 self.assertAlmostEqual(complex(), 0) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
212 self.assertAlmostEqual(complex("-1"), -1) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
213 self.assertAlmostEqual(complex("+1"), +1) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
214 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
215 class complex2(complex): pass |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
216 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
|
217 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
|
218 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
|
219 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
|
220 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
|
221 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
222 c = 3.14 + 1j |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
223 self.assert_(complex(c) is c) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
224 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
|
225 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
226 self.assertRaises(TypeError, complex, "1", "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
227 self.assertRaises(TypeError, complex, 1, "1") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
228 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
229 self.assertEqual(complex(" 3.14+J "), 3.14+1j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
230 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
231 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
|
232 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
233 # 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
|
234 # Fixed in 2.3. |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
235 self.assertRaises(ValueError, complex, '1+1j\0j') |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
236 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
237 self.assertRaises(TypeError, int, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
238 self.assertRaises(TypeError, long, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
239 self.assertRaises(TypeError, float, 5+3j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
240 self.assertRaises(ValueError, complex, "") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
241 self.assertRaises(TypeError, complex, None) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
242 self.assertRaises(ValueError, complex, "\0") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
243 self.assertRaises(TypeError, complex, "1", "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
244 self.assertRaises(TypeError, complex, "1", 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
245 self.assertRaises(TypeError, complex, 1, "2") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
246 self.assertRaises(ValueError, complex, "1+") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
247 self.assertRaises(ValueError, complex, "1+1j+1j") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
248 self.assertRaises(ValueError, complex, "--") |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
249 if test_support.have_unicode: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
250 self.assertRaises(ValueError, complex, unicode("1"*500)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
251 self.assertRaises(ValueError, complex, unicode("x")) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
252 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
253 class EvilExc(Exception): |
|
29668
36a17904a3ed
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
29532
diff
changeset
|
254 pass |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
255 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
256 class evilcomplex: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
257 def __complex__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
258 raise EvilExc |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
259 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
260 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
|
261 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
262 class float2: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
263 def __init__(self, value): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
264 self.value = value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
265 def __float__(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
266 return self.value |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
267 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
268 self.assertAlmostEqual(complex(float2(42.)), 42) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
269 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
|
270 self.assertRaises(TypeError, complex, float2(None)) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
271 |
|
34931
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
272 class complex0(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
273 """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
|
274 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
275 return 42j |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
276 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
277 class complex1(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
278 """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
|
279 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
|
280 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
|
281 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
282 return self |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
283 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
284 class complex2(complex): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
285 """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
|
286 complex is returned""" |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
287 def __complex__(self): |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
288 return None |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
289 |
|
9a1b1e1ae653
Make subclasses of int, long, complex, float, and unicode perform type
Brett Cannon <bcannon@gmail.com>
parents:
33233
diff
changeset
|
290 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
|
291 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
|
292 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
|
293 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
294 def test_hash(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
295 for x in xrange(-30, 30): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
296 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
|
297 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
|
298 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
|
299 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
300 def test_abs(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
301 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
|
302 for num in nums: |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
303 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
|
304 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
305 def test_repr(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
306 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
|
307 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
|
308 |
|
35586
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
309 self.assertNotEqual(repr(-(1+0j)), '(-1+-0j)') |
|
e6119d577cd3
Test case for latest complexobject fix.
Georg Brandl <georg@python.org>
parents:
34931
diff
changeset
|
310 |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
311 def test_neg(self): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
312 self.assertEqual(-(1+6j), -1-6j) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
313 |
|
29867
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
314 def test_file(self): |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
315 a = 3.33+4.43j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
316 b = 5.1+2.3j |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
317 |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
318 fo = None |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
319 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
320 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
|
321 print >>fo, a, b |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
322 fo.close() |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
323 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
|
324 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
|
325 finally: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
326 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
|
327 fo.close() |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
328 try: |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
329 os.remove(test_support.TESTFN) |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
330 except (OSError, IOError): |
|
199cd78d6d78
Add various test cases from SF patch 543867.
Walter Dörwald <walter@livinglogic.de>
parents:
29668
diff
changeset
|
331 pass |
|
29532
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
332 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
333 def test_main(): |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
334 test_support.run_unittest(ComplexTest) |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
335 |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
336 if __name__ == "__main__": |
|
2fbd256a1db2
Port test_complex.py to unittest.
Walter Dörwald <walter@livinglogic.de>
parents:
24442
diff
changeset
|
337 test_main() |
