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 |
| 185 | def __long__(self): return 2 |
| 186 | def __index__(self): return 2 |
| 187 | class MyIntOS: |
| 188 | def __int__(self): return 2 |
| 189 | def __long__(self): return 2 |
| 190 | def __index__(self): return 2 |
| 191 | |
| 192 | # other possible combinations of __float__ and __complex__ |
| 193 | # that should work |
| 194 | class FloatAndComplex(object): |
| 195 | def __float__(self): |
| 196 | return flt_arg |
| 197 | def __complex__(self): |
| 198 | return cx_arg |
| 199 | class FloatAndComplexOS: |
| 200 | def __float__(self): |
| 201 | return flt_arg |
| 202 | def __complex__(self): |
| 203 | return cx_arg |
| 204 | class JustFloat(object): |
| 205 | def __float__(self): |
| 206 | return flt_arg |
| 207 | class JustFloatOS: |
| 208 | def __float__(self): |
| 209 | return flt_arg |
| 210 | |
| 211 | for f in self.test_functions: |
| 212 | # usual usage |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 213 | self.assertEqual(f(MyComplex(cx_arg)), f(cx_arg)) |
| 214 | self.assertEqual(f(MyComplexOS(cx_arg)), f(cx_arg)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 215 | # other combinations of __float__ and __complex__ |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 216 | self.assertEqual(f(FloatAndComplex()), f(cx_arg)) |
| 217 | self.assertEqual(f(FloatAndComplexOS()), f(cx_arg)) |
| 218 | self.assertEqual(f(JustFloat()), f(flt_arg)) |
| 219 | self.assertEqual(f(JustFloatOS()), f(flt_arg)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 220 | # TypeError should be raised for classes not providing |
| 221 | # either __complex__ or __float__, even if they provide |
| 222 | # __int__, __long__ or __index__. An old-style class |
| 223 | # currently raises AttributeError instead of a TypeError; |
| 224 | # this could be considered a bug. |
| 225 | self.assertRaises(TypeError, f, NeitherComplexNorFloat()) |
| 226 | self.assertRaises(TypeError, f, MyInt()) |
| 227 | self.assertRaises(Exception, f, NeitherComplexNorFloatOS()) |
| 228 | self.assertRaises(Exception, f, MyIntOS()) |
| 229 | # non-complex return value from __complex__ -> TypeError |
| 230 | for bad_complex in non_complexes: |
| 231 | self.assertRaises(TypeError, f, MyComplex(bad_complex)) |
| 232 | self.assertRaises(TypeError, f, MyComplexOS(bad_complex)) |
| 233 | # exceptions in __complex__ should be propagated correctly |
| 234 | self.assertRaises(SomeException, f, MyComplexException()) |
| 235 | self.assertRaises(SomeException, f, MyComplexExceptionOS()) |
| 236 | |
| 237 | def test_input_type(self): |
| 238 | # ints and longs should be acceptable inputs to all cmath |
| 239 | # functions, by virtue of providing a __float__ method |
| 240 | for f in self.test_functions: |
| 241 | for arg in [2, 2.]: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 242 | self.assertEqual(f(arg), f(arg.__float__())) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 243 | |
| 244 | # but strings should give a TypeError |
| 245 | for f in self.test_functions: |
| 246 | for arg in ["a", "long_string", "0", "1j", ""]: |
| 247 | self.assertRaises(TypeError, f, arg) |
| 248 | |
| 249 | def test_cmath_matches_math(self): |
| 250 | # check that corresponding cmath and math functions are equal |
| 251 | # for floats in the appropriate range |
| 252 | |
| 253 | # test_values in (0, 1) |
| 254 | test_values = [0.01, 0.1, 0.2, 0.5, 0.9, 0.99] |
| 255 | |
| 256 | # test_values for functions defined on [-1., 1.] |
| 257 | unit_interval = test_values + [-x for x in test_values] + \ |
| 258 | [0., 1., -1.] |
| 259 | |
| 260 | # test_values for log, log10, sqrt |
| 261 | positive = test_values + [1.] + [1./x for x in test_values] |
| 262 | nonnegative = [0.] + positive |
| 263 | |
| 264 | # test_values for functions defined on the whole real line |
| 265 | real_line = [0.] + positive + [-x for x in positive] |
| 266 | |
| 267 | test_functions = { |
| 268 | 'acos' : unit_interval, |
| 269 | 'asin' : unit_interval, |
| 270 | 'atan' : real_line, |
| 271 | 'cos' : real_line, |
| 272 | 'cosh' : real_line, |
| 273 | 'exp' : real_line, |
| 274 | 'log' : positive, |
| 275 | 'log10' : positive, |
| 276 | 'sin' : real_line, |
| 277 | 'sinh' : real_line, |
| 278 | 'sqrt' : nonnegative, |
| 279 | 'tan' : real_line, |
| 280 | 'tanh' : real_line} |
| 281 | |
| 282 | for fn, values in test_functions.items(): |
| 283 | float_fn = getattr(math, fn) |
| 284 | complex_fn = getattr(cmath, fn) |
| 285 | for v in values: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 286 | z = complex_fn(v) |
| 287 | self.rAssertAlmostEqual(float_fn(v), z.real) |
| 288 | self.assertEqual(0., z.imag) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 289 | |
| 290 | # test two-argument version of log with various bases |
| 291 | for base in [0.5, 2., 10.]: |
| 292 | for v in positive: |
Christian Heimes | 53876d9 | 2008-04-19 00:31:39 +0000 | [diff] [blame] | 293 | z = cmath.log(v, base) |
| 294 | self.rAssertAlmostEqual(math.log(v, base), z.real) |
| 295 | self.assertEqual(0., z.imag) |
| 296 | |
| 297 | def test_specific_values(self): |
| 298 | if not float.__getformat__("double").startswith("IEEE"): |
| 299 | return |
| 300 | |
| 301 | def rect_complex(z): |
| 302 | """Wrapped version of rect that accepts a complex number instead of |
| 303 | two float arguments.""" |
| 304 | return cmath.rect(z.real, z.imag) |
| 305 | |
| 306 | def polar_complex(z): |
| 307 | """Wrapped version of polar that returns a complex number instead of |
| 308 | two floats.""" |
| 309 | return complex(*polar(z)) |
| 310 | |
| 311 | for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file): |
| 312 | arg = complex(ar, ai) |
| 313 | expected = complex(er, ei) |
| 314 | if fn == 'rect': |
| 315 | function = rect_complex |
| 316 | elif fn == 'polar': |
| 317 | function = polar_complex |
| 318 | else: |
| 319 | function = getattr(cmath, fn) |
| 320 | if 'divide-by-zero' in flags or 'invalid' in flags: |
| 321 | try: |
| 322 | actual = function(arg) |
| 323 | except ValueError: |
| 324 | continue |
| 325 | else: |
| 326 | test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai) |
| 327 | self.fail('ValueError not raised in test %s' % test_str) |
| 328 | |
| 329 | if 'overflow' in flags: |
| 330 | try: |
| 331 | actual = function(arg) |
| 332 | except OverflowError: |
| 333 | continue |
| 334 | else: |
| 335 | test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai) |
| 336 | self.fail('OverflowError not raised in test %s' % test_str) |
| 337 | |
| 338 | actual = function(arg) |
| 339 | |
| 340 | if 'ignore-real-sign' in flags: |
| 341 | actual = complex(abs(actual.real), actual.imag) |
| 342 | expected = complex(abs(expected.real), expected.imag) |
| 343 | if 'ignore-imag-sign' in flags: |
| 344 | actual = complex(actual.real, abs(actual.imag)) |
| 345 | expected = complex(expected.real, abs(expected.imag)) |
| 346 | |
| 347 | # for the real part of the log function, we allow an |
| 348 | # absolute error of up to 2e-15. |
| 349 | if fn in ('log', 'log10'): |
| 350 | real_abs_err = 2e-15 |
| 351 | else: |
| 352 | real_abs_err = 5e-323 |
| 353 | |
| 354 | if not (almostEqualF(expected.real, actual.real, |
| 355 | abs_err = real_abs_err) and |
| 356 | almostEqualF(expected.imag, actual.imag)): |
| 357 | error_message = ( |
| 358 | "%s: %s(complex(%r, %r))\n" % (id, fn, ar, ai) + |
| 359 | "Expected: complex(%r, %r)\n" % |
| 360 | (expected.real, expected.imag) + |
| 361 | "Received: complex(%r, %r)\n" % |
| 362 | (actual.real, actual.imag) + |
| 363 | "Received value insufficiently close to expected value.") |
| 364 | self.fail(error_message) |
| 365 | |
| 366 | def assertCISEqual(self, a, b): |
| 367 | eps = 1E-7 |
| 368 | if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps: |
| 369 | self.fail((a ,b)) |
| 370 | |
| 371 | def test_polar(self): |
| 372 | self.assertCISEqual(polar(0), (0., 0.)) |
| 373 | self.assertCISEqual(polar(1.), (1., 0.)) |
| 374 | self.assertCISEqual(polar(-1.), (1., pi)) |
| 375 | self.assertCISEqual(polar(1j), (1., pi/2)) |
| 376 | self.assertCISEqual(polar(-1j), (1., -pi/2)) |
| 377 | |
| 378 | def test_phase(self): |
| 379 | self.assertAlmostEqual(phase(0), 0.) |
| 380 | self.assertAlmostEqual(phase(1.), 0.) |
| 381 | self.assertAlmostEqual(phase(-1.), pi) |
| 382 | self.assertAlmostEqual(phase(-1.+1E-300j), pi) |
| 383 | self.assertAlmostEqual(phase(-1.-1E-300j), -pi) |
| 384 | self.assertAlmostEqual(phase(1j), pi/2) |
| 385 | self.assertAlmostEqual(phase(-1j), -pi/2) |
| 386 | |
| 387 | # zeros |
| 388 | self.assertEqual(phase(complex(0.0, 0.0)), 0.0) |
| 389 | self.assertEqual(phase(complex(0.0, -0.0)), -0.0) |
| 390 | self.assertEqual(phase(complex(-0.0, 0.0)), pi) |
| 391 | self.assertEqual(phase(complex(-0.0, -0.0)), -pi) |
| 392 | |
| 393 | # infinities |
| 394 | self.assertAlmostEqual(phase(complex(-INF, -0.0)), -pi) |
| 395 | self.assertAlmostEqual(phase(complex(-INF, -2.3)), -pi) |
| 396 | self.assertAlmostEqual(phase(complex(-INF, -INF)), -0.75*pi) |
| 397 | self.assertAlmostEqual(phase(complex(-2.3, -INF)), -pi/2) |
| 398 | self.assertAlmostEqual(phase(complex(-0.0, -INF)), -pi/2) |
| 399 | self.assertAlmostEqual(phase(complex(0.0, -INF)), -pi/2) |
| 400 | self.assertAlmostEqual(phase(complex(2.3, -INF)), -pi/2) |
| 401 | self.assertAlmostEqual(phase(complex(INF, -INF)), -pi/4) |
| 402 | self.assertEqual(phase(complex(INF, -2.3)), -0.0) |
| 403 | self.assertEqual(phase(complex(INF, -0.0)), -0.0) |
| 404 | self.assertEqual(phase(complex(INF, 0.0)), 0.0) |
| 405 | self.assertEqual(phase(complex(INF, 2.3)), 0.0) |
| 406 | self.assertAlmostEqual(phase(complex(INF, INF)), pi/4) |
| 407 | self.assertAlmostEqual(phase(complex(2.3, INF)), pi/2) |
| 408 | self.assertAlmostEqual(phase(complex(0.0, INF)), pi/2) |
| 409 | self.assertAlmostEqual(phase(complex(-0.0, INF)), pi/2) |
| 410 | self.assertAlmostEqual(phase(complex(-2.3, INF)), pi/2) |
| 411 | self.assertAlmostEqual(phase(complex(-INF, INF)), 0.75*pi) |
| 412 | self.assertAlmostEqual(phase(complex(-INF, 2.3)), pi) |
| 413 | self.assertAlmostEqual(phase(complex(-INF, 0.0)), pi) |
| 414 | |
| 415 | # real or imaginary part NaN |
| 416 | for z in complex_nans: |
| 417 | self.assert_(math.isnan(phase(z))) |
| 418 | |
| 419 | def test_abs(self): |
| 420 | # zeros |
| 421 | for z in complex_zeros: |
| 422 | self.assertEqual(abs(z), 0.0) |
| 423 | |
| 424 | # infinities |
| 425 | for z in complex_infinities: |
| 426 | self.assertEqual(abs(z), INF) |
| 427 | |
| 428 | # real or imaginary part NaN |
| 429 | self.assertEqual(abs(complex(NAN, -INF)), INF) |
| 430 | self.assert_(math.isnan(abs(complex(NAN, -2.3)))) |
| 431 | self.assert_(math.isnan(abs(complex(NAN, -0.0)))) |
| 432 | self.assert_(math.isnan(abs(complex(NAN, 0.0)))) |
| 433 | self.assert_(math.isnan(abs(complex(NAN, 2.3)))) |
| 434 | self.assertEqual(abs(complex(NAN, INF)), INF) |
| 435 | self.assertEqual(abs(complex(-INF, NAN)), INF) |
| 436 | self.assert_(math.isnan(abs(complex(-2.3, NAN)))) |
| 437 | self.assert_(math.isnan(abs(complex(-0.0, NAN)))) |
| 438 | self.assert_(math.isnan(abs(complex(0.0, NAN)))) |
| 439 | self.assert_(math.isnan(abs(complex(2.3, NAN)))) |
| 440 | self.assertEqual(abs(complex(INF, NAN)), INF) |
| 441 | self.assert_(math.isnan(abs(complex(NAN, NAN)))) |
| 442 | |
| 443 | # result overflows |
| 444 | if float.__getformat__("double").startswith("IEEE"): |
| 445 | self.assertRaises(OverflowError, abs, complex(1.4e308, 1.4e308)) |
| 446 | |
| 447 | def assertCEqual(self, a, b): |
| 448 | eps = 1E-7 |
| 449 | if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps: |
| 450 | self.fail((a ,b)) |
| 451 | |
| 452 | def test_rect(self): |
| 453 | self.assertCEqual(rect(0, 0), (0, 0)) |
| 454 | self.assertCEqual(rect(1, 0), (1., 0)) |
| 455 | self.assertCEqual(rect(1, -pi), (-1., 0)) |
| 456 | self.assertCEqual(rect(1, pi/2), (0, 1.)) |
| 457 | self.assertCEqual(rect(1, -pi/2), (0, -1.)) |
| 458 | |
| 459 | def test_isnan(self): |
| 460 | self.failIf(cmath.isnan(1)) |
| 461 | self.failIf(cmath.isnan(1j)) |
| 462 | self.failIf(cmath.isnan(INF)) |
| 463 | self.assert_(cmath.isnan(NAN)) |
| 464 | self.assert_(cmath.isnan(complex(NAN, 0))) |
| 465 | self.assert_(cmath.isnan(complex(0, NAN))) |
| 466 | self.assert_(cmath.isnan(complex(NAN, NAN))) |
| 467 | self.assert_(cmath.isnan(complex(NAN, INF))) |
| 468 | self.assert_(cmath.isnan(complex(INF, NAN))) |
| 469 | |
| 470 | def test_isinf(self): |
| 471 | self.failIf(cmath.isinf(1)) |
| 472 | self.failIf(cmath.isinf(1j)) |
| 473 | self.failIf(cmath.isinf(NAN)) |
| 474 | self.assert_(cmath.isinf(INF)) |
| 475 | self.assert_(cmath.isinf(complex(INF, 0))) |
| 476 | self.assert_(cmath.isinf(complex(0, INF))) |
| 477 | self.assert_(cmath.isinf(complex(INF, INF))) |
| 478 | self.assert_(cmath.isinf(complex(NAN, INF))) |
| 479 | self.assert_(cmath.isinf(complex(INF, NAN))) |
| 480 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 481 | |
| 482 | def test_main(): |
| 483 | run_unittest(CMathTests) |
| 484 | |
| 485 | if __name__ == "__main__": |
| 486 | test_main() |