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