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