blob: c38056da0f87b6b74193b3f3b07d6d72539b1899 [file] [log] [blame]
Walter Dörwalda0021592005-06-13 21:44:48 +00001import unittest
2from test import test_support
3
4import random
5
6# Used for lazy formatting of failure messages
7class Frm(object):
8 def __init__(self, format, *args):
9 self.format = format
10 self.args = args
11
12 def __str__(self):
13 return self.format % self.args
Guido van Rossum4365cab1998-08-13 14:20:17 +000014
15# SHIFT should match the value in longintrepr.h for best testing.
16SHIFT = 15
17BASE = 2 ** SHIFT
18MASK = BASE - 1
Tim Petersdaec9612004-08-30 23:18:23 +000019KARATSUBA_CUTOFF = 70 # from longobject.c
Guido van Rossum4365cab1998-08-13 14:20:17 +000020
21# Max number of base BASE digits to use in test cases. Doubling
Tim Peters28b0e2a2002-08-13 02:17:11 +000022# this will more than double the runtime.
23MAXDIGITS = 15
Guido van Rossum4365cab1998-08-13 14:20:17 +000024
Guido van Rossum4581a0c1998-10-02 01:19:48 +000025# build some special values
Guido van Rossume2a383d2007-01-15 16:59:06 +000026special = map(int, [0, 1, 2, BASE, BASE >> 1])
27special.append(0x5555555555555555)
28special.append(0xaaaaaaaaaaaaaaaa)
Guido van Rossum4581a0c1998-10-02 01:19:48 +000029# some solid strings of one bits
Guido van Rossume2a383d2007-01-15 16:59:06 +000030p2 = 4 # 0 and 1 already added
Guido van Rossum4581a0c1998-10-02 01:19:48 +000031for i in range(2*SHIFT):
32 special.append(p2 - 1)
33 p2 = p2 << 1
34del p2
35# add complements & negations
36special = special + map(lambda x: ~x, special) + \
37 map(lambda x: -x, special)
38
Guido van Rossum4365cab1998-08-13 14:20:17 +000039
Walter Dörwalda0021592005-06-13 21:44:48 +000040class LongTest(unittest.TestCase):
Guido van Rossum4365cab1998-08-13 14:20:17 +000041
Walter Dörwalda0021592005-06-13 21:44:48 +000042 # Get quasi-random long consisting of ndigits digits (in base BASE).
43 # quasi == the most-significant digit will not be 0, and the number
44 # is constructed to contain long strings of 0 and 1 bits. These are
45 # more likely than random bits to provoke digit-boundary errors.
46 # The sign of the number is also random.
Guido van Rossum4365cab1998-08-13 14:20:17 +000047
Walter Dörwalda0021592005-06-13 21:44:48 +000048 def getran(self, ndigits):
49 self.assert_(ndigits > 0)
50 nbits_hi = ndigits * SHIFT
51 nbits_lo = nbits_hi - SHIFT + 1
Guido van Rossume2a383d2007-01-15 16:59:06 +000052 answer = 0
Walter Dörwalda0021592005-06-13 21:44:48 +000053 nbits = 0
54 r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start
55 while nbits < nbits_lo:
56 bits = (r >> 1) + 1
57 bits = min(bits, nbits_hi - nbits)
58 self.assert_(1 <= bits <= SHIFT)
59 nbits = nbits + bits
60 answer = answer << bits
61 if r & 1:
62 answer = answer | ((1 << bits) - 1)
63 r = int(random.random() * (SHIFT * 2))
64 self.assert_(nbits_lo <= nbits <= nbits_hi)
65 if random.random() < 0.5:
66 answer = -answer
67 return answer
Guido van Rossum4581a0c1998-10-02 01:19:48 +000068
Walter Dörwalda0021592005-06-13 21:44:48 +000069 # Get random long consisting of ndigits random digits (relative to base
70 # BASE). The sign bit is also random.
Guido van Rossum4581a0c1998-10-02 01:19:48 +000071
Walter Dörwalda0021592005-06-13 21:44:48 +000072 def getran2(ndigits):
Guido van Rossume2a383d2007-01-15 16:59:06 +000073 answer = 0
Guido van Rossum805365e2007-05-07 22:24:25 +000074 for i in range(ndigits):
Walter Dörwalda0021592005-06-13 21:44:48 +000075 answer = (answer << SHIFT) | random.randint(0, MASK)
76 if random.random() < 0.5:
77 answer = -answer
78 return answer
Guido van Rossum4365cab1998-08-13 14:20:17 +000079
Walter Dörwalda0021592005-06-13 21:44:48 +000080 def check_division(self, x, y):
81 eq = self.assertEqual
82 q, r = divmod(x, y)
83 q2, r2 = x//y, x%y
84 pab, pba = x*y, y*x
85 eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y))
86 eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y))
87 eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y))
88 eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
89 if y > 0:
90 self.assert_(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
91 else:
92 self.assert_(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
Guido van Rossum4365cab1998-08-13 14:20:17 +000093
Walter Dörwalda0021592005-06-13 21:44:48 +000094 def test_division(self):
Guido van Rossum805365e2007-05-07 22:24:25 +000095 digits = list(range(1, MAXDIGITS+1)) + list(range(KARATSUBA_CUTOFF,
96 KARATSUBA_CUTOFF + 14))
Walter Dörwalda0021592005-06-13 21:44:48 +000097 digits.append(KARATSUBA_CUTOFF * 3)
98 for lenx in digits:
99 x = self.getran(lenx)
100 for leny in digits:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000101 y = self.getran(leny) or 1
Walter Dörwalda0021592005-06-13 21:44:48 +0000102 self.check_division(x, y)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000103
Walter Dörwalda0021592005-06-13 21:44:48 +0000104 def test_karatsuba(self):
Guido van Rossum805365e2007-05-07 22:24:25 +0000105 digits = list(range(1, 5)) + list(range(KARATSUBA_CUTOFF,
106 KARATSUBA_CUTOFF + 10))
Walter Dörwalda0021592005-06-13 21:44:48 +0000107 digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100])
Guido van Rossum4365cab1998-08-13 14:20:17 +0000108
Walter Dörwalda0021592005-06-13 21:44:48 +0000109 bits = [digit * SHIFT for digit in digits]
Guido van Rossum4365cab1998-08-13 14:20:17 +0000110
Walter Dörwalda0021592005-06-13 21:44:48 +0000111 # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) ==
112 # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check.
113 for abits in bits:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000114 a = (1 << abits) - 1
Walter Dörwalda0021592005-06-13 21:44:48 +0000115 for bbits in bits:
116 if bbits < abits:
117 continue
Guido van Rossume2a383d2007-01-15 16:59:06 +0000118 b = (1 << bbits) - 1
Walter Dörwalda0021592005-06-13 21:44:48 +0000119 x = a * b
Guido van Rossume2a383d2007-01-15 16:59:06 +0000120 y = ((1 << (abits + bbits)) -
121 (1 << abits) -
122 (1 << bbits) +
Walter Dörwalda0021592005-06-13 21:44:48 +0000123 1)
124 self.assertEqual(x, y,
125 Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))
Tim Peters7f270ba2002-08-13 21:06:55 +0000126
Walter Dörwalda0021592005-06-13 21:44:48 +0000127 def check_bitop_identities_1(self, x):
128 eq = self.assertEqual
129 eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
130 eq(x | 0, x, Frm("x | 0 != x for x=%r", x))
131 eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x))
132 eq(x & -1, x, Frm("x & -1 != x for x=%r", x))
133 eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x))
134 eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x))
135 eq(x, ~~x, Frm("x != ~~x for x=%r", x))
136 eq(x & x, x, Frm("x & x != x for x=%r", x))
137 eq(x | x, x, Frm("x | x != x for x=%r", x))
138 eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x))
139 eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x))
140 eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x))
141 eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x))
142 eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x))
143 eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x))
Guido van Rossum805365e2007-05-07 22:24:25 +0000144 for n in range(2*SHIFT):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000145 p2 = 2 ** n
Walter Dörwalda0021592005-06-13 21:44:48 +0000146 eq(x << n >> n, x,
147 Frm("x << n >> n != x for x=%r, n=%r", (x, n)))
148 eq(x // p2, x >> n,
149 Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2)))
150 eq(x * p2, x << n,
151 Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2)))
152 eq(x & -p2, x >> n << n,
153 Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2)))
154 eq(x & -p2, x & ~(p2 - 1),
155 Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2)))
Tim Peters7f270ba2002-08-13 21:06:55 +0000156
Walter Dörwalda0021592005-06-13 21:44:48 +0000157 def check_bitop_identities_2(self, x, y):
158 eq = self.assertEqual
159 eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y)))
160 eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y)))
161 eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y)))
162 eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y)))
163 eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y)))
164 eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y)))
165 eq(x ^ y, (x | y) & ~(x & y),
166 Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y)))
167 eq(x ^ y, (x & ~y) | (~x & y),
168 Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y)))
169 eq(x ^ y, (x | y) & (~x | ~y),
170 Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y)))
Tim Peters7f270ba2002-08-13 21:06:55 +0000171
Walter Dörwalda0021592005-06-13 21:44:48 +0000172 def check_bitop_identities_3(self, x, y, z):
173 eq = self.assertEqual
174 eq((x & y) & z, x & (y & z),
175 Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z)))
176 eq((x | y) | z, x | (y | z),
177 Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z)))
178 eq((x ^ y) ^ z, x ^ (y ^ z),
179 Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z)))
180 eq(x & (y | z), (x & y) | (x & z),
181 Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z)))
182 eq(x | (y & z), (x | y) & (x | z),
183 Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z)))
Tim Peters7f270ba2002-08-13 21:06:55 +0000184
Walter Dörwalda0021592005-06-13 21:44:48 +0000185 def test_bitop_identities(self):
186 for x in special:
187 self.check_bitop_identities_1(x)
Guido van Rossum805365e2007-05-07 22:24:25 +0000188 digits = range(1, MAXDIGITS+1)
Walter Dörwalda0021592005-06-13 21:44:48 +0000189 for lenx in digits:
190 x = self.getran(lenx)
191 self.check_bitop_identities_1(x)
192 for leny in digits:
193 y = self.getran(leny)
194 self.check_bitop_identities_2(x, y)
195 self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))
Guido van Rossum4365cab1998-08-13 14:20:17 +0000196
Walter Dörwalda0021592005-06-13 21:44:48 +0000197 def slow_format(self, x, base):
198 if (x, base) == (0, 8):
199 # this is an oddball!
Guido van Rossumd2dbecb2006-08-18 16:29:54 +0000200 return "0"
Walter Dörwalda0021592005-06-13 21:44:48 +0000201 digits = []
202 sign = 0
203 if x < 0:
204 sign, x = 1, -x
205 while x:
206 x, r = divmod(x, base)
207 digits.append(int(r))
208 digits.reverse()
209 digits = digits or [0]
210 return '-'[:sign] + \
211 {8: '0', 10: '', 16: '0x'}[base] + \
Guido van Rossumd2dbecb2006-08-18 16:29:54 +0000212 "".join(map(lambda i: "0123456789abcdef"[i], digits))
Guido van Rossum4365cab1998-08-13 14:20:17 +0000213
Walter Dörwalda0021592005-06-13 21:44:48 +0000214 def check_format_1(self, x):
215 for base, mapper in (8, oct), (10, repr), (16, hex):
216 got = mapper(x)
217 expected = self.slow_format(x, base)
218 msg = Frm("%s returned %r but expected %r for %r",
219 mapper.__name__, got, expected, x)
220 self.assertEqual(got, expected, msg)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000221 self.assertEqual(int(got, 0), x, Frm('long("%s", 0) != %r', got, x))
Walter Dörwalda0021592005-06-13 21:44:48 +0000222 # str() has to be checked a little differently since there's no
223 # trailing "L"
224 got = str(x)
Guido van Rossumd2dbecb2006-08-18 16:29:54 +0000225 expected = self.slow_format(x, 10)
Walter Dörwalda0021592005-06-13 21:44:48 +0000226 msg = Frm("%s returned %r but expected %r for %r",
227 mapper.__name__, got, expected, x)
228 self.assertEqual(got, expected, msg)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000229
Walter Dörwalda0021592005-06-13 21:44:48 +0000230 def test_format(self):
231 for x in special:
232 self.check_format_1(x)
Guido van Rossum805365e2007-05-07 22:24:25 +0000233 for i in range(10):
234 for lenx in range(1, MAXDIGITS+1):
Walter Dörwalda0021592005-06-13 21:44:48 +0000235 x = self.getran(lenx)
236 self.check_format_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000237
Walter Dörwalda0021592005-06-13 21:44:48 +0000238 def test_misc(self):
239 import sys
Guido van Rossum4365cab1998-08-13 14:20:17 +0000240
Walter Dörwalda0021592005-06-13 21:44:48 +0000241 # check the extremes in int<->long conversion
242 hugepos = sys.maxint
243 hugeneg = -hugepos - 1
Guido van Rossume2a383d2007-01-15 16:59:06 +0000244 hugepos_aslong = int(hugepos)
245 hugeneg_aslong = int(hugeneg)
Walter Dörwalda0021592005-06-13 21:44:48 +0000246 self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint")
247 self.assertEqual(hugeneg, hugeneg_aslong,
248 "long(-sys.maxint-1) != -sys.maxint-1")
Guido van Rossum4365cab1998-08-13 14:20:17 +0000249
Walter Dörwalda0021592005-06-13 21:44:48 +0000250 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
Thomas Wouters89f507f2006-12-13 04:49:30 +0000251 x = int(hugepos_aslong)
Walter Dörwalda0021592005-06-13 21:44:48 +0000252 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000253 self.assertEqual(x, hugepos,
Walter Dörwalda0021592005-06-13 21:44:48 +0000254 "converting sys.maxint to long and back to int fails")
255 except OverflowError:
256 self.fail("int(long(sys.maxint)) overflowed!")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000257 if not isinstance(x, int):
258 raise TestFailed("int(long(sys.maxint)) should have returned int")
259 x = int(hugeneg_aslong)
Walter Dörwalda0021592005-06-13 21:44:48 +0000260 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000261 self.assertEqual(x, hugeneg,
Walter Dörwalda0021592005-06-13 21:44:48 +0000262 "converting -sys.maxint-1 to long and back to int fails")
263 except OverflowError:
264 self.fail("int(long(-sys.maxint-1)) overflowed!")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000265 if not isinstance(x, int):
266 raise TestFailed("int(long(-sys.maxint-1)) should have "
267 "returned int")
Walter Dörwalda0021592005-06-13 21:44:48 +0000268 # but long -> int should overflow for hugepos+1 and hugeneg-1
269 x = hugepos_aslong + 1
270 try:
271 y = int(x)
272 except OverflowError:
273 self.fail("int(long(sys.maxint) + 1) mustn't overflow")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000274 self.assert_(isinstance(y, int),
Walter Dörwalda0021592005-06-13 21:44:48 +0000275 "int(long(sys.maxint) + 1) should have returned long")
Guido van Rossum4365cab1998-08-13 14:20:17 +0000276
Walter Dörwalda0021592005-06-13 21:44:48 +0000277 x = hugeneg_aslong - 1
278 try:
279 y = int(x)
280 except OverflowError:
281 self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow")
Guido van Rossume2a383d2007-01-15 16:59:06 +0000282 self.assert_(isinstance(y, int),
Walter Dörwalda0021592005-06-13 21:44:48 +0000283 "int(long(-sys.maxint-1) - 1) should have returned long")
Guido van Rossum4365cab1998-08-13 14:20:17 +0000284
Guido van Rossume2a383d2007-01-15 16:59:06 +0000285 class long2(int):
Walter Dörwalda0021592005-06-13 21:44:48 +0000286 pass
Guido van Rossume2a383d2007-01-15 16:59:06 +0000287 x = long2(1<<100)
Walter Dörwaldf1715402002-11-19 20:49:15 +0000288 y = int(x)
Guido van Rossume2a383d2007-01-15 16:59:06 +0000289 self.assert_(type(y) is int,
Walter Dörwalda0021592005-06-13 21:44:48 +0000290 "overflowing int conversion must return long not long subtype")
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000291
Thomas Wouters89f507f2006-12-13 04:49:30 +0000292 # long -> Py_ssize_t conversion
293 class X(object):
294 def __getslice__(self, i, j):
295 return i, j
296
Guido van Rossume2a383d2007-01-15 16:59:06 +0000297 self.assertEqual(X()[-5:7], (-5, 7))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000298 # use the clamping effect to test the smallest and largest longs
299 # that fit a Py_ssize_t
Guido van Rossume2a383d2007-01-15 16:59:06 +0000300 slicemin, slicemax = X()[-2**100:2**100]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000301 self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax))
302
Tim Peters26c7fa32001-08-23 22:56:21 +0000303# ----------------------------------- tests of auto int->long conversion
304
Walter Dörwalda0021592005-06-13 21:44:48 +0000305 def test_auto_overflow(self):
306 import math, sys
Tim Peters26c7fa32001-08-23 22:56:21 +0000307
Walter Dörwalda0021592005-06-13 21:44:48 +0000308 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
309 sqrt = int(math.sqrt(sys.maxint))
310 special.extend([sqrt-1, sqrt, sqrt+1])
311 special.extend([-i for i in special])
Tim Peters26c7fa32001-08-23 22:56:21 +0000312
Walter Dörwalda0021592005-06-13 21:44:48 +0000313 def checkit(*args):
314 # Heavy use of nested scopes here!
315 self.assertEqual(got, expected,
316 Frm("for %r expected %r got %r", args, expected, got))
Tim Peters26c7fa32001-08-23 22:56:21 +0000317
Walter Dörwalda0021592005-06-13 21:44:48 +0000318 for x in special:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000319 longx = int(x)
Tim Peters26c7fa32001-08-23 22:56:21 +0000320
Walter Dörwalda0021592005-06-13 21:44:48 +0000321 expected = -longx
322 got = -x
323 checkit('-', x)
Tim Peters26c7fa32001-08-23 22:56:21 +0000324
Walter Dörwalda0021592005-06-13 21:44:48 +0000325 for y in special:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000326 longy = int(y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000327
Walter Dörwalda0021592005-06-13 21:44:48 +0000328 expected = longx + longy
329 got = x + y
330 checkit(x, '+', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000331
Walter Dörwalda0021592005-06-13 21:44:48 +0000332 expected = longx - longy
333 got = x - y
334 checkit(x, '-', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000335
Walter Dörwalda0021592005-06-13 21:44:48 +0000336 expected = longx * longy
337 got = x * y
338 checkit(x, '*', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000339
Walter Dörwalda0021592005-06-13 21:44:48 +0000340 if y:
341 expected = longx / longy
342 got = x / y
343 checkit(x, '/', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000344
Walter Dörwalda0021592005-06-13 21:44:48 +0000345 expected = longx // longy
346 got = x // y
347 checkit(x, '//', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000348
Walter Dörwalda0021592005-06-13 21:44:48 +0000349 expected = divmod(longx, longy)
350 got = divmod(longx, longy)
351 checkit(x, 'divmod', y)
Tim Petersa3653092001-08-23 23:02:57 +0000352
Walter Dörwalda0021592005-06-13 21:44:48 +0000353 if abs(y) < 5 and not (x == 0 and y < 0):
354 expected = longx ** longy
355 got = x ** y
356 checkit(x, '**', y)
Tim Peters26c7fa32001-08-23 22:56:21 +0000357
Walter Dörwalda0021592005-06-13 21:44:48 +0000358 for z in special:
359 if z != 0 :
360 if y >= 0:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000361 expected = pow(longx, longy, int(z))
Walter Dörwalda0021592005-06-13 21:44:48 +0000362 got = pow(x, y, z)
363 checkit('pow', x, y, '%', z)
Tim Peters32f453e2001-09-03 08:35:41 +0000364 else:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000365 self.assertRaises(TypeError, pow,longx, longy, int(z))
Tim Peters26c7fa32001-08-23 22:56:21 +0000366
Walter Dörwalda0021592005-06-13 21:44:48 +0000367 def test_float_overflow(self):
368 import math
Tim Peters9fffa3e2001-09-04 05:14:19 +0000369
Walter Dörwalda0021592005-06-13 21:44:48 +0000370 for x in -2.0, -1.0, 0.0, 1.0, 2.0:
Guido van Rossume2a383d2007-01-15 16:59:06 +0000371 self.assertEqual(float(int(x)), x)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000372
Walter Dörwalda0021592005-06-13 21:44:48 +0000373 shuge = '12345' * 120
Guido van Rossume2a383d2007-01-15 16:59:06 +0000374 huge = 1 << 30000
Walter Dörwalda0021592005-06-13 21:44:48 +0000375 mhuge = -huge
376 namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math}
377 for test in ["float(huge)", "float(mhuge)",
378 "complex(huge)", "complex(mhuge)",
379 "complex(huge, 1)", "complex(mhuge, 1)",
380 "complex(1, huge)", "complex(1, mhuge)",
381 "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
382 "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
383 "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
384 "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
385 "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
386 "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
387 "math.sin(huge)", "math.sin(mhuge)",
388 "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
389 "math.floor(huge)", "math.floor(mhuge)"]:
Tim Peters9fffa3e2001-09-04 05:14:19 +0000390
Walter Dörwalda0021592005-06-13 21:44:48 +0000391 self.assertRaises(OverflowError, eval, test, namespace)
Tim Peters9fffa3e2001-09-04 05:14:19 +0000392
Walter Dörwalda0021592005-06-13 21:44:48 +0000393 # XXX Perhaps float(shuge) can raise OverflowError on some box?
394 # The comparison should not.
395 self.assertNotEqual(float(shuge), int(shuge),
396 "float(shuge) should not equal int(shuge)")
Tim Peters83e7ccc2001-09-04 06:37:28 +0000397
Walter Dörwalda0021592005-06-13 21:44:48 +0000398 def test_logs(self):
399 import math
Tim Peters78526162001-09-05 00:53:45 +0000400
Walter Dörwalda0021592005-06-13 21:44:48 +0000401 LOG10E = math.log10(math.e)
Tim Peters307fa782004-09-23 08:06:40 +0000402
Guido van Rossum805365e2007-05-07 22:24:25 +0000403 for exp in list(range(10)) + [100, 1000, 10000]:
Walter Dörwalda0021592005-06-13 21:44:48 +0000404 value = 10 ** exp
405 log10 = math.log10(value)
406 self.assertAlmostEqual(log10, exp)
Tim Peters78526162001-09-05 00:53:45 +0000407
Walter Dörwalda0021592005-06-13 21:44:48 +0000408 # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
409 # exp/LOG10E
410 expected = exp / LOG10E
411 log = math.log(value)
412 self.assertAlmostEqual(log, expected)
Tim Peters78526162001-09-05 00:53:45 +0000413
Guido van Rossume2a383d2007-01-15 16:59:06 +0000414 for bad in -(1 << 10000), -2, 0:
Walter Dörwalda0021592005-06-13 21:44:48 +0000415 self.assertRaises(ValueError, math.log, bad)
416 self.assertRaises(ValueError, math.log10, bad)
Tim Peters78526162001-09-05 00:53:45 +0000417
Walter Dörwalda0021592005-06-13 21:44:48 +0000418 def test_mixed_compares(self):
419 eq = self.assertEqual
420 import math
421 import sys
Tim Peters78526162001-09-05 00:53:45 +0000422
Walter Dörwalda0021592005-06-13 21:44:48 +0000423 # We're mostly concerned with that mixing floats and longs does the
424 # right stuff, even when longs are too large to fit in a float.
425 # The safest way to check the results is to use an entirely different
426 # method, which we do here via a skeletal rational class (which
427 # represents all Python ints, longs and floats exactly).
428 class Rat:
429 def __init__(self, value):
Walter Dörwaldaa97f042007-05-03 21:05:51 +0000430 if isinstance(value, int):
Walter Dörwalda0021592005-06-13 21:44:48 +0000431 self.n = value
432 self.d = 1
433 elif isinstance(value, float):
434 # Convert to exact rational equivalent.
435 f, e = math.frexp(abs(value))
436 assert f == 0 or 0.5 <= f < 1.0
437 # |value| = f * 2**e exactly
Tim Peters78526162001-09-05 00:53:45 +0000438
Walter Dörwalda0021592005-06-13 21:44:48 +0000439 # Suck up CHUNK bits at a time; 28 is enough so that we suck
440 # up all bits in 2 iterations for all known binary double-
441 # precision formats, and small enough to fit in an int.
442 CHUNK = 28
443 top = 0
444 # invariant: |value| = (top + f) * 2**e exactly
445 while f:
446 f = math.ldexp(f, CHUNK)
447 digit = int(f)
448 assert digit >> CHUNK == 0
449 top = (top << CHUNK) | digit
450 f -= digit
451 assert 0.0 <= f < 1.0
452 e -= CHUNK
Tim Peters78526162001-09-05 00:53:45 +0000453
Walter Dörwalda0021592005-06-13 21:44:48 +0000454 # Now |value| = top * 2**e exactly.
455 if e >= 0:
456 n = top << e
457 d = 1
458 else:
459 n = top
460 d = 1 << -e
461 if value < 0:
462 n = -n
463 self.n = n
464 self.d = d
465 assert float(n) / float(d) == value
Tim Peters307fa782004-09-23 08:06:40 +0000466 else:
Walter Dörwalda0021592005-06-13 21:44:48 +0000467 raise TypeError("can't deal with %r" % val)
Tim Peters307fa782004-09-23 08:06:40 +0000468
Walter Dörwalda0021592005-06-13 21:44:48 +0000469 def __cmp__(self, other):
470 if not isinstance(other, Rat):
471 other = Rat(other)
472 return cmp(self.n * other.d, self.d * other.n)
Tim Peters307fa782004-09-23 08:06:40 +0000473
Walter Dörwalda0021592005-06-13 21:44:48 +0000474 cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200]
475 # 2**48 is an important boundary in the internals. 2**53 is an
476 # important boundary for IEEE double precision.
477 for t in 2.0**48, 2.0**50, 2.0**53:
478 cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000479 int(t-1), int(t), int(t+1)])
Walter Dörwalda0021592005-06-13 21:44:48 +0000480 cases.extend([0, 1, 2, sys.maxint, float(sys.maxint)])
481 # 1L<<20000 should exceed all double formats. long(1e200) is to
482 # check that we get equality with 1e200 above.
Guido van Rossume2a383d2007-01-15 16:59:06 +0000483 t = int(1e200)
484 cases.extend([0, 1, 2, 1 << 20000, t-1, t, t+1])
Walter Dörwalda0021592005-06-13 21:44:48 +0000485 cases.extend([-x for x in cases])
486 for x in cases:
487 Rx = Rat(x)
488 for y in cases:
489 Ry = Rat(y)
490 Rcmp = cmp(Rx, Ry)
491 xycmp = cmp(x, y)
492 eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
493 eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
494 eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
495 eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp))
496 eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp))
497 eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
498 eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
Tim Peters307fa782004-09-23 08:06:40 +0000499
Walter Dörwalda0021592005-06-13 21:44:48 +0000500def test_main():
501 test_support.run_unittest(LongTest)
Tim Peters307fa782004-09-23 08:06:40 +0000502
Walter Dörwalda0021592005-06-13 21:44:48 +0000503if __name__ == "__main__":
504 test_main()