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_math.py

    r2 r391  
    88import sys
    99import random
     10import struct
    1011
    1112eps = 1E-05
     
    1314INF = float('inf')
    1415NINF = float('-inf')
     16
     17# decorator for skipping tests on non-IEEE 754 platforms
     18requires_IEEE_754 = unittest.skipUnless(
     19    float.__getformat__("double").startswith("IEEE"),
     20    "test requires IEEE 754 doubles")
     21
     22# detect evidence of double-rounding: fsum is not always correctly
     23# rounded on machines that suffer from double rounding.
     24x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
     25HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
    1526
    1627# locate file with test values
     
    2031    file = __file__
    2132test_dir = os.path.dirname(file) or os.curdir
     33math_testcases = os.path.join(test_dir, 'math_testcases.txt')
    2234test_file = os.path.join(test_dir, 'cmath_testcases.txt')
     35
     36def to_ulps(x):
     37    """Convert a non-NaN float x to an integer, in such a way that
     38    adjacent floats are converted to adjacent integers.  Then
     39    abs(ulps(x) - ulps(y)) gives the difference in ulps between two
     40    floats.
     41
     42    The results from this function will only make sense on platforms
     43    where C doubles are represented in IEEE 754 binary64 format.
     44
     45    """
     46    n = struct.unpack('<q', struct.pack('<d', x))[0]
     47    if n < 0:
     48        n = ~(n+2**63)
     49    return n
     50
     51def ulps_check(expected, got, ulps=20):
     52    """Given non-NaN floats `expected` and `got`,
     53    check that they're equal to within the given number of ulps.
     54
     55    Returns None on success and an error message on failure."""
     56
     57    ulps_error = to_ulps(got) - to_ulps(expected)
     58    if abs(ulps_error) <= ulps:
     59        return None
     60    return "error = {} ulps; permitted error = {} ulps".format(ulps_error,
     61                                                               ulps)
     62
     63def acc_check(expected, got, rel_err=2e-15, abs_err = 5e-323):
     64    """Determine whether non-NaN floats a and b are equal to within a
     65    (small) rounding error.  The default values for rel_err and
     66    abs_err are chosen to be suitable for platforms where a float is
     67    represented by an IEEE 754 double.  They allow an error of between
     68    9 and 19 ulps."""
     69
     70    # need to special case infinities, since inf - inf gives nan
     71    if math.isinf(expected) and got == expected:
     72        return None
     73
     74    error = got - expected
     75
     76    permitted_error = max(abs_err, rel_err * abs(expected))
     77    if abs(error) < permitted_error:
     78        return None
     79    return "error = {}; permitted error = {}".format(error,
     80                                                     permitted_error)
     81
     82def parse_mtestfile(fname):
     83    """Parse a file with test values
     84
     85    -- starts a comment
     86    blank lines, or lines containing only a comment, are ignored
     87    other lines are expected to have the form
     88      id fn arg -> expected [flag]*
     89
     90    """
     91    with open(fname) as fp:
     92        for line in fp:
     93            # strip comments, and skip blank lines
     94            if '--' in line:
     95                line = line[:line.index('--')]
     96            if not line.strip():
     97                continue
     98
     99            lhs, rhs = line.split('->')
     100            id, fn, arg = lhs.split()
     101            rhs_pieces = rhs.split()
     102            exp = rhs_pieces[0]
     103            flags = rhs_pieces[1:]
     104
     105            yield (id, fn, float(arg), float(exp), flags)
    23106
    24107def parse_testfile(fname):
     
    68151        self.assertRaises(ValueError, math.acos, INF)
    69152        self.assertRaises(ValueError, math.acos, NINF)
    70         self.assert_(math.isnan(math.acos(NAN)))
     153        self.assertTrue(math.isnan(math.acos(NAN)))
    71154
    72155    def testAcosh(self):
     
    76159        self.assertRaises(ValueError, math.acosh, 0)
    77160        self.assertRaises(ValueError, math.acosh, -1)
    78         self.assertEquals(math.acosh(INF), INF)
     161        self.assertEqual(math.acosh(INF), INF)
    79162        self.assertRaises(ValueError, math.acosh, NINF)
    80         self.assert_(math.isnan(math.acosh(NAN)))
     163        self.assertTrue(math.isnan(math.acosh(NAN)))
    81164
    82165    def testAsin(self):
     
    87170        self.assertRaises(ValueError, math.asin, INF)
    88171        self.assertRaises(ValueError, math.asin, NINF)
    89         self.assert_(math.isnan(math.asin(NAN)))
     172        self.assertTrue(math.isnan(math.asin(NAN)))
    90173
    91174    def testAsinh(self):
     
    94177        self.ftest('asinh(1)', math.asinh(1), 0.88137358701954305)
    95178        self.ftest('asinh(-1)', math.asinh(-1), -0.88137358701954305)
    96         self.assertEquals(math.asinh(INF), INF)
    97         self.assertEquals(math.asinh(NINF), NINF)
    98         self.assert_(math.isnan(math.asinh(NAN)))
     179        self.assertEqual(math.asinh(INF), INF)
     180        self.assertEqual(math.asinh(NINF), NINF)
     181        self.assertTrue(math.isnan(math.asinh(NAN)))
    99182
    100183    def testAtan(self):
     
    105188        self.ftest('atan(inf)', math.atan(INF), math.pi/2)
    106189        self.ftest('atan(-inf)', math.atan(NINF), -math.pi/2)
    107         self.assert_(math.isnan(math.atan(NAN)))
     190        self.assertTrue(math.isnan(math.atan(NAN)))
    108191
    109192    def testAtanh(self):
     
    116199        self.assertRaises(ValueError, math.atanh, INF)
    117200        self.assertRaises(ValueError, math.atanh, NINF)
    118         self.assert_(math.isnan(math.atanh(NAN)))
     201        self.assertTrue(math.isnan(math.atanh(NAN)))
    119202
    120203    def testAtan2(self):
     
    133216        self.assertEqual(math.atan2(0., 2.3), 0.)
    134217        self.assertEqual(math.atan2(0., INF), 0.)
    135         self.assert_(math.isnan(math.atan2(0., NAN)))
     218        self.assertTrue(math.isnan(math.atan2(0., NAN)))
    136219        # math.atan2(-0, x)
    137220        self.ftest('atan2(-0., -inf)', math.atan2(-0., NINF), -math.pi)
     
    141224        self.assertEqual(math.atan2(-0., 2.3), -0.)
    142225        self.assertEqual(math.atan2(-0., INF), -0.)
    143         self.assert_(math.isnan(math.atan2(-0., NAN)))
     226        self.assertTrue(math.isnan(math.atan2(-0., NAN)))
    144227        # math.atan2(INF, x)
    145228        self.ftest('atan2(inf, -inf)', math.atan2(INF, NINF), math.pi*3/4)
     
    149232        self.ftest('atan2(inf, 2.3)', math.atan2(INF, 2.3), math.pi/2)
    150233        self.ftest('atan2(inf, inf)', math.atan2(INF, INF), math.pi/4)
    151         self.assert_(math.isnan(math.atan2(INF, NAN)))
     234        self.assertTrue(math.isnan(math.atan2(INF, NAN)))
    152235        # math.atan2(NINF, x)
    153236        self.ftest('atan2(-inf, -inf)', math.atan2(NINF, NINF), -math.pi*3/4)
     
    157240        self.ftest('atan2(-inf, 2.3)', math.atan2(NINF, 2.3), -math.pi/2)
    158241        self.ftest('atan2(-inf, inf)', math.atan2(NINF, INF), -math.pi/4)
    159         self.assert_(math.isnan(math.atan2(NINF, NAN)))
     242        self.assertTrue(math.isnan(math.atan2(NINF, NAN)))
    160243        # math.atan2(+finite, x)
    161244        self.ftest('atan2(2.3, -inf)', math.atan2(2.3, NINF), math.pi)
     
    163246        self.ftest('atan2(2.3, 0.)', math.atan2(2.3, 0.), math.pi/2)
    164247        self.assertEqual(math.atan2(2.3, INF), 0.)
    165         self.assert_(math.isnan(math.atan2(2.3, NAN)))
     248        self.assertTrue(math.isnan(math.atan2(2.3, NAN)))
    166249        # math.atan2(-finite, x)
    167250        self.ftest('atan2(-2.3, -inf)', math.atan2(-2.3, NINF), -math.pi)
     
    169252        self.ftest('atan2(-2.3, 0.)', math.atan2(-2.3, 0.), -math.pi/2)
    170253        self.assertEqual(math.atan2(-2.3, INF), -0.)
    171         self.assert_(math.isnan(math.atan2(-2.3, NAN)))
     254        self.assertTrue(math.isnan(math.atan2(-2.3, NAN)))
    172255        # math.atan2(NAN, x)
    173         self.assert_(math.isnan(math.atan2(NAN, NINF)))
    174         self.assert_(math.isnan(math.atan2(NAN, -2.3)))
    175         self.assert_(math.isnan(math.atan2(NAN, -0.)))
    176         self.assert_(math.isnan(math.atan2(NAN, 0.)))
    177         self.assert_(math.isnan(math.atan2(NAN, 2.3)))
    178         self.assert_(math.isnan(math.atan2(NAN, INF)))
    179         self.assert_(math.isnan(math.atan2(NAN, NAN)))
     256        self.assertTrue(math.isnan(math.atan2(NAN, NINF)))
     257        self.assertTrue(math.isnan(math.atan2(NAN, -2.3)))
     258        self.assertTrue(math.isnan(math.atan2(NAN, -0.)))
     259        self.assertTrue(math.isnan(math.atan2(NAN, 0.)))
     260        self.assertTrue(math.isnan(math.atan2(NAN, 2.3)))
     261        self.assertTrue(math.isnan(math.atan2(NAN, INF)))
     262        self.assertTrue(math.isnan(math.atan2(NAN, NAN)))
    180263
    181264    def testCeil(self):
    182265        self.assertRaises(TypeError, math.ceil)
    183266        # These types will be int in py3k.
    184         self.assertEquals(float, type(math.ceil(1)))
    185         self.assertEquals(float, type(math.ceil(1L)))
    186         self.assertEquals(float, type(math.ceil(1.0)))
     267        self.assertEqual(float, type(math.ceil(1)))
     268        self.assertEqual(float, type(math.ceil(1L)))
     269        self.assertEqual(float, type(math.ceil(1.0)))
    187270        self.ftest('ceil(0.5)', math.ceil(0.5), 1)
    188271        self.ftest('ceil(1.0)', math.ceil(1.0), 1)
     
    191274        self.ftest('ceil(-1.0)', math.ceil(-1.0), -1)
    192275        self.ftest('ceil(-1.5)', math.ceil(-1.5), -1)
    193         self.assertEquals(math.ceil(INF), INF)
    194         self.assertEquals(math.ceil(NINF), NINF)
    195         self.assert_(math.isnan(math.ceil(NAN)))
     276        self.assertEqual(math.ceil(INF), INF)
     277        self.assertEqual(math.ceil(NINF), NINF)
     278        self.assertTrue(math.isnan(math.ceil(NAN)))
    196279
    197280        class TestCeil(object):
     
    208291        self.assertRaises(TypeError, math.ceil, t, 0)
    209292
    210     if float.__getformat__("double").startswith("IEEE"):
    211         def testCopysign(self):
    212             self.assertEqual(math.copysign(1, 42), 1.0)
    213             self.assertEqual(math.copysign(0., 42), 0.0)
    214             self.assertEqual(math.copysign(1., -42), -1.0)
    215             self.assertEqual(math.copysign(3, 0.), 3.0)
    216             self.assertEqual(math.copysign(4., -0.), -4.0)
    217 
    218             self.assertRaises(TypeError, math.copysign)
    219             # copysign should let us distinguish signs of zeros
    220             self.assertEquals(math.copysign(1., 0.), 1.)
    221             self.assertEquals(math.copysign(1., -0.), -1.)
    222             self.assertEquals(math.copysign(INF, 0.), INF)
    223             self.assertEquals(math.copysign(INF, -0.), NINF)
    224             self.assertEquals(math.copysign(NINF, 0.), INF)
    225             self.assertEquals(math.copysign(NINF, -0.), NINF)
    226             # and of infinities
    227             self.assertEquals(math.copysign(1., INF), 1.)
    228             self.assertEquals(math.copysign(1., NINF), -1.)
    229             self.assertEquals(math.copysign(INF, INF), INF)
    230             self.assertEquals(math.copysign(INF, NINF), NINF)
    231             self.assertEquals(math.copysign(NINF, INF), INF)
    232             self.assertEquals(math.copysign(NINF, NINF), NINF)
    233             self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
    234             self.assertTrue(math.isnan(math.copysign(NAN, INF)))
    235             self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
    236             self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
    237             # copysign(INF, NAN) may be INF or it may be NINF, since
    238             # we don't know whether the sign bit of NAN is set on any
    239             # given platform.
    240             self.assertTrue(math.isinf(math.copysign(INF, NAN)))
    241             # similarly, copysign(2., NAN) could be 2. or -2.
    242             self.assertEquals(abs(math.copysign(2., NAN)), 2.)
     293    @requires_IEEE_754
     294    def testCopysign(self):
     295        self.assertEqual(math.copysign(1, 42), 1.0)
     296        self.assertEqual(math.copysign(0., 42), 0.0)
     297        self.assertEqual(math.copysign(1., -42), -1.0)
     298        self.assertEqual(math.copysign(3, 0.), 3.0)
     299        self.assertEqual(math.copysign(4., -0.), -4.0)
     300
     301        self.assertRaises(TypeError, math.copysign)
     302        # copysign should let us distinguish signs of zeros
     303        self.assertEqual(math.copysign(1., 0.), 1.)
     304        self.assertEqual(math.copysign(1., -0.), -1.)
     305        self.assertEqual(math.copysign(INF, 0.), INF)
     306        self.assertEqual(math.copysign(INF, -0.), NINF)
     307        self.assertEqual(math.copysign(NINF, 0.), INF)
     308        self.assertEqual(math.copysign(NINF, -0.), NINF)
     309        # and of infinities
     310        self.assertEqual(math.copysign(1., INF), 1.)
     311        self.assertEqual(math.copysign(1., NINF), -1.)
     312        self.assertEqual(math.copysign(INF, INF), INF)
     313        self.assertEqual(math.copysign(INF, NINF), NINF)
     314        self.assertEqual(math.copysign(NINF, INF), INF)
     315        self.assertEqual(math.copysign(NINF, NINF), NINF)
     316        self.assertTrue(math.isnan(math.copysign(NAN, 1.)))
     317        self.assertTrue(math.isnan(math.copysign(NAN, INF)))
     318        self.assertTrue(math.isnan(math.copysign(NAN, NINF)))
     319        self.assertTrue(math.isnan(math.copysign(NAN, NAN)))
     320        # copysign(INF, NAN) may be INF or it may be NINF, since
     321        # we don't know whether the sign bit of NAN is set on any
     322        # given platform.
     323        self.assertTrue(math.isinf(math.copysign(INF, NAN)))
     324        # similarly, copysign(2., NAN) could be 2. or -2.
     325        self.assertEqual(abs(math.copysign(2., NAN)), 2.)
    243326
    244327    def testCos(self):
     
    249332        self.ftest('cos(pi)', math.cos(math.pi), -1)
    250333        try:
    251             self.assert_(math.isnan(math.cos(INF)))
    252             self.assert_(math.isnan(math.cos(NINF)))
     334            self.assertTrue(math.isnan(math.cos(INF)))
     335            self.assertTrue(math.isnan(math.cos(NINF)))
    253336        except ValueError:
    254337            self.assertRaises(ValueError, math.cos, INF)
    255338            self.assertRaises(ValueError, math.cos, NINF)
    256         self.assert_(math.isnan(math.cos(NAN)))
     339        self.assertTrue(math.isnan(math.cos(NAN)))
    257340
    258341    def testCosh(self):
     
    260343        self.ftest('cosh(0)', math.cosh(0), 1)
    261344        self.ftest('cosh(2)-2*cosh(1)**2', math.cosh(2)-2*math.cosh(1)**2, -1) # Thanks to Lambert
    262         self.assertEquals(math.cosh(INF), INF)
    263         self.assertEquals(math.cosh(NINF), INF)
    264         self.assert_(math.isnan(math.cosh(NAN)))
     345        self.assertEqual(math.cosh(INF), INF)
     346        self.assertEqual(math.cosh(NINF), INF)
     347        self.assertTrue(math.isnan(math.cosh(NAN)))
    265348
    266349    def testDegrees(self):
     
    275358        self.ftest('exp(0)', math.exp(0), 1)
    276359        self.ftest('exp(1)', math.exp(1), math.e)
    277         self.assertEquals(math.exp(INF), INF)
    278         self.assertEquals(math.exp(NINF), 0.)
    279         self.assert_(math.isnan(math.exp(NAN)))
     360        self.assertEqual(math.exp(INF), INF)
     361        self.assertEqual(math.exp(NINF), 0.)
     362        self.assertTrue(math.isnan(math.exp(NAN)))
    280363
    281364    def testFabs(self):
     
    293376        values = range(10) + [50, 100, 500]
    294377        random.shuffle(values)
    295         for x in range(10):
     378        for x in values:
    296379            for cast in (int, long, float):
    297380                self.assertEqual(math.factorial(cast(x)), fact(x), (x, fact(x), math.factorial(x)))
     
    302385        self.assertRaises(TypeError, math.floor)
    303386        # These types will be int in py3k.
    304         self.assertEquals(float, type(math.floor(1)))
    305         self.assertEquals(float, type(math.floor(1L)))
    306         self.assertEquals(float, type(math.floor(1.0)))
     387        self.assertEqual(float, type(math.floor(1)))
     388        self.assertEqual(float, type(math.floor(1L)))
     389        self.assertEqual(float, type(math.floor(1.0)))
    307390        self.ftest('floor(0.5)', math.floor(0.5), 0)
    308391        self.ftest('floor(1.0)', math.floor(1.0), 1)
     
    315398        self.ftest('floor(1.23e167)', math.floor(1.23e167), 1.23e167)
    316399        self.ftest('floor(-1.23e167)', math.floor(-1.23e167), -1.23e167)
    317         self.assertEquals(math.ceil(INF), INF)
    318         self.assertEquals(math.ceil(NINF), NINF)
    319         self.assert_(math.isnan(math.floor(NAN)))
     400        self.assertEqual(math.ceil(INF), INF)
     401        self.assertEqual(math.ceil(NINF), NINF)
     402        self.assertTrue(math.isnan(math.floor(NAN)))
    320403
    321404        class TestFloor(object):
     
    340423        self.ftest('fmod(-10,0.5)', math.fmod(-10,0.5), 0)
    341424        self.ftest('fmod(-10,1.5)', math.fmod(-10,1.5), -1)
    342         self.assert_(math.isnan(math.fmod(NAN, 1.)))
    343         self.assert_(math.isnan(math.fmod(1., NAN)))
    344         self.assert_(math.isnan(math.fmod(NAN, NAN)))
     425        self.assertTrue(math.isnan(math.fmod(NAN, 1.)))
     426        self.assertTrue(math.isnan(math.fmod(1., NAN)))
     427        self.assertTrue(math.isnan(math.fmod(NAN, NAN)))
    345428        self.assertRaises(ValueError, math.fmod, 1., 0.)
    346429        self.assertRaises(ValueError, math.fmod, INF, 1.)
    347430        self.assertRaises(ValueError, math.fmod, NINF, 1.)
    348431        self.assertRaises(ValueError, math.fmod, INF, 0.)
    349         self.assertEquals(math.fmod(3.0, INF), 3.0)
    350         self.assertEquals(math.fmod(-3.0, INF), -3.0)
    351         self.assertEquals(math.fmod(3.0, NINF), 3.0)
    352         self.assertEquals(math.fmod(-3.0, NINF), -3.0)
    353         self.assertEquals(math.fmod(0.0, 3.0), 0.0)
    354         self.assertEquals(math.fmod(0.0, NINF), 0.0)
     432        self.assertEqual(math.fmod(3.0, INF), 3.0)
     433        self.assertEqual(math.fmod(-3.0, INF), -3.0)
     434        self.assertEqual(math.fmod(3.0, NINF), 3.0)
     435        self.assertEqual(math.fmod(-3.0, NINF), -3.0)
     436        self.assertEqual(math.fmod(0.0, 3.0), 0.0)
     437        self.assertEqual(math.fmod(0.0, NINF), 0.0)
    355438
    356439    def testFrexp(self):
     
    368451        testfrexp('frexp(2)', math.frexp(2), (0.5, 2))
    369452
    370         self.assertEquals(math.frexp(INF)[0], INF)
    371         self.assertEquals(math.frexp(NINF)[0], NINF)
    372         self.assert_(math.isnan(math.frexp(NAN)[0]))
    373 
     453        self.assertEqual(math.frexp(INF)[0], INF)
     454        self.assertEqual(math.frexp(NINF)[0], NINF)
     455        self.assertTrue(math.isnan(math.frexp(NAN)[0]))
     456
     457    @requires_IEEE_754
     458    @unittest.skipIf(HAVE_DOUBLE_ROUNDING,
     459                         "fsum is not exact on machines with double rounding")
    374460    def testFsum(self):
    375461        # math.fsum relies on exact rounding for correct operation.
     
    380466        # problem described in issue #2937, we simply skip the whole
    381467        # test.
    382 
    383         if not float.__getformat__("double").startswith("IEEE"):
    384             return
    385 
    386         # on IEEE 754 compliant machines, both of the expressions
    387         # below should round to 10000000000000002.0.
    388         if 1e16+2.0 != 1e16+2.9999:
    389             return
    390468
    391469        # Python version of math.fsum, for comparison.  Uses a
     
    476554        self.assertEqual(math.hypot(NAN, NINF), INF)
    477555        self.assertEqual(math.hypot(NINF, NAN), INF)
    478         self.assert_(math.isnan(math.hypot(1.0, NAN)))
    479         self.assert_(math.isnan(math.hypot(NAN, -2.0)))
     556        self.assertTrue(math.isnan(math.hypot(1.0, NAN)))
     557        self.assertTrue(math.isnan(math.hypot(NAN, -2.0)))
    480558
    481559    def testLdexp(self):
     
    487565        self.assertRaises(OverflowError, math.ldexp, 1., 1000000)
    488566        self.assertRaises(OverflowError, math.ldexp, -1., 1000000)
    489         self.assertEquals(math.ldexp(1., -1000000), 0.)
    490         self.assertEquals(math.ldexp(-1., -1000000), -0.)
    491         self.assertEquals(math.ldexp(INF, 30), INF)
    492         self.assertEquals(math.ldexp(NINF, -213), NINF)
    493         self.assert_(math.isnan(math.ldexp(NAN, 0)))
     567        self.assertEqual(math.ldexp(1., -1000000), 0.)
     568        self.assertEqual(math.ldexp(-1., -1000000), -0.)
     569        self.assertEqual(math.ldexp(INF, 30), INF)
     570        self.assertEqual(math.ldexp(NINF, -213), NINF)
     571        self.assertTrue(math.isnan(math.ldexp(NAN, 0)))
    494572
    495573        # large second argument
    496574        for n in [10**5, 10L**5, 10**10, 10L**10, 10**20, 10**40]:
    497             self.assertEquals(math.ldexp(INF, -n), INF)
    498             self.assertEquals(math.ldexp(NINF, -n), NINF)
    499             self.assertEquals(math.ldexp(1., -n), 0.)
    500             self.assertEquals(math.ldexp(-1., -n), -0.)
    501             self.assertEquals(math.ldexp(0., -n), 0.)
    502             self.assertEquals(math.ldexp(-0., -n), -0.)
    503             self.assert_(math.isnan(math.ldexp(NAN, -n)))
     575            self.assertEqual(math.ldexp(INF, -n), INF)
     576            self.assertEqual(math.ldexp(NINF, -n), NINF)
     577            self.assertEqual(math.ldexp(1., -n), 0.)
     578            self.assertEqual(math.ldexp(-1., -n), -0.)
     579            self.assertEqual(math.ldexp(0., -n), 0.)
     580            self.assertEqual(math.ldexp(-0., -n), -0.)
     581            self.assertTrue(math.isnan(math.ldexp(NAN, -n)))
    504582
    505583            self.assertRaises(OverflowError, math.ldexp, 1., n)
    506584            self.assertRaises(OverflowError, math.ldexp, -1., n)
    507             self.assertEquals(math.ldexp(0., n), 0.)
    508             self.assertEquals(math.ldexp(-0., n), -0.)
    509             self.assertEquals(math.ldexp(INF, n), INF)
    510             self.assertEquals(math.ldexp(NINF, n), NINF)
    511             self.assert_(math.isnan(math.ldexp(NAN, n)))
     585            self.assertEqual(math.ldexp(0., n), 0.)
     586            self.assertEqual(math.ldexp(-0., n), -0.)
     587            self.assertEqual(math.ldexp(INF, n), INF)
     588            self.assertEqual(math.ldexp(NINF, n), NINF)
     589            self.assertTrue(math.isnan(math.ldexp(NAN, n)))
    512590
    513591    def testLog(self):
     
    519597        self.ftest('log(10**40, 10)', math.log(10**40, 10), 40)
    520598        self.ftest('log(10**40, 10**20)', math.log(10**40, 10**20), 2)
    521         self.assertEquals(math.log(INF), INF)
     599        self.assertEqual(math.log(INF), INF)
    522600        self.assertRaises(ValueError, math.log, NINF)
    523         self.assert_(math.isnan(math.log(NAN)))
     601        self.assertTrue(math.isnan(math.log(NAN)))
     602        # Log values should match for int and long (issue #18739).
     603        for n in range(1, 1000):
     604            self.assertEqual(math.log(n), math.log(long(n)))
    524605
    525606    def testLog1p(self):
     
    529610        self.ftest('log1p(e-1)', math.log1p(math.e-1), 1)
    530611        self.ftest('log1p(1)', math.log1p(1), math.log(2))
    531         self.assertEquals(math.log1p(INF), INF)
     612        self.assertEqual(math.log1p(INF), INF)
    532613        self.assertRaises(ValueError, math.log1p, NINF)
    533         self.assert_(math.isnan(math.log1p(NAN)))
     614        self.assertTrue(math.isnan(math.log1p(NAN)))
    534615        n= 2**90
    535         self.assertAlmostEquals(math.log1p(n), 62.383246250395075)
    536         self.assertAlmostEquals(math.log1p(n), math.log1p(float(n)))
     616        self.assertAlmostEqual(math.log1p(n), 62.383246250395075)
     617        self.assertAlmostEqual(math.log1p(n), math.log1p(float(n)))
    537618
    538619    def testLog10(self):
     
    541622        self.ftest('log10(1)', math.log10(1), 0)
    542623        self.ftest('log10(10)', math.log10(10), 1)
    543         self.assertEquals(math.log(INF), INF)
     624        self.assertEqual(math.log(INF), INF)
    544625        self.assertRaises(ValueError, math.log10, NINF)
    545         self.assert_(math.isnan(math.log10(NAN)))
     626        self.assertTrue(math.isnan(math.log10(NAN)))
     627        # Log values should match for int and long (issue #18739).
     628        for n in range(1, 1000):
     629            self.assertEqual(math.log10(n), math.log10(long(n)))
    546630
    547631    def testModf(self):
     
    557641        testmodf('modf(-1.5)', math.modf(-1.5), (-0.5, -1.0))
    558642
    559         self.assertEquals(math.modf(INF), (0.0, INF))
    560         self.assertEquals(math.modf(NINF), (-0.0, NINF))
     643        self.assertEqual(math.modf(INF), (0.0, INF))
     644        self.assertEqual(math.modf(NINF), (-0.0, NINF))
    561645
    562646        modf_nan = math.modf(NAN)
    563         self.assert_(math.isnan(modf_nan[0]))
    564         self.assert_(math.isnan(modf_nan[1]))
     647        self.assertTrue(math.isnan(modf_nan[0]))
     648        self.assertTrue(math.isnan(modf_nan[1]))
    565649
    566650    def testPow(self):
     
    574658        self.assertEqual((math.pow(1, INF)), 1.)
    575659        self.assertEqual((math.pow(1, NINF)), 1.)
    576         self.assert_(math.isnan(math.pow(NAN, 1)))
    577         self.assert_(math.isnan(math.pow(2, NAN)))
    578         self.assert_(math.isnan(math.pow(0, NAN)))
     660        self.assertTrue(math.isnan(math.pow(NAN, 1)))
     661        self.assertTrue(math.isnan(math.pow(2, NAN)))
     662        self.assertTrue(math.isnan(math.pow(0, NAN)))
    579663        self.assertEqual(math.pow(1, NAN), 1)
    580664
     
    590674        self.assertRaises(ValueError, math.pow, 0., -3.)
    591675        self.assertRaises(ValueError, math.pow, 0., NINF)
    592         self.assert_(math.isnan(math.pow(0., NAN)))
     676        self.assertTrue(math.isnan(math.pow(0., NAN)))
    593677
    594678        # pow(INF, x)
     
    603687        self.assertEqual(math.pow(INF, -3.), 0.)
    604688        self.assertEqual(math.pow(INF, NINF), 0.)
    605         self.assert_(math.isnan(math.pow(INF, NAN)))
     689        self.assertTrue(math.isnan(math.pow(INF, NAN)))
    606690
    607691        # pow(-0., x)
     
    616700        self.assertRaises(ValueError, math.pow, -0., -3.)
    617701        self.assertRaises(ValueError, math.pow, -0., NINF)
    618         self.assert_(math.isnan(math.pow(-0., NAN)))
     702        self.assertTrue(math.isnan(math.pow(-0., NAN)))
    619703
    620704        # pow(NINF, x)
     
    629713        self.assertEqual(math.pow(NINF, -3.), -0.)
    630714        self.assertEqual(math.pow(NINF, NINF), 0.)
    631         self.assert_(math.isnan(math.pow(NINF, NAN)))
     715        self.assertTrue(math.isnan(math.pow(NINF, NAN)))
    632716
    633717        # pow(-1, x)
     
    642726        self.assertEqual(math.pow(-1., -3.), -1.)
    643727        self.assertEqual(math.pow(-1., NINF), 1.)
    644         self.assert_(math.isnan(math.pow(-1., NAN)))
     728        self.assertTrue(math.isnan(math.pow(-1., NAN)))
    645729
    646730        # pow(1, x)
     
    703787        # the following tests have been commented out since they don't
    704788        # really belong here:  the implementation of ** for floats is
    705         # independent of the implemention of math.pow
     789        # independent of the implementation of math.pow
    706790        #self.assertEqual(1**NAN, 1)
    707791        #self.assertEqual(1**INF, 1)
     
    725809        self.ftest('sin(-pi/2)', math.sin(-math.pi/2), -1)
    726810        try:
    727             self.assert_(math.isnan(math.sin(INF)))
    728             self.assert_(math.isnan(math.sin(NINF)))
     811            self.assertTrue(math.isnan(math.sin(INF)))
     812            self.assertTrue(math.isnan(math.sin(NINF)))
    729813        except ValueError:
    730814            self.assertRaises(ValueError, math.sin, INF)
    731815            self.assertRaises(ValueError, math.sin, NINF)
    732         self.assert_(math.isnan(math.sin(NAN)))
     816        self.assertTrue(math.isnan(math.sin(NAN)))
    733817
    734818    def testSinh(self):
     
    737821        self.ftest('sinh(1)**2-cosh(1)**2', math.sinh(1)**2-math.cosh(1)**2, -1)
    738822        self.ftest('sinh(1)+sinh(-1)', math.sinh(1)+math.sinh(-1), 0)
    739         self.assertEquals(math.sinh(INF), INF)
    740         self.assertEquals(math.sinh(NINF), NINF)
    741         self.assert_(math.isnan(math.sinh(NAN)))
     823        self.assertEqual(math.sinh(INF), INF)
     824        self.assertEqual(math.sinh(NINF), NINF)
     825        self.assertTrue(math.isnan(math.sinh(NAN)))
    742826
    743827    def testSqrt(self):
     
    746830        self.ftest('sqrt(1)', math.sqrt(1), 1)
    747831        self.ftest('sqrt(4)', math.sqrt(4), 2)
    748         self.assertEquals(math.sqrt(INF), INF)
     832        self.assertEqual(math.sqrt(INF), INF)
    749833        self.assertRaises(ValueError, math.sqrt, NINF)
    750         self.assert_(math.isnan(math.sqrt(NAN)))
     834        self.assertTrue(math.isnan(math.sqrt(NAN)))
    751835
    752836    def testTan(self):
     
    756840        self.ftest('tan(-pi/4)', math.tan(-math.pi/4), -1)
    757841        try:
    758             self.assert_(math.isnan(math.tan(INF)))
    759             self.assert_(math.isnan(math.tan(NINF)))
     842            self.assertTrue(math.isnan(math.tan(INF)))
     843            self.assertTrue(math.isnan(math.tan(NINF)))
    760844        except:
    761845            self.assertRaises(ValueError, math.tan, INF)
    762846            self.assertRaises(ValueError, math.tan, NINF)
    763         self.assert_(math.isnan(math.tan(NAN)))
     847        self.assertTrue(math.isnan(math.tan(NAN)))
    764848
    765849    def testTanh(self):
     
    769853        self.ftest('tanh(inf)', math.tanh(INF), 1)
    770854        self.ftest('tanh(-inf)', math.tanh(NINF), -1)
    771         self.assert_(math.isnan(math.tanh(NAN)))
     855        self.assertTrue(math.isnan(math.tanh(NAN)))
    772856        # check that tanh(-0.) == -0. on IEEE 754 systems
    773857        if float.__getformat__("double").startswith("IEEE"):
     
    799883        self.assertRaises(TypeError, math.trunc)
    800884        self.assertRaises(TypeError, math.trunc, 1, 2)
    801         # XXX: This is not ideal, but see the comment in math_trunc().
    802         self.assertRaises(AttributeError, math.trunc, TestNoTrunc())
    803 
    804         t = TestNoTrunc()
    805         t.__trunc__ = lambda *args: args
    806         self.assertEquals((), math.trunc(t))
    807         self.assertRaises(TypeError, math.trunc, t, 0)
     885        self.assertRaises((AttributeError, TypeError), math.trunc,
     886                          TestNoTrunc())
    808887
    809888    def testIsnan(self):
    810         self.assert_(math.isnan(float("nan")))
    811         self.assert_(math.isnan(float("inf")* 0.))
    812         self.failIf(math.isnan(float("inf")))
    813         self.failIf(math.isnan(0.))
    814         self.failIf(math.isnan(1.))
     889        self.assertTrue(math.isnan(float("nan")))
     890        self.assertTrue(math.isnan(float("inf")* 0.))
     891        self.assertFalse(math.isnan(float("inf")))
     892        self.assertFalse(math.isnan(0.))
     893        self.assertFalse(math.isnan(1.))
    815894
    816895    def testIsinf(self):
    817         self.assert_(math.isinf(float("inf")))
    818         self.assert_(math.isinf(float("-inf")))
    819         self.assert_(math.isinf(1E400))
    820         self.assert_(math.isinf(-1E400))
    821         self.failIf(math.isinf(float("nan")))
    822         self.failIf(math.isinf(0.))
    823         self.failIf(math.isinf(1.))
     896        self.assertTrue(math.isinf(float("inf")))
     897        self.assertTrue(math.isinf(float("-inf")))
     898        self.assertTrue(math.isinf(1E400))
     899        self.assertTrue(math.isinf(-1E400))
     900        self.assertFalse(math.isinf(float("nan")))
     901        self.assertFalse(math.isinf(0.))
     902        self.assertFalse(math.isinf(1.))
    824903
    825904    # RED_FLAG 16-Oct-2000 Tim
     
    861940                self.fail("sqrt(-1) didn't raise ValueError")
    862941
     942    @requires_IEEE_754
    863943    def test_testfile(self):
    864         if not float.__getformat__("double").startswith("IEEE"):
    865             return
    866944        for id, fn, ar, ai, er, ei, flags in parse_testfile(test_file):
    867945            # Skip if either the input or result is complex, or if
     
    885963            self.ftest("%s:%s(%r)" % (id, fn, ar), result, er)
    886964
     965    @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
     966                         "test requires IEEE 754 doubles")
     967    def test_mtestfile(self):
     968        ALLOWED_ERROR = 20  # permitted error, in ulps
     969        fail_fmt = "{}:{}({!r}): expected {!r}, got {!r}"
     970
     971        failures = []
     972        for id, fn, arg, expected, flags in parse_mtestfile(math_testcases):
     973            func = getattr(math, fn)
     974
     975            if 'invalid' in flags or 'divide-by-zero' in flags:
     976                expected = 'ValueError'
     977            elif 'overflow' in flags:
     978                expected = 'OverflowError'
     979
     980            try:
     981                got = func(arg)
     982            except ValueError:
     983                got = 'ValueError'
     984            except OverflowError:
     985                got = 'OverflowError'
     986
     987            accuracy_failure = None
     988            if isinstance(got, float) and isinstance(expected, float):
     989                if math.isnan(expected) and math.isnan(got):
     990                    continue
     991                if not math.isnan(expected) and not math.isnan(got):
     992                    if fn == 'lgamma':
     993                        # we use a weaker accuracy test for lgamma;
     994                        # lgamma only achieves an absolute error of
     995                        # a few multiples of the machine accuracy, in
     996                        # general.
     997                        accuracy_failure = acc_check(expected, got,
     998                                                  rel_err = 5e-15,
     999                                                  abs_err = 5e-15)
     1000                    elif fn == 'erfc':
     1001                        # erfc has less-than-ideal accuracy for large
     1002                        # arguments (x ~ 25 or so), mainly due to the
     1003                        # error involved in computing exp(-x*x).
     1004                        #
     1005                        # XXX Would be better to weaken this test only
     1006                        # for large x, instead of for all x.
     1007                        accuracy_failure = ulps_check(expected, got, 2000)
     1008
     1009                    else:
     1010                        accuracy_failure = ulps_check(expected, got, 20)
     1011                    if accuracy_failure is None:
     1012                        continue
     1013
     1014            if isinstance(got, str) and isinstance(expected, str):
     1015                if got == expected:
     1016                    continue
     1017
     1018            fail_msg = fail_fmt.format(id, fn, arg, expected, got)
     1019            if accuracy_failure is not None:
     1020                fail_msg += ' ({})'.format(accuracy_failure)
     1021            failures.append(fail_msg)
     1022
     1023        if failures:
     1024            self.fail('Failures in test_mtestfile:\n  ' +
     1025                      '\n  '.join(failures))
     1026
     1027
    8871028def test_main():
    8881029    from doctest import DocFileSuite
Note: See TracChangeset for help on using the changeset viewer.