annotate Lib/test/test_long.py @ 16413:8819867f14ef legacy-trunk

Patch derived from Trent's 101162: a Python/C API testing framework. STILL NEEDS UNIX BUILD CHANGES.
author Tim Peters <tim.peters@gmail.com>
date Fri, 02 Feb 2001 05:57:15 +0000
parents c1a8deb15f0e
children 10b44e3b3905
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15305
diff changeset
1 from test_support import verify, verbose, TestFailed
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 from string import join
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 from random import random, randint
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # SHIFT should match the value in longintrepr.h for best testing.
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 SHIFT = 15
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 BASE = 2 ** SHIFT
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 MASK = BASE - 1
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Max number of base BASE digits to use in test cases. Doubling
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # this will at least quadruple the runtime.
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 MAXDIGITS = 10
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
14 # build some special values
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
15 special = map(long, [0, 1, 2, BASE, BASE >> 1])
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
16 special.append(0x5555555555555555L)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
17 special.append(0xaaaaaaaaaaaaaaaaL)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
18 # some solid strings of one bits
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
19 p2 = 4L # 0 and 1 already added
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
20 for i in range(2*SHIFT):
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
21 special.append(p2 - 1)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
22 p2 = p2 << 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
23 del p2
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
24 # add complements & negations
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
25 special = special + map(lambda x: ~x, special) + \
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
26 map(lambda x: -x, special)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
27
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # ------------------------------------------------------------ utilities
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 # Use check instead of assert so the test still does something
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 # under -O.
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 def check(ok, *args):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 if not ok:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 raise TestFailed, join(map(str, args), " ")
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
37 # Get quasi-random long consisting of ndigits digits (in base BASE).
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
38 # quasi == the most-significant digit will not be 0, and the number
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
39 # is constructed to contain long strings of 0 and 1 bits. These are
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
40 # more likely than random bits to provoke digit-boundary errors.
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
41 # The sign of the number is also random.
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
42
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
43 def getran(ndigits):
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15305
diff changeset
44 verify(ndigits > 0)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
45 nbits_hi = ndigits * SHIFT
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
46 nbits_lo = nbits_hi - SHIFT + 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
47 answer = 0L
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
48 nbits = 0
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
49 r = int(random() * (SHIFT * 2)) | 1 # force 1 bits to start
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
50 while nbits < nbits_lo:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
51 bits = (r >> 1) + 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
52 bits = min(bits, nbits_hi - nbits)
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15305
diff changeset
53 verify(1 <= bits <= SHIFT)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
54 nbits = nbits + bits
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
55 answer = answer << bits
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
56 if r & 1:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
57 answer = answer | ((1 << bits) - 1)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
58 r = int(random() * (SHIFT * 2))
15957
c1a8deb15f0e This patch removes all uses of "assert" in the regression test suite
Marc-André Lemburg <mal@egenix.com>
parents: 15305
diff changeset
59 verify(nbits_lo <= nbits <= nbits_hi)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
60 if random() < 0.5:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
61 answer = -answer
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
62 return answer
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
63
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # Get random long consisting of ndigits random digits (relative to base
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 # BASE). The sign bit is also random.
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
66
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
67 def getran2(ndigits):
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 answer = 0L
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 for i in range(ndigits):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 answer = (answer << SHIFT) | randint(0, MASK)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 if random() < 0.5:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 answer = -answer
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 return answer
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
74
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 # --------------------------------------------------------------- divmod
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
76
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 def test_division_2(x, y):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 q, r = divmod(x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 q2, r2 = x/y, x%y
12342
aa3b02a038a3 Christian Tismer: added test to ensure that multiplication commutes.
Guido van Rossum <guido@python.org>
parents: 11627
diff changeset
80 pab, pba = x*y, y*x
aa3b02a038a3 Christian Tismer: added test to ensure that multiplication commutes.
Guido van Rossum <guido@python.org>
parents: 11627
diff changeset
81 check(pab == pba, "multiplication does not commute for", x, y)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 check(q == q2, "divmod returns different quotient than / for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 check(r == r2, "divmod returns different mod than % for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 check(x == q*y + r, "x != q*y + r after divmod on", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 if y > 0:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 check(0 <= r < y, "bad mod from divmod on", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 else:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 check(y < r <= 0, "bad mod from divmod on", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
89
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 def test_division(maxdigits=MAXDIGITS):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 print "long / * % divmod"
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 digits = range(1, maxdigits+1)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 for lenx in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 for leny in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 y = getran(leny) or 1L
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 test_division_2(x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
98
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 # -------------------------------------------------------------- ~ & | ^
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 def test_bitop_identities_1(x):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
102 check(x & 0 == 0, "x & 0 != 0 for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
103 check(x | 0 == x, "x | 0 != x for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
104 check(x ^ 0 == x, "x ^ 0 != x for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
105 check(x & -1 == x, "x & -1 != x for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
106 check(x | -1 == -1, "x | -1 != -1 for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
107 check(x ^ -1 == ~x, "x ^ -1 != ~x for", x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 check(x == ~~x, "x != ~~x for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 check(x & x == x, "x & x != x for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 check(x | x == x, "x | x != x for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 check(x ^ x == 0, "x ^ x != 0 for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 check(x & ~x == 0, "x & ~x != 0 for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 check(x | ~x == -1, "x | ~x != -1 for", x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
115 check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
116 for n in range(2*SHIFT):
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
117 p2 = 2L ** n
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
118 check(x << n >> n == x, "x << n >> n != x for", x, n)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
119 check(x / p2 == x >> n, "x / p2 != x >> n for x n p2", x, n, p2)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
120 check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
121 check(x & -p2 == x >> n << n == x & ~(p2 - 1),
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
122 "not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2",
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
123 x, n, p2)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
124
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 def test_bitop_identities_2(x, y):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
126 check(x & y == y & x, "x & y != y & x for", x, y)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
127 check(x | y == y | x, "x | y != y | x for", x, y)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
128 check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
129 check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
131 check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
132 check(x ^ y == (x | y) & ~(x & y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 "x ^ y != (x | y) & ~(x & y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 check(x ^ y == (x & ~y) | (~x & y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 "x ^ y == (x & ~y) | (~x & y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 check(x ^ y == (x | y) & (~x | ~y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 "x ^ y == (x | y) & (~x | ~y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
138
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 def test_bitop_identities_3(x, y, z):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
140 check((x & y) & z == x & (y & z),
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
141 "(x & y) & z != x & (y & z) for", x, y, z)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
142 check((x | y) | z == x | (y | z),
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
143 "(x | y) | z != x | (y | z) for", x, y, z)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
144 check((x ^ y) ^ z == x ^ (y ^ z),
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
145 "(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 check(x & (y | z) == (x & y) | (x & z),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 "x & (y | z) != (x & y) | (x & z) for", x, y, z)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 check(x | (y & z) == (x | y) & (x | z),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 "x | (y & z) != (x | y) & (x | z) for", x, y, z)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 def test_bitop_identities(maxdigits=MAXDIGITS):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 print "long bit-operation identities"
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
153 for x in special:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
154 test_bitop_identities_1(x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 digits = range(1, maxdigits+1)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 for lenx in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 test_bitop_identities_1(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 for leny in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 y = getran(leny)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 test_bitop_identities_2(x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 test_bitop_identities_3(x, y, getran((lenx + leny)/2))
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
163
11627
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
164 # ------------------------------------------------- hex oct repr str atol
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 def slow_format(x, base):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 if (x, base) == (0, 8):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 # this is an oddball!
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 return "0L"
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 digits = []
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 sign = 0
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 if x < 0:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 sign, x = 1, -x
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 while x:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 x, r = divmod(x, base)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 digits.append(int(r))
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 digits.reverse()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 digits = digits or [0]
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
179 return '-'[:sign] + \
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 {8: '0', 10: '', 16: '0x'}[base] + \
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
181 join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 "L"
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
183
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 def test_format_1(x):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
185 from string import atol
11627
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
186 for base, mapper in (8, oct), (10, repr), (16, hex):
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 got = mapper(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 expected = slow_format(x, base)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 check(got == expected, mapper.__name__, "returned",
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 got, "but expected", expected, "for", x)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
191 check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x)
11627
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
192 # str() has to be checked a little differently since there's no
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
193 # trailing "L"
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
194 got = str(x)
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
195 expected = slow_format(x, 10)[:-1]
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
196 check(got == expected, mapper.__name__, "returned",
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
197 got, "but expected", expected, "for", x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 def test_format(maxdigits=MAXDIGITS):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
200 print "long str/hex/oct/atol"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
201 for x in special:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
202 test_format_1(x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
203 for i in range(10):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 for lenx in range(1, maxdigits+1):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 test_format_1(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
208 # ----------------------------------------------------------------- misc
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
210 def test_misc(maxdigits=MAXDIGITS):
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
211 print "long miscellaneous operations"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
212 import sys
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
213
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
214 # check the extremes in int<->long conversion
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
215 hugepos = sys.maxint
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
216 hugeneg = -hugepos - 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
217 hugepos_aslong = long(hugepos)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
218 hugeneg_aslong = long(hugeneg)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
219 check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint")
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
220 check(hugeneg == hugeneg_aslong,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
221 "long(-sys.maxint-1) != -sys.maxint-1")
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
222
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
223 # long -> int should not fail for hugepos_aslong or hugeneg_aslong
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
224 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
225 check(int(hugepos_aslong) == hugepos,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
226 "converting sys.maxint to long and back to int fails")
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
227 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
228 raise TestFailed, "int(long(sys.maxint)) overflowed!"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
229 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
230 check(int(hugeneg_aslong) == hugeneg,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
231 "converting -sys.maxint-1 to long and back to int fails")
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
232 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
233 raise TestFailed, "int(long(-sys.maxint-1)) overflowed!"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
234
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
235 # but long -> int should overflow for hugepos+1 and hugeneg-1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
236 x = hugepos_aslong + 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
237 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
238 int(x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
239 raise ValueError
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
240 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
241 pass
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
242 except:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
243 raise TestFailed, "int(long(sys.maxint) + 1) didn't overflow"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
244
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
245 x = hugeneg_aslong - 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
246 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
247 int(x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
248 raise ValueError
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
249 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
250 pass
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
251 except:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
252 raise TestFailed, "int(long(-sys.maxint-1) - 1) didn't overflow"
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
253
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 # ---------------------------------------------------------------- do it
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
255
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 test_division()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 test_bitop_identities()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 test_format()
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
259 test_misc()