blob: 25abcc836584db34d0dd8b0c08692349b99c840a [file] [log] [blame]
Guido van Rossum4365cab1998-08-13 14:20:17 +00001from test_support import TestFailed, verbose
2from 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
9
10# Max number of base BASE digits to use in test cases. Doubling
11# this will at least quadruple the runtime.
12MAXDIGITS = 10
13
Guido van Rossum4581a0c1998-10-02 01:19:48 +000014# build some special values
15special = map(long, [0, 1, 2, BASE, BASE >> 1])
16special.append(0x5555555555555555L)
17special.append(0xaaaaaaaaaaaaaaaaL)
18# some solid strings of one bits
19p2 = 4L # 0 and 1 already added
20for i in range(2*SHIFT):
21 special.append(p2 - 1)
22 p2 = p2 << 1
23del p2
24# add complements & negations
25special = special + map(lambda x: ~x, special) + \
26 map(lambda x: -x, special)
27
Guido van Rossum4365cab1998-08-13 14:20:17 +000028# ------------------------------------------------------------ utilities
29
30# Use check instead of assert so the test still does something
31# under -O.
32
33def check(ok, *args):
34 if not ok:
35 raise TestFailed, join(map(str, args), " ")
36
Guido van Rossum4581a0c1998-10-02 01:19:48 +000037# Get quasi-random long consisting of ndigits digits (in base BASE).
38# quasi == the most-significant digit will not be 0, and the number
39# is constructed to contain long strings of 0 and 1 bits. These are
40# more likely than random bits to provoke digit-boundary errors.
41# The sign of the number is also random.
42
43def getran(ndigits):
44 assert ndigits > 0
45 nbits_hi = ndigits * SHIFT
46 nbits_lo = nbits_hi - SHIFT + 1
47 answer = 0L
48 nbits = 0
49 r = int(random() * (SHIFT * 2)) | 1 # force 1 bits to start
50 while nbits < nbits_lo:
51 bits = (r >> 1) + 1
52 bits = min(bits, nbits_hi - nbits)
53 assert 1 <= bits <= SHIFT
54 nbits = nbits + bits
55 answer = answer << bits
56 if r & 1:
57 answer = answer | ((1 << bits) - 1)
58 r = int(random() * (SHIFT * 2))
59 assert nbits_lo <= nbits <= nbits_hi
60 if random() < 0.5:
61 answer = -answer
62 return answer
63
Guido van Rossum4365cab1998-08-13 14:20:17 +000064# Get random long consisting of ndigits random digits (relative to base
65# BASE). The sign bit is also random.
66
Guido van Rossum4581a0c1998-10-02 01:19:48 +000067def getran2(ndigits):
Guido van Rossum4365cab1998-08-13 14:20:17 +000068 answer = 0L
69 for i in range(ndigits):
70 answer = (answer << SHIFT) | randint(0, MASK)
71 if random() < 0.5:
72 answer = -answer
73 return answer
74
75# --------------------------------------------------------------- divmod
76
77def test_division_2(x, y):
78 q, r = divmod(x, y)
79 q2, r2 = x/y, x%y
80 check(q == q2, "divmod returns different quotient than / for", x, y)
81 check(r == r2, "divmod returns different mod than % for", x, y)
82 check(x == q*y + r, "x != q*y + r after divmod on", x, y)
83 if y > 0:
84 check(0 <= r < y, "bad mod from divmod on", x, y)
85 else:
86 check(y < r <= 0, "bad mod from divmod on", x, y)
87
88def test_division(maxdigits=MAXDIGITS):
89 print "long / * % divmod"
90 digits = range(1, maxdigits+1)
91 for lenx in digits:
92 x = getran(lenx)
93 for leny in digits:
94 y = getran(leny) or 1L
95 test_division_2(x, y)
96
97# -------------------------------------------------------------- ~ & | ^
98
99def test_bitop_identities_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000100 check(x & 0 == 0, "x & 0 != 0 for", x)
101 check(x | 0 == x, "x | 0 != x for", x)
102 check(x ^ 0 == x, "x ^ 0 != x for", x)
103 check(x & -1 == x, "x & -1 != x for", x)
104 check(x | -1 == -1, "x | -1 != -1 for", x)
105 check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000106 check(x == ~~x, "x != ~~x for", x)
107 check(x & x == x, "x & x != x for", x)
108 check(x | x == x, "x | x != x for", x)
109 check(x ^ x == 0, "x ^ x != 0 for", x)
110 check(x & ~x == 0, "x & ~x != 0 for", x)
111 check(x | ~x == -1, "x | ~x != -1 for", x)
112 check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000113 check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
114 for n in range(2*SHIFT):
115 p2 = 2L ** n
116 check(x << n >> n == x, "x << n >> n != x for", x, n)
117 check(x / p2 == x >> n, "x / p2 != x >> n for x n p2", x, n, p2)
118 check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
119 check(x & -p2 == x >> n << n == x & ~(p2 - 1),
120 "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
121 x, n, p2)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000122
123def test_bitop_identities_2(x, y):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000124 check(x & y == y & x, "x & y != y & x for", x, y)
125 check(x | y == y | x, "x | y != y | x for", x, y)
126 check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y)
127 check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000128 check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
129 check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
130 check(x ^ y == (x | y) & ~(x & y),
131 "x ^ y != (x | y) & ~(x & y) for", x, y)
132 check(x ^ y == (x & ~y) | (~x & y),
133 "x ^ y == (x & ~y) | (~x & y) for", x, y)
134 check(x ^ y == (x | y) & (~x | ~y),
135 "x ^ y == (x | y) & (~x | ~y) for", x, y)
136
137def test_bitop_identities_3(x, y, z):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000138 check((x & y) & z == x & (y & z),
139 "(x & y) & z != x & (y & z) for", x, y, z)
140 check((x | y) | z == x | (y | z),
141 "(x | y) | z != x | (y | z) for", x, y, z)
142 check((x ^ y) ^ z == x ^ (y ^ z),
143 "(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000144 check(x & (y | z) == (x & y) | (x & z),
145 "x & (y | z) != (x & y) | (x & z) for", x, y, z)
146 check(x | (y & z) == (x | y) & (x | z),
147 "x | (y & z) != (x | y) & (x | z) for", x, y, z)
148
149def test_bitop_identities(maxdigits=MAXDIGITS):
150 print "long bit-operation identities"
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000151 for x in special:
152 test_bitop_identities_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000153 digits = range(1, maxdigits+1)
154 for lenx in digits:
155 x = getran(lenx)
156 test_bitop_identities_1(x)
157 for leny in digits:
158 y = getran(leny)
159 test_bitop_identities_2(x, y)
160 test_bitop_identities_3(x, y, getran((lenx + leny)/2))
161
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000162# ------------------------------------------------- hex oct repr str atol
Guido van Rossum4365cab1998-08-13 14:20:17 +0000163
164def slow_format(x, base):
165 if (x, base) == (0, 8):
166 # this is an oddball!
167 return "0L"
168 digits = []
169 sign = 0
170 if x < 0:
171 sign, x = 1, -x
Guido van Rossum4365cab1998-08-13 14:20:17 +0000172 while x:
173 x, r = divmod(x, base)
174 digits.append(int(r))
175 digits.reverse()
176 digits = digits or [0]
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000177 return '-'[:sign] + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000178 {8: '0', 10: '', 16: '0x'}[base] + \
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000179 join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
Guido van Rossum4365cab1998-08-13 14:20:17 +0000180 "L"
181
182def test_format_1(x):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000183 from string import atol
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000184 for base, mapper in (8, oct), (10, repr), (16, hex):
Guido van Rossum4365cab1998-08-13 14:20:17 +0000185 got = mapper(x)
186 expected = slow_format(x, base)
187 check(got == expected, mapper.__name__, "returned",
188 got, "but expected", expected, "for", x)
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000189 check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
Fred Drakedb1bd5c1999-12-23 15:36:42 +0000190 # str() has to be checked a little differently since there's no
191 # trailing "L"
192 got = str(x)
193 expected = slow_format(x, 10)[:-1]
194 check(got == expected, mapper.__name__, "returned",
195 got, "but expected", expected, "for", x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000196
197def test_format(maxdigits=MAXDIGITS):
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000198 print "long str/hex/oct/atol"
199 for x in special:
200 test_format_1(x)
Guido van Rossum4365cab1998-08-13 14:20:17 +0000201 for i in range(10):
202 for lenx in range(1, maxdigits+1):
203 x = getran(lenx)
204 test_format_1(x)
205
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000206# ----------------------------------------------------------------- misc
207
208def test_misc(maxdigits=MAXDIGITS):
209 print "long miscellaneous operations"
210 import sys
211
212 # check the extremes in int<->long conversion
213 hugepos = sys.maxint
214 hugeneg = -hugepos - 1
215 hugepos_aslong = long(hugepos)
216 hugeneg_aslong = long(hugeneg)
217 check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
218 check(hugeneg == hugeneg_aslong,
219 "long(-sys.maxint-1) != -sys.maxint-1")
220
221 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
222 try:
223 check(int(hugepos_aslong) == hugepos,
224 "converting sys.maxint to long and back to int fails")
225 except OverflowError:
226 raise TestFailed, "int(long(sys.maxint)) overflowed!"
227 try:
228 check(int(hugeneg_aslong) == hugeneg,
229 "converting -sys.maxint-1 to long and back to int fails")
230 except OverflowError:
231 raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
232
233 # but long -> int should overflow for hugepos+1 and hugeneg-1
234 x = hugepos_aslong + 1
235 try:
236 int(x)
237 raise ValueError
238 except OverflowError:
239 pass
240 except:
241 raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
242
243 x = hugeneg_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) - 1) didn't overflow"
251
Guido van Rossum4365cab1998-08-13 14:20:17 +0000252# ---------------------------------------------------------------- do it
253
254test_division()
255test_bitop_identities()
256test_format()
Guido van Rossum4581a0c1998-10-02 01:19:48 +0000257test_misc()
Guido van Rossum4365cab1998-08-13 14:20:17 +0000258