Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1 | from test.support import run_unittest |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 2 | from test.test_math import parse_testfile, test_file |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3 | import unittest |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 4 | import os, sys |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 5 | import cmath, math |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 6 | from cmath import phase, polar, rect, pi |
| 7 | |
| 8 | INF = float('inf') |
| 9 | NAN = float('nan') |
| 10 | |
| 11 | complex_zeros = [complex(x, y) for x in [0.0, -0.0] for y in [0.0, -0.0]] |
| 12 | complex_infinities = [complex(x, y) for x, y in [ |
| 13 | (INF, 0.0), # 1st quadrant |
| 14 | (INF, 2.3), |
| 15 | (INF, INF), |
| 16 | (2.3, INF), |
| 17 | (0.0, INF), |
| 18 | (-0.0, INF), # 2nd quadrant |
| 19 | (-2.3, INF), |
| 20 | (-INF, INF), |
| 21 | (-INF, 2.3), |
| 22 | (-INF, 0.0), |
| 23 | (-INF, -0.0), # 3rd quadrant |
| 24 | (-INF, -2.3), |
| 25 | (-INF, -INF), |
| 26 | (-2.3, -INF), |
| 27 | (-0.0, -INF), |
| 28 | (0.0, -INF), # 4th quadrant |
| 29 | (2.3, -INF), |
| 30 | (INF, -INF), |
| 31 | (INF, -2.3), |
| 32 | (INF, -0.0) |
| 33 | ]] |
| 34 | complex_nans = [complex(x, y) for x, y in [ |
| 35 | (NAN, -INF), |
| 36 | (NAN, -2.3), |
| 37 | (NAN, -0.0), |
| 38 | (NAN, 0.0), |
| 39 | (NAN, 2.3), |
| 40 | (NAN, INF), |
| 41 | (-INF, NAN), |
| 42 | (-2.3, NAN), |
| 43 | (-0.0, NAN), |
| 44 | (0.0, NAN), |
| 45 | (2.3, NAN), |
| 46 | (INF, NAN) |
| 47 | ]] |
| 48 | |
| 49 | def almostEqualF(a, b, rel_err=2e-15, abs_err = 5e-323): |
| 50 | """Determine whether floating-point values a and b are equal to within |
| 51 | a (small) rounding error. The default values for rel_err and |
| 52 | abs_err are chosen to be suitable for platforms where a float is |
| 53 | represented by an IEEE 754 double. They allow an error of between |
| 54 | 9 and 19 ulps.""" |
| 55 | |
| 56 | # special values testing |
| 57 | if math.isnan(a): |
| 58 | return math.isnan(b) |
| 59 | if math.isinf(a): |
| 60 | return a == b |
| 61 | |
| 62 | # if both a and b are zero, check whether they have the same sign |
| 63 | # (in theory there are examples where it would be legitimate for a |
| 64 | # and b to have opposite signs; in practice these hardly ever |
| 65 | # occur). |
| 66 | if not a and not b: |
| 67 | return math.copysign(1., a) == math.copysign(1., b) |
| 68 | |
| 69 | # if a-b overflows, or b is infinite, return False. Again, in |
| 70 | # theory there are examples where a is within a few ulps of the |
| 71 | # max representable float, and then b could legitimately be |
| 72 | # infinite. In practice these examples are rare. |
| 73 | try: |
| 74 | absolute_error = abs(b-a) |
| 75 | except OverflowError: |
| 76 | return False |
| 77 | else: |
| 78 | return absolute_error <= max(abs_err, rel_err * abs(a)) |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 79 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 80 | class CMathTests(unittest.TestCase): |
| 81 | # list of all functions in cmath |
| 82 | test_functions = [getattr(cmath, fname) for fname in [ |
| 83 | 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atanh', |
| 84 | 'cos', 'cosh', 'exp', 'log', 'log10', 'sin', 'sinh', |
| 85 | 'sqrt', 'tan', 'tanh']] |
| 86 | # test first and second arguments independently for 2-argument log |
| 87 | test_functions.append(lambda x : cmath.log(x, 1729. + 0j)) |
| 88 | test_functions.append(lambda x : cmath.log(14.-27j, x)) |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 89 | |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 90 | def setUp(self): |
| 91 | self.test_values = open(test_file) |
| 92 | |
| 93 | def tearDown(self): |
| 94 | self.test_values.close() |
| 95 | |
| 96 | def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323): |
| 97 | """Check that two floating-point numbers are almost equal.""" |
| 98 | |
| 99 | # special values testing |
| 100 | if math.isnan(a): |
| 101 | if math.isnan(b): |
| 102 | return |
| 103 | self.fail("%s should be nan" % repr(b)) |
| 104 | |
| 105 | if math.isinf(a): |
| 106 | if a == b: |
| 107 | return |
| 108 | self.fail("finite result where infinity excpected: " |
| 109 | "expected %s, got %s" % (repr(a), repr(b))) |
| 110 | |
| 111 | if not a and not b: |
| 112 | if math.atan2(a, -1.) != math.atan2(b, -1.): |
| 113 | self.fail("zero has wrong sign: expected %s, got %s" % |
| 114 | (repr(a), repr(b))) |
| 115 | |
| 116 | # test passes if either the absolute error or the relative |
| 117 | # error is sufficiently small. The defaults amount to an |
| 118 | # error of between 9 ulps and 19 ulps on an IEEE-754 compliant |
| 119 | # machine. |
| 120 | |
| 121 | try: |
| 122 | absolute_error = abs(b-a) |
| 123 | except OverflowError: |
| 124 | pass |
| 125 | else: |
| 126 | if absolute_error <= max(abs_err, rel_err * abs(a)): |
| 127 | return |
| 128 | self.fail("%s and %s are not sufficiently close" % (repr(a), repr(b))) |
Raymond Hettinger | b67ad7e | 2004-06-14 07:40:10 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 130 | def test_constants(self): |
| 131 | e_expected = 2.71828182845904523536 |
| 132 | pi_expected = 3.14159265358979323846 |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 133 | self.assertAlmostEqual(cmath.pi, pi_expected) |
| 134 | self.assertAlmostEqual(cmath.e, e_expected) |
Roger E. Masse | 3daddda | 1996-12-09 22:59:15 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 136 | def test_user_object(self): |
| 137 | # Test automatic calling of __complex__ and __float__ by cmath |
| 138 | # functions |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 139 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 140 | # some random values to use as test values; we avoid values |
| 141 | # for which any of the functions in cmath is undefined |
| 142 | # (i.e. 0., 1., -1., 1j, -1j) or would cause overflow |
| 143 | cx_arg = 4.419414439 + 1.497100113j |
| 144 | flt_arg = -6.131677725 |
Roger E. Masse | fab8ab8 | 1996-12-20 22:36:52 +0000 | [diff] [blame] | 145 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 146 | # a variety of non-complex numbers, used to check that |
| 147 | # non-complex return values from __complex__ give an error |
| 148 | non_complexes = ["not complex", 1, 5, 2., None, |
| 149 | object(), NotImplemented] |
| 150 | |
| 151 | # Now we introduce a variety of classes whose instances might |
| 152 | # end up being passed to the cmath functions |
| 153 | |
| 154 | # usual case: new-style class implementing __complex__ |
| 155 | class MyComplex(object): |
| 156 | def __init__(self, value): |
| 157 | self.value = value |
| 158 | def __complex__(self): |
| 159 | return self.value |
| 160 | |
| 161 | # old-style class implementing __complex__ |
| 162 | class MyComplexOS: |
| 163 | def __init__(self, value): |
| 164 | self.value = value |
| 165 | def __complex__(self): |
| 166 | return self.value |
| 167 | |
| 168 | # classes for which __complex__ raises an exception |
| 169 | class SomeException(Exception): |
| 170 | pass |
| 171 | class MyComplexException(object): |
| 172 | def __complex__(self): |
| 173 | raise SomeException |
| 174 | class MyComplexExceptionOS: |
| 175 | def __complex__(self): |
| 176 | raise SomeException |
| 177 | |
| 178 | # some classes not providing __float__ or __complex__ |
| 179 | class NeitherComplexNorFloat(object): |
| 180 | pass |
| 181 | class NeitherComplexNorFloatOS: |
| 182 | pass |
| 183 | class MyInt(object): |
| 184 | def __int__(self): return 2 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 185 | def __index__(self): return 2 |
| 186 | class MyIntOS: |
| 187 | def __int__(self): return 2 |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 188 | def __index__(self): return 2 |
| 189 | |
| 190 | # other possible combinations of __float__ and __complex__ |
| 191 | # that should work |
| 192 | class FloatAndComplex(object): |
| 193 | def __float__(self): |
| 194 | return flt_arg |
| 195 | def __complex__(self): |
| 196 | return cx_arg |
| 197 | class FloatAndComplexOS: |
| 198 | def __float__(self): |
| 199 | return flt_arg |
| 200 | def __complex__(self): |
| 201 | return cx_arg |
| 202 | class JustFloat(object): |
| 203 | def __float__(self): |
| 204 | return flt_arg |
| 205 | class JustFloatOS: |
| 206 | def __float__(self): |
| 207 | return flt_arg |
| 208 | |
| 209 | for f in self.test_functions: |
| 210 | # usual usage |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 211 | self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg)) |
| 212 | self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 213 | # other combinations of __float__ and __complex__ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 214 | self.assertEqual(f(FloatAndComplex()), f(cx_arg)) |
| 215 | self.assertEqual(f(FloatAndComplexOS()), f(cx_arg)) |
| 216 | self.assertEqual(f(JustFloat()), f(flt_arg)) |
| 217 | self.assertEqual(f(JustFloatOS()), f(flt_arg)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 218 | # TypeError should be raised for classes not providing |
| 219 | # either __complex__ or __float__, even if they provide |
Mark Dickinson | cce2f21 | 2009-01-15 19:32:23 +0000 | [diff] [blame] | 220 | # __int__ or __index__. An old-style class |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 221 | # currently raises AttributeError instead of a TypeError; |
| 222 | # this could be considered a bug. |
| 223 | self.assertRaises(TypeError, f, NeitherComplexNorFloat()) |
| 224 | self.assertRaises(TypeError, f, MyInt()) |
| 225 | self.assertRaises(Exception, f, NeitherComplexNorFloatOS()) |
| 226 | self.assertRaises(Exception, f, MyIntOS()) |
| 227 | # non-complex return value from __complex__ -> TypeError |
| 228 | for bad_complex in non_complexes: |
| 229 | self.assertRaises(TypeError, f, MyComplex(bad_complex)) |
| 230 | self.assertRaises(TypeError, f, MyComplexOS(bad_complex)) |
| 231 | # exceptions in __complex__ should be propagated correctly |
| 232 | self.assertRaises(SomeException, f, MyComplexException()) |
| 233 | self.assertRaises(SomeException, f, MyComplexExceptionOS()) |
| 234 | |
| 235 | def test_input_type(self): |
| 236 | # ints and longs should be acceptable inputs to all cmath |
| 237 | # functions, by virtue of providing a __float__ method |
| 238 | for f in self.test_functions: |
| 239 | for arg in [2, 2.]: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 240 | self.assertEqual(f(arg), f(arg.__float__())) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 241 | |
| 242 | # but strings should give a TypeError |
| 243 | for f in self.test_functions: |
| 244 | for arg in ["a", "long_string", "0", "1j", ""]: |
| 245 | self.assertRaises(TypeError, f, arg) |
| 246 | |
| 247 | def test_cmath_matches_math(self): |
| 248 | # check that corresponding cmath and math functions are equal |
| 249 | # for floats in the appropriate range |
| 250 | |
| 251 | # test_values in (0, 1) |
| 252 | test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99] |
| 253 | |
| 254 | # test_values for functions defined on [-1., 1.] |
| 255 | unit_interval = test_values + [-x for x in test_values] + \ |
| 256 | [0., 1., -1.] |
| 257 | |
| 258 | # test_values for log, log10, sqrt |
| 259 | positive = test_values + [1.] + [1./x for x in test_values] |
| 260 | nonnegative = [0.] + positive |
| 261 | |
| 262 | # test_values for functions defined on the whole real line |
| 263 | real_line = [0.] + positive + [-x for x in positive] |
| 264 | |
| 265 | test_functions = { |
| 266 | 'acos' : unit_interval, |
| 267 | 'asin' : unit_interval, |
| 268 | 'atan' : real_line, |
| 269 | 'cos' : real_line, |
| 270 | 'cosh' : real_line, |
| 271 | 'exp' : real_line, |
| 272 | 'log' : positive, |
| 273 | 'log10' : positive, |
| 274 | 'sin' : real_line, |
| 275 | 'sinh' : real_line, |
| 276 | 'sqrt' : nonnegative, |
| 277 | 'tan' : real_line, |
| 278 | 'tanh' : real_line} |
| 279 | |
| 280 | for fn, values in test_functions.items(): |
| 281 | float_fn = getattr(math, fn) |
| 282 | complex_fn = getattr(cmath, fn) |
| 283 | for v in values: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 284 | z = complex_fn(v) |
| 285 | self.rAssertAlmostEqual(float_fn(v), z.real) |
| 286 | self.assertEqual(0., z.imag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 287 | |
| 288 | # test two-argument version of log with various bases |
| 289 | for base in [0.5, 2., 10.]: |
| 290 | for v in positive: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 291 | z = cmath.log(v, base) |
| 292 | self.rAssertAlmostEqual(math.log(v, base), z.real) |
| 293 | self.assertEqual(0., z.imag) |
| 294 | |
| 295 | def test_specific_values(self): |
| 296 | if not float.__getformat__("double").startswith("IEEE"): |
| 297 | return |
| 298 | |
| 299 | def rect_complex(z): |
| 300 | """Wrapped version of rect that accepts a complex number instead of |
| 301 | two float arguments.""" |
| 302 | return cmath.rect(z.real, z.imag) |
| 303 | |
| 304 | def polar_complex(z): |
| 305 | """Wrapped version of polar that returns a complex number instead of |
| 306 | two floats.""" |
| 307 | return complex(*polar(z)) |
| 308 | |
| 309 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 310 | arg = complex(ar, ai) |
| 311 | expected = complex(er, ei) |
| 312 | if fn == 'rect': |
| 313 | function = rect_complex |
| 314 | elif fn == 'polar': |
| 315 | function = polar_complex |
| 316 | else: |
| 317 | function = getattr(cmath, fn) |
| 318 | if 'divide-by-zero' in flags or 'invalid' in flags: |
| 319 | try: |
| 320 | actual = function(arg) |
| 321 | except ValueError: |
| 322 | continue |
| 323 | else: |
| 324 | test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai) |
| 325 | self.fail('ValueError not raised in test %s' % test_str) |
| 326 | |
| 327 | if 'overflow' in flags: |
| 328 | try: |
| 329 | actual = function(arg) |
| 330 | except OverflowError: |
| 331 | continue |
| 332 | else: |
| 333 | test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai) |
| 334 | self.fail('OverflowError not raised in test %s' % test_str) |
| 335 | |
| 336 | actual = function(arg) |
| 337 | |
| 338 | if 'ignore-real-sign' in flags: |
| 339 | actual = complex(abs(actual.real), actual.imag) |
| 340 | expected = complex(abs(expected.real), expected.imag) |
| 341 | if 'ignore-imag-sign' in flags: |
| 342 | actual = complex(actual.real, abs(actual.imag)) |
| 343 | expected = complex(expected.real, abs(expected.imag)) |
| 344 | |
| 345 | # for the real part of the log function, we allow an |
| 346 | # absolute error of up to 2e-15. |
| 347 | if fn in ('log', 'log10'): |
| 348 | real_abs_err = 2e-15 |
| 349 | else: |
| 350 | real_abs_err = 5e-323 |
| 351 | |
| 352 | if not (almostEqualF(expected.real, actual.real, |
| 353 | abs_err = real_abs_err) and |
| 354 | almostEqualF(expected.imag, actual.imag)): |
| 355 | error_message = ( |
| 356 | "%s: %s(complex(%r, %r))\n" % (id, fn, ar, ai) + |
| 357 | "Expected: complex(%r, %r)\n" % |
| 358 | (expected.real, expected.imag) + |
| 359 | "Received: complex(%r, %r)\n" % |
| 360 | (actual.real, actual.imag) + |
| 361 | "Received value insufficiently close to expected value.") |
| 362 | self.fail(error_message) |
| 363 | |
| 364 | def assertCISEqual(self, a, b): |
| 365 | eps = 1E-7 |
| 366 | if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps: |
| 367 | self.fail((a ,b)) |
| 368 | |
| 369 | def test_polar(self): |
| 370 | self.assertCISEqual(polar(0), (0., 0.)) |
| 371 | self.assertCISEqual(polar(1.), (1., 0.)) |
| 372 | self.assertCISEqual(polar(-1.), (1., pi)) |
| 373 | self.assertCISEqual(polar(1j), (1., pi/2)) |
| 374 | self.assertCISEqual(polar(-1j), (1., -pi/2)) |
| 375 | |
| 376 | def test_phase(self): |
| 377 | self.assertAlmostEqual(phase(0), 0.) |
| 378 | self.assertAlmostEqual(phase(1.), 0.) |
| 379 | self.assertAlmostEqual(phase(-1.), pi) |
| 380 | self.assertAlmostEqual(phase(-1.+1E-300j), pi) |
| 381 | self.assertAlmostEqual(phase(-1.-1E-300j), -pi) |
| 382 | self.assertAlmostEqual(phase(1j), pi/2) |
| 383 | self.assertAlmostEqual(phase(-1j), -pi/2) |
| 384 | |
| 385 | # zeros |
| 386 | self.assertEqual(phase(complex(0.0, 0.0)), 0.0) |
| 387 | self.assertEqual(phase(complex(0.0, -0.0)), -0.0) |
| 388 | self.assertEqual(phase(complex(-0.0, 0.0)), pi) |
| 389 | self.assertEqual(phase(complex(-0.0, -0.0)), -pi) |
| 390 | |
| 391 | # infinities |
| 392 | self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi) |
| 393 | self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi) |
| 394 | self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi) |
| 395 | self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2) |
| 396 | self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2) |
| 397 | self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2) |
| 398 | self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2) |
| 399 | self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4) |
| 400 | self.assertEqual(phase(complex(INF, -2.3)), -0.0) |
| 401 | self.assertEqual(phase(complex(INF, -0.0)), -0.0) |
| 402 | self.assertEqual(phase(complex(INF, 0.0)), 0.0) |
| 403 | self.assertEqual(phase(complex(INF, 2.3)), 0.0) |
| 404 | self.assertAlmostEqual(phase(complex(INF, INF)), pi/4) |
| 405 | self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2) |
| 406 | self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2) |
| 407 | self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2) |
| 408 | self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2) |
| 409 | self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi) |
| 410 | self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi) |
| 411 | self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi) |
| 412 | |
| 413 | # real or imaginary part NaN |
| 414 | for z in complex_nans: |
| 415 | self.assert_(math.isnan(phase(z))) |
| 416 | |
| 417 | def test_abs(self): |
| 418 | # zeros |
| 419 | for z in complex_zeros: |
| 420 | self.assertEqual(abs(z), 0.0) |
| 421 | |
| 422 | # infinities |
| 423 | for z in complex_infinities: |
| 424 | self.assertEqual(abs(z), INF) |
| 425 | |
| 426 | # real or imaginary part NaN |
| 427 | self.assertEqual(abs(complex(NAN, -INF)), INF) |
| 428 | self.assert_(math.isnan(abs(complex(NAN, -2.3)))) |
| 429 | self.assert_(math.isnan(abs(complex(NAN, -0.0)))) |
| 430 | self.assert_(math.isnan(abs(complex(NAN, 0.0)))) |
| 431 | self.assert_(math.isnan(abs(complex(NAN, 2.3)))) |
| 432 | self.assertEqual(abs(complex(NAN, INF)), INF) |
| 433 | self.assertEqual(abs(complex(-INF, NAN)), INF) |
| 434 | self.assert_(math.isnan(abs(complex(-2.3, NAN)))) |
| 435 | self.assert_(math.isnan(abs(complex(-0.0, NAN)))) |
| 436 | self.assert_(math.isnan(abs(complex(0.0, NAN)))) |
| 437 | self.assert_(math.isnan(abs(complex(2.3, NAN)))) |
| 438 | self.assertEqual(abs(complex(INF, NAN)), INF) |
| 439 | self.assert_(math.isnan(abs(complex(NAN, NAN)))) |
| 440 | |
| 441 | # result overflows |
| 442 | if float.__getformat__("double").startswith("IEEE"): |
| 443 | self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308)) |
| 444 | |
| 445 | def assertCEqual(self, a, b): |
| 446 | eps = 1E-7 |
| 447 | if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps: |
| 448 | self.fail((a ,b)) |
| 449 | |
| 450 | def test_rect(self): |
| 451 | self.assertCEqual(rect(0, 0), (0, 0)) |
| 452 | self.assertCEqual(rect(1, 0), (1., 0)) |
| 453 | self.assertCEqual(rect(1, -pi), (-1., 0)) |
| 454 | self.assertCEqual(rect(1, pi/2), (0, 1.)) |
| 455 | self.assertCEqual(rect(1, -pi/2), (0, -1.)) |
| 456 | |
| 457 | def test_isnan(self): |
| 458 | self.failIf(cmath.isnan(1)) |
| 459 | self.failIf(cmath.isnan(1j)) |
| 460 | self.failIf(cmath.isnan(INF)) |
| 461 | self.assert_(cmath.isnan(NAN)) |
| 462 | self.assert_(cmath.isnan(complex(NAN, 0))) |
| 463 | self.assert_(cmath.isnan(complex(0, NAN))) |
| 464 | self.assert_(cmath.isnan(complex(NAN, NAN))) |
| 465 | self.assert_(cmath.isnan(complex(NAN, INF))) |
| 466 | self.assert_(cmath.isnan(complex(INF, NAN))) |
| 467 | |
| 468 | def test_isinf(self): |
| 469 | self.failIf(cmath.isinf(1)) |
| 470 | self.failIf(cmath.isinf(1j)) |
| 471 | self.failIf(cmath.isinf(NAN)) |
| 472 | self.assert_(cmath.isinf(INF)) |
| 473 | self.assert_(cmath.isinf(complex(INF, 0))) |
| 474 | self.assert_(cmath.isinf(complex(0, INF))) |
| 475 | self.assert_(cmath.isinf(complex(INF, INF))) |
| 476 | self.assert_(cmath.isinf(complex(NAN, INF))) |
| 477 | self.assert_(cmath.isinf(complex(INF, NAN))) |
| 478 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 479 | |
| 480 | def test_main(): |
| 481 | run_unittest(CMathTests) |
| 482 | |
| 483 | if __name__ == "__main__": |
| 484 | test_main() |