Ignore:
Timestamp:
Mar 19, 2014, 11:31:01 PM (11 years ago)
Author:
dmik
Message:

python: Merge vendor 2.7.6 to trunk.

Location:
python/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • python/trunk

  • python/trunk/Lib/test/test_cmath.py

    r2 r391  
    22from test.test_math import parse_testfile, test_file
    33import unittest
    4 import os, sys
    54import cmath, math
    65from cmath import phase, polar, rect, pi
     
    4746        ]]
    4847
    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))
    79 
    8048class CMathTests(unittest.TestCase):
    8149    # list of all functions in cmath
     
    9462        self.test_values.close()
    9563
    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        """
    9874
    9975        # special values testing
     
    10177            if math.isnan(b):
    10278                return
    103             self.fail("%s should be nan" % repr(b))
     79            self.fail(msg or '{!r} should be nan'.format(b))
    10480
    10581        if math.isinf(a):
    10682            if a == b:
    10783                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).
    11191        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.
    121100        try:
    122101            absolute_error = abs(b-a)
     
    124103            pass
    125104        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.
    126109            if absolute_error <= max(abs_err, rel_err * abs(a)):
    127110                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))
    129113
    130114    def test_constants(self):
     
    132116        pi_expected = 3.14159265358979323846
    133117        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))
    135119        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))
    137121
    138122    def test_user_object(self):
     
    326310                    continue
    327311                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))
    330314
    331315            if 'overflow' in flags:
     
    335319                    continue
    336320                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))
    339323
    340324            actual = function(arg)
     
    354338                real_abs_err = 5e-323
    355339
    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)
    367353
    368354    def assertCISEqual(self, a, b):
     
    417403        # real or imaginary part NaN
    418404        for z in complex_nans:
    419             self.assert_(math.isnan(phase(z)))
     405            self.assertTrue(math.isnan(phase(z)))
    420406
    421407    def test_abs(self):
     
    430416        # real or imaginary part NaN
    431417        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))))
    436422        self.assertEqual(abs(complex(NAN, INF)), INF)
    437423        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))))
    442428        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))))
    444430
    445431        # result overflows
     
    460446
    461447    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)))
    471457
    472458    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)))
    482468
    483469
Note: See TracChangeset for help on using the changeset viewer.