Changeset 391 for python/trunk/Lib/test/test_cmath.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_cmath.py
r2 r391 2 2 from test.test_math import parse_testfile, test_file 3 3 import unittest 4 import os, sys5 4 import cmath, math 6 5 from cmath import phase, polar, rect, pi … … 47 46 ]] 48 47 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 within51 a (small) rounding error. The default values for rel_err and52 abs_err are chosen to be suitable for platforms where a float is53 represented by an IEEE 754 double. They allow an error of between54 9 and 19 ulps."""55 56 # special values testing57 if math.isnan(a):58 return math.isnan(b)59 if math.isinf(a):60 return a == b61 62 # if both a and b are zero, check whether they have the same sign63 # (in theory there are examples where it would be legitimate for a64 # and b to have opposite signs; in practice these hardly ever65 # 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, in70 # theory there are examples where a is within a few ulps of the71 # max representable float, and then b could legitimately be72 # infinite. In practice these examples are rare.73 try:74 absolute_error = abs(b-a)75 except OverflowError:76 return False77 else:78 return absolute_error <= max(abs_err, rel_err * abs(a))79 80 48 class CMathTests(unittest.TestCase): 81 49 # list of all functions in cmath … … 94 62 self.test_values.close() 95 63 96 def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323): 97 """Check that two floating-point numbers are almost equal.""" 64 def rAssertAlmostEqual(self, a, b, rel_err = 2e-15, abs_err = 5e-323, 65 msg=None): 66 """Fail if the two floating-point numbers are not almost equal. 67 68 Determine whether floating-point values a and b are equal to within 69 a (small) rounding error. The default values for rel_err and 70 abs_err are chosen to be suitable for platforms where a float is 71 represented by an IEEE 754 double. They allow an error of between 72 9 and 19 ulps. 73 """ 98 74 99 75 # special values testing … … 101 77 if math.isnan(b): 102 78 return 103 self.fail( "%s should be nan" % repr(b))79 self.fail(msg or '{!r} should be nan'.format(b)) 104 80 105 81 if math.isinf(a): 106 82 if a == b: 107 83 return 108 self.fail("finite result where infinity excpected: " 109 "expected %s, got %s" % (repr(a), repr(b))) 110 84 self.fail(msg or 'finite result where infinity expected: ' 85 'expected {!r}, got {!r}'.format(a, b)) 86 87 # if both a and b are zero, check whether they have the same sign 88 # (in theory there are examples where it would be legitimate for a 89 # and b to have opposite signs; in practice these hardly ever 90 # occur). 111 91 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 92 if math.copysign(1., a) != math.copysign(1., b): 93 self.fail(msg or 'zero has wrong sign: expected {!r}, ' 94 'got {!r}'.format(a, b)) 95 96 # if a-b overflows, or b is infinite, return False. Again, in 97 # theory there are examples where a is within a few ulps of the 98 # max representable float, and then b could legitimately be 99 # infinite. In practice these examples are rare. 121 100 try: 122 101 absolute_error = abs(b-a) … … 124 103 pass 125 104 else: 105 # test passes if either the absolute error or the relative 106 # error is sufficiently small. The defaults amount to an 107 # error of between 9 ulps and 19 ulps on an IEEE-754 compliant 108 # machine. 126 109 if absolute_error <= max(abs_err, rel_err * abs(a)): 127 110 return 128 self.fail("%s and %s are not sufficiently close" % (repr(a), repr(b))) 111 self.fail(msg or 112 '{!r} and {!r} are not sufficiently close'.format(a, b)) 129 113 130 114 def test_constants(self): … … 132 116 pi_expected = 3.14159265358979323846 133 117 self.assertAlmostEqual(cmath.pi, pi_expected, places=9, 134 msg="cmath.pi is %s; should be %s" %(cmath.pi, pi_expected))118 msg="cmath.pi is {}; should be {}".format(cmath.pi, pi_expected)) 135 119 self.assertAlmostEqual(cmath.e, e_expected, places=9, 136 msg="cmath.e is %s; should be %s" %(cmath.e, e_expected))120 msg="cmath.e is {}; should be {}".format(cmath.e, e_expected)) 137 121 138 122 def test_user_object(self): … … 326 310 continue 327 311 else: 328 test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai)329 self.fail('ValueError not raised in test %s' % test_str)312 self.fail('ValueError not raised in test ' 313 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) 330 314 331 315 if 'overflow' in flags: … … 335 319 continue 336 320 else: 337 test_str = "%s: %s(complex(%r, %r))" % (id, fn, ar, ai)338 self.fail('OverflowError not raised in test %s' % test_str)321 self.fail('OverflowError not raised in test ' 322 '{}: {}(complex({!r}, {!r}))'.format(id, fn, ar, ai)) 339 323 340 324 actual = function(arg) … … 354 338 real_abs_err = 5e-323 355 339 356 if not (almostEqualF(expected.real, actual.real, 357 abs_err = real_abs_err) and 358 almostEqualF(expected.imag, actual.imag)): 359 error_message = ( 360 "%s: %s(complex(%r, %r))\n" % (id, fn, ar, ai) + 361 "Expected: complex(%r, %r)\n" % 362 (expected.real, expected.imag) + 363 "Received: complex(%r, %r)\n" % 364 (actual.real, actual.imag) + 365 "Received value insufficiently close to expected value.") 366 self.fail(error_message) 340 error_message = ( 341 '{}: {}(complex({!r}, {!r}))\n' 342 'Expected: complex({!r}, {!r})\n' 343 'Received: complex({!r}, {!r})\n' 344 'Received value insufficiently close to expected value.' 345 ).format(id, fn, ar, ai, 346 expected.real, expected.imag, 347 actual.real, actual.imag) 348 self.rAssertAlmostEqual(expected.real, actual.real, 349 abs_err=real_abs_err, 350 msg=error_message) 351 self.rAssertAlmostEqual(expected.imag, actual.imag, 352 msg=error_message) 367 353 368 354 def assertCISEqual(self, a, b): … … 417 403 # real or imaginary part NaN 418 404 for z in complex_nans: 419 self.assert _(math.isnan(phase(z)))405 self.assertTrue(math.isnan(phase(z))) 420 406 421 407 def test_abs(self): … … 430 416 # real or imaginary part NaN 431 417 self.assertEqual(abs(complex(NAN, -INF)), INF) 432 self.assert _(math.isnan(abs(complex(NAN, -2.3))))433 self.assert _(math.isnan(abs(complex(NAN, -0.0))))434 self.assert _(math.isnan(abs(complex(NAN, 0.0))))435 self.assert _(math.isnan(abs(complex(NAN, 2.3))))418 self.assertTrue(math.isnan(abs(complex(NAN, -2.3)))) 419 self.assertTrue(math.isnan(abs(complex(NAN, -0.0)))) 420 self.assertTrue(math.isnan(abs(complex(NAN, 0.0)))) 421 self.assertTrue(math.isnan(abs(complex(NAN, 2.3)))) 436 422 self.assertEqual(abs(complex(NAN, INF)), INF) 437 423 self.assertEqual(abs(complex(-INF, NAN)), INF) 438 self.assert _(math.isnan(abs(complex(-2.3, NAN))))439 self.assert _(math.isnan(abs(complex(-0.0, NAN))))440 self.assert _(math.isnan(abs(complex(0.0, NAN))))441 self.assert _(math.isnan(abs(complex(2.3, NAN))))424 self.assertTrue(math.isnan(abs(complex(-2.3, NAN)))) 425 self.assertTrue(math.isnan(abs(complex(-0.0, NAN)))) 426 self.assertTrue(math.isnan(abs(complex(0.0, NAN)))) 427 self.assertTrue(math.isnan(abs(complex(2.3, NAN)))) 442 428 self.assertEqual(abs(complex(INF, NAN)), INF) 443 self.assert _(math.isnan(abs(complex(NAN, NAN))))429 self.assertTrue(math.isnan(abs(complex(NAN, NAN)))) 444 430 445 431 # result overflows … … 460 446 461 447 def test_isnan(self): 462 self. failIf(cmath.isnan(1))463 self. failIf(cmath.isnan(1j))464 self. failIf(cmath.isnan(INF))465 self.assert _(cmath.isnan(NAN))466 self.assert _(cmath.isnan(complex(NAN, 0)))467 self.assert _(cmath.isnan(complex(0, NAN)))468 self.assert _(cmath.isnan(complex(NAN, NAN)))469 self.assert _(cmath.isnan(complex(NAN, INF)))470 self.assert _(cmath.isnan(complex(INF, NAN)))448 self.assertFalse(cmath.isnan(1)) 449 self.assertFalse(cmath.isnan(1j)) 450 self.assertFalse(cmath.isnan(INF)) 451 self.assertTrue(cmath.isnan(NAN)) 452 self.assertTrue(cmath.isnan(complex(NAN, 0))) 453 self.assertTrue(cmath.isnan(complex(0, NAN))) 454 self.assertTrue(cmath.isnan(complex(NAN, NAN))) 455 self.assertTrue(cmath.isnan(complex(NAN, INF))) 456 self.assertTrue(cmath.isnan(complex(INF, NAN))) 471 457 472 458 def test_isinf(self): 473 self. failIf(cmath.isinf(1))474 self. failIf(cmath.isinf(1j))475 self. failIf(cmath.isinf(NAN))476 self.assert _(cmath.isinf(INF))477 self.assert _(cmath.isinf(complex(INF, 0)))478 self.assert _(cmath.isinf(complex(0, INF)))479 self.assert _(cmath.isinf(complex(INF, INF)))480 self.assert _(cmath.isinf(complex(NAN, INF)))481 self.assert _(cmath.isinf(complex(INF, NAN)))459 self.assertFalse(cmath.isinf(1)) 460 self.assertFalse(cmath.isinf(1j)) 461 self.assertFalse(cmath.isinf(NAN)) 462 self.assertTrue(cmath.isinf(INF)) 463 self.assertTrue(cmath.isinf(complex(INF, 0))) 464 self.assertTrue(cmath.isinf(complex(0, INF))) 465 self.assertTrue(cmath.isinf(complex(INF, INF))) 466 self.assertTrue(cmath.isinf(complex(NAN, INF))) 467 self.assertTrue(cmath.isinf(complex(INF, NAN))) 482 468 483 469
Note:
See TracChangeset
for help on using the changeset viewer.