Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame^] | 1 | from test.test_support import TestFailed, vereq |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 2 | from random import random |
| 3 | |
Fred Drake | 68773e7 | 2001-12-13 19:57:53 +0000 | [diff] [blame] | 4 | # These tests ensure that complex math does the right thing; tests of |
| 5 | # the complex() function/constructor are in test_b1.py. |
| 6 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 7 | # XXX need many, many more tests here. |
| 8 | |
| 9 | nerrors = 0 |
| 10 | |
Tim Peters | c5b235c | 2001-09-06 23:00:21 +0000 | [diff] [blame] | 11 | def check_close_real(x, y, eps=1e-9): |
| 12 | """Return true iff floats x and y "are close\"""" |
| 13 | # put the one with larger magnitude second |
| 14 | if abs(x) > abs(y): |
| 15 | x, y = y, x |
| 16 | if y == 0: |
| 17 | return abs(x) < eps |
| 18 | if x == 0: |
| 19 | return abs(y) < eps |
| 20 | # check that relative difference < eps |
| 21 | return abs((x-y)/y) < eps |
| 22 | |
| 23 | def check_close(x, y, eps=1e-9): |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 24 | """Return true iff complexes x and y "are close\"""" |
Tim Peters | c5b235c | 2001-09-06 23:00:21 +0000 | [diff] [blame] | 25 | return check_close_real(x.real, y.real, eps) and \ |
| 26 | check_close_real(x.imag, y.imag, eps) |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 27 | |
| 28 | def test_div(x, y): |
| 29 | """Compute complex z=x*y, and check that z/x==y and z/y==x.""" |
| 30 | global nerrors |
| 31 | z = x * y |
| 32 | if x != 0: |
| 33 | q = z / x |
| 34 | if not check_close(q, y): |
| 35 | nerrors += 1 |
Tim Peters | 0b76d3a | 2001-05-29 22:18:09 +0000 | [diff] [blame] | 36 | print "%r / %r == %r but expected %r" % (z, x, q, y) |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 37 | if y != 0: |
| 38 | q = z / y |
| 39 | if not check_close(q, x): |
| 40 | nerrors += 1 |
Tim Peters | 0b76d3a | 2001-05-29 22:18:09 +0000 | [diff] [blame] | 41 | print "%r / %r == %r but expected %r" % (z, y, q, x) |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 42 | |
| 43 | simple_real = [float(i) for i in range(-5, 6)] |
| 44 | simple_complex = [complex(x, y) for x in simple_real for y in simple_real] |
| 45 | for x in simple_complex: |
| 46 | for y in simple_complex: |
| 47 | test_div(x, y) |
| 48 | |
| 49 | # A naive complex division algorithm (such as in 2.0) is very prone to |
| 50 | # nonsense errors for these (overflows and underflows). |
| 51 | test_div(complex(1e200, 1e200), 1+0j) |
| 52 | test_div(complex(1e-200, 1e-200), 1+0j) |
| 53 | |
| 54 | # Just for fun. |
| 55 | for i in range(100): |
| 56 | test_div(complex(random(), random()), |
| 57 | complex(random(), random())) |
| 58 | |
Neal Norwitz | fc37af8 | 2001-12-29 01:02:21 +0000 | [diff] [blame] | 59 | for i in range(100): |
| 60 | if not complex(random() + 1e-6, random() + 1e-6): |
| 61 | raise TestFailed("complex(random(), random()) should be true") |
| 62 | |
| 63 | if complex(0.0, 0.0): |
| 64 | raise TestFailed("complex(0.0, 0.0) should be false") |
| 65 | |
Tim Peters | 7790297 | 2001-12-29 17:34:57 +0000 | [diff] [blame] | 66 | vereq(complex(5.3, 9.8).conjugate(), 5.3-9.8j) |
Neal Norwitz | 5a0f010 | 2001-12-29 14:31:46 +0000 | [diff] [blame] | 67 | |
Neal Norwitz | fc37af8 | 2001-12-29 01:02:21 +0000 | [diff] [blame] | 68 | try: |
| 69 | print int(5+3j) |
| 70 | except TypeError: |
| 71 | pass |
| 72 | else: |
| 73 | raise TestFailed("int(complex()) didn't raise TypeError") |
| 74 | |
| 75 | try: |
| 76 | print float(5+3j) |
| 77 | except TypeError: |
| 78 | pass |
| 79 | else: |
| 80 | raise TestFailed("float(complex()) didn't raise TypeError") |
| 81 | |
Tim Peters | 0f33604 | 2001-03-18 08:21:57 +0000 | [diff] [blame] | 82 | try: |
| 83 | z = 1.0 / (0+0j) |
| 84 | except ZeroDivisionError: |
| 85 | pass |
| 86 | else: |
| 87 | nerrors += 1 |
| 88 | raise TestFailed("Division by complex 0 didn't raise ZeroDivisionError") |
| 89 | |
| 90 | if nerrors: |
| 91 | raise TestFailed("%d tests failed" % nerrors) |