blob: f5416d3d87dcedc4002d504d7b8d653611bf70a1 [file] [log] [blame]
Barry Warsaw04f357c2002-07-23 19:04:11 +00001from test.test_support import verify, verbose, TestFailed, fcmp
Guido van Rossum4365cab1998-08-13 14:20:17 +00002from string import join
3from random import random, randint
4
5# SHIFT should match the value in longintrepr.h for best testing.
6SHIFT = 15
7BASE = 2 ** SHIFT
8MASK = BASE - 1
Tim Peters28b0e2a2002-08-13 02:17:11 +00009KARATSUBA_CUTOFF = 35 # from longobject.c
Guido van Rossum4365cab1998-08-13 14:20:17 +000010
11# Max number of base BASE digits to use in test cases. Doubling
Tim Peters28b0e2a2002-08-13 02:17:11 +000012# this will more than double the runtime.
13MAXDIGITS = 15
Guido van Rossum4365cab1998-08-13 14:20:17 +000014
Guido van Rossum4581a0c1998-10-02 01:19:48 +000015# build some special values
16special = map(long, [0, 1, 2, BASE, BASE >> 1])
17special.append(0x5555555555555555L)
18special.append(0xaaaaaaaaaaaaaaaaL)
19# some solid strings of one bits
20p2 = 4L # 0 and 1 already added
21for i in range(2*SHIFT):
22 special.append(p2 - 1)
23 p2 = p2 << 1
24del p2
25# add complements & negations
26special = special + map(lambda x: ~x, special) + \
27 map(lambda x: -x, special)
28
Guido van Rossum4365cab1998-08-13 14:20:17 +000029# ------------------------------------------------------------ utilities
30
31# Use check instead of assert so the test still does something
32# under -O.
33
34def check(ok, *args):
35 if not ok:
36 raise TestFailed, join(map(str, args), " ")
37
Guido van Rossum4581a0c1998-10-02 01:19:48 +000038# Get quasi-random long consisting of ndigits digits (in base BASE).
39# quasi == the most-significant digit will not be 0, and the number
40# is constructed to contain long strings of 0 and 1 bits. These are
41# more likely than random bits to provoke digit-boundary errors.
42# The sign of the number is also random.
43
44def getran(ndigits):
Marc-André Lemburg36619082001-01-17 19:11:13 +000045 verify(ndigits > 0)
Guido van Rossum4581a0c1998-10-02 01:19:48 +000046 nbits_hi = ndigits * SHIFT
47 nbits_lo = nbits_hi - SHIFT + 1
48 answer = 0L
49 nbits = 0
50 r = int(random() * (SHIFT * 2)) | 1 # force 1 bits to start
51 while nbits < nbits_lo:
52 bits = (r >> 1) + 1
53 bits = min(bits, nbits_hi - nbits)
Marc-André Lemburg36619082001-01-17 19:11:13 +000054 verify(1 <= bits <= SHIFT)
Guido van Rossum4581a0c1998-10-02 01:19:48 +000055 nbits = nbits + bits
56 answer = answer << bits
57 if r & 1:
58 answer = answer | ((1 << bits) - 1)
59 r = int(random() * (SHIFT * 2))
Marc-André Lemburg36619082001-01-17 19:11:13 +000060 verify(nbits_lo <= nbits <= nbits_hi)
Guido van Rossum4581a0c1998-10-02 01:19:48 +000061 if random() < 0.5:
62 answer = -answer
63 return answer
64
Guido van Rossum4365cab1998-08-13 14:20:17 +000065# Get random long consisting of ndigits random digits (relative to base
66# BASE). The sign bit is also random.
67
Guido van Rossum4581a0c1998-10-02 01:19:48 +000068def getran2(ndigits):
Guido van Rossum4365cab1998-08-13 14:20:17 +000069 answer = 0L
70 for i in range(ndigits):
71 answer = (answer << SHIFT) | randint(0, MASK)
72 if random() < 0.5:
73 answer = -answer
74 return answer
75
76# --------------------------------------------------------------- divmod
77
78def test_division_2(x, y):
79 q, r = divmod(x, y)
Guido van Rossum54e54c62001-09-04 19:14:14 +000080 q2, r2 = x//y, x%y
Guido van Rossum6e277cf2000-04-10 17:41:37 +000081 pab, pba = x*y, y*x
82 check(pab == pba, "multiplication does not commute for", x, y)
Guido van Rossum4365cab1998-08-13 14:20:17 +000083 check(q == q2, "divmod returns different quotient than / for", x, y)
84 check(r == r2, "divmod returns different mod than % for", x, y)
85 check(x == q*y + r, "x != q*y + r after divmod on", x, y)
86 if y > 0:
87 check(0 <= r < y, "bad mod from divmod on", x, y)
88 else:
89 check(y < r <= 0, "bad mod from divmod on", x, y)
90
91def test_division(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +000092 if verbose:
93 print "long / * % divmod"
Tim Peters28b0e2a2002-08-13 02:17:11 +000094 digits = range(1, maxdigits+1) + range(KARATSUBA_CUTOFF,
Tim Petersd0876b82002-08-13 02:24:25 +000095 KARATSUBA_CUTOFF + 14)
96 digits.append(KARATSUBA_CUTOFF * 3)
Guido van Rossum4365cab1998-08-13 14:20:17 +000097 for lenx in digits:
98 x = getran(lenx)
99 for leny in digits:
100 y = getran(leny) or 1L
101 test_division_2(x, y)
102
103# -------------------------------------------------------------- ~ & | ^
104
105def test_bitop_identities_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000106 check(x & 0 == 0, "x & 0 != 0 for", x)
107 check(x | 0 == x, "x | 0 != x for", x)
108 check(x ^ 0 == x, "x ^ 0 != x for", x)
109 check(x & -1 == x, "x & -1 != x for", x)
110 check(x | -1 == -1, "x | -1 != -1 for", x)
111 check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000112 check(x == ~~x, "x != ~~x for", x)
113 check(x & x == x, "x & x != x for", x)
114 check(x | x == x, "x | x != x for", x)
115 check(x ^ x == 0, "x ^ x != 0 for", x)
116 check(x & ~x == 0, "x & ~x != 0 for", x)
117 check(x | ~x == -1, "x | ~x != -1 for", x)
118 check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000119 check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
120 for n in range(2*SHIFT):
121 p2 = 2L ** n
122 check(x << n >> n == x, "x << n >> n != x for", x, n)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000123 check(x // p2 == x >> n, "x // p2 != x >> n for x n p2", x, n, p2)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000124 check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
125 check(x & -p2 == x >> n << n == x & ~(p2 - 1),
126 "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
127 x, n, p2)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000128
129def test_bitop_identities_2(x, y):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000130 check(x & y == y & x, "x & y != y & x for", x, y)
131 check(x | y == y | x, "x | y != y | x for", x, y)
132 check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y)
133 check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000134 check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
135 check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
136 check(x ^ y == (x | y) & ~(x & y),
137 "x ^ y != (x | y) & ~(x & y) for", x, y)
138 check(x ^ y == (x & ~y) | (~x & y),
139 "x ^ y == (x & ~y) | (~x & y) for", x, y)
140 check(x ^ y == (x | y) & (~x | ~y),
141 "x ^ y == (x | y) & (~x | ~y) for", x, y)
142
143def test_bitop_identities_3(x, y, z):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000144 check((x & y) & z == x & (y & z),
145 "(x & y) & z != x & (y & z) for", x, y, z)
146 check((x | y) | z == x | (y | z),
147 "(x | y) | z != x | (y | z) for", x, y, z)
148 check((x ^ y) ^ z == x ^ (y ^ z),
149 "(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000150 check(x & (y | z) == (x & y) | (x & z),
151 "x & (y | z) != (x & y) | (x & z) for", x, y, z)
152 check(x | (y & z) == (x | y) & (x | z),
153 "x | (y & z) != (x | y) & (x | z) for", x, y, z)
154
155def test_bitop_identities(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000156 if verbose:
157 print "long bit-operation identities"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000158 for x in special:
159 test_bitop_identities_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000160 digits = range(1, maxdigits+1)
161 for lenx in digits:
162 x = getran(lenx)
163 test_bitop_identities_1(x)
164 for leny in digits:
165 y = getran(leny)
166 test_bitop_identities_2(x, y)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000167 test_bitop_identities_3(x, y, getran((lenx + leny)//2))
Guido van Rossum4365cab1998-08-13 14:20:17 +0000168
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000169# ------------------------------------------------- hex oct repr str atol
Guido van Rossum4365cab1998-08-13 14:20:17 +0000170
171def slow_format(x, base):
172 if (x, base) == (0, 8):
173 # this is an oddball!
174 return "0L"
175 digits = []
176 sign = 0
177 if x < 0:
178 sign, x = 1, -x
Guido van Rossum4365cab1998-08-13 14:20:17 +0000179 while x:
180 x, r = divmod(x, base)
181 digits.append(int(r))
182 digits.reverse()
183 digits = digits or [0]
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000184 return '-'[:sign] + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000185 {8: '0', 10: '', 16: '0x'}[base] + \
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000186 join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000187 "L"
188
189def test_format_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000190 from string import atol
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000191 for base, mapper in (8, oct), (10, repr), (16, hex):
Guido van Rossum4365cab1998-08-13 14:20:17 +0000192 got = mapper(x)
193 expected = slow_format(x, base)
194 check(got == expected, mapper.__name__, "returned",
195 got, "but expected", expected, "for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000196 check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000197 # str() has to be checked a little differently since there's no
198 # trailing "L"
199 got = str(x)
200 expected = slow_format(x, 10)[:-1]
201 check(got == expected, mapper.__name__, "returned",
202 got, "but expected", expected, "for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000203
204def test_format(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000205 if verbose:
206 print "long str/hex/oct/atol"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000207 for x in special:
208 test_format_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000209 for i in range(10):
210 for lenx in range(1, maxdigits+1):
211 x = getran(lenx)
212 test_format_1(x)
213
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000214# ----------------------------------------------------------------- misc
215
216def test_misc(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000217 if verbose:
218 print "long miscellaneous operations"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000219 import sys
220
221 # check the extremes in int<->long conversion
222 hugepos = sys.maxint
223 hugeneg = -hugepos - 1
224 hugepos_aslong = long(hugepos)
225 hugeneg_aslong = long(hugeneg)
226 check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
227 check(hugeneg == hugeneg_aslong,
228 "long(-sys.maxint-1) != -sys.maxint-1")
229
230 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
231 try:
232 check(int(hugepos_aslong) == hugepos,
233 "converting sys.maxint to long and back to int fails")
234 except OverflowError:
235 raise TestFailed, "int(long(sys.maxint)) overflowed!"
236 try:
237 check(int(hugeneg_aslong) == hugeneg,
238 "converting -sys.maxint-1 to long and back to int fails")
239 except OverflowError:
240 raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
241
242 # but long -> int should overflow for hugepos+1 and hugeneg-1
243 x = hugepos_aslong + 1
244 try:
245 int(x)
246 raise ValueError
247 except OverflowError:
248 pass
249 except:
250 raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
251
252 x = hugeneg_aslong - 1
253 try:
254 int(x)
255 raise ValueError
256 except OverflowError:
257 pass
258 except:
259 raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
260
Tim Peters26c7fa32001-08-23 22:56:21 +0000261# ----------------------------------- tests of auto int->long conversion
262
263def test_auto_overflow():
264 import math, sys
265
266 if verbose:
267 print "auto-convert int->long on overflow"
268
269 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
270 sqrt = int(math.sqrt(sys.maxint))
271 special.extend([sqrt-1, sqrt, sqrt+1])
272 special.extend([-i for i in special])
273
274 def checkit(*args):
275 # Heavy use of nested scopes here!
276 verify(got == expected, "for %r expected %r got %r" %
277 (args, expected, got))
278
279 for x in special:
280 longx = long(x)
281
282 expected = -longx
283 got = -x
284 checkit('-', x)
285
286 for y in special:
287 longy = long(y)
288
289 expected = longx + longy
290 got = x + y
291 checkit(x, '+', y)
292
293 expected = longx - longy
294 got = x - y
295 checkit(x, '-', y)
296
297 expected = longx * longy
298 got = x * y
299 checkit(x, '*', y)
300
301 if y:
Tim Peters0dad0f72001-09-04 19:48:01 +0000302 expected = longx / longy
303 got = x / y
Tim Peters26c7fa32001-08-23 22:56:21 +0000304 checkit(x, '/', y)
305
Tim Petersa3653092001-08-23 23:02:57 +0000306 expected = longx // longy
307 got = x // y
308 checkit(x, '//', y)
309
Tim Peters26c7fa32001-08-23 22:56:21 +0000310 expected = divmod(longx, longy)
311 got = divmod(longx, longy)
312 checkit(x, 'divmod', y)
313
314 if abs(y) < 5 and not (x == 0 and y < 0):
315 expected = longx ** longy
316 got = x ** y
317 checkit(x, '**', y)
318
319 for z in special:
Tim Peters32f453e2001-09-03 08:35:41 +0000320 if z != 0 :
321 if y >= 0:
322 expected = pow(longx, longy, long(z))
323 got = pow(x, y, z)
324 checkit('pow', x, y, '%', z)
325 else:
326 try:
327 pow(longx, longy, long(z))
328 except TypeError:
329 pass
330 else:
331 raise TestFailed("pow%r should have raised "
Neal Norwitz05c09d02002-04-01 19:01:39 +0000332 "TypeError" % ((longx, longy, long(z)),))
Tim Peters26c7fa32001-08-23 22:56:21 +0000333
Tim Peters9fffa3e2001-09-04 05:14:19 +0000334# ---------------------------------------- tests of long->float overflow
335
336def test_float_overflow():
337 import math
338
339 if verbose:
340 print "long->float overflow"
341
342 for x in -2.0, -1.0, 0.0, 1.0, 2.0:
343 verify(float(long(x)) == x)
344
345 huge = 1L << 30000
346 mhuge = -huge
347 namespace = {'huge': huge, 'mhuge': mhuge, 'math': math}
348 for test in ["float(huge)", "float(mhuge)",
349 "complex(huge)", "complex(mhuge)",
350 "complex(huge, 1)", "complex(mhuge, 1)",
351 "complex(1, huge)", "complex(1, mhuge)",
352 "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
353 "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
354 "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
355 "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
356 "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
357 "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
358 "math.sin(huge)", "math.sin(mhuge)",
Tim Peters9fffa3e2001-09-04 05:14:19 +0000359 "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
Tim Peters9fffa3e2001-09-04 05:14:19 +0000360 "math.floor(huge)", "math.floor(mhuge)"]:
Tim Peters83e7ccc2001-09-04 06:37:28 +0000361
Tim Peters9fffa3e2001-09-04 05:14:19 +0000362 try:
363 eval(test, namespace)
364 except OverflowError:
365 pass
366 else:
367 raise TestFailed("expected OverflowError from %s" % test)
Tim Peters78526162001-09-05 00:53:45 +0000368
369# ---------------------------------------------- test huge log and log10
370
371def test_logs():
372 import math
373
374 if verbose:
375 print "log and log10"
376
377 LOG10E = math.log10(math.e)
378
379 for exp in range(10) + [100, 1000, 10000]:
380 value = 10 ** exp
381 log10 = math.log10(value)
382 verify(fcmp(log10, exp) == 0)
383
384 # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
385 # exp/LOG10E
386 expected = exp / LOG10E
387 log = math.log(value)
388 verify(fcmp(log, expected) == 0)
389
390 for bad in -(1L << 10000), -2L, 0L:
391 try:
392 math.log(bad)
393 raise TestFailed("expected ValueError from log(<= 0)")
394 except ValueError:
395 pass
396
397 try:
398 math.log10(bad)
399 raise TestFailed("expected ValueError from log10(<= 0)")
400 except ValueError:
401 pass
402
Guido van Rossum4365cab1998-08-13 14:20:17 +0000403# ---------------------------------------------------------------- do it
404
405test_division()
406test_bitop_identities()
407test_format()
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000408test_misc()
Tim Peters26c7fa32001-08-23 22:56:21 +0000409test_auto_overflow()
Tim Peters9fffa3e2001-09-04 05:14:19 +0000410test_float_overflow()
Tim Peters78526162001-09-05 00:53:45 +0000411test_logs()