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