blob: 4602bfb0b18bea109d4fc73e9a041bfa4bc179d2 [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,
95 KARATSUBA_CUTOFF + 15)
Guido van Rossum4365cab1998-08-13 14:20:17 +000096 for lenx in digits:
97 x = getran(lenx)
98 for leny in digits:
99 y = getran(leny) or 1L
100 test_division_2(x, y)
101
102# -------------------------------------------------------------- ~ & | ^
103
104def test_bitop_identities_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000105 check(x & 0 == 0, "x & 0 != 0 for", x)
106 check(x | 0 == x, "x | 0 != x for", x)
107 check(x ^ 0 == x, "x ^ 0 != x for", x)
108 check(x & -1 == x, "x & -1 != x for", x)
109 check(x | -1 == -1, "x | -1 != -1 for", x)
110 check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000111 check(x == ~~x, "x != ~~x for", x)
112 check(x & x == x, "x & x != x for", x)
113 check(x | x == x, "x | x != x for", x)
114 check(x ^ x == 0, "x ^ x != 0 for", x)
115 check(x & ~x == 0, "x & ~x != 0 for", x)
116 check(x | ~x == -1, "x | ~x != -1 for", x)
117 check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000118 check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
119 for n in range(2*SHIFT):
120 p2 = 2L ** n
121 check(x << n >> n == x, "x << n >> n != x for", x, n)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000122 check(x // p2 == x >> n, "x // p2 != x >> n for x n p2", x, n, p2)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000123 check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
124 check(x & -p2 == x >> n << n == x & ~(p2 - 1),
125 "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
126 x, n, p2)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000127
128def test_bitop_identities_2(x, y):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000129 check(x & y == y & x, "x & y != y & x for", x, y)
130 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 ^ x == y, "x ^ y ^ x != y for", x, y)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000133 check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
134 check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
135 check(x ^ y == (x | y) & ~(x & y),
136 "x ^ y != (x | y) & ~(x & y) for", x, y)
137 check(x ^ y == (x & ~y) | (~x & y),
138 "x ^ y == (x & ~y) | (~x & y) for", x, y)
139 check(x ^ y == (x | y) & (~x | ~y),
140 "x ^ y == (x | y) & (~x | ~y) for", x, y)
141
142def test_bitop_identities_3(x, y, z):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000143 check((x & y) & z == x & (y & z),
144 "(x & y) & z != x & (y & z) for", x, y, z)
145 check((x | y) | z == x | (y | z),
146 "(x | y) | z != x | (y | z) for", x, y, z)
147 check((x ^ y) ^ z == x ^ (y ^ z),
148 "(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000149 check(x & (y | z) == (x & y) | (x & z),
150 "x & (y | z) != (x & y) | (x & z) for", x, y, z)
151 check(x | (y & z) == (x | y) & (x | z),
152 "x | (y & z) != (x | y) & (x | z) for", x, y, z)
153
154def test_bitop_identities(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000155 if verbose:
156 print "long bit-operation identities"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000157 for x in special:
158 test_bitop_identities_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000159 digits = range(1, maxdigits+1)
160 for lenx in digits:
161 x = getran(lenx)
162 test_bitop_identities_1(x)
163 for leny in digits:
164 y = getran(leny)
165 test_bitop_identities_2(x, y)
Guido van Rossum54e54c62001-09-04 19:14:14 +0000166 test_bitop_identities_3(x, y, getran((lenx + leny)//2))
Guido van Rossum4365cab1998-08-13 14:20:17 +0000167
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000168# ------------------------------------------------- hex oct repr str atol
Guido van Rossum4365cab1998-08-13 14:20:17 +0000169
170def slow_format(x, base):
171 if (x, base) == (0, 8):
172 # this is an oddball!
173 return "0L"
174 digits = []
175 sign = 0
176 if x < 0:
177 sign, x = 1, -x
Guido van Rossum4365cab1998-08-13 14:20:17 +0000178 while x:
179 x, r = divmod(x, base)
180 digits.append(int(r))
181 digits.reverse()
182 digits = digits or [0]
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000183 return '-'[:sign] + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000184 {8: '0', 10: '', 16: '0x'}[base] + \
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000185 join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000186 "L"
187
188def test_format_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000189 from string import atol
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000190 for base, mapper in (8, oct), (10, repr), (16, hex):
Guido van Rossum4365cab1998-08-13 14:20:17 +0000191 got = mapper(x)
192 expected = slow_format(x, base)
193 check(got == expected, mapper.__name__, "returned",
194 got, "but expected", expected, "for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000195 check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000196 # str() has to be checked a little differently since there's no
197 # trailing "L"
198 got = str(x)
199 expected = slow_format(x, 10)[:-1]
200 check(got == expected, mapper.__name__, "returned",
201 got, "but expected", expected, "for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000202
203def test_format(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000204 if verbose:
205 print "long str/hex/oct/atol"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000206 for x in special:
207 test_format_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000208 for i in range(10):
209 for lenx in range(1, maxdigits+1):
210 x = getran(lenx)
211 test_format_1(x)
212
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000213# ----------------------------------------------------------------- misc
214
215def test_misc(maxdigits=MAXDIGITS):
Tim Peters971e0692001-08-23 20:34:01 +0000216 if verbose:
217 print "long miscellaneous operations"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000218 import sys
219
220 # check the extremes in int<->long conversion
221 hugepos = sys.maxint
222 hugeneg = -hugepos - 1
223 hugepos_aslong = long(hugepos)
224 hugeneg_aslong = long(hugeneg)
225 check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
226 check(hugeneg == hugeneg_aslong,
227 "long(-sys.maxint-1) != -sys.maxint-1")
228
229 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
230 try:
231 check(int(hugepos_aslong) == hugepos,
232 "converting sys.maxint to long and back to int fails")
233 except OverflowError:
234 raise TestFailed, "int(long(sys.maxint)) overflowed!"
235 try:
236 check(int(hugeneg_aslong) == hugeneg,
237 "converting -sys.maxint-1 to long and back to int fails")
238 except OverflowError:
239 raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
240
241 # but long -> int should overflow for hugepos+1 and hugeneg-1
242 x = hugepos_aslong + 1
243 try:
244 int(x)
245 raise ValueError
246 except OverflowError:
247 pass
248 except:
249 raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
250
251 x = hugeneg_aslong - 1
252 try:
253 int(x)
254 raise ValueError
255 except OverflowError:
256 pass
257 except:
258 raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
259
Tim Peters26c7fa32001-08-23 22:56:21 +0000260# ----------------------------------- tests of auto int->long conversion
261
262def test_auto_overflow():
263 import math, sys
264
265 if verbose:
266 print "auto-convert int->long on overflow"
267
268 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
269 sqrt = int(math.sqrt(sys.maxint))
270 special.extend([sqrt-1, sqrt, sqrt+1])
271 special.extend([-i for i in special])
272
273 def checkit(*args):
274 # Heavy use of nested scopes here!
275 verify(got == expected, "for %r expected %r got %r" %
276 (args, expected, got))
277
278 for x in special:
279 longx = long(x)
280
281 expected = -longx
282 got = -x
283 checkit('-', x)
284
285 for y in special:
286 longy = long(y)
287
288 expected = longx + longy
289 got = x + y
290 checkit(x, '+', y)
291
292 expected = longx - longy
293 got = x - y
294 checkit(x, '-', y)
295
296 expected = longx * longy
297 got = x * y
298 checkit(x, '*', y)
299
300 if y:
Tim Peters0dad0f72001-09-04 19:48:01 +0000301 expected = longx / longy
302 got = x / y
Tim Peters26c7fa32001-08-23 22:56:21 +0000303 checkit(x, '/', y)
304
Tim Petersa3653092001-08-23 23:02:57 +0000305 expected = longx // longy
306 got = x // y
307 checkit(x, '//', y)
308
Tim Peters26c7fa32001-08-23 22:56:21 +0000309 expected = divmod(longx, longy)
310 got = divmod(longx, longy)
311 checkit(x, 'divmod', y)
312
313 if abs(y) < 5 and not (x == 0 and y < 0):
314 expected = longx ** longy
315 got = x ** y
316 checkit(x, '**', y)
317
318 for z in special:
Tim Peters32f453e2001-09-03 08:35:41 +0000319 if z != 0 :
320 if y >= 0:
321 expected = pow(longx, longy, long(z))
322 got = pow(x, y, z)
323 checkit('pow', x, y, '%', z)
324 else:
325 try:
326 pow(longx, longy, long(z))
327 except TypeError:
328 pass
329 else:
330 raise TestFailed("pow%r should have raised "
Neal Norwitz05c09d02002-04-01 19:01:39 +0000331 "TypeError" % ((longx, longy, long(z)),))
Tim Peters26c7fa32001-08-23 22:56:21 +0000332
Tim Peters9fffa3e2001-09-04 05:14:19 +0000333# ---------------------------------------- tests of long->float overflow
334
335def test_float_overflow():
336 import math
337
338 if verbose:
339 print "long->float overflow"
340
341 for x in -2.0, -1.0, 0.0, 1.0, 2.0:
342 verify(float(long(x)) == x)
343
344 huge = 1L << 30000
345 mhuge = -huge
346 namespace = {'huge': huge, 'mhuge': mhuge, 'math': math}
347 for test in ["float(huge)", "float(mhuge)",
348 "complex(huge)", "complex(mhuge)",
349 "complex(huge, 1)", "complex(mhuge, 1)",
350 "complex(1, huge)", "complex(1, mhuge)",
351 "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
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 "math.sin(huge)", "math.sin(mhuge)",
Tim Peters9fffa3e2001-09-04 05:14:19 +0000358 "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
Tim Peters9fffa3e2001-09-04 05:14:19 +0000359 "math.floor(huge)", "math.floor(mhuge)"]:
Tim Peters83e7ccc2001-09-04 06:37:28 +0000360
Tim Peters9fffa3e2001-09-04 05:14:19 +0000361 try:
362 eval(test, namespace)
363 except OverflowError:
364 pass
365 else:
366 raise TestFailed("expected OverflowError from %s" % test)
Tim Peters78526162001-09-05 00:53:45 +0000367
368# ---------------------------------------------- test huge log and log10
369
370def test_logs():
371 import math
372
373 if verbose:
374 print "log and log10"
375
376 LOG10E = math.log10(math.e)
377
378 for exp in range(10) + [100, 1000, 10000]:
379 value = 10 ** exp
380 log10 = math.log10(value)
381 verify(fcmp(log10, exp) == 0)
382
383 # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
384 # exp/LOG10E
385 expected = exp / LOG10E
386 log = math.log(value)
387 verify(fcmp(log, expected) == 0)
388
389 for bad in -(1L << 10000), -2L, 0L:
390 try:
391 math.log(bad)
392 raise TestFailed("expected ValueError from log(<= 0)")
393 except ValueError:
394 pass
395
396 try:
397 math.log10(bad)
398 raise TestFailed("expected ValueError from log10(<= 0)")
399 except ValueError:
400 pass
401
Guido van Rossum4365cab1998-08-13 14:20:17 +0000402# ---------------------------------------------------------------- do it
403
404test_division()
405test_bitop_identities()
406test_format()
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000407test_misc()
Tim Peters26c7fa32001-08-23 22:56:21 +0000408test_auto_overflow()
Tim Peters9fffa3e2001-09-04 05:14:19 +0000409test_float_overflow()
Tim Peters78526162001-09-05 00:53:45 +0000410test_logs()