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

    r2 r391  
    11import unittest
    2 from test import test_support
    32import sys
    43
    54import random
     5import math
     6
     7from test import test_int, test_support
    68
    79# Used for lazy formatting of failure messages
     
    1517
    1618# SHIFT should match the value in longintrepr.h for best testing.
    17 SHIFT = 15
     19SHIFT = sys.long_info.bits_per_digit
    1820BASE = 2 ** SHIFT
    1921MASK = BASE - 1
     
    7880]
    7981
    80 
    81 class LongTest(unittest.TestCase):
     82class LongTest(test_int.IntLongCommonTests, unittest.TestCase):
     83
     84    ntype = long
    8285
    8386    # Get quasi-random long consisting of ndigits digits (in base BASE).
     
    8891
    8992    def getran(self, ndigits):
    90         self.assert_(ndigits > 0)
     93        self.assertTrue(ndigits > 0)
    9194        nbits_hi = ndigits * SHIFT
    9295        nbits_lo = nbits_hi - SHIFT + 1
     
    97100            bits = (r >> 1) + 1
    98101            bits = min(bits, nbits_hi - nbits)
    99             self.assert_(1 <= bits <= SHIFT)
     102            self.assertTrue(1 <= bits <= SHIFT)
    100103            nbits = nbits + bits
    101104            answer = answer << bits
     
    103106                answer = answer | ((1 << bits) - 1)
    104107            r = int(random.random() * (SHIFT * 2))
    105         self.assert_(nbits_lo <= nbits <= nbits_hi)
     108        self.assertTrue(nbits_lo <= nbits <= nbits_hi)
    106109        if random.random() < 0.5:
    107110            answer = -answer
     
    129132        eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
    130133        if y > 0:
    131             self.assert_(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
     134            self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
    132135        else:
    133             self.assert_(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
     136            self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
    134137
    135138    def test_division(self):
     
    142145                y = self.getran(leny) or 1L
    143146                self.check_division(x, y)
     147
     148        # specific numbers chosen to exercise corner cases of the
     149        # current long division implementation
     150
     151        # 30-bit cases involving a quotient digit estimate of BASE+1
     152        self.check_division(1231948412290879395966702881L,
     153                            1147341367131428698L)
     154        self.check_division(815427756481275430342312021515587883L,
     155                       707270836069027745L)
     156        self.check_division(627976073697012820849443363563599041L,
     157                       643588798496057020L)
     158        self.check_division(1115141373653752303710932756325578065L,
     159                       1038556335171453937726882627L)
     160        # 30-bit cases that require the post-subtraction correction step
     161        self.check_division(922498905405436751940989320930368494L,
     162                       949985870686786135626943396L)
     163        self.check_division(768235853328091167204009652174031844L,
     164                       1091555541180371554426545266L)
     165
     166        # 15-bit cases involving a quotient digit estimate of BASE+1
     167        self.check_division(20172188947443L, 615611397L)
     168        self.check_division(1020908530270155025L, 950795710L)
     169        self.check_division(128589565723112408L, 736393718L)
     170        self.check_division(609919780285761575L, 18613274546784L)
     171        # 15-bit cases that require the post-subtraction correction step
     172        self.check_division(710031681576388032L, 26769404391308L)
     173        self.check_division(1933622614268221L, 30212853348836L)
     174
     175
    144176
    145177    def test_karatsuba(self):
     
    291323        self.assertEqual(long(-3.5), -3L)
    292324        self.assertEqual(long("-3"), -3L)
     325        self.assertEqual(long("0b10", 2), 2L)
     326        self.assertEqual(long("0o10", 8), 8L)
     327        self.assertEqual(long("0x10", 16), 16L)
    293328        if test_support.have_unicode:
    294329            self.assertEqual(long(unicode("-3")), -3L)
     
    323358        self.assertRaises(ValueError, long, '53', 40)
    324359        self.assertRaises(TypeError, long, 1, 12)
     360
     361        # tests with base 0
     362        self.assertEqual(long(' 0123  ', 0), 83)
     363        self.assertEqual(long(' 0123  ', 0), 83)
     364        self.assertEqual(long('000', 0), 0)
     365        self.assertEqual(long('0o123', 0), 83)
     366        self.assertEqual(long('0x123', 0), 291)
     367        self.assertEqual(long('0b100', 0), 4)
     368        self.assertEqual(long(' 0O123   ', 0), 83)
     369        self.assertEqual(long(' 0X123  ', 0), 291)
     370        self.assertEqual(long(' 0B100 ', 0), 4)
     371        self.assertEqual(long('0', 0), 0)
     372        self.assertEqual(long('+0', 0), 0)
     373        self.assertEqual(long('-0', 0), 0)
     374        self.assertEqual(long('00', 0), 0)
     375        self.assertRaises(ValueError, long, '08', 0)
     376        self.assertRaises(ValueError, long, '-012395', 0)
    325377
    326378        # SF patch #1638879: embedded NULs were not detected with
     
    481533                    long(TruncReturnsNonIntegral())
    482534                except TypeError as e:
    483                     self.assertEquals(str(e),
    484                                       "__trunc__ returned non-Integral"
    485                                       " (type NonIntegral)")
     535                    self.assertEqual(str(e),
     536                                     "__trunc__ returned non-Integral"
     537                                     " (type NonIntegral)")
    486538                else:
    487539                    self.fail("Failed to raise TypeError with %s" %
     
    522574        except OverflowError:
    523575            self.fail("int(long(sys.maxint) + 1) mustn't overflow")
    524         self.assert_(isinstance(y, long),
     576        self.assertIsInstance(y, long,
    525577            "int(long(sys.maxint) + 1) should have returned long")
    526578
     
    530582        except OverflowError:
    531583            self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow")
    532         self.assert_(isinstance(y, long),
     584        self.assertIsInstance(y, long,
    533585               "int(long(-sys.maxint-1) - 1) should have returned long")
    534586
     
    537589        x = long2(1L<<100)
    538590        y = int(x)
    539         self.assert_(type(y) is long,
     591        self.assertTrue(type(y) is long,
    540592            "overflowing int conversion must return long not long subtype")
    541593
     
    545597                return i, j
    546598
    547         self.assertEqual(X()[-5L:7L], (-5, 7))
    548         # use the clamping effect to test the smallest and largest longs
    549         # that fit a Py_ssize_t
    550         slicemin, slicemax = X()[-2L**100:2L**100]
    551         self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax))
     599        with test_support.check_py3k_warnings():
     600            self.assertEqual(X()[-5L:7L], (-5, 7))
     601            # use the clamping effect to test the smallest and largest longs
     602            # that fit a Py_ssize_t
     603            slicemin, slicemax = X()[-2L**100:2L**100]
     604            self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax))
     605
     606    def test_issue9869(self):
     607        # Issue 9869: Interpreter crash when initializing an instance
     608        # of a long subclass from an object whose __long__ method returns
     609        # a plain int.
     610        class BadLong(object):
     611            def __long__(self):
     612                return 1000000
     613
     614        class MyLong(long):
     615            pass
     616
     617        x = MyLong(BadLong())
     618        self.assertIsInstance(x, long)
     619        self.assertEqual(x, 1000000)
     620
    552621
    553622# ----------------------------------- tests of auto int->long conversion
    554623
    555624    def test_auto_overflow(self):
    556         import math, sys
    557 
    558625        special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
    559626        sqrt = int(math.sqrt(sys.maxint))
     
    589656
    590657                if y:
    591                     expected = longx / longy
    592                     got = x / y
     658                    with test_support.check_py3k_warnings():
     659                        expected = longx / longy
     660                        got = x / y
    593661                    checkit(x, '/', y)
    594662
     
    615683                                self.assertRaises(TypeError, pow,longx, longy, long(z))
    616684
     685    @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
     686                         "test requires IEEE 754 doubles")
     687    def test_float_conversion(self):
     688        import sys
     689        DBL_MAX = sys.float_info.max
     690        DBL_MAX_EXP = sys.float_info.max_exp
     691        DBL_MANT_DIG = sys.float_info.mant_dig
     692
     693        exact_values = [0L, 1L, 2L,
     694                         long(2**53-3),
     695                         long(2**53-2),
     696                         long(2**53-1),
     697                         long(2**53),
     698                         long(2**53+2),
     699                         long(2**54-4),
     700                         long(2**54-2),
     701                         long(2**54),
     702                         long(2**54+4)]
     703        for x in exact_values:
     704            self.assertEqual(long(float(x)), x)
     705            self.assertEqual(long(float(-x)), -x)
     706
     707        # test round-half-even
     708        for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]:
     709            for p in xrange(15):
     710                self.assertEqual(long(float(2L**p*(2**53+x))), 2L**p*(2**53+y))
     711
     712        for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8),
     713                     (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12),
     714                     (13, 12), (14, 16), (15, 16)]:
     715            for p in xrange(15):
     716                self.assertEqual(long(float(2L**p*(2**54+x))), 2L**p*(2**54+y))
     717
     718        # behaviour near extremes of floating-point range
     719        long_dbl_max = long(DBL_MAX)
     720        top_power = 2**DBL_MAX_EXP
     721        halfway = (long_dbl_max + top_power)//2
     722        self.assertEqual(float(long_dbl_max), DBL_MAX)
     723        self.assertEqual(float(long_dbl_max+1), DBL_MAX)
     724        self.assertEqual(float(halfway-1), DBL_MAX)
     725        self.assertRaises(OverflowError, float, halfway)
     726        self.assertEqual(float(1-halfway), -DBL_MAX)
     727        self.assertRaises(OverflowError, float, -halfway)
     728        self.assertRaises(OverflowError, float, top_power-1)
     729        self.assertRaises(OverflowError, float, top_power)
     730        self.assertRaises(OverflowError, float, top_power+1)
     731        self.assertRaises(OverflowError, float, 2*top_power-1)
     732        self.assertRaises(OverflowError, float, 2*top_power)
     733        self.assertRaises(OverflowError, float, top_power*top_power)
     734
     735        for p in xrange(100):
     736            x = long(2**p * (2**53 + 1) + 1)
     737            y = long(2**p * (2**53+ 2))
     738            self.assertEqual(long(float(x)), y)
     739
     740            x = long(2**p * (2**53 + 1))
     741            y = long(2**p * 2**53)
     742            self.assertEqual(long(float(x)), y)
     743
    617744    def test_float_overflow(self):
    618         import math
    619 
    620745        for x in -2.0, -1.0, 0.0, 1.0, 2.0:
    621746            self.assertEqual(float(long(x)), x)
     
    647772
    648773    def test_logs(self):
    649         import math
    650 
    651774        LOG10E = math.log10(math.e)
    652775
     
    668791    def test_mixed_compares(self):
    669792        eq = self.assertEqual
    670         import math
    671793
    672794        # We're mostly concerned with that mixing floats and longs does the
     
    752874        self.assertRaises(ValueError, long, float('nan'))
    753875
     876    def test_bit_length(self):
     877        tiny = 1e-10
     878        for x in xrange(-65000, 65000):
     879            x = long(x)
     880            k = x.bit_length()
     881            # Check equivalence with Python version
     882            self.assertEqual(k, len(bin(x).lstrip('-0b')))
     883            # Behaviour as specified in the docs
     884            if x != 0:
     885                self.assertTrue(2**(k-1) <= abs(x) < 2**k)
     886            else:
     887                self.assertEqual(k, 0)
     888            # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
     889            if x != 0:
     890                # When x is an exact power of 2, numeric errors can
     891                # cause floor(log(x)/log(2)) to be one too small; for
     892                # small x this can be fixed by adding a small quantity
     893                # to the quotient before taking the floor.
     894                self.assertEqual(k, 1 + math.floor(
     895                        math.log(abs(x))/math.log(2) + tiny))
     896
     897        self.assertEqual((0L).bit_length(), 0)
     898        self.assertEqual((1L).bit_length(), 1)
     899        self.assertEqual((-1L).bit_length(), 1)
     900        self.assertEqual((2L).bit_length(), 2)
     901        self.assertEqual((-2L).bit_length(), 2)
     902        for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]:
     903            a = 2L**i
     904            self.assertEqual((a-1).bit_length(), i)
     905            self.assertEqual((1-a).bit_length(), i)
     906            self.assertEqual((a).bit_length(), i+1)
     907            self.assertEqual((-a).bit_length(), i+1)
     908            self.assertEqual((a+1).bit_length(), i+1)
     909            self.assertEqual((-a-1).bit_length(), i+1)
     910
     911
    754912def test_main():
    755913    test_support.run_unittest(LongTest)
Note: See TracChangeset for help on using the changeset viewer.