10a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport unittest
20a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport sys
30a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
40a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport random
50a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoimport math
60a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
70a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofrom test import test_int, test_support
80a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
90a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Used for lazy formatting of failure messages
100a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass Frm(object):
110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __init__(self, format, *args):
120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.format = format
130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.args = args
140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def __str__(self):
160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return self.format % self.args
170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# SHIFT should match the value in longintrepr.h for best testing.
190a8c90248264a8b26970b4473770bcc3df8515fJosh GaoSHIFT = sys.long_info.bits_per_digit
200a8c90248264a8b26970b4473770bcc3df8515fJosh GaoBASE = 2 ** SHIFT
210a8c90248264a8b26970b4473770bcc3df8515fJosh GaoMASK = BASE - 1
220a8c90248264a8b26970b4473770bcc3df8515fJosh GaoKARATSUBA_CUTOFF = 70   # from longobject.c
230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# Max number of base BASE digits to use in test cases.  Doubling
250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# this will more than double the runtime.
260a8c90248264a8b26970b4473770bcc3df8515fJosh GaoMAXDIGITS = 15
270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# build some special values
290a8c90248264a8b26970b4473770bcc3df8515fJosh Gaospecial = map(long, [0, 1, 2, BASE, BASE >> 1])
300a8c90248264a8b26970b4473770bcc3df8515fJosh Gaospecial.append(0x5555555555555555L)
310a8c90248264a8b26970b4473770bcc3df8515fJosh Gaospecial.append(0xaaaaaaaaaaaaaaaaL)
320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao#  some solid strings of one bits
330a8c90248264a8b26970b4473770bcc3df8515fJosh Gaop2 = 4L  # 0 and 1 already added
340a8c90248264a8b26970b4473770bcc3df8515fJosh Gaofor i in range(2*SHIFT):
350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    special.append(p2 - 1)
360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    p2 = p2 << 1
370a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodel p2
380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# add complements & negations
390a8c90248264a8b26970b4473770bcc3df8515fJosh Gaospecial = special + map(lambda x: ~x, special) + \
400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    map(lambda x: -x, special)
410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
420a8c90248264a8b26970b4473770bcc3df8515fJosh GaoL = [
430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('0', 0),
440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('1', 1),
450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('9', 9),
460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('10', 10),
470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('99', 99),
480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('100', 100),
490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('314', 314),
500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (' 314', 314),
510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('314 ', 314),
520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('  \t\t  314  \t\t  ', 314),
530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (repr(sys.maxint), sys.maxint),
540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('  1x', ValueError),
550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('  1  ', 1),
560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('  1\02  ', ValueError),
570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('', ValueError),
580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (' ', ValueError),
590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ('  \t\t  ', ValueError)
600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao]
610a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif test_support.have_unicode:
620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    L += [
630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('0'), 0),
640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('1'), 1),
650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('9'), 9),
660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('10'), 10),
670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('99'), 99),
680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('100'), 100),
690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('314'), 314),
700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode(' 314'), 314),
710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('  \t\t  314  \t\t  '), 314),
730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('  1x'), ValueError),
740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('  1  '), 1),
750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('  1\02  '), ValueError),
760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode(''), ValueError),
770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode(' '), ValueError),
780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unicode('  \t\t  '), ValueError),
790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        (unichr(0x200), ValueError),
800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao]
810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
820a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoclass LongTest(test_int.IntLongCommonTests, unittest.TestCase):
830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    ntype = long
850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Get quasi-random long consisting of ndigits digits (in base BASE).
870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # quasi == the most-significant digit will not be 0, and the number
880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # is constructed to contain long strings of 0 and 1 bits.  These are
890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # more likely than random bits to provoke digit-boundary errors.
900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # The sign of the number is also random.
910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getran(self, ndigits):
930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(ndigits > 0)
940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        nbits_hi = ndigits * SHIFT
950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        nbits_lo = nbits_hi - SHIFT + 1
960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        answer = 0L
970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        nbits = 0
980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        r = int(random.random() * (SHIFT * 2)) | 1  # force 1 bits to start
990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while nbits < nbits_lo:
1000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            bits = (r >> 1) + 1
1010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            bits = min(bits, nbits_hi - nbits)
1020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(1 <= bits <= SHIFT)
1030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            nbits = nbits + bits
1040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            answer = answer << bits
1050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if r & 1:
1060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                answer = answer | ((1 << bits) - 1)
1070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            r = int(random.random() * (SHIFT * 2))
1080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(nbits_lo <= nbits <= nbits_hi)
1090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if random.random() < 0.5:
1100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            answer = -answer
1110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return answer
1120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # Get random long consisting of ndigits random digits (relative to base
1140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    # BASE).  The sign bit is also random.
1150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def getran2(ndigits):
1170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        answer = 0L
1180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in xrange(ndigits):
1190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            answer = (answer << SHIFT) | random.randint(0, MASK)
1200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if random.random() < 0.5:
1210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            answer = -answer
1220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return answer
1230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_division(self, x, y):
1250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
1260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q, r = divmod(x, y)
1270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        q2, r2 = x//y, x%y
1280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        pab, pba = x*y, y*x
1290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y))
1300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y))
1310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y))
1320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
1330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if y > 0:
1340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
1350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        else:
1360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
1370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_division(self):
1390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits = range(1, MAXDIGITS+1) + range(KARATSUBA_CUTOFF,
1400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                               KARATSUBA_CUTOFF + 14)
1410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits.append(KARATSUBA_CUTOFF * 3)
1420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for lenx in digits:
1430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = self.getran(lenx)
1440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for leny in digits:
1450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                y = self.getran(leny) or 1L
1460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.check_division(x, y)
1470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # specific numbers chosen to exercise corner cases of the
1490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # current long division implementation
1500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 30-bit cases involving a quotient digit estimate of BASE+1
1520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(1231948412290879395966702881L,
1530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            1147341367131428698L)
1540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(815427756481275430342312021515587883L,
1550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       707270836069027745L)
1560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(627976073697012820849443363563599041L,
1570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       643588798496057020L)
1580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(1115141373653752303710932756325578065L,
1590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       1038556335171453937726882627L)
1600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 30-bit cases that require the post-subtraction correction step
1610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(922498905405436751940989320930368494L,
1620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       949985870686786135626943396L)
1630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(768235853328091167204009652174031844L,
1640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                       1091555541180371554426545266L)
1650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 15-bit cases involving a quotient digit estimate of BASE+1
1670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(20172188947443L, 615611397L)
1680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(1020908530270155025L, 950795710L)
1690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(128589565723112408L, 736393718L)
1700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(609919780285761575L, 18613274546784L)
1710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 15-bit cases that require the post-subtraction correction step
1720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(710031681576388032L, 26769404391308L)
1730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.check_division(1933622614268221L, 30212853348836L)
1740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_karatsuba(self):
1780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10)
1790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100])
1800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        bits = [digit * SHIFT for digit in digits]
1820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) ==
1840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check.
1850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for abits in bits:
1860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            a = (1L << abits) - 1
1870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for bbits in bits:
1880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if bbits < abits:
1890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    continue
1900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                b = (1L << bbits) - 1
1910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x = a * b
1920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                y = ((1L << (abits + bbits)) -
1930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     (1L << abits) -
1940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     (1L << bbits) +
1950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     1)
1960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(x, y,
1970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))
1980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
1990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_bitop_identities_1(self, x):
2000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
2010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
2020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | 0, x, Frm("x | 0 != x for x=%r", x))
2030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x))
2040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & -1, x, Frm("x & -1 != x for x=%r", x))
2050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x))
2060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x))
2070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x, ~~x, Frm("x != ~~x for x=%r", x))
2080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & x, x, Frm("x & x != x for x=%r", x))
2090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | x, x, Frm("x | x != x for x=%r", x))
2100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x))
2110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x))
2120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x))
2130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x))
2140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x))
2150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x))
2160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for n in xrange(2*SHIFT):
2170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            p2 = 2L ** n
2180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(x << n >> n, x,
2190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("x << n >> n != x for x=%r, n=%r", (x, n)))
2200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(x // p2, x >> n,
2210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2)))
2220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(x * p2, x << n,
2230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2)))
2240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(x & -p2, x >> n << n,
2250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2)))
2260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            eq(x & -p2, x & ~(p2 - 1),
2270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2)))
2280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_bitop_identities_2(self, x, y):
2300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
2310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y)))
2320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y)))
2330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y)))
2340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y)))
2350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y)))
2360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y)))
2370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ y, (x | y) & ~(x & y),
2380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y)))
2390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ y, (x & ~y) | (~x & y),
2400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y)))
2410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x ^ y, (x | y) & (~x | ~y),
2420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y)))
2430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_bitop_identities_3(self, x, y, z):
2450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
2460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq((x & y) & z, x & (y & z),
2470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z)))
2480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq((x | y) | z, x | (y | z),
2490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z)))
2500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq((x ^ y) ^ z, x ^ (y ^ z),
2510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z)))
2520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x & (y | z), (x & y) | (x & z),
2530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z)))
2540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq(x | (y & z), (x | y) & (x | z),
2550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao             Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z)))
2560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_bitop_identities(self):
2580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in special:
2590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.check_bitop_identities_1(x)
2600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits = xrange(1, MAXDIGITS+1)
2610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for lenx in digits:
2620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = self.getran(lenx)
2630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.check_bitop_identities_1(x)
2640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for leny in digits:
2650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                y = self.getran(leny)
2660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.check_bitop_identities_2(x, y)
2670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))
2680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def slow_format(self, x, base):
2700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if (x, base) == (0, 8):
2710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # this is an oddball!
2720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            return "0L"
2730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits = []
2740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sign = 0
2750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if x < 0:
2760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            sign, x = 1, -x
2770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        while x:
2780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x, r = divmod(x, base)
2790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            digits.append(int(r))
2800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits.reverse()
2810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        digits = digits or [0]
2820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        return '-'[:sign] + \
2830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               {8: '0', 10: '', 16: '0x'}[base] + \
2840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               "".join(map(lambda i: "0123456789abcdef"[i], digits)) + "L"
2850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
2860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def check_format_1(self, x):
2870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for base, mapper in (8, oct), (10, repr), (16, hex):
2880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            got = mapper(x)
2890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected = self.slow_format(x, base)
2900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            msg = Frm("%s returned %r but expected %r for %r",
2910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                mapper.__name__, got, expected, x)
2920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(got, expected, msg)
2930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(got, 0), x, Frm('long("%s", 0) != %r', got, x))
2940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # str() has to be checked a little differently since there's no
2950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # trailing "L"
2960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        got = str(x)
2970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        expected = self.slow_format(x, 10)[:-1]
2980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        msg = Frm("%s returned %r but expected %r for %r",
2990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            mapper.__name__, got, expected, x)
3000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(got, expected, msg)
3010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_format(self):
3030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in special:
3040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.check_format_1(x)
3050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in xrange(10):
3060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for lenx in xrange(1, MAXDIGITS+1):
3070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                x = self.getran(lenx)
3080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.check_format_1(x)
3090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_long(self):
3110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(314), 314L)
3120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(3.14), 3L)
3130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(314L), 314L)
3140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check that long() of basic types actually returns a long
3150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(type(long(314)), long)
3160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(type(long(3.14)), long)
3170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(type(long(314L)), long)
3180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check that conversion from float truncates towards zero
3190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(-3.14), -3L)
3200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(3.9), 3L)
3210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(-3.9), -3L)
3220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(3.5), 3L)
3230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(-3.5), -3L)
3240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long("-3"), -3L)
3250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long("0b10", 2), 2L)
3260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long("0o10", 8), 8L)
3270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long("0x10", 16), 16L)
3280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if test_support.have_unicode:
3290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(unicode("-3")), -3L)
3300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Different base:
3310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long("10",16), 16L)
3320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if test_support.have_unicode:
3330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(unicode("10"),16), 16L)
3340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Check conversions from string (same test set as for int(), and then some)
3350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        LL = [
3360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ('1' + '0'*20, 10L**20),
3370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                ('1' + '0'*100, 10L**100)
3380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ]
3390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        L2 = L[:]
3400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if test_support.have_unicode:
3410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            L2 += [
3420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (unicode('1') + unicode('0')*20, 10L**20),
3430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                (unicode('1') + unicode('0')*100, 10L**100),
3440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        ]
3450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for s, v in L2 + LL:
3460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for sign in "", "+", "-":
3470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                for prefix in "", " ", "\t", "  \t\t  ":
3480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    ss = prefix + sign + s
3490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    vv = v
3500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if sign == "-" and v is not ValueError:
3510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        vv = -v
3520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    try:
3530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        self.assertEqual(long(ss), long(vv))
3540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    except v:
3550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        pass
3560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '123\0')
3580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '53', 40)
3590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, long, 1, 12)
3600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # tests with base 0
3620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(' 0123  ', 0), 83)
3630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(' 0123  ', 0), 83)
3640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('000', 0), 0)
3650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('0o123', 0), 83)
3660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('0x123', 0), 291)
3670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('0b100', 0), 4)
3680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(' 0O123   ', 0), 83)
3690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(' 0X123  ', 0), 291)
3700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(' 0B100 ', 0), 4)
3710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('0', 0), 0)
3720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('+0', 0), 0)
3730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('-0', 0), 0)
3740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('00', 0), 0)
3750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '08', 0)
3760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '-012395', 0)
3770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # SF patch #1638879: embedded NULs were not detected with
3790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # explicit base
3800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '123\0', 10)
3810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, '123\x00 245', 20)
3820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
3830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('100000000000000000000000000000000', 2),
3840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         4294967296)
3850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('102002022201221111211', 3), 4294967296)
3860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('10000000000000000', 4), 4294967296)
3870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('32244002423141', 5), 4294967296)
3880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1550104015504', 6), 4294967296)
3890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('211301422354', 7), 4294967296)
3900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('40000000000', 8), 4294967296)
3910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('12068657454', 9), 4294967296)
3920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4294967296', 10), 4294967296)
3930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1904440554', 11), 4294967296)
3940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('9ba461594', 12), 4294967296)
3950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('535a79889', 13), 4294967296)
3960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2ca5b7464', 14), 4294967296)
3970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1a20dcd81', 15), 4294967296)
3980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('100000000', 16), 4294967296)
3990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('a7ffda91', 17), 4294967296)
4000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('704he7g4', 18), 4294967296)
4010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4f5aff66', 19), 4294967296)
4020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('3723ai4g', 20), 4294967296)
4030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('281d55i4', 21), 4294967296)
4040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1fj8b184', 22), 4294967296)
4050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1606k7ic', 23), 4294967296)
4060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('mb994ag', 24), 4294967296)
4070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('hek2mgl', 25), 4294967296)
4080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('dnchbnm', 26), 4294967296)
4090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('b28jpdm', 27), 4294967296)
4100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('8pfgih4', 28), 4294967296)
4110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('76beigg', 29), 4294967296)
4120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('5qmcpqg', 30), 4294967296)
4130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4q0jto4', 31), 4294967296)
4140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4000000', 32), 4294967296)
4150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('3aokq94', 33), 4294967296)
4160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2qhxjli', 34), 4294967296)
4170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2br45qb', 35), 4294967296)
4180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1z141z4', 36), 4294967296)
4190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('100000000000000000000000000000001', 2),
4210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         4294967297)
4220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('102002022201221111212', 3), 4294967297)
4230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('10000000000000001', 4), 4294967297)
4240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('32244002423142', 5), 4294967297)
4250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1550104015505', 6), 4294967297)
4260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('211301422355', 7), 4294967297)
4270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('40000000001', 8), 4294967297)
4280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('12068657455', 9), 4294967297)
4290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4294967297', 10), 4294967297)
4300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1904440555', 11), 4294967297)
4310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('9ba461595', 12), 4294967297)
4320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('535a7988a', 13), 4294967297)
4330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2ca5b7465', 14), 4294967297)
4340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1a20dcd82', 15), 4294967297)
4350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('100000001', 16), 4294967297)
4360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('a7ffda92', 17), 4294967297)
4370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('704he7g5', 18), 4294967297)
4380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4f5aff67', 19), 4294967297)
4390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('3723ai4h', 20), 4294967297)
4400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('281d55i5', 21), 4294967297)
4410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1fj8b185', 22), 4294967297)
4420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1606k7id', 23), 4294967297)
4430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('mb994ah', 24), 4294967297)
4440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('hek2mgm', 25), 4294967297)
4450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('dnchbnn', 26), 4294967297)
4460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('b28jpdn', 27), 4294967297)
4470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('8pfgih5', 28), 4294967297)
4480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('76beigh', 29), 4294967297)
4490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('5qmcpqh', 30), 4294967297)
4500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4q0jto5', 31), 4294967297)
4510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('4000001', 32), 4294967297)
4520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('3aokq95', 33), 4294967297)
4530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2qhxjlj', 34), 4294967297)
4540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('2br45qc', 35), 4294967297)
4550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long('1z141z5', 36), 4294967297)
4560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_conversion(self):
4590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Test __long__()
4600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class ClassicMissingMethods:
4610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
4620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(AttributeError, long, ClassicMissingMethods())
4630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class MissingMethods(object):
4650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
4660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, long, MissingMethods())
4670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo0:
4690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 42L
4710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo1(object):
4730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 42L
4750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo2(long):
4770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 42L
4790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo3(long):
4810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return self
4830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo4(long):
4850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 42
4870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Foo5(long):
4890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
4900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 42.
4910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(Foo0()), 42L)
4930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(Foo1()), 42L)
4940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(Foo2()), 42L)
4950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(Foo3()), 0)
4960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(long(Foo4()), 42)
4970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(TypeError, long, Foo5())
4980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
4990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Classic:
5000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
5010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for base in (object, Classic):
5020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class LongOverridesTrunc(base):
5030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                def __long__(self):
5040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return 42
5050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                def __trunc__(self):
5060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return -12
5070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(LongOverridesTrunc()), 42)
5080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            class JustTrunc(base):
5100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                def __trunc__(self):
5110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    return 42
5120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(JustTrunc()), 42)
5130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for trunc_result_base in (object, Classic):
5150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                class Integral(trunc_result_base):
5160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    def __int__(self):
5170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return 42
5180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                class TruncReturnsNonLong(base):
5200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    def __trunc__(self):
5210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return Integral()
5220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(long(TruncReturnsNonLong()), 42)
5230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                class NonIntegral(trunc_result_base):
5250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    def __trunc__(self):
5260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        # Check that we avoid infinite recursion.
5270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return NonIntegral()
5280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                class TruncReturnsNonIntegral(base):
5300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    def __trunc__(self):
5310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        return NonIntegral()
5320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                try:
5330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    long(TruncReturnsNonIntegral())
5340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                except TypeError as e:
5350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.assertEqual(str(e),
5360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     "__trunc__ returned non-Integral"
5370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                     " (type NonIntegral)")
5380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
5390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.fail("Failed to raise TypeError with %s" %
5400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                              ((base, trunc_result_base),))
5410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_misc(self):
5430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check the extremes in int<->long conversion
5450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hugepos = sys.maxint
5460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hugeneg = -hugepos - 1
5470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hugepos_aslong = long(hugepos)
5480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        hugeneg_aslong = long(hugeneg)
5490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint")
5500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(hugeneg, hugeneg_aslong,
5510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "long(-sys.maxint-1) != -sys.maxint-1")
5520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # long -> int should not fail for hugepos_aslong or hugeneg_aslong
5540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = int(hugepos_aslong)
5550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, hugepos,
5570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "converting sys.maxint to long and back to int fails")
5580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OverflowError:
5590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(sys.maxint)) overflowed!")
5600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(x, int):
5610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(sys.maxint)) should have returned int")
5620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = int(hugeneg_aslong)
5630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(x, hugeneg,
5650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                  "converting -sys.maxint-1 to long and back to int fails")
5660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OverflowError:
5670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(-sys.maxint-1)) overflowed!")
5680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        if not isinstance(x, int):
5690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(-sys.maxint-1)) should have returned int")
5700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # but long -> int should overflow for hugepos+1 and hugeneg-1
5710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = hugepos_aslong + 1
5720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            y = int(x)
5740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OverflowError:
5750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(sys.maxint) + 1) mustn't overflow")
5760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(y, long,
5770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "int(long(sys.maxint) + 1) should have returned long")
5780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = hugeneg_aslong - 1
5800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        try:
5810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            y = int(x)
5820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        except OverflowError:
5830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow")
5840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(y, long,
5850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao               "int(long(-sys.maxint-1) - 1) should have returned long")
5860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class long2(long):
5880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
5890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = long2(1L<<100)
5900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        y = int(x)
5910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertTrue(type(y) is long,
5920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            "overflowing int conversion must return long not long subtype")
5930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # long -> Py_ssize_t conversion
5950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class X(object):
5960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __getslice__(self, i, j):
5970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return i, j
5980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
5990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        with test_support.check_py3k_warnings():
6000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(X()[-5L:7L], (-5, 7))
6010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # use the clamping effect to test the smallest and largest longs
6020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # that fit a Py_ssize_t
6030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            slicemin, slicemax = X()[-2L**100:2L**100]
6040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax))
6050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_issue9869(self):
6070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # Issue 9869: Interpreter crash when initializing an instance
6080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # of a long subclass from an object whose __long__ method returns
6090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # a plain int.
6100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class BadLong(object):
6110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __long__(self):
6120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return 1000000
6130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class MyLong(long):
6150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            pass
6160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        x = MyLong(BadLong())
6180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertIsInstance(x, long)
6190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(x, 1000000)
6200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao# ----------------------------------- tests of auto int->long conversion
6230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_auto_overflow(self):
6250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
6260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        sqrt = int(math.sqrt(sys.maxint))
6270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        special.extend([sqrt-1, sqrt, sqrt+1])
6280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        special.extend([-i for i in special])
6290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        def checkit(*args):
6310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Heavy use of nested scopes here!
6320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(got, expected,
6330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Frm("for %r expected %r got %r", args, expected, got))
6340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in special:
6360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            longx = long(x)
6370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected = -longx
6390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            got = -x
6400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            checkit('-', x)
6410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for y in special:
6430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                longy = long(y)
6440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                expected = longx + longy
6460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                got = x + y
6470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                checkit(x, '+', y)
6480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                expected = longx - longy
6500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                got = x - y
6510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                checkit(x, '-', y)
6520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                expected = longx * longy
6540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                got = x * y
6550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                checkit(x, '*', y)
6560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if y:
6580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    with test_support.check_py3k_warnings():
6590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        expected = longx / longy
6600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        got = x / y
6610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    checkit(x, '/', y)
6620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    expected = longx // longy
6640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    got = x // y
6650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    checkit(x, '//', y)
6660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    expected = divmod(longx, longy)
6680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    got = divmod(longx, longy)
6690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    checkit(x, 'divmod', y)
6700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if abs(y) < 5 and not (x == 0 and y < 0):
6720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    expected = longx ** longy
6730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    got = x ** y
6740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    checkit(x, '**', y)
6750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    for z in special:
6770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        if z != 0 :
6780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            if y >= 0:
6790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                expected = pow(longx, longy, long(z))
6800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                got = pow(x, y, z)
6810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                checkit('pow', x, y, '%', z)
6820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                            else:
6830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                                self.assertRaises(TypeError, pow,longx, longy, long(z))
6840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
6860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         "test requires IEEE 754 doubles")
6870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_float_conversion(self):
6880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        import sys
6890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        DBL_MAX = sys.float_info.max
6900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        DBL_MAX_EXP = sys.float_info.max_exp
6910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        DBL_MANT_DIG = sys.float_info.mant_dig
6920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
6930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        exact_values = [0L, 1L, 2L,
6940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**53-3),
6950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**53-2),
6960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**53-1),
6970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**53),
6980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**53+2),
6990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**54-4),
7000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**54-2),
7010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**54),
7020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                         long(2**54+4)]
7030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in exact_values:
7040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(float(x)), x)
7050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(float(-x)), -x)
7060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # test round-half-even
7080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]:
7090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for p in xrange(15):
7100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(long(float(2L**p*(2**53+x))), 2L**p*(2**53+y))
7110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8),
7130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12),
7140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     (13, 12), (14, 16), (15, 16)]:
7150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for p in xrange(15):
7160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(long(float(2L**p*(2**54+x))), 2L**p*(2**54+y))
7170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # behaviour near extremes of floating-point range
7190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        long_dbl_max = long(DBL_MAX)
7200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        top_power = 2**DBL_MAX_EXP
7210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        halfway = (long_dbl_max + top_power)//2
7220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(float(long_dbl_max), DBL_MAX)
7230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(float(long_dbl_max+1), DBL_MAX)
7240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(float(halfway-1), DBL_MAX)
7250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, halfway)
7260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual(float(1-halfway), -DBL_MAX)
7270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, -halfway)
7280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, top_power-1)
7290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, top_power)
7300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, top_power+1)
7310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, 2*top_power-1)
7320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, 2*top_power)
7330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, float, top_power*top_power)
7340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for p in xrange(100):
7360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = long(2**p * (2**53 + 1) + 1)
7370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            y = long(2**p * (2**53+ 2))
7380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(float(x)), y)
7390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = long(2**p * (2**53 + 1))
7410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            y = long(2**p * 2**53)
7420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(long(float(x)), y)
7430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_float_overflow(self):
7450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in -2.0, -1.0, 0.0, 1.0, 2.0:
7460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(float(long(x)), x)
7470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        shuge = '12345' * 120
7490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        huge = 1L << 30000
7500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        mhuge = -huge
7510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math}
7520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for test in ["float(huge)", "float(mhuge)",
7530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "complex(huge)", "complex(mhuge)",
7540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "complex(huge, 1)", "complex(mhuge, 1)",
7550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "complex(1, huge)", "complex(1, mhuge)",
7560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
7570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
7580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
7590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
7600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
7610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
7620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "math.sin(huge)", "math.sin(mhuge)",
7630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
7640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                     "math.floor(huge)", "math.floor(mhuge)"]:
7650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(OverflowError, eval, test, namespace)
7670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # XXX Perhaps float(shuge) can raise OverflowError on some box?
7690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # The comparison should not.
7700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertNotEqual(float(shuge), int(shuge),
7710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                "float(shuge) should not equal int(shuge)")
7720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_logs(self):
7740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        LOG10E = math.log10(math.e)
7750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for exp in range(10) + [100, 1000, 10000]:
7770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            value = 10 ** exp
7780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            log10 = math.log10(value)
7790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertAlmostEqual(log10, exp)
7800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
7820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # exp/LOG10E
7830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            expected = exp / LOG10E
7840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            log = math.log(value)
7850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertAlmostEqual(log, expected)
7860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for bad in -(1L << 10000), -2L, 0L:
7880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(ValueError, math.log, bad)
7890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertRaises(ValueError, math.log10, bad)
7900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_mixed_compares(self):
7920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        eq = self.assertEqual
7930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
7940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # We're mostly concerned with that mixing floats and longs does the
7950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # right stuff, even when longs are too large to fit in a float.
7960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # The safest way to check the results is to use an entirely different
7970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # method, which we do here via a skeletal rational class (which
7980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # represents all Python ints, longs and floats exactly).
7990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        class Rat:
8000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __init__(self, value):
8010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if isinstance(value, (int, long)):
8020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.n = value
8030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.d = 1
8040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                elif isinstance(value, float):
8050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # Convert to exact rational equivalent.
8060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    f, e = math.frexp(abs(value))
8070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    assert f == 0 or 0.5 <= f < 1.0
8080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # |value| = f * 2**e exactly
8090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # Suck up CHUNK bits at a time; 28 is enough so that we suck
8110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # up all bits in 2 iterations for all known binary double-
8120a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # precision formats, and small enough to fit in an int.
8130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    CHUNK = 28
8140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    top = 0
8150a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # invariant: |value| = (top + f) * 2**e exactly
8160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    while f:
8170a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        f = math.ldexp(f, CHUNK)
8180a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        digit = int(f)
8190a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        assert digit >> CHUNK == 0
8200a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        top = (top << CHUNK) | digit
8210a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        f -= digit
8220a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        assert 0.0 <= f < 1.0
8230a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        e -= CHUNK
8240a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8250a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    # Now |value| = top * 2**e exactly.
8260a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if e >= 0:
8270a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        n = top << e
8280a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        d = 1
8290a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    else:
8300a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        n = top
8310a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        d = 1 << -e
8320a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    if value < 0:
8330a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        n = -n
8340a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.n = n
8350a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    self.d = d
8360a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    assert float(n) / float(d) == value
8370a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                else:
8380a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    raise TypeError("can't deal with %r" % value)
8390a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8400a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            def __cmp__(self, other):
8410a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                if not isinstance(other, Rat):
8420a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                    other = Rat(other)
8430a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                return cmp(self.n * other.d, self.d * other.n)
8440a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8450a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200]
8460a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 2**48 is an important boundary in the internals.  2**53 is an
8470a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # important boundary for IEEE double precision.
8480a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for t in 2.0**48, 2.0**50, 2.0**53:
8490a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0,
8500a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                          long(t-1), long(t), long(t+1)])
8510a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cases.extend([0, 1, 2, sys.maxint, float(sys.maxint)])
8520a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # 1L<<20000 should exceed all double formats.  long(1e200) is to
8530a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        # check that we get equality with 1e200 above.
8540a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        t = long(1e200)
8550a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cases.extend([0L, 1L, 2L, 1L << 20000, t-1, t, t+1])
8560a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        cases.extend([-x for x in cases])
8570a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in cases:
8580a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            Rx = Rat(x)
8590a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            for y in cases:
8600a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Ry = Rat(y)
8610a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                Rcmp = cmp(Rx, Ry)
8620a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                xycmp = cmp(x, y)
8630a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
8640a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
8650a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
8660a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp))
8670a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp))
8680a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
8690a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
8700a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8710a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_nan_inf(self):
8720a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, long, float('inf'))
8730a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(OverflowError, long, float('-inf'))
8740a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertRaises(ValueError, long, float('nan'))
8750a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8760a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    def test_bit_length(self):
8770a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        tiny = 1e-10
8780a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for x in xrange(-65000, 65000):
8790a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            x = long(x)
8800a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            k = x.bit_length()
8810a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Check equivalence with Python version
8820a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual(k, len(bin(x).lstrip('-0b')))
8830a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Behaviour as specified in the docs
8840a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if x != 0:
8850a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertTrue(2**(k-1) <= abs(x) < 2**k)
8860a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            else:
8870a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(k, 0)
8880a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
8890a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            if x != 0:
8900a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # When x is an exact power of 2, numeric errors can
8910a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # cause floor(log(x)/log(2)) to be one too small; for
8920a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # small x this can be fixed by adding a small quantity
8930a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                # to the quotient before taking the floor.
8940a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                self.assertEqual(k, 1 + math.floor(
8950a8c90248264a8b26970b4473770bcc3df8515fJosh Gao                        math.log(abs(x))/math.log(2) + tiny))
8960a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
8970a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((0L).bit_length(), 0)
8980a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((1L).bit_length(), 1)
8990a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((-1L).bit_length(), 1)
9000a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((2L).bit_length(), 2)
9010a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        self.assertEqual((-2L).bit_length(), 2)
9020a8c90248264a8b26970b4473770bcc3df8515fJosh Gao        for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]:
9030a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            a = 2L**i
9040a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((a-1).bit_length(), i)
9050a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((1-a).bit_length(), i)
9060a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((a).bit_length(), i+1)
9070a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((-a).bit_length(), i+1)
9080a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((a+1).bit_length(), i+1)
9090a8c90248264a8b26970b4473770bcc3df8515fJosh Gao            self.assertEqual((-a-1).bit_length(), i+1)
9100a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9110a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9120a8c90248264a8b26970b4473770bcc3df8515fJosh Gaodef test_main():
9130a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_support.run_unittest(LongTest)
9140a8c90248264a8b26970b4473770bcc3df8515fJosh Gao
9150a8c90248264a8b26970b4473770bcc3df8515fJosh Gaoif __name__ == "__main__":
9160a8c90248264a8b26970b4473770bcc3df8515fJosh Gao    test_main()
917