Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 1 | import unittest |
| 2 | from test import test_support |
| 3 | |
| 4 | import random |
| 5 | |
| 6 | # Used for lazy formatting of failure messages |
| 7 | class Frm(object): |
| 8 | def __init__(self, format, *args): |
| 9 | self.format = format |
| 10 | self.args = args |
| 11 | |
| 12 | def __str__(self): |
| 13 | return self.format % self.args |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 14 | |
| 15 | # SHIFT should match the value in longintrepr.h for best testing. |
| 16 | SHIFT = 15 |
| 17 | BASE = 2 ** SHIFT |
| 18 | MASK = BASE - 1 |
Tim Peters | daec961 | 2004-08-30 23:18:23 +0000 | [diff] [blame] | 19 | KARATSUBA_CUTOFF = 70 # from longobject.c |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 20 | |
| 21 | # Max number of base BASE digits to use in test cases. Doubling |
Tim Peters | 28b0e2a | 2002-08-13 02:17:11 +0000 | [diff] [blame] | 22 | # this will more than double the runtime. |
| 23 | MAXDIGITS = 15 |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 25 | # build some special values |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 26 | special = [0, 1, 2, BASE, BASE >> 1, 0x5555555555555555, 0xaaaaaaaaaaaaaaaa] |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 27 | # some solid strings of one bits |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 28 | p2 = 4 # 0 and 1 already added |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 29 | for i in range(2*SHIFT): |
| 30 | special.append(p2 - 1) |
| 31 | p2 = p2 << 1 |
| 32 | del p2 |
| 33 | # add complements & negations |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 34 | special += [~x for x in special] + [-x for x in special] |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 35 | |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 36 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 37 | class LongTest(unittest.TestCase): |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 38 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 39 | # Get quasi-random long consisting of ndigits digits (in base BASE). |
| 40 | # quasi == the most-significant digit will not be 0, and the number |
| 41 | # is constructed to contain long strings of 0 and 1 bits. These are |
| 42 | # more likely than random bits to provoke digit-boundary errors. |
| 43 | # The sign of the number is also random. |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 44 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 45 | def getran(self, ndigits): |
| 46 | self.assert_(ndigits > 0) |
| 47 | nbits_hi = ndigits * SHIFT |
| 48 | nbits_lo = nbits_hi - SHIFT + 1 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 49 | answer = 0 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 50 | nbits = 0 |
| 51 | r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start |
| 52 | while nbits < nbits_lo: |
| 53 | bits = (r >> 1) + 1 |
| 54 | bits = min(bits, nbits_hi - nbits) |
| 55 | self.assert_(1 <= bits <= SHIFT) |
| 56 | nbits = nbits + bits |
| 57 | answer = answer << bits |
| 58 | if r & 1: |
| 59 | answer = answer | ((1 << bits) - 1) |
| 60 | r = int(random.random() * (SHIFT * 2)) |
| 61 | self.assert_(nbits_lo <= nbits <= nbits_hi) |
| 62 | if random.random() < 0.5: |
| 63 | answer = -answer |
| 64 | return answer |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 65 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 66 | # Get random long consisting of ndigits random digits (relative to base |
| 67 | # BASE). The sign bit is also random. |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 68 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 69 | def getran2(ndigits): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 70 | answer = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 71 | for i in range(ndigits): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 72 | answer = (answer << SHIFT) | random.randint(0, MASK) |
| 73 | if random.random() < 0.5: |
| 74 | answer = -answer |
| 75 | return answer |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 76 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 77 | def check_division(self, x, y): |
| 78 | eq = self.assertEqual |
| 79 | q, r = divmod(x, y) |
| 80 | q2, r2 = x//y, x%y |
| 81 | pab, pba = x*y, y*x |
| 82 | eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y)) |
| 83 | eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y)) |
| 84 | eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y)) |
| 85 | eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y)) |
| 86 | if y > 0: |
| 87 | self.assert_(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y)) |
| 88 | else: |
| 89 | self.assert_(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y)) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 90 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 91 | def test_division(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 92 | digits = list(range(1, MAXDIGITS+1)) + list(range(KARATSUBA_CUTOFF, |
| 93 | KARATSUBA_CUTOFF + 14)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 94 | digits.append(KARATSUBA_CUTOFF * 3) |
| 95 | for lenx in digits: |
| 96 | x = self.getran(lenx) |
| 97 | for leny in digits: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 98 | y = self.getran(leny) or 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 99 | self.check_division(x, y) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 100 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 101 | def test_karatsuba(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 102 | digits = list(range(1, 5)) + list(range(KARATSUBA_CUTOFF, |
| 103 | KARATSUBA_CUTOFF + 10)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 104 | digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100]) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 105 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 106 | bits = [digit * SHIFT for digit in digits] |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 107 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 108 | # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) == |
| 109 | # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check. |
| 110 | for abits in bits: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 111 | a = (1 << abits) - 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 112 | for bbits in bits: |
| 113 | if bbits < abits: |
| 114 | continue |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 115 | b = (1 << bbits) - 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 116 | x = a * b |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 117 | y = ((1 << (abits + bbits)) - |
| 118 | (1 << abits) - |
| 119 | (1 << bbits) + |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 120 | 1) |
| 121 | self.assertEqual(x, y, |
| 122 | Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y)) |
Tim Peters | 7f270ba | 2002-08-13 21:06:55 +0000 | [diff] [blame] | 123 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 124 | def check_bitop_identities_1(self, x): |
| 125 | eq = self.assertEqual |
| 126 | eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x)) |
| 127 | eq(x | 0, x, Frm("x | 0 != x for x=%r", x)) |
| 128 | eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x)) |
| 129 | eq(x & -1, x, Frm("x & -1 != x for x=%r", x)) |
| 130 | eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x)) |
| 131 | eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x)) |
| 132 | eq(x, ~~x, Frm("x != ~~x for x=%r", x)) |
| 133 | eq(x & x, x, Frm("x & x != x for x=%r", x)) |
| 134 | eq(x | x, x, Frm("x | x != x for x=%r", x)) |
| 135 | eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x)) |
| 136 | eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x)) |
| 137 | eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x)) |
| 138 | eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x)) |
| 139 | eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x)) |
| 140 | eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x)) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 141 | for n in range(2*SHIFT): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 142 | p2 = 2 ** n |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 143 | eq(x << n >> n, x, |
| 144 | Frm("x << n >> n != x for x=%r, n=%r", (x, n))) |
| 145 | eq(x // p2, x >> n, |
| 146 | Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2))) |
| 147 | eq(x * p2, x << n, |
| 148 | Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2))) |
| 149 | eq(x & -p2, x >> n << n, |
| 150 | Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2))) |
| 151 | eq(x & -p2, x & ~(p2 - 1), |
| 152 | Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2))) |
Tim Peters | 7f270ba | 2002-08-13 21:06:55 +0000 | [diff] [blame] | 153 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 154 | def check_bitop_identities_2(self, x, y): |
| 155 | eq = self.assertEqual |
| 156 | eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y))) |
| 157 | eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y))) |
| 158 | eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y))) |
| 159 | eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y))) |
| 160 | eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y))) |
| 161 | eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y))) |
| 162 | eq(x ^ y, (x | y) & ~(x & y), |
| 163 | Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y))) |
| 164 | eq(x ^ y, (x & ~y) | (~x & y), |
| 165 | Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y))) |
| 166 | eq(x ^ y, (x | y) & (~x | ~y), |
| 167 | Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y))) |
Tim Peters | 7f270ba | 2002-08-13 21:06:55 +0000 | [diff] [blame] | 168 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 169 | def check_bitop_identities_3(self, x, y, z): |
| 170 | eq = self.assertEqual |
| 171 | eq((x & y) & z, x & (y & z), |
| 172 | Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 173 | eq((x | y) | z, x | (y | z), |
| 174 | Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 175 | eq((x ^ y) ^ z, x ^ (y ^ z), |
| 176 | Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 177 | eq(x & (y | z), (x & y) | (x & z), |
| 178 | Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 179 | eq(x | (y & z), (x | y) & (x | z), |
| 180 | Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z))) |
Tim Peters | 7f270ba | 2002-08-13 21:06:55 +0000 | [diff] [blame] | 181 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 182 | def test_bitop_identities(self): |
| 183 | for x in special: |
| 184 | self.check_bitop_identities_1(x) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 185 | digits = range(1, MAXDIGITS+1) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 186 | for lenx in digits: |
| 187 | x = self.getran(lenx) |
| 188 | self.check_bitop_identities_1(x) |
| 189 | for leny in digits: |
| 190 | y = self.getran(leny) |
| 191 | self.check_bitop_identities_2(x, y) |
| 192 | self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2)) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 193 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 194 | def slow_format(self, x, base): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 195 | digits = [] |
| 196 | sign = 0 |
| 197 | if x < 0: |
| 198 | sign, x = 1, -x |
| 199 | while x: |
| 200 | x, r = divmod(x, base) |
| 201 | digits.append(int(r)) |
| 202 | digits.reverse() |
| 203 | digits = digits or [0] |
| 204 | return '-'[:sign] + \ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 205 | {2: '0b', 8: '0o', 10: '', 16: '0x'}[base] + \ |
Guido van Rossum | d2dbecb | 2006-08-18 16:29:54 +0000 | [diff] [blame] | 206 | "".join(map(lambda i: "0123456789abcdef"[i], digits)) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 207 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 208 | def check_format_1(self, x): |
| 209 | for base, mapper in (8, oct), (10, repr), (16, hex): |
| 210 | got = mapper(x) |
| 211 | expected = self.slow_format(x, base) |
| 212 | msg = Frm("%s returned %r but expected %r for %r", |
| 213 | mapper.__name__, got, expected, x) |
| 214 | self.assertEqual(got, expected, msg) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 215 | self.assertEqual(int(got, 0), x, Frm('long("%s", 0) != %r', got, x)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 216 | # str() has to be checked a little differently since there's no |
| 217 | # trailing "L" |
| 218 | got = str(x) |
Guido van Rossum | d2dbecb | 2006-08-18 16:29:54 +0000 | [diff] [blame] | 219 | expected = self.slow_format(x, 10) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 220 | msg = Frm("%s returned %r but expected %r for %r", |
| 221 | mapper.__name__, got, expected, x) |
| 222 | self.assertEqual(got, expected, msg) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 223 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 224 | def test_format(self): |
| 225 | for x in special: |
| 226 | self.check_format_1(x) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 227 | for i in range(10): |
| 228 | for lenx in range(1, MAXDIGITS+1): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 229 | x = self.getran(lenx) |
| 230 | self.check_format_1(x) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 231 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 232 | def test_misc(self): |
| 233 | import sys |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 234 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 235 | # check the extremes in int<->long conversion |
| 236 | hugepos = sys.maxint |
| 237 | hugeneg = -hugepos - 1 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 238 | hugepos_aslong = int(hugepos) |
| 239 | hugeneg_aslong = int(hugeneg) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 240 | self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint") |
| 241 | self.assertEqual(hugeneg, hugeneg_aslong, |
| 242 | "long(-sys.maxint-1) != -sys.maxint-1") |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 243 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 244 | # long -> int should not fail for hugepos_aslong or hugeneg_aslong |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 245 | x = int(hugepos_aslong) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 246 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 247 | self.assertEqual(x, hugepos, |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 248 | "converting sys.maxint to long and back to int fails") |
| 249 | except OverflowError: |
| 250 | self.fail("int(long(sys.maxint)) overflowed!") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 251 | if not isinstance(x, int): |
| 252 | raise TestFailed("int(long(sys.maxint)) should have returned int") |
| 253 | x = int(hugeneg_aslong) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 254 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 255 | self.assertEqual(x, hugeneg, |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 256 | "converting -sys.maxint-1 to long and back to int fails") |
| 257 | except OverflowError: |
| 258 | self.fail("int(long(-sys.maxint-1)) overflowed!") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 259 | if not isinstance(x, int): |
| 260 | raise TestFailed("int(long(-sys.maxint-1)) should have " |
| 261 | "returned int") |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 262 | # but long -> int should overflow for hugepos+1 and hugeneg-1 |
| 263 | x = hugepos_aslong + 1 |
| 264 | try: |
| 265 | y = int(x) |
| 266 | except OverflowError: |
| 267 | self.fail("int(long(sys.maxint) + 1) mustn't overflow") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 268 | self.assert_(isinstance(y, int), |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 269 | "int(long(sys.maxint) + 1) should have returned long") |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 270 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 271 | x = hugeneg_aslong - 1 |
| 272 | try: |
| 273 | y = int(x) |
| 274 | except OverflowError: |
| 275 | self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow") |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 276 | self.assert_(isinstance(y, int), |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 277 | "int(long(-sys.maxint-1) - 1) should have returned long") |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 278 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 279 | class long2(int): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 280 | pass |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 281 | x = long2(1<<100) |
Walter Dörwald | f171540 | 2002-11-19 20:49:15 +0000 | [diff] [blame] | 282 | y = int(x) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 283 | self.assert_(type(y) is int, |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 284 | "overflowing int conversion must return long not long subtype") |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 285 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 286 | # long -> Py_ssize_t conversion |
| 287 | class X(object): |
| 288 | def __getslice__(self, i, j): |
| 289 | return i, j |
| 290 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 291 | self.assertEqual(X()[-5:7], (-5, 7)) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 292 | # use the clamping effect to test the smallest and largest longs |
| 293 | # that fit a Py_ssize_t |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 294 | slicemin, slicemax = X()[-2**100:2**100] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 295 | self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax)) |
| 296 | |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 297 | # ----------------------------------- tests of auto int->long conversion |
| 298 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 299 | def test_auto_overflow(self): |
| 300 | import math, sys |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 301 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 302 | special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1] |
| 303 | sqrt = int(math.sqrt(sys.maxint)) |
| 304 | special.extend([sqrt-1, sqrt, sqrt+1]) |
| 305 | special.extend([-i for i in special]) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 306 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 307 | def checkit(*args): |
| 308 | # Heavy use of nested scopes here! |
| 309 | self.assertEqual(got, expected, |
| 310 | Frm("for %r expected %r got %r", args, expected, got)) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 311 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 312 | for x in special: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 313 | longx = int(x) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 314 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 315 | expected = -longx |
| 316 | got = -x |
| 317 | checkit('-', x) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 318 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 319 | for y in special: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 320 | longy = int(y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 321 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 322 | expected = longx + longy |
| 323 | got = x + y |
| 324 | checkit(x, '+', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 325 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 326 | expected = longx - longy |
| 327 | got = x - y |
| 328 | checkit(x, '-', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 329 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 330 | expected = longx * longy |
| 331 | got = x * y |
| 332 | checkit(x, '*', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 333 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 334 | if y: |
| 335 | expected = longx / longy |
| 336 | got = x / y |
| 337 | checkit(x, '/', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 338 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 339 | expected = longx // longy |
| 340 | got = x // y |
| 341 | checkit(x, '//', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 342 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 343 | expected = divmod(longx, longy) |
| 344 | got = divmod(longx, longy) |
| 345 | checkit(x, 'divmod', y) |
Tim Peters | a365309 | 2001-08-23 23:02:57 +0000 | [diff] [blame] | 346 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 347 | if abs(y) < 5 and not (x == 0 and y < 0): |
| 348 | expected = longx ** longy |
| 349 | got = x ** y |
| 350 | checkit(x, '**', y) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 351 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 352 | for z in special: |
| 353 | if z != 0 : |
| 354 | if y >= 0: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 355 | expected = pow(longx, longy, int(z)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 356 | got = pow(x, y, z) |
| 357 | checkit('pow', x, y, '%', z) |
Tim Peters | 32f453e | 2001-09-03 08:35:41 +0000 | [diff] [blame] | 358 | else: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 359 | self.assertRaises(TypeError, pow,longx, longy, int(z)) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 360 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 361 | def test_float_overflow(self): |
| 362 | import math |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 363 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 364 | for x in -2.0, -1.0, 0.0, 1.0, 2.0: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 365 | self.assertEqual(float(int(x)), x) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 366 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 367 | shuge = '12345' * 120 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 368 | huge = 1 << 30000 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 369 | mhuge = -huge |
| 370 | namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math} |
| 371 | for test in ["float(huge)", "float(mhuge)", |
| 372 | "complex(huge)", "complex(mhuge)", |
| 373 | "complex(huge, 1)", "complex(mhuge, 1)", |
| 374 | "complex(1, huge)", "complex(1, mhuge)", |
| 375 | "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.", |
| 376 | "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.", |
| 377 | "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.", |
| 378 | "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.", |
| 379 | "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.", |
| 380 | "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.", |
| 381 | "math.sin(huge)", "math.sin(mhuge)", |
| 382 | "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better |
Guido van Rossum | 28bbe42 | 2007-08-24 03:46:30 +0000 | [diff] [blame] | 383 | # math.floor() of an int returns an int now |
| 384 | ##"math.floor(huge)", "math.floor(mhuge)", |
| 385 | ]: |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 386 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 387 | self.assertRaises(OverflowError, eval, test, namespace) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 388 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 389 | # XXX Perhaps float(shuge) can raise OverflowError on some box? |
| 390 | # The comparison should not. |
| 391 | self.assertNotEqual(float(shuge), int(shuge), |
| 392 | "float(shuge) should not equal int(shuge)") |
Tim Peters | 83e7ccc | 2001-09-04 06:37:28 +0000 | [diff] [blame] | 393 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 394 | def test_logs(self): |
| 395 | import math |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 396 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 397 | LOG10E = math.log10(math.e) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 398 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 399 | for exp in list(range(10)) + [100, 1000, 10000]: |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 400 | value = 10 ** exp |
| 401 | log10 = math.log10(value) |
| 402 | self.assertAlmostEqual(log10, exp) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 403 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 404 | # log10(value) == exp, so log(value) == log10(value)/log10(e) == |
| 405 | # exp/LOG10E |
| 406 | expected = exp / LOG10E |
| 407 | log = math.log(value) |
| 408 | self.assertAlmostEqual(log, expected) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 410 | for bad in -(1 << 10000), -2, 0: |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 411 | self.assertRaises(ValueError, math.log, bad) |
| 412 | self.assertRaises(ValueError, math.log10, bad) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 413 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 414 | def test_mixed_compares(self): |
| 415 | eq = self.assertEqual |
| 416 | import math |
| 417 | import sys |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 418 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 419 | # We're mostly concerned with that mixing floats and longs does the |
| 420 | # right stuff, even when longs are too large to fit in a float. |
| 421 | # The safest way to check the results is to use an entirely different |
| 422 | # method, which we do here via a skeletal rational class (which |
| 423 | # represents all Python ints, longs and floats exactly). |
| 424 | class Rat: |
| 425 | def __init__(self, value): |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 426 | if isinstance(value, int): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 427 | self.n = value |
| 428 | self.d = 1 |
| 429 | elif isinstance(value, float): |
| 430 | # Convert to exact rational equivalent. |
| 431 | f, e = math.frexp(abs(value)) |
| 432 | assert f == 0 or 0.5 <= f < 1.0 |
| 433 | # |value| = f * 2**e exactly |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 434 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 435 | # Suck up CHUNK bits at a time; 28 is enough so that we suck |
| 436 | # up all bits in 2 iterations for all known binary double- |
| 437 | # precision formats, and small enough to fit in an int. |
| 438 | CHUNK = 28 |
| 439 | top = 0 |
| 440 | # invariant: |value| = (top + f) * 2**e exactly |
| 441 | while f: |
| 442 | f = math.ldexp(f, CHUNK) |
| 443 | digit = int(f) |
| 444 | assert digit >> CHUNK == 0 |
| 445 | top = (top << CHUNK) | digit |
| 446 | f -= digit |
| 447 | assert 0.0 <= f < 1.0 |
| 448 | e -= CHUNK |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 449 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 450 | # Now |value| = top * 2**e exactly. |
| 451 | if e >= 0: |
| 452 | n = top << e |
| 453 | d = 1 |
| 454 | else: |
| 455 | n = top |
| 456 | d = 1 << -e |
| 457 | if value < 0: |
| 458 | n = -n |
| 459 | self.n = n |
| 460 | self.d = d |
| 461 | assert float(n) / float(d) == value |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 462 | else: |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 463 | raise TypeError("can't deal with %r" % val) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 464 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 465 | def __cmp__(self, other): |
| 466 | if not isinstance(other, Rat): |
| 467 | other = Rat(other) |
| 468 | return cmp(self.n * other.d, self.d * other.n) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 469 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 470 | cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200] |
| 471 | # 2**48 is an important boundary in the internals. 2**53 is an |
| 472 | # important boundary for IEEE double precision. |
| 473 | for t in 2.0**48, 2.0**50, 2.0**53: |
| 474 | cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 475 | int(t-1), int(t), int(t+1)]) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 476 | cases.extend([0, 1, 2, sys.maxint, float(sys.maxint)]) |
| 477 | # 1L<<20000 should exceed all double formats. long(1e200) is to |
| 478 | # check that we get equality with 1e200 above. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 479 | t = int(1e200) |
| 480 | cases.extend([0, 1, 2, 1 << 20000, t-1, t, t+1]) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 481 | cases.extend([-x for x in cases]) |
| 482 | for x in cases: |
| 483 | Rx = Rat(x) |
| 484 | for y in cases: |
| 485 | Ry = Rat(y) |
| 486 | Rcmp = cmp(Rx, Ry) |
| 487 | xycmp = cmp(x, y) |
| 488 | eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp)) |
| 489 | eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp)) |
| 490 | eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp)) |
| 491 | eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp)) |
| 492 | eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp)) |
| 493 | eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp)) |
| 494 | eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp)) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 495 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame^] | 496 | def test_format(self): |
| 497 | self.assertEqual(format(123456789, 'd'), '123456789') |
| 498 | self.assertEqual(format(123456789, 'd'), '123456789') |
| 499 | |
| 500 | # hex |
| 501 | self.assertEqual(format(3, "x"), "3") |
| 502 | self.assertEqual(format(3, "X"), "3") |
| 503 | self.assertEqual(format(1234, "x"), "4d2") |
| 504 | self.assertEqual(format(-1234, "x"), "-4d2") |
| 505 | self.assertEqual(format(1234, "8x"), " 4d2") |
| 506 | # XXX fix self.assertEqual(format(-1234, "8x"), " -4d2") |
| 507 | self.assertEqual(format(1234, "x"), "4d2") |
| 508 | self.assertEqual(format(-1234, "x"), "-4d2") |
| 509 | self.assertEqual(format(-3, "x"), "-3") |
| 510 | self.assertEqual(format(-3, "X"), "-3") |
| 511 | self.assertEqual(format(int('be', 16), "x"), "be") |
| 512 | self.assertEqual(format(int('be', 16), "X"), "BE") |
| 513 | self.assertEqual(format(-int('be', 16), "x"), "-be") |
| 514 | self.assertEqual(format(-int('be', 16), "X"), "-BE") |
| 515 | |
| 516 | # octal |
| 517 | self.assertEqual(format(3, "b"), "11") |
| 518 | self.assertEqual(format(-3, "b"), "-11") |
| 519 | self.assertEqual(format(1234, "b"), "10011010010") |
| 520 | self.assertEqual(format(-1234, "b"), "-10011010010") |
| 521 | self.assertEqual(format(1234, "-b"), "10011010010") |
| 522 | self.assertEqual(format(-1234, "-b"), "-10011010010") |
| 523 | self.assertEqual(format(1234, " b"), " 10011010010") |
| 524 | self.assertEqual(format(-1234, " b"), "-10011010010") |
| 525 | self.assertEqual(format(1234, "+b"), "+10011010010") |
| 526 | self.assertEqual(format(-1234, "+b"), "-10011010010") |
| 527 | |
| 528 | # conversion to float |
| 529 | self.assertEqual(format(0, 'f'), '0.000000') |
| 530 | |
| 531 | # make sure these are errors |
| 532 | self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed |
| 533 | return |
| 534 | self.assertRaises(ValueError, format, 3, "+c") # sign not allowed |
| 535 | # with 'c' |
| 536 | self.assertRaises(ValueError, format, 3, "R") # bogus format type |
| 537 | # conversion to string should fail |
| 538 | self.assertRaises(ValueError, format, 3, "s") |
| 539 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 540 | def test_main(): |
| 541 | test_support.run_unittest(LongTest) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 542 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 543 | if __name__ == "__main__": |
| 544 | test_main() |