1edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport unittest
2edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport sys
3edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
4edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport random
5edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepimport math
6edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
7edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfrom test import test_int, test_support
8edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
9edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Used for lazy formatting of failure messages
10edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass Frm(object):
11edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __init__(self, format, *args):
12edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.format = format
13edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.args = args
14edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
15edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def __str__(self):
16edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return self.format % self.args
17edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
18edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# SHIFT should match the value in longintrepr.h for best testing.
19edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepSHIFT = sys.long_info.bits_per_digit
20edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepBASE = 2 ** SHIFT
21edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepMASK = BASE - 1
22edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepKARATSUBA_CUTOFF = 70   # from longobject.c
23edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
24edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# Max number of base BASE digits to use in test cases.  Doubling
25edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# this will more than double the runtime.
26edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepMAXDIGITS = 15
27edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
28edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# build some special values
29edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepspecial = map(long, [0, 1, 2, BASE, BASE >> 1])
30edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepspecial.append(0x5555555555555555L)
31edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepspecial.append(0xaaaaaaaaaaaaaaaaL)
32edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep#  some solid strings of one bits
33edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepp2 = 4L  # 0 and 1 already added
34edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepfor i in range(2*SHIFT):
35edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    special.append(p2 - 1)
36edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    p2 = p2 << 1
37edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdel p2
38edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# add complements & negations
39edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepspecial = special + map(lambda x: ~x, special) + \
40edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    map(lambda x: -x, special)
41edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
42edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander StoepL = [
43edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('0', 0),
44edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('1', 1),
45edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('9', 9),
46edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('10', 10),
47edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('99', 99),
48edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('100', 100),
49edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('314', 314),
50edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (' 314', 314),
51edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('314 ', 314),
52edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('  \t\t  314  \t\t  ', 314),
53edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (repr(sys.maxint), sys.maxint),
54edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('  1x', ValueError),
55edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('  1  ', 1),
56edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('  1\02  ', ValueError),
57edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('', ValueError),
58edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (' ', ValueError),
59edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ('  \t\t  ', ValueError)
60edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep]
61edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif test_support.have_unicode:
62edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    L += [
63edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('0'), 0),
64edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('1'), 1),
65edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('9'), 9),
66edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('10'), 10),
67edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('99'), 99),
68edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('100'), 100),
69edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('314'), 314),
70edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode(' 314'), 314),
71edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
72edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('  \t\t  314  \t\t  '), 314),
73edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('  1x'), ValueError),
74edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('  1  '), 1),
75edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('  1\02  '), ValueError),
76edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode(''), ValueError),
77edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode(' '), ValueError),
78edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unicode('  \t\t  '), ValueError),
79edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        (unichr(0x200), ValueError),
80edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep]
81edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
82edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepclass LongTest(test_int.IntLongCommonTests, unittest.TestCase):
83edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
84edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    ntype = long
85edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
86edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Get quasi-random long consisting of ndigits digits (in base BASE).
87edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # quasi == the most-significant digit will not be 0, and the number
88edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # is constructed to contain long strings of 0 and 1 bits.  These are
89edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # more likely than random bits to provoke digit-boundary errors.
90edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # The sign of the number is also random.
91edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
92edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def getran(self, ndigits):
93edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(ndigits > 0)
94edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nbits_hi = ndigits * SHIFT
95edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nbits_lo = nbits_hi - SHIFT + 1
96edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        answer = 0L
97edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        nbits = 0
98edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        r = int(random.random() * (SHIFT * 2)) | 1  # force 1 bits to start
99edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while nbits < nbits_lo:
100edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            bits = (r >> 1) + 1
101edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            bits = min(bits, nbits_hi - nbits)
102edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(1 <= bits <= SHIFT)
103edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            nbits = nbits + bits
104edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            answer = answer << bits
105edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if r & 1:
106edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                answer = answer | ((1 << bits) - 1)
107edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            r = int(random.random() * (SHIFT * 2))
108edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(nbits_lo <= nbits <= nbits_hi)
109edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if random.random() < 0.5:
110edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            answer = -answer
111edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return answer
112edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
113edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # Get random long consisting of ndigits random digits (relative to base
114edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    # BASE).  The sign bit is also random.
115edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
116edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def getran2(ndigits):
117edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        answer = 0L
118edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in xrange(ndigits):
119edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            answer = (answer << SHIFT) | random.randint(0, MASK)
120edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if random.random() < 0.5:
121edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            answer = -answer
122edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return answer
123edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
124edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def check_division(self, x, y):
125edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq = self.assertEqual
126edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        q, r = divmod(x, y)
127edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        q2, r2 = x//y, x%y
128edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        pab, pba = x*y, y*x
129edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y))
130edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y))
131edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y))
132edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
133edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if y > 0:
134edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
135edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        else:
136edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
137edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
138edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_division(self):
139edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits = range(1, MAXDIGITS+1) + range(KARATSUBA_CUTOFF,
140edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                               KARATSUBA_CUTOFF + 14)
141edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits.append(KARATSUBA_CUTOFF * 3)
142edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for lenx in digits:
143edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.getran(lenx)
144edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for leny in digits:
145edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = self.getran(leny) or 1L
146edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.check_division(x, y)
147edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
148edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # specific numbers chosen to exercise corner cases of the
149edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # current long division implementation
150edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
151edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 30-bit cases involving a quotient digit estimate of BASE+1
152edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(1231948412290879395966702881L,
153edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            1147341367131428698L)
154edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(815427756481275430342312021515587883L,
155edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       707270836069027745L)
156edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(627976073697012820849443363563599041L,
157edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       643588798496057020L)
158edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(1115141373653752303710932756325578065L,
159edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       1038556335171453937726882627L)
160edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 30-bit cases that require the post-subtraction correction step
161edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(922498905405436751940989320930368494L,
162edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       949985870686786135626943396L)
163edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(768235853328091167204009652174031844L,
164edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                       1091555541180371554426545266L)
165edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
166edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 15-bit cases involving a quotient digit estimate of BASE+1
167edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(20172188947443L, 615611397L)
168edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(1020908530270155025L, 950795710L)
169edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(128589565723112408L, 736393718L)
170edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(609919780285761575L, 18613274546784L)
171edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 15-bit cases that require the post-subtraction correction step
172edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(710031681576388032L, 26769404391308L)
173edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.check_division(1933622614268221L, 30212853348836L)
174edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
175edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
176edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
177edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_karatsuba(self):
178edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10)
179edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100])
180edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
181edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        bits = [digit * SHIFT for digit in digits]
182edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
183edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) ==
184edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check.
185edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for abits in bits:
186edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a = (1L << abits) - 1
187edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for bbits in bits:
188edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if bbits < abits:
189edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    continue
190edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                b = (1L << bbits) - 1
191edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = a * b
192edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = ((1L << (abits + bbits)) -
193edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     (1L << abits) -
194edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     (1L << bbits) +
195edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     1)
196edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(x, y,
197edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))
198edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
199edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def check_bitop_identities_1(self, x):
200edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq = self.assertEqual
201edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
202edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | 0, x, Frm("x | 0 != x for x=%r", x))
203edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x))
204edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & -1, x, Frm("x & -1 != x for x=%r", x))
205edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x))
206edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x))
207edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x, ~~x, Frm("x != ~~x for x=%r", x))
208edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & x, x, Frm("x & x != x for x=%r", x))
209edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | x, x, Frm("x | x != x for x=%r", x))
210edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x))
211edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x))
212edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x))
213edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x))
214edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x))
215edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x))
216edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for n in xrange(2*SHIFT):
217edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            p2 = 2L ** n
218edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eq(x << n >> n, x,
219edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("x << n >> n != x for x=%r, n=%r", (x, n)))
220edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eq(x // p2, x >> n,
221edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2)))
222edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eq(x * p2, x << n,
223edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2)))
224edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eq(x & -p2, x >> n << n,
225edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2)))
226edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            eq(x & -p2, x & ~(p2 - 1),
227edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2)))
228edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
229edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def check_bitop_identities_2(self, x, y):
230edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq = self.assertEqual
231edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y)))
232edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y)))
233edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y)))
234edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y)))
235edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y)))
236edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y)))
237edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ y, (x | y) & ~(x & y),
238edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y)))
239edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ y, (x & ~y) | (~x & y),
240edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y)))
241edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x ^ y, (x | y) & (~x | ~y),
242edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y)))
243edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
244edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def check_bitop_identities_3(self, x, y, z):
245edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq = self.assertEqual
246edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq((x & y) & z, x & (y & z),
247edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z)))
248edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq((x | y) | z, x | (y | z),
249edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z)))
250edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq((x ^ y) ^ z, x ^ (y ^ z),
251edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z)))
252edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x & (y | z), (x & y) | (x & z),
253edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z)))
254edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq(x | (y & z), (x | y) & (x | z),
255edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep             Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z)))
256edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
257edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_bitop_identities(self):
258edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in special:
259edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.check_bitop_identities_1(x)
260edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits = xrange(1, MAXDIGITS+1)
261edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for lenx in digits:
262edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = self.getran(lenx)
263edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.check_bitop_identities_1(x)
264edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for leny in digits:
265edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                y = self.getran(leny)
266edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.check_bitop_identities_2(x, y)
267edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))
268edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
269edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def slow_format(self, x, base):
270edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if (x, base) == (0, 8):
271edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # this is an oddball!
272edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            return "0L"
273edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits = []
274edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sign = 0
275edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if x < 0:
276edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            sign, x = 1, -x
277edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        while x:
278edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x, r = divmod(x, base)
279edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            digits.append(int(r))
280edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits.reverse()
281edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        digits = digits or [0]
282edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        return '-'[:sign] + \
283edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               {8: '0', 10: '', 16: '0x'}[base] + \
284edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               "".join(map(lambda i: "0123456789abcdef"[i], digits)) + "L"
285edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
286edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def check_format_1(self, x):
287edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for base, mapper in (8, oct), (10, repr), (16, hex):
288edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = mapper(x)
289edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            expected = self.slow_format(x, base)
290edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            msg = Frm("%s returned %r but expected %r for %r",
291edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                mapper.__name__, got, expected, x)
292edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(got, expected, msg)
293edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(got, 0), x, Frm('long("%s", 0) != %r', got, x))
294edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # str() has to be checked a little differently since there's no
295edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # trailing "L"
296edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        got = str(x)
297edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        expected = self.slow_format(x, 10)[:-1]
298edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        msg = Frm("%s returned %r but expected %r for %r",
299edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            mapper.__name__, got, expected, x)
300edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(got, expected, msg)
301edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
302edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_format(self):
303edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in special:
304edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.check_format_1(x)
305edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in xrange(10):
306edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for lenx in xrange(1, MAXDIGITS+1):
307edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                x = self.getran(lenx)
308edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.check_format_1(x)
309edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
310edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_long(self):
311edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(314), 314L)
312edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(3.14), 3L)
313edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(314L), 314L)
314edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check that long() of basic types actually returns a long
315edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(long(314)), long)
316edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(long(3.14)), long)
317edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(type(long(314L)), long)
318edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check that conversion from float truncates towards zero
319edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(-3.14), -3L)
320edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(3.9), 3L)
321edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(-3.9), -3L)
322edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(3.5), 3L)
323edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(-3.5), -3L)
324edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long("-3"), -3L)
325edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long("0b10", 2), 2L)
326edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long("0o10", 8), 8L)
327edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long("0x10", 16), 16L)
328edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.have_unicode:
329edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(unicode("-3")), -3L)
330edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Different base:
331edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long("10",16), 16L)
332edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.have_unicode:
333edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(unicode("10"),16), 16L)
334edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Check conversions from string (same test set as for int(), and then some)
335edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        LL = [
336edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                ('1' + '0'*20, 10L**20),
337edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                ('1' + '0'*100, 10L**100)
338edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ]
339edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        L2 = L[:]
340edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if test_support.have_unicode:
341edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            L2 += [
342edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                (unicode('1') + unicode('0')*20, 10L**20),
343edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                (unicode('1') + unicode('0')*100, 10L**100),
344edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        ]
345edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for s, v in L2 + LL:
346edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for sign in "", "+", "-":
347edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                for prefix in "", " ", "\t", "  \t\t  ":
348edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    ss = prefix + sign + s
349edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    vv = v
350edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if sign == "-" and v is not ValueError:
351edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        vv = -v
352edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    try:
353edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        self.assertEqual(long(ss), long(vv))
354edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    except v:
355edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        pass
356edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
357edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '123\0')
358edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '53', 40)
359edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, long, 1, 12)
360edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
361edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # tests with base 0
362edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(' 0123  ', 0), 83)
363edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(' 0123  ', 0), 83)
364edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('000', 0), 0)
365edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('0o123', 0), 83)
366edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('0x123', 0), 291)
367edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('0b100', 0), 4)
368edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(' 0O123   ', 0), 83)
369edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(' 0X123  ', 0), 291)
370edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(' 0B100 ', 0), 4)
371edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('0', 0), 0)
372edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('+0', 0), 0)
373edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('-0', 0), 0)
374edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('00', 0), 0)
375edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '08', 0)
376edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '-012395', 0)
377edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
378edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # SF patch #1638879: embedded NULs were not detected with
379edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # explicit base
380edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '123\0', 10)
381edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, '123\x00 245', 20)
382edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
383edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('100000000000000000000000000000000', 2),
384edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         4294967296)
385edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('102002022201221111211', 3), 4294967296)
386edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('10000000000000000', 4), 4294967296)
387edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('32244002423141', 5), 4294967296)
388edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1550104015504', 6), 4294967296)
389edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('211301422354', 7), 4294967296)
390edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('40000000000', 8), 4294967296)
391edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('12068657454', 9), 4294967296)
392edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4294967296', 10), 4294967296)
393edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1904440554', 11), 4294967296)
394edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('9ba461594', 12), 4294967296)
395edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('535a79889', 13), 4294967296)
396edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2ca5b7464', 14), 4294967296)
397edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1a20dcd81', 15), 4294967296)
398edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('100000000', 16), 4294967296)
399edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('a7ffda91', 17), 4294967296)
400edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('704he7g4', 18), 4294967296)
401edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4f5aff66', 19), 4294967296)
402edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('3723ai4g', 20), 4294967296)
403edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('281d55i4', 21), 4294967296)
404edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1fj8b184', 22), 4294967296)
405edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1606k7ic', 23), 4294967296)
406edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('mb994ag', 24), 4294967296)
407edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('hek2mgl', 25), 4294967296)
408edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('dnchbnm', 26), 4294967296)
409edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('b28jpdm', 27), 4294967296)
410edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('8pfgih4', 28), 4294967296)
411edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('76beigg', 29), 4294967296)
412edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('5qmcpqg', 30), 4294967296)
413edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4q0jto4', 31), 4294967296)
414edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4000000', 32), 4294967296)
415edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('3aokq94', 33), 4294967296)
416edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2qhxjli', 34), 4294967296)
417edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2br45qb', 35), 4294967296)
418edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1z141z4', 36), 4294967296)
419edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
420edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('100000000000000000000000000000001', 2),
421edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         4294967297)
422edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('102002022201221111212', 3), 4294967297)
423edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('10000000000000001', 4), 4294967297)
424edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('32244002423142', 5), 4294967297)
425edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1550104015505', 6), 4294967297)
426edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('211301422355', 7), 4294967297)
427edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('40000000001', 8), 4294967297)
428edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('12068657455', 9), 4294967297)
429edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4294967297', 10), 4294967297)
430edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1904440555', 11), 4294967297)
431edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('9ba461595', 12), 4294967297)
432edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('535a7988a', 13), 4294967297)
433edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2ca5b7465', 14), 4294967297)
434edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1a20dcd82', 15), 4294967297)
435edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('100000001', 16), 4294967297)
436edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('a7ffda92', 17), 4294967297)
437edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('704he7g5', 18), 4294967297)
438edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4f5aff67', 19), 4294967297)
439edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('3723ai4h', 20), 4294967297)
440edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('281d55i5', 21), 4294967297)
441edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1fj8b185', 22), 4294967297)
442edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1606k7id', 23), 4294967297)
443edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('mb994ah', 24), 4294967297)
444edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('hek2mgm', 25), 4294967297)
445edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('dnchbnn', 26), 4294967297)
446edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('b28jpdn', 27), 4294967297)
447edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('8pfgih5', 28), 4294967297)
448edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('76beigh', 29), 4294967297)
449edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('5qmcpqh', 30), 4294967297)
450edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4q0jto5', 31), 4294967297)
451edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('4000001', 32), 4294967297)
452edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('3aokq95', 33), 4294967297)
453edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2qhxjlj', 34), 4294967297)
454edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('2br45qc', 35), 4294967297)
455edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long('1z141z5', 36), 4294967297)
456edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
457edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
458edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_conversion(self):
459edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Test __long__()
460edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class ClassicMissingMethods:
461edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
462edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(AttributeError, long, ClassicMissingMethods())
463edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
464edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MissingMethods(object):
465edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
466edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, long, MissingMethods())
467edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
468edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo0:
469edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
470edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42L
471edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
472edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo1(object):
473edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
474edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42L
475edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
476edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo2(long):
477edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
478edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42L
479edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
480edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo3(long):
481edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
482edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return self
483edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
484edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo4(long):
485edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
486edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42
487edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
488edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Foo5(long):
489edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
490edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 42.
491edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
492edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(Foo0()), 42L)
493edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(Foo1()), 42L)
494edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(Foo2()), 42L)
495edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(Foo3()), 0)
496edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(long(Foo4()), 42)
497edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(TypeError, long, Foo5())
498edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
499edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Classic:
500edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
501edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for base in (object, Classic):
502edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class LongOverridesTrunc(base):
503edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __long__(self):
504edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return 42
505edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __trunc__(self):
506edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return -12
507edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(LongOverridesTrunc()), 42)
508edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
509edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            class JustTrunc(base):
510edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                def __trunc__(self):
511edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    return 42
512edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(JustTrunc()), 42)
513edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
514edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for trunc_result_base in (object, Classic):
515edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class Integral(trunc_result_base):
516edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def __int__(self):
517edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return 42
518edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
519edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class TruncReturnsNonLong(base):
520edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def __trunc__(self):
521edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return Integral()
522edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(long(TruncReturnsNonLong()), 42)
523edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
524edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class NonIntegral(trunc_result_base):
525edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def __trunc__(self):
526edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        # Check that we avoid infinite recursion.
527edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return NonIntegral()
528edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
529edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                class TruncReturnsNonIntegral(base):
530edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    def __trunc__(self):
531edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        return NonIntegral()
532edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                try:
533edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    long(TruncReturnsNonIntegral())
534edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                except TypeError as e:
535edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.assertEqual(str(e),
536edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                     "__trunc__ returned non-Integral"
537edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                     " (type NonIntegral)")
538edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
539edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.fail("Failed to raise TypeError with %s" %
540edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                              ((base, trunc_result_base),))
541edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
542edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_misc(self):
543edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
544edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check the extremes in int<->long conversion
545edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hugepos = sys.maxint
546edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hugeneg = -hugepos - 1
547edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hugepos_aslong = long(hugepos)
548edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        hugeneg_aslong = long(hugeneg)
549edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint")
550edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(hugeneg, hugeneg_aslong,
551edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            "long(-sys.maxint-1) != -sys.maxint-1")
552edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
553edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # long -> int should not fail for hugepos_aslong or hugeneg_aslong
554edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = int(hugepos_aslong)
555edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
556edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, hugepos,
557edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                  "converting sys.maxint to long and back to int fails")
558edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OverflowError:
559edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(sys.maxint)) overflowed!")
560edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not isinstance(x, int):
561edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(sys.maxint)) should have returned int")
562edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = int(hugeneg_aslong)
563edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
564edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(x, hugeneg,
565edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                  "converting -sys.maxint-1 to long and back to int fails")
566edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OverflowError:
567edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(-sys.maxint-1)) overflowed!")
568edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        if not isinstance(x, int):
569edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(-sys.maxint-1)) should have returned int")
570edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # but long -> int should overflow for hugepos+1 and hugeneg-1
571edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = hugepos_aslong + 1
572edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
573edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = int(x)
574edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OverflowError:
575edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(sys.maxint) + 1) mustn't overflow")
576edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(y, long,
577edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            "int(long(sys.maxint) + 1) should have returned long")
578edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
579edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = hugeneg_aslong - 1
580edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        try:
581edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = int(x)
582edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        except OverflowError:
583edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow")
584edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(y, long,
585edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep               "int(long(-sys.maxint-1) - 1) should have returned long")
586edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
587edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class long2(long):
588edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
589edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = long2(1L<<100)
590edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        y = int(x)
591edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertTrue(type(y) is long,
592edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            "overflowing int conversion must return long not long subtype")
593edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
594edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # long -> Py_ssize_t conversion
595edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class X(object):
596edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __getslice__(self, i, j):
597edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return i, j
598edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
599edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        with test_support.check_py3k_warnings():
600edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(X()[-5L:7L], (-5, 7))
601edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # use the clamping effect to test the smallest and largest longs
602edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # that fit a Py_ssize_t
603edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            slicemin, slicemax = X()[-2L**100:2L**100]
604edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax))
605edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
606edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_issue9869(self):
607edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # Issue 9869: Interpreter crash when initializing an instance
608edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # of a long subclass from an object whose __long__ method returns
609edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # a plain int.
610edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class BadLong(object):
611edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __long__(self):
612edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return 1000000
613edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
614edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class MyLong(long):
615edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            pass
616edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
617edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        x = MyLong(BadLong())
618edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertIsInstance(x, long)
619edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(x, 1000000)
620edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
621edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
622edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep# ----------------------------------- tests of auto int->long conversion
623edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
624edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_auto_overflow(self):
625edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
626edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        sqrt = int(math.sqrt(sys.maxint))
627edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        special.extend([sqrt-1, sqrt, sqrt+1])
628edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        special.extend([-i for i in special])
629edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
630edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        def checkit(*args):
631edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Heavy use of nested scopes here!
632edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(got, expected,
633edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Frm("for %r expected %r got %r", args, expected, got))
634edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
635edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in special:
636edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            longx = long(x)
637edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
638edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            expected = -longx
639edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            got = -x
640edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            checkit('-', x)
641edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
642edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for y in special:
643edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                longy = long(y)
644edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
645edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = longx + longy
646edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                got = x + y
647edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                checkit(x, '+', y)
648edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
649edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = longx - longy
650edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                got = x - y
651edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                checkit(x, '-', y)
652edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
653edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                expected = longx * longy
654edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                got = x * y
655edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                checkit(x, '*', y)
656edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
657edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if y:
658edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    with test_support.check_py3k_warnings():
659edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        expected = longx / longy
660edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        got = x / y
661edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    checkit(x, '/', y)
662edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
663edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    expected = longx // longy
664edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    got = x // y
665edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    checkit(x, '//', y)
666edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
667edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    expected = divmod(longx, longy)
668edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    got = divmod(longx, longy)
669edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    checkit(x, 'divmod', y)
670edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
671edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if abs(y) < 5 and not (x == 0 and y < 0):
672edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    expected = longx ** longy
673edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    got = x ** y
674edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    checkit(x, '**', y)
675edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
676edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    for z in special:
677edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        if z != 0 :
678edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            if y >= 0:
679edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                expected = pow(longx, longy, long(z))
680edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                got = pow(x, y, z)
681edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                checkit('pow', x, y, '%', z)
682edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                            else:
683edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                                self.assertRaises(TypeError, pow,longx, longy, long(z))
684edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
685edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"),
686edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         "test requires IEEE 754 doubles")
687edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_float_conversion(self):
688edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        import sys
689edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        DBL_MAX = sys.float_info.max
690edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        DBL_MAX_EXP = sys.float_info.max_exp
691edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        DBL_MANT_DIG = sys.float_info.mant_dig
692edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
693edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        exact_values = [0L, 1L, 2L,
694edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**53-3),
695edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**53-2),
696edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**53-1),
697edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**53),
698edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**53+2),
699edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**54-4),
700edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**54-2),
701edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**54),
702edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                         long(2**54+4)]
703edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in exact_values:
704edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(float(x)), x)
705edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(float(-x)), -x)
706edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
707edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # test round-half-even
708edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]:
709edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for p in xrange(15):
710edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(long(float(2L**p*(2**53+x))), 2L**p*(2**53+y))
711edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
712edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8),
713edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12),
714edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     (13, 12), (14, 16), (15, 16)]:
715edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for p in xrange(15):
716edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(long(float(2L**p*(2**54+x))), 2L**p*(2**54+y))
717edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
718edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # behaviour near extremes of floating-point range
719edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        long_dbl_max = long(DBL_MAX)
720edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        top_power = 2**DBL_MAX_EXP
721edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        halfway = (long_dbl_max + top_power)//2
722edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(long_dbl_max), DBL_MAX)
723edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(long_dbl_max+1), DBL_MAX)
724edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(halfway-1), DBL_MAX)
725edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, halfway)
726edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual(float(1-halfway), -DBL_MAX)
727edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, -halfway)
728edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, top_power-1)
729edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, top_power)
730edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, top_power+1)
731edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, 2*top_power-1)
732edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, 2*top_power)
733edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, float, top_power*top_power)
734edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
735edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for p in xrange(100):
736edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = long(2**p * (2**53 + 1) + 1)
737edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = long(2**p * (2**53+ 2))
738edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(float(x)), y)
739edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
740edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = long(2**p * (2**53 + 1))
741edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            y = long(2**p * 2**53)
742edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(long(float(x)), y)
743edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
744edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_float_overflow(self):
745edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in -2.0, -1.0, 0.0, 1.0, 2.0:
746edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(float(long(x)), x)
747edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
748edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        shuge = '12345' * 120
749edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        huge = 1L << 30000
750edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        mhuge = -huge
751edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math}
752edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for test in ["float(huge)", "float(mhuge)",
753edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "complex(huge)", "complex(mhuge)",
754edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "complex(huge, 1)", "complex(mhuge, 1)",
755edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "complex(1, huge)", "complex(1, mhuge)",
756edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
757edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
758edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
759edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
760edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
761edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
762edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "math.sin(huge)", "math.sin(mhuge)",
763edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
764edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                     "math.floor(huge)", "math.floor(mhuge)"]:
765edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
766edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(OverflowError, eval, test, namespace)
767edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
768edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # XXX Perhaps float(shuge) can raise OverflowError on some box?
769edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # The comparison should not.
770edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertNotEqual(float(shuge), int(shuge),
771edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                "float(shuge) should not equal int(shuge)")
772edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
773edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_logs(self):
774edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        LOG10E = math.log10(math.e)
775edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
776edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for exp in range(10) + [100, 1000, 10000]:
777edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            value = 10 ** exp
778edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            log10 = math.log10(value)
779edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertAlmostEqual(log10, exp)
780edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
781edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
782edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # exp/LOG10E
783edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            expected = exp / LOG10E
784edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            log = math.log(value)
785edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertAlmostEqual(log, expected)
786edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
787edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for bad in -(1L << 10000), -2L, 0L:
788edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(ValueError, math.log, bad)
789edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertRaises(ValueError, math.log10, bad)
790edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
791edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_mixed_compares(self):
792edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        eq = self.assertEqual
793edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
794edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # We're mostly concerned with that mixing floats and longs does the
795edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # right stuff, even when longs are too large to fit in a float.
796edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # The safest way to check the results is to use an entirely different
797edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # method, which we do here via a skeletal rational class (which
798edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # represents all Python ints, longs and floats exactly).
799edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        class Rat:
800edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __init__(self, value):
801edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if isinstance(value, (int, long)):
802edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.n = value
803edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.d = 1
804edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                elif isinstance(value, float):
805edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Convert to exact rational equivalent.
806edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    f, e = math.frexp(abs(value))
807edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    assert f == 0 or 0.5 <= f < 1.0
808edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # |value| = f * 2**e exactly
809edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
810edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Suck up CHUNK bits at a time; 28 is enough so that we suck
811edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # up all bits in 2 iterations for all known binary double-
812edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # precision formats, and small enough to fit in an int.
813edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    CHUNK = 28
814edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    top = 0
815edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # invariant: |value| = (top + f) * 2**e exactly
816edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    while f:
817edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        f = math.ldexp(f, CHUNK)
818edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        digit = int(f)
819edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        assert digit >> CHUNK == 0
820edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        top = (top << CHUNK) | digit
821edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        f -= digit
822edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        assert 0.0 <= f < 1.0
823edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        e -= CHUNK
824edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
825edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    # Now |value| = top * 2**e exactly.
826edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if e >= 0:
827edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        n = top << e
828edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        d = 1
829edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    else:
830edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        n = top
831edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        d = 1 << -e
832edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    if value < 0:
833edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        n = -n
834edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.n = n
835edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    self.d = d
836edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    assert float(n) / float(d) == value
837edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                else:
838edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    raise TypeError("can't deal with %r" % value)
839edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
840edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            def __cmp__(self, other):
841edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                if not isinstance(other, Rat):
842edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                    other = Rat(other)
843edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                return cmp(self.n * other.d, self.d * other.n)
844edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
845edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200]
846edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 2**48 is an important boundary in the internals.  2**53 is an
847edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # important boundary for IEEE double precision.
848edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for t in 2.0**48, 2.0**50, 2.0**53:
849edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0,
850edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                          long(t-1), long(t), long(t+1)])
851edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cases.extend([0, 1, 2, sys.maxint, float(sys.maxint)])
852edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # 1L<<20000 should exceed all double formats.  long(1e200) is to
853edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        # check that we get equality with 1e200 above.
854edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        t = long(1e200)
855edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cases.extend([0L, 1L, 2L, 1L << 20000, t-1, t, t+1])
856edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        cases.extend([-x for x in cases])
857edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in cases:
858edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            Rx = Rat(x)
859edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            for y in cases:
860edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Ry = Rat(y)
861edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                Rcmp = cmp(Rx, Ry)
862edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                xycmp = cmp(x, y)
863edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
864edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
865edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
866edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp))
867edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp))
868edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
869edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
870edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
871edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_nan_inf(self):
872edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, long, float('inf'))
873edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(OverflowError, long, float('-inf'))
874edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertRaises(ValueError, long, float('nan'))
875edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
876edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    def test_bit_length(self):
877edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        tiny = 1e-10
878edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for x in xrange(-65000, 65000):
879edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            x = long(x)
880edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            k = x.bit_length()
881edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Check equivalence with Python version
882edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual(k, len(bin(x).lstrip('-0b')))
883edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Behaviour as specified in the docs
884edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if x != 0:
885edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertTrue(2**(k-1) <= abs(x) < 2**k)
886edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            else:
887edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(k, 0)
888edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
889edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            if x != 0:
890edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # When x is an exact power of 2, numeric errors can
891edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # cause floor(log(x)/log(2)) to be one too small; for
892edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # small x this can be fixed by adding a small quantity
893edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                # to the quotient before taking the floor.
894edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                self.assertEqual(k, 1 + math.floor(
895edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep                        math.log(abs(x))/math.log(2) + tiny))
896edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
897edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((0L).bit_length(), 0)
898edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((1L).bit_length(), 1)
899edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((-1L).bit_length(), 1)
900edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((2L).bit_length(), 2)
901edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        self.assertEqual((-2L).bit_length(), 2)
902edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep        for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]:
903edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            a = 2L**i
904edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((a-1).bit_length(), i)
905edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((1-a).bit_length(), i)
906edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((a).bit_length(), i+1)
907edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((-a).bit_length(), i+1)
908edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((a+1).bit_length(), i+1)
909edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep            self.assertEqual((-a-1).bit_length(), i+1)
910edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
911edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
912edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepdef test_main():
913edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_support.run_unittest(LongTest)
914edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep
915edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoepif __name__ == "__main__":
916edbb763a2b63074cd468a5d33a17908b2cc0654Jeff Vander Stoep    test_main()
917