Merged release21-maint changes.
[python/dscho.git] / Lib / test / test_long.py
blob1059ef6d8cf353c4434a8c9e36da504cbc3f3a7f
1 from test_support import verify, verbose, TestFailed
2 from string import join
3 from random import random, randint
5 # SHIFT should match the value in longintrepr.h for best testing.
6 SHIFT = 15
7 BASE = 2 ** SHIFT
8 MASK = BASE - 1
10 # Max number of base BASE digits to use in test cases. Doubling
11 # this will at least quadruple the runtime.
12 MAXDIGITS = 10
14 # build some special values
15 special = map(long, [0, 1, 2, BASE, BASE >> 1])
16 special.append(0x5555555555555555L)
17 special.append(0xaaaaaaaaaaaaaaaaL)
18 # some solid strings of one bits
19 p2 = 4L # 0 and 1 already added
20 for i in range(2*SHIFT):
21 special.append(p2 - 1)
22 p2 = p2 << 1
23 del p2
24 # add complements & negations
25 special = special + map(lambda x: ~x, special) + \
26 map(lambda x: -x, special)
28 # ------------------------------------------------------------ utilities
30 # Use check instead of assert so the test still does something
31 # under -O.
33 def check(ok, *args):
34 if not ok:
35 raise TestFailed, join(map(str, args), " ")
37 # 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.
43 def getran(ndigits):
44 verify(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 verify(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 verify(nbits_lo <= nbits <= nbits_hi)
60 if random() < 0.5:
61 answer = -answer
62 return answer
64 # Get random long consisting of ndigits random digits (relative to base
65 # BASE). The sign bit is also random.
67 def getran2(ndigits):
68 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
75 # --------------------------------------------------------------- divmod
77 def test_division_2(x, y):
78 q, r = divmod(x, y)
79 q2, r2 = x/y, x%y
80 pab, pba = x*y, y*x
81 check(pab == pba, "multiplication does not commute for", x, y)
82 check(q == q2, "divmod returns different quotient than / for", x, y)
83 check(r == r2, "divmod returns different mod than % for", x, y)
84 check(x == q*y + r, "x != q*y + r after divmod on", x, y)
85 if y > 0:
86 check(0 <= r < y, "bad mod from divmod on", x, y)
87 else:
88 check(y < r <= 0, "bad mod from divmod on", x, y)
90 def test_division(maxdigits=MAXDIGITS):
91 print "long / * % divmod"
92 digits = range(1, maxdigits+1)
93 for lenx in digits:
94 x = getran(lenx)
95 for leny in digits:
96 y = getran(leny) or 1L
97 test_division_2(x, y)
99 # -------------------------------------------------------------- ~ & | ^
101 def test_bitop_identities_1(x):
102 check(x & 0 == 0, "x & 0 != 0 for", x)
103 check(x | 0 == x, "x | 0 != x for", x)
104 check(x ^ 0 == x, "x ^ 0 != x for", x)
105 check(x & -1 == x, "x & -1 != x for", x)
106 check(x | -1 == -1, "x | -1 != -1 for", x)
107 check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
108 check(x == ~~x, "x != ~~x for", x)
109 check(x & x == x, "x & x != x for", x)
110 check(x | x == x, "x | x != x for", x)
111 check(x ^ x == 0, "x ^ x != 0 for", x)
112 check(x & ~x == 0, "x & ~x != 0 for", x)
113 check(x | ~x == -1, "x | ~x != -1 for", x)
114 check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
115 check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
116 for n in range(2*SHIFT):
117 p2 = 2L ** n
118 check(x << n >> n == x, "x << n >> n != x for", x, n)
119 check(x / p2 == x >> n, "x / p2 != x >> n for x n p2", x, n, p2)
120 check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
121 check(x & -p2 == x >> n << n == x & ~(p2 - 1),
122 "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
123 x, n, p2)
125 def test_bitop_identities_2(x, y):
126 check(x & y == y & x, "x & y != y & x for", x, y)
127 check(x | y == y | x, "x | y != y | x for", x, y)
128 check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y)
129 check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y)
130 check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
131 check(x | y == ~(~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 check(x ^ y == (x | y) & (~x | ~y),
137 "x ^ y == (x | y) & (~x | ~y) for", x, y)
139 def test_bitop_identities_3(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)
144 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) | (x & z),
147 "x & (y | z) != (x & y) | (x & z) for", x, y, z)
148 check(x | (y & z) == (x | y) & (x | z),
149 "x | (y & z) != (x | y) & (x | z) for", x, y, z)
151 def test_bitop_identities(maxdigits=MAXDIGITS):
152 print "long bit-operation identities"
153 for x in special:
154 test_bitop_identities_1(x)
155 digits = range(1, maxdigits+1)
156 for lenx in digits:
157 x = getran(lenx)
158 test_bitop_identities_1(x)
159 for leny in digits:
160 y = getran(leny)
161 test_bitop_identities_2(x, y)
162 test_bitop_identities_3(x, y, getran((lenx + leny)/2))
164 # ------------------------------------------------- hex oct repr str atol
166 def slow_format(x, base):
167 if (x, base) == (0, 8):
168 # this is an oddball!
169 return "0L"
170 digits = []
171 sign = 0
172 if x < 0:
173 sign, x = 1, -x
174 while x:
175 x, r = divmod(x, base)
176 digits.append(int(r))
177 digits.reverse()
178 digits = digits or [0]
179 return '-'[:sign] + \
180 {8: '0', 10: '', 16: '0x'}[base] + \
181 join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
184 def test_format_1(x):
185 from string import atol
186 for base, mapper in (8, oct), (10, repr), (16, hex):
187 got = mapper(x)
188 expected = slow_format(x, base)
189 check(got == expected, mapper.__name__, "returned",
190 got, "but expected", expected, "for", x)
191 check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
192 # str() has to be checked a little differently since there's no
193 # trailing "L"
194 got = str(x)
195 expected = slow_format(x, 10)[:-1]
196 check(got == expected, mapper.__name__, "returned",
197 got, "but expected", expected, "for", x)
199 def test_format(maxdigits=MAXDIGITS):
200 print "long str/hex/oct/atol"
201 for x in special:
202 test_format_1(x)
203 for i in range(10):
204 for lenx in range(1, maxdigits+1):
205 x = getran(lenx)
206 test_format_1(x)
208 # ----------------------------------------------------------------- misc
210 def test_misc(maxdigits=MAXDIGITS):
211 print "long miscellaneous operations"
212 import sys
214 # check the extremes in int<->long conversion
215 hugepos = sys.maxint
216 hugeneg = -hugepos - 1
217 hugepos_aslong = long(hugepos)
218 hugeneg_aslong = long(hugeneg)
219 check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
220 check(hugeneg == hugeneg_aslong,
221 "long(-sys.maxint-1) != -sys.maxint-1")
223 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
224 try:
225 check(int(hugepos_aslong) == hugepos,
226 "converting sys.maxint to long and back to int fails")
227 except OverflowError:
228 raise TestFailed, "int(long(sys.maxint)) overflowed!"
229 try:
230 check(int(hugeneg_aslong) == hugeneg,
231 "converting -sys.maxint-1 to long and back to int fails")
232 except OverflowError:
233 raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
235 # but long -> int should overflow for hugepos+1 and hugeneg-1
236 x = hugepos_aslong + 1
237 try:
238 int(x)
239 raise ValueError
240 except OverflowError:
241 pass
242 except:
243 raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
245 x = hugeneg_aslong - 1
246 try:
247 int(x)
248 raise ValueError
249 except OverflowError:
250 pass
251 except:
252 raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
254 # ---------------------------------------------------------------- do it
256 test_division()
257 test_bitop_identities()
258 test_format()
259 test_misc()