Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_complex.py @ 22360:c8a6ea00a9e1 legacy-trunk
restructure a bit to not rely on test case execution ordering
add test case for bug #482460
| author | Skip Montanaro <skip@pobox.com> |
|---|---|
| date | Sun, 17 Mar 2002 23:03:42 +0000 |
| parents | e56047db6538 |
| children | 226ba34bd998 |
| rev | line source |
|---|---|
|
21594
18a70a82af15
SF Patch #497487 add test to compare conjugate of a complex number
Neal Norwitz <nnorwitz@gmail.com>
parents:
21593
diff
changeset
|
1 from test_support import TestFailed, vereq |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
2 from random import random |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
3 |
|
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
|
4 # These tests ensure that complex math does the right thing; tests of |
|
6da6091a31ee
Add a comment explaining what these tests are for, and where to look for
Fred Drake <fdrake@acm.org>
parents:
19791
diff
changeset
|
5 # the complex() function/constructor are in test_b1.py. |
|
6da6091a31ee
Add a comment explaining what these tests are for, and where to look for
Fred Drake <fdrake@acm.org>
parents:
19791
diff
changeset
|
6 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
7 # XXX need many, many more tests here. |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
8 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
9 nerrors = 0 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
10 |
|
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
|
11 def check_close_real(x, y, eps=1e-9): |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
12 """Return true iff floats x and y "are close\"""" |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
13 # put the one with larger magnitude second |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
14 if abs(x) > abs(y): |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
15 x, y = y, x |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
16 if y == 0: |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
17 return abs(x) < eps |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
18 if x == 0: |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
19 return abs(y) < eps |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
20 # check that relative difference < eps |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
21 return abs((x-y)/y) < eps |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
22 |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
23 def check_close(x, y, eps=1e-9): |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
24 """Return true iff complexes x and y "are close\"""" |
|
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
|
25 return check_close_real(x.real, y.real, eps) and \ |
|
c97dda238b36
Reverting to rev 1.2. Apparently gcc doesn't use the extended-precision
Tim Peters <tim.peters@gmail.com>
parents:
19790
diff
changeset
|
26 check_close_real(x.imag, y.imag, eps) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
27 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
28 def test_div(x, y): |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
29 """Compute complex z=x*y, and check that z/x==y and z/y==x.""" |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
30 global nerrors |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
31 z = x * y |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
32 if x != 0: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
33 q = z / x |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
34 if not check_close(q, y): |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
35 nerrors += 1 |
|
18004
64e1ea34b553
This division test was too stringent in its accuracy expectations for
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
36 print "%r / %r == %r but expected %r" % (z, x, q, y) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
37 if y != 0: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
38 q = z / y |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
39 if not check_close(q, x): |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
40 nerrors += 1 |
|
18004
64e1ea34b553
This division test was too stringent in its accuracy expectations for
Tim Peters <tim.peters@gmail.com>
parents:
17066
diff
changeset
|
41 print "%r / %r == %r but expected %r" % (z, y, q, x) |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
42 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
43 simple_real = [float(i) for i in range(-5, 6)] |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
44 simple_complex = [complex(x, y) for x in simple_real for y in simple_real] |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
45 for x in simple_complex: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
46 for y in simple_complex: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
47 test_div(x, y) |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
48 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
49 # A naive complex division algorithm (such as in 2.0) is very prone to |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
50 # nonsense errors for these (overflows and underflows). |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
51 test_div(complex(1e200, 1e200), 1+0j) |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
52 test_div(complex(1e-200, 1e-200), 1+0j) |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
53 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
54 # Just for fun. |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
55 for i in range(100): |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
56 test_div(complex(random(), random()), |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
57 complex(random(), random())) |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
58 |
|
21593
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
59 for i in range(100): |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
60 if not complex(random() + 1e-6, random() + 1e-6): |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
61 raise TestFailed("complex(random(), random()) should be true") |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
62 |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
63 if complex(0.0, 0.0): |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
64 raise TestFailed("complex(0.0, 0.0) should be false") |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
65 |
|
21595
e56047db6538
test_support: add a docstring to vereq().
Tim Peters <tim.peters@gmail.com>
parents:
21594
diff
changeset
|
66 vereq(complex(5.3, 9.8).conjugate(), 5.3-9.8j) |
|
21594
18a70a82af15
SF Patch #497487 add test to compare conjugate of a complex number
Neal Norwitz <nnorwitz@gmail.com>
parents:
21593
diff
changeset
|
67 |
|
21593
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
68 try: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
69 print int(5+3j) |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
70 except TypeError: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
71 pass |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
72 else: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
73 raise TestFailed("int(complex()) didn't raise TypeError") |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
74 |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
75 try: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
76 print float(5+3j) |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
77 except TypeError: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
78 pass |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
79 else: |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
80 raise TestFailed("float(complex()) didn't raise TypeError") |
|
ff70e5634ae9
SF Patch #494873 add tests for complex numbers including calls to int()/long()
Neal Norwitz <nnorwitz@gmail.com>
parents:
21401
diff
changeset
|
81 |
|
17066
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
82 try: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
83 z = 1.0 / (0+0j) |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
84 except ZeroDivisionError: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
85 pass |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
86 else: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
87 nerrors += 1 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
88 raise TestFailed("Division by complex 0 didn't raise ZeroDivisionError") |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
89 |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
90 if nerrors: |
|
2c1be7924b04
SF bug [ #409448 ] Complex division is braindead
Tim Peters <tim.peters@gmail.com>
parents:
diff
changeset
|
91 raise TestFailed("%d tests failed" % nerrors) |
