Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 1 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2 | from test import support |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 3 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 4 | import sys |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 5 | |
| 6 | import random |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 7 | import math |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 8 | import array |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 9 | |
| 10 | # Used for lazy formatting of failure messages |
| 11 | class Frm(object): |
| 12 | def __init__(self, format, *args): |
| 13 | self.format = format |
| 14 | self.args = args |
| 15 | |
| 16 | def __str__(self): |
| 17 | return self.format % self.args |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 18 | |
| 19 | # SHIFT should match the value in longintrepr.h for best testing. |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 20 | SHIFT = sys.int_info.bits_per_digit |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 21 | BASE = 2 ** SHIFT |
| 22 | MASK = BASE - 1 |
Tim Peters | daec961 | 2004-08-30 23:18:23 +0000 | [diff] [blame] | 23 | KARATSUBA_CUTOFF = 70 # from longobject.c |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 24 | |
| 25 | # Max number of base BASE digits to use in test cases. Doubling |
Tim Peters | 28b0e2a | 2002-08-13 02:17:11 +0000 | [diff] [blame] | 26 | # this will more than double the runtime. |
| 27 | MAXDIGITS = 15 |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 29 | # build some special values |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 30 | special = [0, 1, 2, BASE, BASE >> 1, 0x5555555555555555, 0xaaaaaaaaaaaaaaaa] |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 31 | # some solid strings of one bits |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 32 | p2 = 4 # 0 and 1 already added |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 33 | for i in range(2*SHIFT): |
| 34 | special.append(p2 - 1) |
| 35 | p2 = p2 << 1 |
| 36 | del p2 |
| 37 | # add complements & negations |
Guido van Rossum | c1f779c | 2007-07-03 08:25:58 +0000 | [diff] [blame] | 38 | special += [~x for x in special] + [-x for x in special] |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 39 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 40 | DBL_MAX = sys.float_info.max |
| 41 | DBL_MAX_EXP = sys.float_info.max_exp |
| 42 | DBL_MIN_EXP = sys.float_info.min_exp |
| 43 | DBL_MANT_DIG = sys.float_info.mant_dig |
| 44 | DBL_MIN_OVERFLOW = 2**DBL_MAX_EXP - 2**(DBL_MAX_EXP - DBL_MANT_DIG - 1) |
| 45 | |
Mark Dickinson | 30970e9 | 2011-10-23 20:07:13 +0100 | [diff] [blame] | 46 | |
| 47 | # Pure Python version of correctly-rounded integer-to-float conversion. |
| 48 | def int_to_float(n): |
| 49 | """ |
| 50 | Correctly-rounded integer-to-float conversion. |
| 51 | |
| 52 | """ |
| 53 | # Constants, depending only on the floating-point format in use. |
| 54 | # We use an extra 2 bits of precision for rounding purposes. |
| 55 | PRECISION = sys.float_info.mant_dig + 2 |
| 56 | SHIFT_MAX = sys.float_info.max_exp - PRECISION |
| 57 | Q_MAX = 1 << PRECISION |
| 58 | ROUND_HALF_TO_EVEN_CORRECTION = [0, -1, -2, 1, 0, -1, 2, 1] |
| 59 | |
| 60 | # Reduce to the case where n is positive. |
| 61 | if n == 0: |
| 62 | return 0.0 |
| 63 | elif n < 0: |
| 64 | return -int_to_float(-n) |
| 65 | |
| 66 | # Convert n to a 'floating-point' number q * 2**shift, where q is an |
| 67 | # integer with 'PRECISION' significant bits. When shifting n to create q, |
| 68 | # the least significant bit of q is treated as 'sticky'. That is, the |
| 69 | # least significant bit of q is set if either the corresponding bit of n |
| 70 | # was already set, or any one of the bits of n lost in the shift was set. |
| 71 | shift = n.bit_length() - PRECISION |
| 72 | q = n << -shift if shift < 0 else (n >> shift) | bool(n & ~(-1 << shift)) |
| 73 | |
| 74 | # Round half to even (actually rounds to the nearest multiple of 4, |
| 75 | # rounding ties to a multiple of 8). |
| 76 | q += ROUND_HALF_TO_EVEN_CORRECTION[q & 7] |
| 77 | |
| 78 | # Detect overflow. |
| 79 | if shift + (q == Q_MAX) > SHIFT_MAX: |
| 80 | raise OverflowError("integer too large to convert to float") |
| 81 | |
| 82 | # Checks: q is exactly representable, and q**2**shift doesn't overflow. |
| 83 | assert q % 4 == 0 and q // 4 <= 2**(sys.float_info.mant_dig) |
| 84 | assert q * 2**shift <= sys.float_info.max |
| 85 | |
| 86 | # Some circularity here, since float(q) is doing an int-to-float |
| 87 | # conversion. But here q is of bounded size, and is exactly representable |
| 88 | # as a float. In a low-level C-like language, this operation would be a |
| 89 | # simple cast (e.g., from unsigned long long to double). |
| 90 | return math.ldexp(float(q), shift) |
| 91 | |
| 92 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 93 | # pure Python version of correctly-rounded true division |
| 94 | def truediv(a, b): |
| 95 | """Correctly-rounded true division for integers.""" |
| 96 | negative = a^b < 0 |
| 97 | a, b = abs(a), abs(b) |
| 98 | |
| 99 | # exceptions: division by zero, overflow |
| 100 | if not b: |
| 101 | raise ZeroDivisionError("division by zero") |
| 102 | if a >= DBL_MIN_OVERFLOW * b: |
| 103 | raise OverflowError("int/int too large to represent as a float") |
| 104 | |
| 105 | # find integer d satisfying 2**(d - 1) <= a/b < 2**d |
| 106 | d = a.bit_length() - b.bit_length() |
| 107 | if d >= 0 and a >= 2**d * b or d < 0 and a * 2**-d >= b: |
| 108 | d += 1 |
| 109 | |
| 110 | # compute 2**-exp * a / b for suitable exp |
| 111 | exp = max(d, DBL_MIN_EXP) - DBL_MANT_DIG |
| 112 | a, b = a << max(-exp, 0), b << max(exp, 0) |
| 113 | q, r = divmod(a, b) |
| 114 | |
| 115 | # round-half-to-even: fractional part is r/b, which is > 0.5 iff |
| 116 | # 2*r > b, and == 0.5 iff 2*r == b. |
| 117 | if 2*r > b or 2*r == b and q % 2 == 1: |
| 118 | q += 1 |
| 119 | |
Mark Dickinson | a4e1506 | 2009-12-27 19:03:31 +0000 | [diff] [blame] | 120 | result = math.ldexp(q, exp) |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 121 | return -result if negative else result |
| 122 | |
| 123 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 124 | class LongTest(unittest.TestCase): |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 125 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 126 | # Get quasi-random long consisting of ndigits digits (in base BASE). |
| 127 | # quasi == the most-significant digit will not be 0, and the number |
| 128 | # is constructed to contain long strings of 0 and 1 bits. These are |
| 129 | # more likely than random bits to provoke digit-boundary errors. |
| 130 | # The sign of the number is also random. |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 131 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 132 | def getran(self, ndigits): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 133 | self.assertTrue(ndigits > 0) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 134 | nbits_hi = ndigits * SHIFT |
| 135 | nbits_lo = nbits_hi - SHIFT + 1 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 136 | answer = 0 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 137 | nbits = 0 |
| 138 | r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start |
| 139 | while nbits < nbits_lo: |
| 140 | bits = (r >> 1) + 1 |
| 141 | bits = min(bits, nbits_hi - nbits) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 142 | self.assertTrue(1 <= bits <= SHIFT) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 143 | nbits = nbits + bits |
| 144 | answer = answer << bits |
| 145 | if r & 1: |
| 146 | answer = answer | ((1 << bits) - 1) |
| 147 | r = int(random.random() * (SHIFT * 2)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 148 | self.assertTrue(nbits_lo <= nbits <= nbits_hi) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 149 | if random.random() < 0.5: |
| 150 | answer = -answer |
| 151 | return answer |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 152 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 153 | # Get random long consisting of ndigits random digits (relative to base |
| 154 | # BASE). The sign bit is also random. |
Guido van Rossum | 4581a0c | 1998-10-02 01:19:48 +0000 | [diff] [blame] | 155 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 156 | def getran2(ndigits): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 157 | answer = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 158 | for i in range(ndigits): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 159 | answer = (answer << SHIFT) | random.randint(0, MASK) |
| 160 | if random.random() < 0.5: |
| 161 | answer = -answer |
| 162 | return answer |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 163 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 164 | def check_division(self, x, y): |
| 165 | eq = self.assertEqual |
| 166 | q, r = divmod(x, y) |
| 167 | q2, r2 = x//y, x%y |
| 168 | pab, pba = x*y, y*x |
| 169 | eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y)) |
| 170 | eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y)) |
| 171 | eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y)) |
| 172 | eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y)) |
| 173 | if y > 0: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 174 | self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 175 | else: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 176 | self.assertTrue(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] | 177 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 178 | def test_division(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 179 | digits = list(range(1, MAXDIGITS+1)) + list(range(KARATSUBA_CUTOFF, |
| 180 | KARATSUBA_CUTOFF + 14)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 181 | digits.append(KARATSUBA_CUTOFF * 3) |
| 182 | for lenx in digits: |
| 183 | x = self.getran(lenx) |
| 184 | for leny in digits: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 185 | y = self.getran(leny) or 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 186 | self.check_division(x, y) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 187 | |
Mark Dickinson | bd79264 | 2009-03-18 20:06:12 +0000 | [diff] [blame] | 188 | # specific numbers chosen to exercise corner cases of the |
| 189 | # current long division implementation |
| 190 | |
| 191 | # 30-bit cases involving a quotient digit estimate of BASE+1 |
| 192 | self.check_division(1231948412290879395966702881, |
| 193 | 1147341367131428698) |
| 194 | self.check_division(815427756481275430342312021515587883, |
| 195 | 707270836069027745) |
| 196 | self.check_division(627976073697012820849443363563599041, |
| 197 | 643588798496057020) |
| 198 | self.check_division(1115141373653752303710932756325578065, |
| 199 | 1038556335171453937726882627) |
| 200 | # 30-bit cases that require the post-subtraction correction step |
| 201 | self.check_division(922498905405436751940989320930368494, |
| 202 | 949985870686786135626943396) |
| 203 | self.check_division(768235853328091167204009652174031844, |
| 204 | 1091555541180371554426545266) |
| 205 | |
| 206 | # 15-bit cases involving a quotient digit estimate of BASE+1 |
| 207 | self.check_division(20172188947443, 615611397) |
| 208 | self.check_division(1020908530270155025, 950795710) |
| 209 | self.check_division(128589565723112408, 736393718) |
| 210 | self.check_division(609919780285761575, 18613274546784) |
| 211 | # 15-bit cases that require the post-subtraction correction step |
| 212 | self.check_division(710031681576388032, 26769404391308) |
| 213 | self.check_division(1933622614268221, 30212853348836) |
| 214 | |
| 215 | |
| 216 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 217 | def test_karatsuba(self): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 218 | digits = list(range(1, 5)) + list(range(KARATSUBA_CUTOFF, |
| 219 | KARATSUBA_CUTOFF + 10)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 220 | digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100]) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 221 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 222 | bits = [digit * SHIFT for digit in digits] |
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 | # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) == |
| 225 | # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check. |
| 226 | for abits in bits: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 227 | a = (1 << abits) - 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 228 | for bbits in bits: |
| 229 | if bbits < abits: |
| 230 | continue |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 231 | b = (1 << bbits) - 1 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 232 | x = a * b |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 233 | y = ((1 << (abits + bbits)) - |
| 234 | (1 << abits) - |
| 235 | (1 << bbits) + |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 236 | 1) |
| 237 | self.assertEqual(x, y, |
| 238 | 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] | 239 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 240 | def check_bitop_identities_1(self, x): |
| 241 | eq = self.assertEqual |
| 242 | eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x)) |
| 243 | eq(x | 0, x, Frm("x | 0 != x for x=%r", x)) |
| 244 | eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x)) |
| 245 | eq(x & -1, x, Frm("x & -1 != x for x=%r", x)) |
| 246 | eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x)) |
| 247 | eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x)) |
| 248 | eq(x, ~~x, Frm("x != ~~x for x=%r", x)) |
| 249 | eq(x & x, x, Frm("x & x != x for x=%r", x)) |
| 250 | eq(x | x, x, Frm("x | x != x for x=%r", x)) |
| 251 | eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x)) |
| 252 | eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x)) |
| 253 | eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x)) |
| 254 | eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x)) |
| 255 | eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x)) |
| 256 | 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] | 257 | for n in range(2*SHIFT): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 258 | p2 = 2 ** n |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 259 | eq(x << n >> n, x, |
| 260 | Frm("x << n >> n != x for x=%r, n=%r", (x, n))) |
| 261 | eq(x // p2, x >> n, |
| 262 | Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2))) |
| 263 | eq(x * p2, x << n, |
| 264 | Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2))) |
| 265 | eq(x & -p2, x >> n << n, |
| 266 | Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2))) |
| 267 | eq(x & -p2, x & ~(p2 - 1), |
| 268 | 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] | 269 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 270 | def check_bitop_identities_2(self, x, y): |
| 271 | eq = self.assertEqual |
| 272 | eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y))) |
| 273 | eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y))) |
| 274 | eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y))) |
| 275 | eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y))) |
| 276 | eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y))) |
| 277 | eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y))) |
| 278 | eq(x ^ y, (x | y) & ~(x & y), |
| 279 | Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y))) |
| 280 | eq(x ^ y, (x & ~y) | (~x & y), |
| 281 | Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y))) |
| 282 | eq(x ^ y, (x | y) & (~x | ~y), |
| 283 | 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] | 284 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 285 | def check_bitop_identities_3(self, x, y, z): |
| 286 | eq = self.assertEqual |
| 287 | eq((x & y) & z, x & (y & z), |
| 288 | Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 289 | eq((x | y) | z, x | (y | z), |
| 290 | Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 291 | eq((x ^ y) ^ z, x ^ (y ^ z), |
| 292 | Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 293 | eq(x & (y | z), (x & y) | (x & z), |
| 294 | Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z))) |
| 295 | eq(x | (y & z), (x | y) & (x | z), |
| 296 | 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] | 297 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 298 | def test_bitop_identities(self): |
| 299 | for x in special: |
| 300 | self.check_bitop_identities_1(x) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 301 | digits = range(1, MAXDIGITS+1) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 302 | for lenx in digits: |
| 303 | x = self.getran(lenx) |
| 304 | self.check_bitop_identities_1(x) |
| 305 | for leny in digits: |
| 306 | y = self.getran(leny) |
| 307 | self.check_bitop_identities_2(x, y) |
| 308 | 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] | 309 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 310 | def slow_format(self, x, base): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 311 | digits = [] |
| 312 | sign = 0 |
| 313 | if x < 0: |
| 314 | sign, x = 1, -x |
| 315 | while x: |
| 316 | x, r = divmod(x, base) |
| 317 | digits.append(int(r)) |
| 318 | digits.reverse() |
| 319 | digits = digits or [0] |
| 320 | return '-'[:sign] + \ |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 321 | {2: '0b', 8: '0o', 10: '', 16: '0x'}[base] + \ |
Georg Brandl | cbd2ab1 | 2010-12-04 10:39:14 +0000 | [diff] [blame] | 322 | "".join("0123456789abcdef"[i] for i in digits) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 323 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 324 | def check_format_1(self, x): |
| 325 | for base, mapper in (8, oct), (10, repr), (16, hex): |
| 326 | got = mapper(x) |
| 327 | expected = self.slow_format(x, base) |
| 328 | msg = Frm("%s returned %r but expected %r for %r", |
| 329 | mapper.__name__, got, expected, x) |
| 330 | self.assertEqual(got, expected, msg) |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 331 | self.assertEqual(int(got, 0), x, Frm('int("%s", 0) != %r', got, x)) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 332 | # str() has to be checked a little differently since there's no |
| 333 | # trailing "L" |
| 334 | got = str(x) |
Guido van Rossum | d2dbecb | 2006-08-18 16:29:54 +0000 | [diff] [blame] | 335 | expected = self.slow_format(x, 10) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 336 | msg = Frm("%s returned %r but expected %r for %r", |
| 337 | mapper.__name__, got, expected, x) |
| 338 | self.assertEqual(got, expected, msg) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 339 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 340 | def test_format(self): |
| 341 | for x in special: |
| 342 | self.check_format_1(x) |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 343 | for i in range(10): |
| 344 | for lenx in range(1, MAXDIGITS+1): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 345 | x = self.getran(lenx) |
| 346 | self.check_format_1(x) |
Guido van Rossum | 4365cab | 1998-08-13 14:20:17 +0000 | [diff] [blame] | 347 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 348 | def test_long(self): |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 349 | # Check conversions from string |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 350 | LL = [ |
| 351 | ('1' + '0'*20, 10**20), |
| 352 | ('1' + '0'*100, 10**100) |
| 353 | ] |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 354 | for s, v in LL: |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 355 | for sign in "", "+", "-": |
| 356 | for prefix in "", " ", "\t", " \t\t ": |
| 357 | ss = prefix + sign + s |
| 358 | vv = v |
| 359 | if sign == "-" and v is not ValueError: |
| 360 | vv = -v |
| 361 | try: |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 362 | self.assertEqual(int(ss), vv) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 363 | except ValueError: |
| 364 | pass |
| 365 | |
Mark Dickinson | 9ffc020 | 2009-01-20 20:45:53 +0000 | [diff] [blame] | 366 | # trailing L should no longer be accepted... |
| 367 | self.assertRaises(ValueError, int, '123L') |
| 368 | self.assertRaises(ValueError, int, '123l') |
| 369 | self.assertRaises(ValueError, int, '0L') |
| 370 | self.assertRaises(ValueError, int, '-37L') |
| 371 | self.assertRaises(ValueError, int, '0x32L', 16) |
| 372 | self.assertRaises(ValueError, int, '1L', 21) |
| 373 | # ... but it's just a normal digit if base >= 22 |
| 374 | self.assertEqual(int('1L', 22), 43) |
| 375 | |
Mark Dickinson | 56544db | 2010-05-26 19:14:01 +0000 | [diff] [blame] | 376 | # tests with base 0 |
| 377 | self.assertEqual(int('000', 0), 0) |
| 378 | self.assertEqual(int('0o123', 0), 83) |
| 379 | self.assertEqual(int('0x123', 0), 291) |
| 380 | self.assertEqual(int('0b100', 0), 4) |
| 381 | self.assertEqual(int(' 0O123 ', 0), 83) |
| 382 | self.assertEqual(int(' 0X123 ', 0), 291) |
| 383 | self.assertEqual(int(' 0B100 ', 0), 4) |
| 384 | self.assertEqual(int('0', 0), 0) |
| 385 | self.assertEqual(int('+0', 0), 0) |
| 386 | self.assertEqual(int('-0', 0), 0) |
| 387 | self.assertEqual(int('00', 0), 0) |
| 388 | self.assertRaises(ValueError, int, '08', 0) |
| 389 | self.assertRaises(ValueError, int, '-012395', 0) |
| 390 | |
Mark Dickinson | f9a5a8e | 2010-05-26 20:07:58 +0000 | [diff] [blame] | 391 | # invalid bases |
| 392 | invalid_bases = [-909, |
| 393 | 2**31-1, 2**31, -2**31, -2**31-1, |
| 394 | 2**63-1, 2**63, -2**63, -2**63-1, |
| 395 | 2**100, -2**100, |
| 396 | ] |
| 397 | for base in invalid_bases: |
| 398 | self.assertRaises(ValueError, int, '42', base) |
| 399 | |
| 400 | |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 401 | def test_conversion(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 402 | |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 403 | class JustLong: |
| 404 | # test that __long__ no longer used in 3.x |
| 405 | def __long__(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 406 | return 42 |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 407 | self.assertRaises(TypeError, int, JustLong()) |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 408 | |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 409 | class LongTrunc: |
| 410 | # __long__ should be ignored in 3.x |
| 411 | def __long__(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 412 | return 42 |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 413 | def __trunc__(self): |
| 414 | return 1729 |
| 415 | self.assertEqual(int(LongTrunc()), 1729) |
Tim Peters | 26c7fa3 | 2001-08-23 22:56:21 +0000 | [diff] [blame] | 416 | |
Mark Dickinson | 30970e9 | 2011-10-23 20:07:13 +0100 | [diff] [blame] | 417 | def check_float_conversion(self, n): |
| 418 | # Check that int -> float conversion behaviour matches |
| 419 | # that of the pure Python version above. |
| 420 | try: |
| 421 | actual = float(n) |
| 422 | except OverflowError: |
| 423 | actual = 'overflow' |
| 424 | |
| 425 | try: |
| 426 | expected = int_to_float(n) |
| 427 | except OverflowError: |
| 428 | expected = 'overflow' |
| 429 | |
| 430 | msg = ("Error in conversion of integer {} to float. " |
| 431 | "Got {}, expected {}.".format(n, actual, expected)) |
| 432 | self.assertEqual(actual, expected, msg) |
| 433 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 434 | @support.requires_IEEE_754 |
Mark Dickinson | c630039 | 2009-04-20 21:38:00 +0000 | [diff] [blame] | 435 | def test_float_conversion(self): |
Mark Dickinson | c630039 | 2009-04-20 21:38:00 +0000 | [diff] [blame] | 436 | |
| 437 | exact_values = [0, 1, 2, |
| 438 | 2**53-3, |
| 439 | 2**53-2, |
| 440 | 2**53-1, |
| 441 | 2**53, |
| 442 | 2**53+2, |
| 443 | 2**54-4, |
| 444 | 2**54-2, |
| 445 | 2**54, |
| 446 | 2**54+4] |
| 447 | for x in exact_values: |
| 448 | self.assertEqual(float(x), x) |
| 449 | self.assertEqual(float(-x), -x) |
| 450 | |
| 451 | # test round-half-even |
| 452 | for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]: |
| 453 | for p in range(15): |
| 454 | self.assertEqual(int(float(2**p*(2**53+x))), 2**p*(2**53+y)) |
| 455 | |
| 456 | for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8), |
| 457 | (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12), |
| 458 | (13, 12), (14, 16), (15, 16)]: |
| 459 | for p in range(15): |
| 460 | self.assertEqual(int(float(2**p*(2**54+x))), 2**p*(2**54+y)) |
| 461 | |
| 462 | # behaviour near extremes of floating-point range |
| 463 | int_dbl_max = int(DBL_MAX) |
| 464 | top_power = 2**DBL_MAX_EXP |
| 465 | halfway = (int_dbl_max + top_power)//2 |
| 466 | self.assertEqual(float(int_dbl_max), DBL_MAX) |
| 467 | self.assertEqual(float(int_dbl_max+1), DBL_MAX) |
| 468 | self.assertEqual(float(halfway-1), DBL_MAX) |
| 469 | self.assertRaises(OverflowError, float, halfway) |
| 470 | self.assertEqual(float(1-halfway), -DBL_MAX) |
| 471 | self.assertRaises(OverflowError, float, -halfway) |
| 472 | self.assertRaises(OverflowError, float, top_power-1) |
| 473 | self.assertRaises(OverflowError, float, top_power) |
| 474 | self.assertRaises(OverflowError, float, top_power+1) |
| 475 | self.assertRaises(OverflowError, float, 2*top_power-1) |
| 476 | self.assertRaises(OverflowError, float, 2*top_power) |
| 477 | self.assertRaises(OverflowError, float, top_power*top_power) |
| 478 | |
| 479 | for p in range(100): |
| 480 | x = 2**p * (2**53 + 1) + 1 |
| 481 | y = 2**p * (2**53 + 2) |
| 482 | self.assertEqual(int(float(x)), y) |
| 483 | |
| 484 | x = 2**p * (2**53 + 1) |
| 485 | y = 2**p * 2**53 |
| 486 | self.assertEqual(int(float(x)), y) |
| 487 | |
Mark Dickinson | 30970e9 | 2011-10-23 20:07:13 +0100 | [diff] [blame] | 488 | # Compare builtin float conversion with pure Python int_to_float |
| 489 | # function above. |
| 490 | test_values = [ |
| 491 | int_dbl_max-1, int_dbl_max, int_dbl_max+1, |
| 492 | halfway-1, halfway, halfway + 1, |
| 493 | top_power-1, top_power, top_power+1, |
| 494 | 2*top_power-1, 2*top_power, top_power*top_power, |
| 495 | ] |
| 496 | test_values.extend(exact_values) |
| 497 | for p in range(-4, 8): |
| 498 | for x in range(-128, 128): |
| 499 | test_values.append(2**(p+53) + x) |
| 500 | for value in test_values: |
| 501 | self.check_float_conversion(value) |
| 502 | self.check_float_conversion(-value) |
| 503 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 504 | def test_float_overflow(self): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 505 | 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] | 506 | self.assertEqual(float(int(x)), x) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 507 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 508 | shuge = '12345' * 120 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 509 | huge = 1 << 30000 |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 510 | mhuge = -huge |
| 511 | namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math} |
| 512 | for test in ["float(huge)", "float(mhuge)", |
| 513 | "complex(huge)", "complex(mhuge)", |
| 514 | "complex(huge, 1)", "complex(mhuge, 1)", |
| 515 | "complex(1, huge)", "complex(1, mhuge)", |
| 516 | "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.", |
| 517 | "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.", |
| 518 | "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.", |
| 519 | "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.", |
| 520 | "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.", |
| 521 | "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.", |
| 522 | "math.sin(huge)", "math.sin(mhuge)", |
| 523 | "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better |
Guido van Rossum | 28bbe42 | 2007-08-24 03:46:30 +0000 | [diff] [blame] | 524 | # math.floor() of an int returns an int now |
| 525 | ##"math.floor(huge)", "math.floor(mhuge)", |
| 526 | ]: |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 527 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 528 | self.assertRaises(OverflowError, eval, test, namespace) |
Tim Peters | 9fffa3e | 2001-09-04 05:14:19 +0000 | [diff] [blame] | 529 | |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 530 | # XXX Perhaps float(shuge) can raise OverflowError on some box? |
| 531 | # The comparison should not. |
| 532 | self.assertNotEqual(float(shuge), int(shuge), |
| 533 | "float(shuge) should not equal int(shuge)") |
Tim Peters | 83e7ccc | 2001-09-04 06:37:28 +0000 | [diff] [blame] | 534 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 535 | def test_logs(self): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 536 | LOG10E = math.log10(math.e) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 537 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 538 | for exp in list(range(10)) + [100, 1000, 10000]: |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 539 | value = 10 ** exp |
| 540 | log10 = math.log10(value) |
| 541 | self.assertAlmostEqual(log10, exp) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 542 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 543 | # log10(value) == exp, so log(value) == log10(value)/log10(e) == |
| 544 | # exp/LOG10E |
| 545 | expected = exp / LOG10E |
| 546 | log = math.log(value) |
| 547 | self.assertAlmostEqual(log, expected) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 548 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 549 | for bad in -(1 << 10000), -2, 0: |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 550 | self.assertRaises(ValueError, math.log, bad) |
| 551 | self.assertRaises(ValueError, math.log10, bad) |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 552 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 553 | def test_mixed_compares(self): |
| 554 | eq = self.assertEqual |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 555 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 556 | # We're mostly concerned with that mixing floats and longs does the |
| 557 | # right stuff, even when longs are too large to fit in a float. |
| 558 | # The safest way to check the results is to use an entirely different |
| 559 | # method, which we do here via a skeletal rational class (which |
| 560 | # represents all Python ints, longs and floats exactly). |
| 561 | class Rat: |
| 562 | def __init__(self, value): |
Walter Dörwald | aa97f04 | 2007-05-03 21:05:51 +0000 | [diff] [blame] | 563 | if isinstance(value, int): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 564 | self.n = value |
| 565 | self.d = 1 |
| 566 | elif isinstance(value, float): |
| 567 | # Convert to exact rational equivalent. |
| 568 | f, e = math.frexp(abs(value)) |
| 569 | assert f == 0 or 0.5 <= f < 1.0 |
| 570 | # |value| = f * 2**e exactly |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 571 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 572 | # Suck up CHUNK bits at a time; 28 is enough so that we suck |
| 573 | # up all bits in 2 iterations for all known binary double- |
| 574 | # precision formats, and small enough to fit in an int. |
| 575 | CHUNK = 28 |
| 576 | top = 0 |
| 577 | # invariant: |value| = (top + f) * 2**e exactly |
| 578 | while f: |
| 579 | f = math.ldexp(f, CHUNK) |
| 580 | digit = int(f) |
| 581 | assert digit >> CHUNK == 0 |
| 582 | top = (top << CHUNK) | digit |
| 583 | f -= digit |
| 584 | assert 0.0 <= f < 1.0 |
| 585 | e -= CHUNK |
Tim Peters | 7852616 | 2001-09-05 00:53:45 +0000 | [diff] [blame] | 586 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 587 | # Now |value| = top * 2**e exactly. |
| 588 | if e >= 0: |
| 589 | n = top << e |
| 590 | d = 1 |
| 591 | else: |
| 592 | n = top |
| 593 | d = 1 << -e |
| 594 | if value < 0: |
| 595 | n = -n |
| 596 | self.n = n |
| 597 | self.d = d |
| 598 | assert float(n) / float(d) == value |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 599 | else: |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 600 | raise TypeError("can't deal with %r" % value) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 601 | |
Benjamin Peterson | 6019208 | 2008-10-16 19:34:46 +0000 | [diff] [blame] | 602 | def _cmp__(self, other): |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 603 | if not isinstance(other, Rat): |
| 604 | other = Rat(other) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 605 | x, y = self.n * other.d, self.d * other.n |
| 606 | return (x > y) - (x < y) |
Benjamin Peterson | 6019208 | 2008-10-16 19:34:46 +0000 | [diff] [blame] | 607 | def __eq__(self, other): |
| 608 | return self._cmp__(other) == 0 |
| 609 | def __ne__(self, other): |
| 610 | return self._cmp__(other) != 0 |
| 611 | def __ge__(self, other): |
| 612 | return self._cmp__(other) >= 0 |
| 613 | def __gt__(self, other): |
| 614 | return self._cmp__(other) > 0 |
| 615 | def __le__(self, other): |
| 616 | return self._cmp__(other) <= 0 |
| 617 | def __lt__(self, other): |
| 618 | return self._cmp__(other) < 0 |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 619 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 620 | cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200] |
| 621 | # 2**48 is an important boundary in the internals. 2**53 is an |
| 622 | # important boundary for IEEE double precision. |
| 623 | for t in 2.0**48, 2.0**50, 2.0**53: |
| 624 | 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] | 625 | int(t-1), int(t), int(t+1)]) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 626 | cases.extend([0, 1, 2, sys.maxsize, float(sys.maxsize)]) |
Mark Dickinson | 5c2db37 | 2009-12-05 20:28:34 +0000 | [diff] [blame] | 627 | # 1 << 20000 should exceed all double formats. int(1e200) is to |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 628 | # check that we get equality with 1e200 above. |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 629 | t = int(1e200) |
| 630 | 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] | 631 | cases.extend([-x for x in cases]) |
| 632 | for x in cases: |
| 633 | Rx = Rat(x) |
| 634 | for y in cases: |
| 635 | Ry = Rat(y) |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 636 | Rcmp = (Rx > Ry) - (Rx < Ry) |
| 637 | xycmp = (x > y) - (x < y) |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 638 | eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp)) |
| 639 | eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp)) |
| 640 | eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp)) |
| 641 | eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp)) |
| 642 | eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp)) |
| 643 | eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp)) |
| 644 | eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp)) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 645 | |
Eric Smith | 0dd1b63 | 2008-02-11 17:55:01 +0000 | [diff] [blame] | 646 | def test__format__(self): |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 647 | self.assertEqual(format(123456789, 'd'), '123456789') |
| 648 | self.assertEqual(format(123456789, 'd'), '123456789') |
| 649 | |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 650 | # sign and aligning are interdependent |
| 651 | self.assertEqual(format(1, "-"), '1') |
| 652 | self.assertEqual(format(-1, "-"), '-1') |
| 653 | self.assertEqual(format(1, "-3"), ' 1') |
| 654 | self.assertEqual(format(-1, "-3"), ' -1') |
| 655 | self.assertEqual(format(1, "+3"), ' +1') |
| 656 | self.assertEqual(format(-1, "+3"), ' -1') |
| 657 | self.assertEqual(format(1, " 3"), ' 1') |
| 658 | self.assertEqual(format(-1, " 3"), ' -1') |
| 659 | self.assertEqual(format(1, " "), ' 1') |
| 660 | self.assertEqual(format(-1, " "), '-1') |
| 661 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 662 | # hex |
| 663 | self.assertEqual(format(3, "x"), "3") |
| 664 | self.assertEqual(format(3, "X"), "3") |
| 665 | self.assertEqual(format(1234, "x"), "4d2") |
| 666 | self.assertEqual(format(-1234, "x"), "-4d2") |
| 667 | self.assertEqual(format(1234, "8x"), " 4d2") |
Eric Smith | 185e30c | 2007-08-30 22:23:08 +0000 | [diff] [blame] | 668 | self.assertEqual(format(-1234, "8x"), " -4d2") |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 669 | self.assertEqual(format(1234, "x"), "4d2") |
| 670 | self.assertEqual(format(-1234, "x"), "-4d2") |
| 671 | self.assertEqual(format(-3, "x"), "-3") |
| 672 | self.assertEqual(format(-3, "X"), "-3") |
| 673 | self.assertEqual(format(int('be', 16), "x"), "be") |
| 674 | self.assertEqual(format(int('be', 16), "X"), "BE") |
| 675 | self.assertEqual(format(-int('be', 16), "x"), "-be") |
| 676 | self.assertEqual(format(-int('be', 16), "X"), "-BE") |
| 677 | |
| 678 | # octal |
| 679 | self.assertEqual(format(3, "b"), "11") |
| 680 | self.assertEqual(format(-3, "b"), "-11") |
| 681 | self.assertEqual(format(1234, "b"), "10011010010") |
| 682 | self.assertEqual(format(-1234, "b"), "-10011010010") |
| 683 | self.assertEqual(format(1234, "-b"), "10011010010") |
| 684 | self.assertEqual(format(-1234, "-b"), "-10011010010") |
| 685 | self.assertEqual(format(1234, " b"), " 10011010010") |
| 686 | self.assertEqual(format(-1234, " b"), "-10011010010") |
| 687 | self.assertEqual(format(1234, "+b"), "+10011010010") |
| 688 | self.assertEqual(format(-1234, "+b"), "-10011010010") |
| 689 | |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 690 | # make sure these are errors |
| 691 | self.assertRaises(ValueError, format, 3, "1.3") # precision disallowed |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 692 | self.assertRaises(ValueError, format, 3, "+c") # sign not allowed |
| 693 | # with 'c' |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 694 | |
| 695 | # ensure that only int and float type specifiers work |
Eric Smith | 7b69c6c | 2008-01-27 21:07:59 +0000 | [diff] [blame] | 696 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 697 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 698 | if not format_spec in 'bcdoxXeEfFgGn%': |
Eric Smith | 7b69c6c | 2008-01-27 21:07:59 +0000 | [diff] [blame] | 699 | self.assertRaises(ValueError, format, 0, format_spec) |
| 700 | self.assertRaises(ValueError, format, 1, format_spec) |
| 701 | self.assertRaises(ValueError, format, -1, format_spec) |
| 702 | self.assertRaises(ValueError, format, 2**100, format_spec) |
| 703 | self.assertRaises(ValueError, format, -(2**100), format_spec) |
| 704 | |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 705 | # ensure that float type specifiers work; format converts |
| 706 | # the int to a float |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 707 | for format_spec in 'eEfFgG%': |
Eric Smith | fa767ef | 2008-01-28 10:59:27 +0000 | [diff] [blame] | 708 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 709 | self.assertEqual(format(value, format_spec), |
| 710 | format(float(value), format_spec)) |
Eric Smith | 8c66326 | 2007-08-25 02:26:07 +0000 | [diff] [blame] | 711 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 712 | def test_nan_inf(self): |
Christian Heimes | 1aa7b30 | 2008-01-04 03:22:53 +0000 | [diff] [blame] | 713 | self.assertRaises(OverflowError, int, float('inf')) |
Georg Brandl | 6aa2d1f | 2008-08-12 08:35:52 +0000 | [diff] [blame] | 714 | self.assertRaises(OverflowError, int, float('-inf')) |
| 715 | self.assertRaises(ValueError, int, float('nan')) |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 716 | |
Benjamin Peterson | 875d4c0 | 2008-07-13 17:44:16 +0000 | [diff] [blame] | 717 | def test_true_division(self): |
| 718 | huge = 1 << 40000 |
| 719 | mhuge = -huge |
| 720 | self.assertEqual(huge / huge, 1.0) |
| 721 | self.assertEqual(mhuge / mhuge, 1.0) |
| 722 | self.assertEqual(huge / mhuge, -1.0) |
| 723 | self.assertEqual(mhuge / huge, -1.0) |
| 724 | self.assertEqual(1 / huge, 0.0) |
| 725 | self.assertEqual(1 / huge, 0.0) |
| 726 | self.assertEqual(1 / mhuge, 0.0) |
| 727 | self.assertEqual(1 / mhuge, 0.0) |
| 728 | self.assertEqual((666 * huge + (huge >> 1)) / huge, 666.5) |
| 729 | self.assertEqual((666 * mhuge + (mhuge >> 1)) / mhuge, 666.5) |
| 730 | self.assertEqual((666 * huge + (huge >> 1)) / mhuge, -666.5) |
| 731 | self.assertEqual((666 * mhuge + (mhuge >> 1)) / huge, -666.5) |
| 732 | self.assertEqual(huge / (huge << 1), 0.5) |
| 733 | self.assertEqual((1000000 * huge) / huge, 1000000) |
| 734 | |
| 735 | namespace = {'huge': huge, 'mhuge': mhuge} |
| 736 | |
| 737 | for overflow in ["float(huge)", "float(mhuge)", |
| 738 | "huge / 1", "huge / 2", "huge / -1", "huge / -2", |
| 739 | "mhuge / 100", "mhuge / 200"]: |
| 740 | self.assertRaises(OverflowError, eval, overflow, namespace) |
| 741 | |
| 742 | for underflow in ["1 / huge", "2 / huge", "-1 / huge", "-2 / huge", |
| 743 | "100 / mhuge", "200 / mhuge"]: |
| 744 | result = eval(underflow, namespace) |
| 745 | self.assertEqual(result, 0.0, |
| 746 | "expected underflow to 0 from %r" % underflow) |
| 747 | |
| 748 | for zero in ["huge / 0", "mhuge / 0"]: |
| 749 | self.assertRaises(ZeroDivisionError, eval, zero, namespace) |
| 750 | |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 751 | def check_truediv(self, a, b, skip_small=True): |
| 752 | """Verify that the result of a/b is correctly rounded, by |
| 753 | comparing it with a pure Python implementation of correctly |
| 754 | rounded division. b should be nonzero.""" |
| 755 | |
| 756 | # skip check for small a and b: in this case, the current |
| 757 | # implementation converts the arguments to float directly and |
| 758 | # then applies a float division. This can give doubly-rounded |
| 759 | # results on x87-using machines (particularly 32-bit Linux). |
| 760 | if skip_small and max(abs(a), abs(b)) < 2**DBL_MANT_DIG: |
| 761 | return |
| 762 | |
| 763 | try: |
| 764 | # use repr so that we can distinguish between -0.0 and 0.0 |
| 765 | expected = repr(truediv(a, b)) |
| 766 | except OverflowError: |
| 767 | expected = 'overflow' |
| 768 | except ZeroDivisionError: |
| 769 | expected = 'zerodivision' |
| 770 | |
| 771 | try: |
| 772 | got = repr(a / b) |
| 773 | except OverflowError: |
| 774 | got = 'overflow' |
| 775 | except ZeroDivisionError: |
| 776 | got = 'zerodivision' |
| 777 | |
Mark Dickinson | 2cfda80 | 2009-12-27 21:34:05 +0000 | [diff] [blame] | 778 | self.assertEqual(expected, got, "Incorrectly rounded division {}/{}: " |
| 779 | "expected {}, got {}".format(a, b, expected, got)) |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 780 | |
Eric Smith | 3ab08ca | 2010-12-04 15:17:38 +0000 | [diff] [blame] | 781 | @support.requires_IEEE_754 |
Mark Dickinson | cbb6274 | 2009-12-27 15:09:50 +0000 | [diff] [blame] | 782 | def test_correctly_rounded_true_division(self): |
| 783 | # more stringent tests than those above, checking that the |
| 784 | # result of true division of ints is always correctly rounded. |
| 785 | # This test should probably be considered CPython-specific. |
| 786 | |
| 787 | # Exercise all the code paths not involving Gb-sized ints. |
| 788 | # ... divisions involving zero |
| 789 | self.check_truediv(123, 0) |
| 790 | self.check_truediv(-456, 0) |
| 791 | self.check_truediv(0, 3) |
| 792 | self.check_truediv(0, -3) |
| 793 | self.check_truediv(0, 0) |
| 794 | # ... overflow or underflow by large margin |
| 795 | self.check_truediv(671 * 12345 * 2**DBL_MAX_EXP, 12345) |
| 796 | self.check_truediv(12345, 345678 * 2**(DBL_MANT_DIG - DBL_MIN_EXP)) |
| 797 | # ... a much larger or smaller than b |
| 798 | self.check_truediv(12345*2**100, 98765) |
| 799 | self.check_truediv(12345*2**30, 98765*7**81) |
| 800 | # ... a / b near a boundary: one of 1, 2**DBL_MANT_DIG, 2**DBL_MIN_EXP, |
| 801 | # 2**DBL_MAX_EXP, 2**(DBL_MIN_EXP-DBL_MANT_DIG) |
| 802 | bases = (0, DBL_MANT_DIG, DBL_MIN_EXP, |
| 803 | DBL_MAX_EXP, DBL_MIN_EXP - DBL_MANT_DIG) |
| 804 | for base in bases: |
| 805 | for exp in range(base - 15, base + 15): |
| 806 | self.check_truediv(75312*2**max(exp, 0), 69187*2**max(-exp, 0)) |
| 807 | self.check_truediv(69187*2**max(exp, 0), 75312*2**max(-exp, 0)) |
| 808 | |
| 809 | # overflow corner case |
| 810 | for m in [1, 2, 7, 17, 12345, 7**100, |
| 811 | -1, -2, -5, -23, -67891, -41**50]: |
| 812 | for n in range(-10, 10): |
| 813 | self.check_truediv(m*DBL_MIN_OVERFLOW + n, m) |
| 814 | self.check_truediv(m*DBL_MIN_OVERFLOW + n, -m) |
| 815 | |
| 816 | # check detection of inexactness in shifting stage |
| 817 | for n in range(250): |
| 818 | # (2**DBL_MANT_DIG+1)/(2**DBL_MANT_DIG) lies halfway |
| 819 | # between two representable floats, and would usually be |
| 820 | # rounded down under round-half-to-even. The tiniest of |
| 821 | # additions to the numerator should cause it to be rounded |
| 822 | # up instead. |
| 823 | self.check_truediv((2**DBL_MANT_DIG + 1)*12345*2**200 + 2**n, |
| 824 | 2**DBL_MANT_DIG*12345) |
| 825 | |
| 826 | # 1/2731 is one of the smallest division cases that's subject |
| 827 | # to double rounding on IEEE 754 machines working internally with |
| 828 | # 64-bit precision. On such machines, the next check would fail, |
| 829 | # were it not explicitly skipped in check_truediv. |
| 830 | self.check_truediv(1, 2731) |
| 831 | |
| 832 | # a particularly bad case for the old algorithm: gives an |
| 833 | # error of close to 3.5 ulps. |
| 834 | self.check_truediv(295147931372582273023, 295147932265116303360) |
| 835 | for i in range(1000): |
| 836 | self.check_truediv(10**(i+1), 10**i) |
| 837 | self.check_truediv(10**i, 10**(i+1)) |
| 838 | |
| 839 | # test round-half-to-even behaviour, normal result |
| 840 | for m in [1, 2, 4, 7, 8, 16, 17, 32, 12345, 7**100, |
| 841 | -1, -2, -5, -23, -67891, -41**50]: |
| 842 | for n in range(-10, 10): |
| 843 | self.check_truediv(2**DBL_MANT_DIG*m + n, m) |
| 844 | |
| 845 | # test round-half-to-even, subnormal result |
| 846 | for n in range(-20, 20): |
| 847 | self.check_truediv(n, 2**1076) |
| 848 | |
| 849 | # largeish random divisions: a/b where |a| <= |b| <= |
| 850 | # 2*|a|; |ans| is between 0.5 and 1.0, so error should |
| 851 | # always be bounded by 2**-54 with equality possible only |
| 852 | # if the least significant bit of q=ans*2**53 is zero. |
| 853 | for M in [10**10, 10**100, 10**1000]: |
| 854 | for i in range(1000): |
| 855 | a = random.randrange(1, M) |
| 856 | b = random.randrange(a, 2*a+1) |
| 857 | self.check_truediv(a, b) |
| 858 | self.check_truediv(-a, b) |
| 859 | self.check_truediv(a, -b) |
| 860 | self.check_truediv(-a, -b) |
| 861 | |
| 862 | # and some (genuinely) random tests |
| 863 | for _ in range(10000): |
| 864 | a_bits = random.randrange(1000) |
| 865 | b_bits = random.randrange(1, 1000) |
| 866 | x = random.randrange(2**a_bits) |
| 867 | y = random.randrange(1, 2**b_bits) |
| 868 | self.check_truediv(x, y) |
| 869 | self.check_truediv(x, -y) |
| 870 | self.check_truediv(-x, y) |
| 871 | self.check_truediv(-x, -y) |
Benjamin Peterson | 875d4c0 | 2008-07-13 17:44:16 +0000 | [diff] [blame] | 872 | |
Facundo Batista | 6e6f59b | 2008-07-24 18:57:11 +0000 | [diff] [blame] | 873 | def test_small_ints(self): |
| 874 | for i in range(-5, 257): |
| 875 | self.assertTrue(i is i + 0) |
| 876 | self.assertTrue(i is i * 1) |
| 877 | self.assertTrue(i is i - 0) |
| 878 | self.assertTrue(i is i // 1) |
| 879 | self.assertTrue(i is i & -1) |
| 880 | self.assertTrue(i is i | 0) |
| 881 | self.assertTrue(i is i ^ 0) |
| 882 | self.assertTrue(i is ~~i) |
| 883 | self.assertTrue(i is i**1) |
| 884 | self.assertTrue(i is int(str(i))) |
| 885 | self.assertTrue(i is i<<2>>2, str(i)) |
| 886 | # corner cases |
| 887 | i = 1 << 70 |
| 888 | self.assertTrue(i - i is 0) |
| 889 | self.assertTrue(0 * i is 0) |
| 890 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 891 | def test_bit_length(self): |
| 892 | tiny = 1e-10 |
| 893 | for x in range(-65000, 65000): |
| 894 | k = x.bit_length() |
| 895 | # Check equivalence with Python version |
| 896 | self.assertEqual(k, len(bin(x).lstrip('-0b'))) |
| 897 | # Behaviour as specified in the docs |
| 898 | if x != 0: |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 899 | self.assertTrue(2**(k-1) <= abs(x) < 2**k) |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 900 | else: |
| 901 | self.assertEqual(k, 0) |
| 902 | # Alternative definition: x.bit_length() == 1 + floor(log_2(x)) |
| 903 | if x != 0: |
| 904 | # When x is an exact power of 2, numeric errors can |
| 905 | # cause floor(log(x)/log(2)) to be one too small; for |
| 906 | # small x this can be fixed by adding a small quantity |
| 907 | # to the quotient before taking the floor. |
| 908 | self.assertEqual(k, 1 + math.floor( |
| 909 | math.log(abs(x))/math.log(2) + tiny)) |
| 910 | |
| 911 | self.assertEqual((0).bit_length(), 0) |
| 912 | self.assertEqual((1).bit_length(), 1) |
| 913 | self.assertEqual((-1).bit_length(), 1) |
| 914 | self.assertEqual((2).bit_length(), 2) |
| 915 | self.assertEqual((-2).bit_length(), 2) |
| 916 | for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]: |
| 917 | a = 2**i |
| 918 | self.assertEqual((a-1).bit_length(), i) |
| 919 | self.assertEqual((1-a).bit_length(), i) |
| 920 | self.assertEqual((a).bit_length(), i+1) |
| 921 | self.assertEqual((-a).bit_length(), i+1) |
| 922 | self.assertEqual((a+1).bit_length(), i+1) |
| 923 | self.assertEqual((-a-1).bit_length(), i+1) |
| 924 | |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 925 | def test_round(self): |
| 926 | # check round-half-even algorithm. For round to nearest ten; |
| 927 | # rounding map is invariant under adding multiples of 20 |
| 928 | test_dict = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0, |
| 929 | 6:10, 7:10, 8:10, 9:10, 10:10, 11:10, 12:10, 13:10, 14:10, |
| 930 | 15:20, 16:20, 17:20, 18:20, 19:20} |
| 931 | for offset in range(-520, 520, 20): |
| 932 | for k, v in test_dict.items(): |
| 933 | got = round(k+offset, -1) |
| 934 | expected = v+offset |
| 935 | self.assertEqual(got, expected) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 936 | self.assertTrue(type(got) is int) |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 937 | |
| 938 | # larger second argument |
| 939 | self.assertEqual(round(-150, -2), -200) |
| 940 | self.assertEqual(round(-149, -2), -100) |
| 941 | self.assertEqual(round(-51, -2), -100) |
| 942 | self.assertEqual(round(-50, -2), 0) |
| 943 | self.assertEqual(round(-49, -2), 0) |
| 944 | self.assertEqual(round(-1, -2), 0) |
| 945 | self.assertEqual(round(0, -2), 0) |
| 946 | self.assertEqual(round(1, -2), 0) |
| 947 | self.assertEqual(round(49, -2), 0) |
| 948 | self.assertEqual(round(50, -2), 0) |
| 949 | self.assertEqual(round(51, -2), 100) |
| 950 | self.assertEqual(round(149, -2), 100) |
| 951 | self.assertEqual(round(150, -2), 200) |
| 952 | self.assertEqual(round(250, -2), 200) |
| 953 | self.assertEqual(round(251, -2), 300) |
| 954 | self.assertEqual(round(172500, -3), 172000) |
| 955 | self.assertEqual(round(173500, -3), 174000) |
| 956 | self.assertEqual(round(31415926535, -1), 31415926540) |
| 957 | self.assertEqual(round(31415926535, -2), 31415926500) |
| 958 | self.assertEqual(round(31415926535, -3), 31415927000) |
| 959 | self.assertEqual(round(31415926535, -4), 31415930000) |
| 960 | self.assertEqual(round(31415926535, -5), 31415900000) |
| 961 | self.assertEqual(round(31415926535, -6), 31416000000) |
| 962 | self.assertEqual(round(31415926535, -7), 31420000000) |
| 963 | self.assertEqual(round(31415926535, -8), 31400000000) |
| 964 | self.assertEqual(round(31415926535, -9), 31000000000) |
| 965 | self.assertEqual(round(31415926535, -10), 30000000000) |
| 966 | self.assertEqual(round(31415926535, -11), 0) |
| 967 | self.assertEqual(round(31415926535, -12), 0) |
| 968 | self.assertEqual(round(31415926535, -999), 0) |
| 969 | |
| 970 | # should get correct results even for huge inputs |
| 971 | for k in range(10, 100): |
| 972 | got = round(10**k + 324678, -3) |
| 973 | expect = 10**k + 325000 |
| 974 | self.assertEqual(got, expect) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 975 | self.assertTrue(type(got) is int) |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 976 | |
| 977 | # nonnegative second argument: round(x, n) should just return x |
| 978 | for n in range(5): |
| 979 | for i in range(100): |
| 980 | x = random.randrange(-10000, 10000) |
| 981 | got = round(x, n) |
| 982 | self.assertEqual(got, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 983 | self.assertTrue(type(got) is int) |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 984 | for huge_n in 2**31-1, 2**31, 2**63-1, 2**63, 2**100, 10**100: |
| 985 | self.assertEqual(round(8979323, huge_n), 8979323) |
| 986 | |
| 987 | # omitted second argument |
| 988 | for i in range(100): |
| 989 | x = random.randrange(-10000, 10000) |
| 990 | got = round(x) |
| 991 | self.assertEqual(got, x) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 992 | self.assertTrue(type(got) is int) |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 993 | |
| 994 | # bad second argument |
| 995 | bad_exponents = ('brian', 2.0, 0j, None) |
| 996 | for e in bad_exponents: |
| 997 | self.assertRaises(TypeError, round, 3, e) |
| 998 | |
Alexandre Vassalotti | c36c378 | 2010-01-09 20:35:09 +0000 | [diff] [blame] | 999 | def test_to_bytes(self): |
| 1000 | def check(tests, byteorder, signed=False): |
| 1001 | for test, expected in tests.items(): |
| 1002 | try: |
| 1003 | self.assertEqual( |
| 1004 | test.to_bytes(len(expected), byteorder, signed=signed), |
| 1005 | expected) |
| 1006 | except Exception as err: |
| 1007 | raise AssertionError( |
| 1008 | "failed to convert {0} with byteorder={1} and signed={2}" |
| 1009 | .format(test, byteorder, signed)) from err |
| 1010 | |
| 1011 | # Convert integers to signed big-endian byte arrays. |
| 1012 | tests1 = { |
| 1013 | 0: b'\x00', |
| 1014 | 1: b'\x01', |
| 1015 | -1: b'\xff', |
| 1016 | -127: b'\x81', |
| 1017 | -128: b'\x80', |
| 1018 | -129: b'\xff\x7f', |
| 1019 | 127: b'\x7f', |
| 1020 | 129: b'\x00\x81', |
| 1021 | -255: b'\xff\x01', |
| 1022 | -256: b'\xff\x00', |
| 1023 | 255: b'\x00\xff', |
| 1024 | 256: b'\x01\x00', |
| 1025 | 32767: b'\x7f\xff', |
| 1026 | -32768: b'\xff\x80\x00', |
| 1027 | 65535: b'\x00\xff\xff', |
| 1028 | -65536: b'\xff\x00\x00', |
| 1029 | -8388608: b'\x80\x00\x00' |
| 1030 | } |
| 1031 | check(tests1, 'big', signed=True) |
| 1032 | |
| 1033 | # Convert integers to signed little-endian byte arrays. |
| 1034 | tests2 = { |
| 1035 | 0: b'\x00', |
| 1036 | 1: b'\x01', |
| 1037 | -1: b'\xff', |
| 1038 | -127: b'\x81', |
| 1039 | -128: b'\x80', |
| 1040 | -129: b'\x7f\xff', |
| 1041 | 127: b'\x7f', |
| 1042 | 129: b'\x81\x00', |
| 1043 | -255: b'\x01\xff', |
| 1044 | -256: b'\x00\xff', |
| 1045 | 255: b'\xff\x00', |
| 1046 | 256: b'\x00\x01', |
| 1047 | 32767: b'\xff\x7f', |
| 1048 | -32768: b'\x00\x80', |
| 1049 | 65535: b'\xff\xff\x00', |
| 1050 | -65536: b'\x00\x00\xff', |
| 1051 | -8388608: b'\x00\x00\x80' |
| 1052 | } |
| 1053 | check(tests2, 'little', signed=True) |
| 1054 | |
| 1055 | # Convert integers to unsigned big-endian byte arrays. |
| 1056 | tests3 = { |
| 1057 | 0: b'\x00', |
| 1058 | 1: b'\x01', |
| 1059 | 127: b'\x7f', |
| 1060 | 128: b'\x80', |
| 1061 | 255: b'\xff', |
| 1062 | 256: b'\x01\x00', |
| 1063 | 32767: b'\x7f\xff', |
| 1064 | 32768: b'\x80\x00', |
| 1065 | 65535: b'\xff\xff', |
| 1066 | 65536: b'\x01\x00\x00' |
| 1067 | } |
| 1068 | check(tests3, 'big', signed=False) |
| 1069 | |
| 1070 | # Convert integers to unsigned little-endian byte arrays. |
| 1071 | tests4 = { |
| 1072 | 0: b'\x00', |
| 1073 | 1: b'\x01', |
| 1074 | 127: b'\x7f', |
| 1075 | 128: b'\x80', |
| 1076 | 255: b'\xff', |
| 1077 | 256: b'\x00\x01', |
| 1078 | 32767: b'\xff\x7f', |
| 1079 | 32768: b'\x00\x80', |
| 1080 | 65535: b'\xff\xff', |
| 1081 | 65536: b'\x00\x00\x01' |
| 1082 | } |
| 1083 | check(tests4, 'little', signed=False) |
| 1084 | |
| 1085 | self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=False) |
| 1086 | self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True) |
| 1087 | self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=False) |
| 1088 | self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=True) |
| 1089 | self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False), |
| 1090 | self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False) |
| 1091 | self.assertEqual((0).to_bytes(0, 'big'), b'') |
| 1092 | self.assertEqual((1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01') |
| 1093 | self.assertEqual((0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00') |
| 1094 | self.assertEqual((-1).to_bytes(5, 'big', signed=True), |
| 1095 | b'\xff\xff\xff\xff\xff') |
| 1096 | self.assertRaises(OverflowError, (1).to_bytes, 0, 'big') |
| 1097 | |
| 1098 | def test_from_bytes(self): |
| 1099 | def check(tests, byteorder, signed=False): |
| 1100 | for test, expected in tests.items(): |
| 1101 | try: |
| 1102 | self.assertEqual( |
| 1103 | int.from_bytes(test, byteorder, signed=signed), |
| 1104 | expected) |
| 1105 | except Exception as err: |
| 1106 | raise AssertionError( |
| 1107 | "failed to convert {0} with byteorder={1!r} and signed={2}" |
| 1108 | .format(test, byteorder, signed)) from err |
| 1109 | |
| 1110 | # Convert signed big-endian byte arrays to integers. |
| 1111 | tests1 = { |
| 1112 | b'': 0, |
| 1113 | b'\x00': 0, |
| 1114 | b'\x00\x00': 0, |
| 1115 | b'\x01': 1, |
| 1116 | b'\x00\x01': 1, |
| 1117 | b'\xff': -1, |
| 1118 | b'\xff\xff': -1, |
| 1119 | b'\x81': -127, |
| 1120 | b'\x80': -128, |
| 1121 | b'\xff\x7f': -129, |
| 1122 | b'\x7f': 127, |
| 1123 | b'\x00\x81': 129, |
| 1124 | b'\xff\x01': -255, |
| 1125 | b'\xff\x00': -256, |
| 1126 | b'\x00\xff': 255, |
| 1127 | b'\x01\x00': 256, |
| 1128 | b'\x7f\xff': 32767, |
| 1129 | b'\x80\x00': -32768, |
| 1130 | b'\x00\xff\xff': 65535, |
| 1131 | b'\xff\x00\x00': -65536, |
| 1132 | b'\x80\x00\x00': -8388608 |
| 1133 | } |
| 1134 | check(tests1, 'big', signed=True) |
| 1135 | |
| 1136 | # Convert signed little-endian byte arrays to integers. |
| 1137 | tests2 = { |
| 1138 | b'': 0, |
| 1139 | b'\x00': 0, |
| 1140 | b'\x00\x00': 0, |
| 1141 | b'\x01': 1, |
| 1142 | b'\x00\x01': 256, |
| 1143 | b'\xff': -1, |
| 1144 | b'\xff\xff': -1, |
| 1145 | b'\x81': -127, |
| 1146 | b'\x80': -128, |
| 1147 | b'\x7f\xff': -129, |
| 1148 | b'\x7f': 127, |
| 1149 | b'\x81\x00': 129, |
| 1150 | b'\x01\xff': -255, |
| 1151 | b'\x00\xff': -256, |
| 1152 | b'\xff\x00': 255, |
| 1153 | b'\x00\x01': 256, |
| 1154 | b'\xff\x7f': 32767, |
| 1155 | b'\x00\x80': -32768, |
| 1156 | b'\xff\xff\x00': 65535, |
| 1157 | b'\x00\x00\xff': -65536, |
| 1158 | b'\x00\x00\x80': -8388608 |
| 1159 | } |
| 1160 | check(tests2, 'little', signed=True) |
| 1161 | |
| 1162 | # Convert unsigned big-endian byte arrays to integers. |
| 1163 | tests3 = { |
| 1164 | b'': 0, |
| 1165 | b'\x00': 0, |
| 1166 | b'\x01': 1, |
| 1167 | b'\x7f': 127, |
| 1168 | b'\x80': 128, |
| 1169 | b'\xff': 255, |
| 1170 | b'\x01\x00': 256, |
| 1171 | b'\x7f\xff': 32767, |
| 1172 | b'\x80\x00': 32768, |
| 1173 | b'\xff\xff': 65535, |
| 1174 | b'\x01\x00\x00': 65536, |
| 1175 | } |
| 1176 | check(tests3, 'big', signed=False) |
| 1177 | |
| 1178 | # Convert integers to unsigned little-endian byte arrays. |
| 1179 | tests4 = { |
| 1180 | b'': 0, |
| 1181 | b'\x00': 0, |
| 1182 | b'\x01': 1, |
| 1183 | b'\x7f': 127, |
| 1184 | b'\x80': 128, |
| 1185 | b'\xff': 255, |
| 1186 | b'\x00\x01': 256, |
| 1187 | b'\xff\x7f': 32767, |
| 1188 | b'\x00\x80': 32768, |
| 1189 | b'\xff\xff': 65535, |
| 1190 | b'\x00\x00\x01': 65536, |
| 1191 | } |
| 1192 | check(tests4, 'little', signed=False) |
| 1193 | |
| 1194 | class myint(int): |
| 1195 | pass |
| 1196 | |
| 1197 | self.assertTrue(type(myint.from_bytes(b'\x00', 'big')) is myint) |
| 1198 | self.assertEqual(myint.from_bytes(b'\x01', 'big'), 1) |
| 1199 | self.assertTrue( |
| 1200 | type(myint.from_bytes(b'\x00', 'big', signed=False)) is myint) |
| 1201 | self.assertEqual(myint.from_bytes(b'\x01', 'big', signed=False), 1) |
| 1202 | self.assertTrue(type(myint.from_bytes(b'\x00', 'little')) is myint) |
| 1203 | self.assertEqual(myint.from_bytes(b'\x01', 'little'), 1) |
| 1204 | self.assertTrue(type(myint.from_bytes( |
| 1205 | b'\x00', 'little', signed=False)) is myint) |
| 1206 | self.assertEqual(myint.from_bytes(b'\x01', 'little', signed=False), 1) |
| 1207 | self.assertEqual( |
| 1208 | int.from_bytes([255, 0, 0], 'big', signed=True), -65536) |
| 1209 | self.assertEqual( |
| 1210 | int.from_bytes((255, 0, 0), 'big', signed=True), -65536) |
| 1211 | self.assertEqual(int.from_bytes( |
| 1212 | bytearray(b'\xff\x00\x00'), 'big', signed=True), -65536) |
| 1213 | self.assertEqual(int.from_bytes( |
| 1214 | bytearray(b'\xff\x00\x00'), 'big', signed=True), -65536) |
| 1215 | self.assertEqual(int.from_bytes( |
| 1216 | array.array('B', b'\xff\x00\x00'), 'big', signed=True), -65536) |
| 1217 | self.assertEqual(int.from_bytes( |
| 1218 | memoryview(b'\xff\x00\x00'), 'big', signed=True), -65536) |
| 1219 | self.assertRaises(ValueError, int.from_bytes, [256], 'big') |
| 1220 | self.assertRaises(ValueError, int.from_bytes, [0], 'big\x00') |
| 1221 | self.assertRaises(ValueError, int.from_bytes, [0], 'little\x00') |
| 1222 | self.assertRaises(TypeError, int.from_bytes, "", 'big') |
| 1223 | self.assertRaises(TypeError, int.from_bytes, "\x00", 'big') |
| 1224 | self.assertRaises(TypeError, int.from_bytes, 0, 'big') |
| 1225 | self.assertRaises(TypeError, int.from_bytes, 0, 'big', True) |
| 1226 | self.assertRaises(TypeError, myint.from_bytes, "", 'big') |
| 1227 | self.assertRaises(TypeError, myint.from_bytes, "\x00", 'big') |
| 1228 | self.assertRaises(TypeError, myint.from_bytes, 0, 'big') |
| 1229 | self.assertRaises(TypeError, int.from_bytes, 0, 'big', True) |
Mark Dickinson | 1124e71 | 2009-01-28 21:25:58 +0000 | [diff] [blame] | 1230 | |
Mark Dickinson | bcc17ee | 2012-04-20 21:42:49 +0100 | [diff] [blame] | 1231 | def test_access_to_nonexistent_digit_0(self): |
| 1232 | # http://bugs.python.org/issue14630: A bug in _PyLong_Copy meant that |
| 1233 | # ob_digit[0] was being incorrectly accessed for instances of a |
| 1234 | # subclass of int, with value 0. |
| 1235 | class Integer(int): |
| 1236 | def __new__(cls, value=0): |
| 1237 | self = int.__new__(cls, value) |
| 1238 | self.foo = 'foo' |
| 1239 | return self |
| 1240 | |
| 1241 | integers = [Integer(0) for i in range(1000)] |
| 1242 | for n in map(int, integers): |
| 1243 | self.assertEqual(n, 0) |
| 1244 | |
Mark Dickinson | 54bc1ec | 2008-12-17 16:19:07 +0000 | [diff] [blame] | 1245 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 1246 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 1247 | support.run_unittest(LongTest) |
Tim Peters | 307fa78 | 2004-09-23 08:06:40 +0000 | [diff] [blame] | 1248 | |
Walter Dörwald | a002159 | 2005-06-13 21:44:48 +0000 | [diff] [blame] | 1249 | if __name__ == "__main__": |
| 1250 | test_main() |