annotate Lib/test/test_long.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents 9433dd3c197e
children 36f71fef5b10 e449c81c1c01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
1 from test_support import verify, verbose, TestFailed, fcmp
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)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19679
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):
19452
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
91 if verbose:
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
92 print "long / * % divmod"
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 digits = range(1, maxdigits+1)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 for lenx in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 for leny in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 y = getran(leny) or 1L
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 test_division_2(x, y)
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
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 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
103 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
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 ^ 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
106 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
107 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
108 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
109 check(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 == x, "x | x != x 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 == 0, "x & ~x != 0 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)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
115 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
116 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
117 for n in range(2*SHIFT):
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
118 p2 = 2L ** n
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
119 check(x << n >> n == x, "x << n >> n != x for", x, n)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19679
diff changeset
120 check(x // p2 == x >> n, "x // p2 != x >> n for x n p2", x, n, p2)
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
121 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
122 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
123 "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
124 x, n, p2)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
125
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 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
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 == 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
130 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
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 != ~(~x & ~y) for", x, y)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 check(x ^ y == (x | y) & ~(x & y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 "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
135 check(x ^ y == (x & ~y) | (~x & y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 "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
137 check(x ^ y == (x | y) & (~x | ~y),
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 "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
139
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 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
141 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
142 "(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
143 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
144 "(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
145 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
146 "(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
147 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
148 "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
149 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
150 "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
151
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
152 def test_bitop_identities(maxdigits=MAXDIGITS):
19452
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
153 if verbose:
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
154 print "long bit-operation identities"
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
155 for x in special:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
156 test_bitop_identities_1(x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 digits = range(1, maxdigits+1)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 for lenx in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 test_bitop_identities_1(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 for leny in digits:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 y = getran(leny)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 test_bitop_identities_2(x, y)
19694
3716b116fb31 The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents: 19679
diff changeset
164 test_bitop_identities_3(x, y, getran((lenx + leny)//2))
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
11627
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
166 # ------------------------------------------------- hex oct repr str atol
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 def slow_format(x, base):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 if (x, base) == (0, 8):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 # this is an oddball!
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 return "0L"
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 digits = []
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 sign = 0
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 if x < 0:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 sign, x = 1, -x
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 while x:
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 x, r = divmod(x, base)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 digits.append(int(r))
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 digits.reverse()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 digits = digits or [0]
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
181 return '-'[:sign] + \
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 {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
183 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
184 "L"
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
185
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 def test_format_1(x):
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
187 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
188 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
189 got = mapper(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 expected = slow_format(x, base)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 check(got == expected, mapper.__name__, "returned",
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 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
193 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
194 # 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
195 # trailing "L"
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
196 got = str(x)
d4ea1361731b Revise tests to support str(<long int object>) not appending "L".
Fred Drake <fdrake@acm.org>
parents: 9209
diff changeset
197 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
198 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
199 got, "but expected", expected, "for", x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
200
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 def test_format(maxdigits=MAXDIGITS):
19452
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
202 if verbose:
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
203 print "long str/hex/oct/atol"
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
204 for x in special:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
205 test_format_1(x)
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 for i in range(10):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 for lenx in range(1, maxdigits+1):
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 x = getran(lenx)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 test_format_1(x)
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
210
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
211 # ----------------------------------------------------------------- misc
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
212
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
213 def test_misc(maxdigits=MAXDIGITS):
19452
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
214 if verbose:
10b44e3b3905 Remove test_long's expected-output file.
Tim Peters <tim.peters@gmail.com>
parents: 15957
diff changeset
215 print "long miscellaneous operations"
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
216 import sys
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
217
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
218 # 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
219 hugepos = sys.maxint
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
220 hugeneg = -hugepos - 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
221 hugepos_aslong = long(hugepos)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
222 hugeneg_aslong = long(hugeneg)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
223 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
224 check(hugeneg == hugeneg_aslong,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
225 "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
226
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
227 # 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
228 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
229 check(int(hugepos_aslong) == hugepos,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
230 "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
231 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
232 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
233 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
234 check(int(hugeneg_aslong) == hugeneg,
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
235 "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
236 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
237 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
238
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
239 # 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
240 x = hugepos_aslong + 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
241 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
242 int(x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
243 raise ValueError
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
244 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
245 pass
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
246 except:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
247 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
248
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
249 x = hugeneg_aslong - 1
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
250 try:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
251 int(x)
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
252 raise ValueError
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
253 except OverflowError:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
254 pass
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
255 except:
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
256 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
257
19458
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
258 # ----------------------------------- tests of auto int->long conversion
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
259
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
260 def test_auto_overflow():
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
261 import math, sys
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
262
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
263 if verbose:
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
264 print "auto-convert int->long on overflow"
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
265
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
266 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
267 sqrt = int(math.sqrt(sys.maxint))
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
268 special.extend([sqrt-1, sqrt, sqrt+1])
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
269 special.extend([-i for i in special])
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
270
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
271 def checkit(*args):
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
272 # Heavy use of nested scopes here!
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
273 verify(got == expected, "for %r expected %r got %r" %
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
274 (args, expected, got))
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
275
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
276 for x in special:
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
277 longx = long(x)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
278
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
279 expected = -longx
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
280 got = -x
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
281 checkit('-', x)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
282
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
283 for y in special:
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
284 longy = long(y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
285
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
286 expected = longx + longy
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
287 got = x + y
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
288 checkit(x, '+', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
289
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
290 expected = longx - longy
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
291 got = x - y
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
292 checkit(x, '-', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
293
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
294 expected = longx * longy
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
295 got = x * y
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
296 checkit(x, '*', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
297
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
298 if y:
19698
372d8c1d2b64 Revert one of the "division fixes" in test_long. It intends to try both
Tim Peters <tim.peters@gmail.com>
parents: 19694
diff changeset
299 expected = longx / longy
372d8c1d2b64 Revert one of the "division fixes" in test_long. It intends to try both
Tim Peters <tim.peters@gmail.com>
parents: 19694
diff changeset
300 got = x / y
19458
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
301 checkit(x, '/', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
302
19459
50019cb784e0 Add a test for the new // operator too.
Tim Peters <tim.peters@gmail.com>
parents: 19458
diff changeset
303 expected = longx // longy
50019cb784e0 Add a test for the new // operator too.
Tim Peters <tim.peters@gmail.com>
parents: 19458
diff changeset
304 got = x // y
50019cb784e0 Add a test for the new // operator too.
Tim Peters <tim.peters@gmail.com>
parents: 19458
diff changeset
305 checkit(x, '//', y)
50019cb784e0 Add a test for the new // operator too.
Tim Peters <tim.peters@gmail.com>
parents: 19458
diff changeset
306
19458
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
307 expected = divmod(longx, longy)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
308 got = divmod(longx, longy)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
309 checkit(x, 'divmod', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
310
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
311 if abs(y) < 5 and not (x == 0 and y < 0):
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
312 expected = longx ** longy
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
313 got = x ** y
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
314 checkit(x, '**', y)
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
315
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
316 for z in special:
19666
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
317 if z != 0 :
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
318 if y >= 0:
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
319 expected = pow(longx, longy, long(z))
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
320 got = pow(x, y, z)
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
321 checkit('pow', x, y, '%', z)
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
322 else:
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
323 try:
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
324 pow(longx, longy, long(z))
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
325 except TypeError:
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
326 pass
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
327 else:
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
328 raise TestFailed("pow%r should have raised "
cf65ebf87560 New restriction on pow(x, y, z): If z is not None, x and y must be of
Tim Peters <tim.peters@gmail.com>
parents: 19459
diff changeset
329 "TypeError" % ((longx, longy, long(z))))
19458
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
330
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
331 # ---------------------------------------- tests of long->float overflow
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
332
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
333 def test_float_overflow():
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
334 import math
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
335
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
336 if verbose:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
337 print "long->float overflow"
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
338
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
339 for x in -2.0, -1.0, 0.0, 1.0, 2.0:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
340 verify(float(long(x)) == x)
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
341
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
342 huge = 1L << 30000
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
343 mhuge = -huge
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
344 namespace = {'huge': huge, 'mhuge': mhuge, 'math': math}
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
345 for test in ["float(huge)", "float(mhuge)",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
346 "complex(huge)", "complex(mhuge)",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
347 "complex(huge, 1)", "complex(mhuge, 1)",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
348 "complex(1, huge)", "complex(1, mhuge)",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
349 "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
350 "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
351 "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
352 "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
353 "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
354 "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
355 "math.sin(huge)", "math.sin(mhuge)",
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
356 "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
357 "math.floor(huge)", "math.floor(mhuge)"]:
19679
6958784b1cef Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 19674
diff changeset
358
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
359 try:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
360 eval(test, namespace)
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
361 except OverflowError:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
362 pass
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
363 else:
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
364 raise TestFailed("expected OverflowError from %s" % test)
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
365
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
366 # ---------------------------------------------- test huge log and log10
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
367
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
368 def test_logs():
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
369 import math
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
370
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
371 if verbose:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
372 print "log and log10"
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
373
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
374 LOG10E = math.log10(math.e)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
375
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
376 for exp in range(10) + [100, 1000, 10000]:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
377 value = 10 ** exp
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
378 log10 = math.log10(value)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
379 verify(fcmp(log10, exp) == 0)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
380
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
381 # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
382 # exp/LOG10E
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
383 expected = exp / LOG10E
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
384 log = math.log(value)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
385 verify(fcmp(log, expected) == 0)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
386
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
387 for bad in -(1L << 10000), -2L, 0L:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
388 try:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
389 math.log(bad)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
390 raise TestFailed("expected ValueError from log(<= 0)")
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
391 except ValueError:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
392 pass
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
393
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
394 try:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
395 math.log10(bad)
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
396 raise TestFailed("expected ValueError from log10(<= 0)")
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
397 except ValueError:
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
398 pass
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
399
8989
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 # ---------------------------------------------------------------- do it
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
401
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 test_division()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 test_bitop_identities()
5fb3c683e862 Add Tim Peters' test for long ints
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 test_format()
9209
d3af310f230c New test_long.py from Tim Peters.
Guido van Rossum <guido@python.org>
parents: 8989
diff changeset
405 test_misc()
19458
8f90e1f50c11 SF bug [#454456] int overflow code needs tests.
Tim Peters <tim.peters@gmail.com>
parents: 19452
diff changeset
406 test_auto_overflow()
19674
715244108faa Raise OverflowError when appropriate on long->float conversion. Most of
Tim Peters <tim.peters@gmail.com>
parents: 19666
diff changeset
407 test_float_overflow()
19713
9433dd3c197e Return reasonable results for math.log(long) and math.log10(long) (we were
Tim Peters <tim.peters@gmail.com>
parents: 19698
diff changeset
408 test_logs()