Debian Codegraph @

pypy/objspace/std/test/test_longobject.py

raw symbols SCIP
1 # -*- encoding: utf-8 -*-
2 import py
3 from pypy.objspace.std import longobject as lobj
4 from rpython.rlib.rbigint import rbigint
5
6 class TestW_LongObject:
7 def test_bigint_w(self):
8 space = self.space
9 fromlong = lobj.W_LongObject.fromlong
10 assert isinstance(space.bigint_w(fromlong(42)), rbigint)
11 assert space.bigint_w(fromlong(42)).eq(rbigint.fromint(42))
12 assert space.bigint_w(fromlong(-1)).eq(rbigint.fromint(-1))
13 w_obj = space.wrap("hello world")
14 space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
15 w_obj = space.wrap(123.456)
16 space.raises_w(space.w_TypeError, space.bigint_w, w_obj)
17
18 w_obj = fromlong(42)
19 assert space.unwrap(w_obj) == 42
20
21 def test_overflow_error(self):
22 space = self.space
23 fromlong = lobj.W_LongObject.fromlong
24 w_big = fromlong(10**900)
25 space.raises_w(space.w_OverflowError, space.float_w, w_big)
26
27 def test_rint_variants(self):
28 from rpython.rtyper.tool.rfficache import platform
29 space = self.space
30 for r in platform.numbertype_to_rclass.values():
31 if r is int:
32 continue
33 print r
34 values = [0, -1, r.MASK>>1, -(r.MASK>>1)-1]
35 for x in values:
36 if not r.SIGNED:
37 x &= r.MASK
38 w_obj = space.newlong_from_rarith_int(r(x))
39 assert space.bigint_w(w_obj).eq(rbigint.fromlong(x))
40
41 def test_int_mod_gives_int(self):
42 space = self.space
43 fromlong = lobj.W_LongObject.fromlong
44 x = fromlong(13)
45 y = space.newint(2)
46 z = space.mod(x, y)
47 assert space.eq_w(z, space.newint(1))
48 assert not isinstance(z, lobj.W_LongObject)
49
50 def test_space_newlong_gives_int(self):
51 space = self.space
52 w_x = space.newlong(2)
53 assert not isinstance(w_x, lobj.W_LongObject)
54
55 class AppTestLong:
56
57 def w__long(self, obj):
58 # XXX: currently returns a W_LongObject but might return
59 # W_IntObject in the future
60 huge = 1 << 65
61 return obj + huge - huge
62
63 def test_trunc(self):
64 import math
65 assert math.trunc(self._long(1)) == self._long(1)
66 assert math.trunc(-self._long(1)) == -self._long(1)
67
68 def test_add(self):
69 x = self._long(123)
70 assert int(x + self._long(12443)) == 123 + 12443
71 x = -20
72 assert x + 2 + self._long(3) + True == -self._long(14)
73
74 def test_sub(self):
75 assert int(self._long(58543) - self._long(12332)) == 58543 - 12332
76 assert int(self._long(58543) - 12332) == 58543 - 12332
77 assert int(58543 - self._long(12332)) == 58543 - 12332
78 x = self._long(237123838281233)
79 assert x * 12 == x * self._long(12)
80
81 def test_mul(self):
82 x = self._long(363)
83 assert x * 2 ** 40 == x << 40
84
85 def test_truediv(self):
86 a = self._long(31415926) / self._long(10000000)
87 assert a == 3.1415926
88
89 def test_floordiv(self):
90 x = self._long(31415926)
91 a = x // self._long(10000000)
92 assert a == self._long(3)
93
94 def test_int_floordiv(self):
95 import sys
96 long = self._long
97
98 x = long(3000)
99 a = x // 1000
100 assert a == 3
101
102 x = long(3000)
103 a = x // -1000
104 assert a == -3
105
106 x = long(3000)
107 raises(ZeroDivisionError, "x // 0")
108
109 n = sys.maxsize + 1
110 assert n / int(-n) == long(-1)
111
112 def test_numerator_denominator(self):
113 assert (self._long(1)).numerator == self._long(1)
114 assert (self._long(1)).denominator == self._long(1)
115 assert (self._long(42)).numerator == self._long(42)
116 assert (self._long(42)).denominator == self._long(1)
117
118 def test_compare(self):
119 Z = 0
120 ZL = self._long(0)
121
122 assert Z == ZL
123 assert not (Z != ZL)
124 assert ZL == Z
125 assert not (ZL != Z)
126 assert Z <= ZL
127 assert not (Z < ZL)
128 assert ZL <= ZL
129 assert not (ZL < ZL)
130
131 for BIG in (self._long(1), self._long(1) << 62, self._long(1) << 9999):
132 assert not (Z == BIG)
133 assert Z != BIG
134 assert not (BIG == Z)
135 assert BIG != Z
136 assert not (ZL == BIG)
137 assert ZL != BIG
138 assert Z <= BIG
139 assert Z < BIG
140 assert not (BIG <= Z)
141 assert not (BIG < Z)
142 assert ZL <= BIG
143 assert ZL < BIG
144 assert not (BIG <= ZL)
145 assert not (BIG < ZL)
146 assert not (Z <= -BIG)
147 assert not (Z < -BIG)
148 assert -BIG <= Z
149 assert -BIG < Z
150 assert not (ZL <= -BIG)
151 assert not (ZL < -BIG)
152 assert -BIG <= ZL
153 assert -BIG < ZL
154 #
155 assert not (BIG < int(BIG))
156 assert (BIG <= int(BIG))
157 assert (BIG == int(BIG))
158 assert not (BIG != int(BIG))
159 assert not (BIG > int(BIG))
160 assert (BIG >= int(BIG))
161 #
162 assert (BIG < int(BIG)+1)
163 assert (BIG <= int(BIG)+1)
164 assert not (BIG == int(BIG)+1)
165 assert (BIG != int(BIG)+1)
166 assert not (BIG > int(BIG)+1)
167 assert not (BIG >= int(BIG)+1)
168 #
169 assert not (BIG < int(BIG)-1)
170 assert not (BIG <= int(BIG)-1)
171 assert not (BIG == int(BIG)-1)
172 assert (BIG != int(BIG)-1)
173 assert (BIG > int(BIG)-1)
174 assert (BIG >= int(BIG)-1)
175 #
176 assert not (int(BIG) < BIG)
177 assert (int(BIG) <= BIG)
178 assert (int(BIG) == BIG)
179 assert not (int(BIG) != BIG)
180 assert not (int(BIG) > BIG)
181 assert (int(BIG) >= BIG)
182 #
183 assert not (int(BIG)+1 < BIG)
184 assert not (int(BIG)+1 <= BIG)
185 assert not (int(BIG)+1 == BIG)
186 assert (int(BIG)+1 != BIG)
187 assert (int(BIG)+1 > BIG)
188 assert (int(BIG)+1 >= BIG)
189 #
190 assert (int(BIG)-1 < BIG)
191 assert (int(BIG)-1 <= BIG)
192 assert not (int(BIG)-1 == BIG)
193 assert (int(BIG)-1 != BIG)
194 assert not (int(BIG)-1 > BIG)
195 assert not (int(BIG)-1 >= BIG)
196
197 def test_conversion(self):
198 class long2(int):
199 pass
200 x = self._long(1)
201 x = long2(x<<100)
202 y = int(x)
203 assert type(y) == int
204 assert type(+long2(5)) is int
205 assert type(long2(5) << 0) is int
206 assert type(long2(5) >> 0) is int
207 assert type(long2(5) + 0) is int
208 assert type(long2(5) - 0) is int
209 assert type(long2(5) * 1) is int
210 assert type(1 * long2(5)) is int
211 assert type(0 + long2(5)) is int
212 assert type(-long2(0)) is int
213 assert type(long2(5) // 1) is int
214
215 def test_shift(self):
216 long = self._long
217 assert long(65) >> long(2) == long(16)
218 assert long(65) >> 2 == long(16)
219 assert 65 >> long(2) == long(16)
220 assert long(65) << long(2) == long(65) * 4
221 assert long(65) << 2 == long(65) * 4
222 assert 65 << long(2) == long(65) * 4
223 raises(ValueError, "long(1) << long(-1)")
224 raises(ValueError, "long(1) << -1")
225 raises(OverflowError, "long(1) << (2 ** 100)")
226 raises(ValueError, "long(1) >> long(-1)")
227 raises(ValueError, "long(1) >> -1")
228 assert long(1) >> (2 ** 100) == 0
229 assert long(-1) >> (2 ** 100) == -1
230 assert 1 >> (2 ** 100) == 0 # int compatibility
231 assert -1 >> (2 ** 100) == -1 # int compatibility
232 assert 0 << (1 << 1000) == 0
233
234
235 def test_pow(self):
236 long = self._long
237 x = self._long(0)
238 assert pow(x, self._long(0), self._long(1)) == self._long(0)
239 assert pow(-self._long(1), -self._long(1)) == -1.0
240 assert pow(2 ** 68, 0.5) == 2.0 ** 34
241 assert pow(2 ** 68, 2) == 2 ** 136
242 raises(ValueError, pow, long(2), 5, 0)
243
244 # some rpow tests
245 assert pow(0, long(0), long(1)) == long(0)
246 assert pow(-1, long(-1)) == -1.0
247
248 def test_int_pow(self):
249 long = self._long
250 x = long(2)
251 assert pow(x, 2) == long(4)
252 assert pow(x, 2, 2) == long(0)
253 assert pow(x, 2, long(3)) == long(1)
254
255 def test_issue_3912(self):
256
257 class A(int):
258 # A class that uses the default 0 value but does not override __pow__
259 def __rpow__(self, other, modulo):
260 return None
261
262 raises(ValueError, pow, 1, A(), A())
263
264 def test_getnewargs(self):
265 assert self._long(0) .__getnewargs__() == (self._long(0),)
266 assert (-self._long(1)) .__getnewargs__() == (-self._long(1),)
267
268 def test_divmod(self):
269 long = self._long
270 def check_division(x, y):
271 q, r = divmod(x, y)
272 pab, pba = x*y, y*x
273 assert pab == pba
274 assert q == x // y
275 assert r == x % y
276 assert x == q*y + r
277 if y > 0:
278 assert 0 <= r < y
279 else:
280 assert y < r <= 0
281 for x in [-self._long(1), self._long(0), self._long(1), self._long(2) ** 100 - 1, -self._long(2) ** 100 - 1]:
282 for y in [-self._long(105566530), -self._long(1), self._long(1), self._long(1034522340)]:
283 print("checking division for %s, %s" % (x, y))
284 check_division(x, y)
285 check_division(x, int(y))
286 check_division(int(x), y)
287 # special case from python tests:
288 s1 = 33
289 s2 = 2
290 x = 16565645174462751485571442763871865344588923363439663038777355323778298703228675004033774331442052275771343018700586987657790981527457655176938756028872904152013524821759375058141439
291 x >>= s1*16
292 y = 10953035502453784575
293 y >>= s2*16
294 x = 0x3FE0003FFFFC0001FFF
295 y = self._long(0x9800FFC1)
296 check_division(x, y)
297 raises(ZeroDivisionError, "x // self._long(0)")
298 divmod(3, self._long(4))
299 raises(ZeroDivisionError, "x % long(0)")
300 raises(ZeroDivisionError, divmod, x, long(0))
301 raises(ZeroDivisionError, "x // 0")
302 raises(ZeroDivisionError, "x % 0")
303 raises(ZeroDivisionError, divmod, x, 0)
304
305 def test_int_divmod(self):
306 long = self._long
307 q, r = divmod(long(100), 11)
308 assert q == 9
309 assert r == 1
310
311 def test_format(self):
312 assert repr(12345678901234567890) == '12345678901234567890'
313 assert str(12345678901234567890) == '12345678901234567890'
314 assert hex(self._long(0x1234567890ABCDEF)) == '0x1234567890abcdef'
315 assert oct(self._long(0o1234567012345670)) == '0o1234567012345670'
316
317 def test_bits(self):
318 x = self._long(0xAAAAAAAA)
319 assert x | self._long(0x55555555) == self._long(0xFFFFFFFF)
320 assert x & self._long(0x55555555) == self._long(0x00000000)
321 assert x ^ self._long(0x55555555) == self._long(0xFFFFFFFF)
322 assert -x | self._long(0x55555555) == -self._long(0xAAAAAAA9)
323 assert x | self._long(0x555555555) == self._long(0x5FFFFFFFF)
324 assert x & self._long(0x555555555) == self._long(0x000000000)
325 assert x ^ self._long(0x555555555) == self._long(0x5FFFFFFFF)
326
327 def test_hash(self):
328 import sys
329 modulus = sys.hash_info.modulus
330 def longhash(x):
331 return hash(self._long(x))
332 for x in (list(range(200)) +
333 [1234567890123456789, 18446743523953737727,
334 987685321987685321987685321987685321987685321,
335 10**50]):
336 y = x % modulus
337 assert longhash(x) == longhash(y)
338 assert longhash(-x) == longhash(-y)
339 assert longhash(modulus - 1) == modulus - 1
340 assert longhash(modulus) == 0
341 assert longhash(modulus + 1) == 1
342
343 assert longhash(-1) == -2
344 value = -(modulus + 1)
345 assert longhash(value) == -2
346 assert longhash(value * 2 + 1) == -2
347 assert longhash(value * 4 + 3) == -2
348
349 def test_hash_2(self):
350 class AAA:
351 def __hash__(a):
352 return self._long(-1)
353 assert hash(AAA()) == -2
354
355 def test_math_log(self):
356 import math
357 raises(ValueError, math.log, self._long(0))
358 raises(ValueError, math.log, -self._long(1))
359 raises(ValueError, math.log, -self._long(2))
360 raises(ValueError, math.log, -(self._long(1) << 10000))
361 #raises(ValueError, math.log, 0)
362 raises(ValueError, math.log, -1)
363 raises(ValueError, math.log, -2)
364
365 def test_long(self):
366 import sys
367 n = -sys.maxsize-1
368 assert int(n) == n
369 assert str(int(n)) == str(n)
370 a = memoryview(b'123')
371 assert int(a) == self._long(123)
372
373 def test_huge_longs(self):
374 import operator
375 x = self._long(1)
376 huge = x << self._long(40000)
377 raises(OverflowError, float, huge)
378 raises(OverflowError, operator.truediv, huge, 3)
379 raises(OverflowError, operator.truediv, huge, self._long(3))
380
381 def test_just_trunc(self):
382 class myint(object):
383 def __trunc__(self):
384 return 42
385 assert int(myint()) == 42
386
387 def test_override___int__(self):
388 class myint(int):
389 def __int__(self):
390 return 42
391 assert int(myint(21)) == 42
392 class myotherint(int):
393 pass
394 assert int(myotherint(21)) == 21
395
396 def test___int__(self):
397 class A(object):
398 def __int__(self):
399 return 42
400 assert int(A()) == 42
401
402 class IntSubclass(int):
403 pass
404 class ReturnsIntSubclass(object):
405 def __int__(self):
406 return IntSubclass(42)
407 n = int(ReturnsIntSubclass())
408 assert n == 42
409 # cpython 3.6 fixed behaviour to actually return type int here
410 assert type(n) is int
411
412 def test_trunc_returns(self):
413 # but!: (blame CPython 2.7)
414 class Integral(object):
415 def __int__(self):
416 return 42
417 class TruncReturnsNonInt(object):
418 def __trunc__(self):
419 return Integral()
420 raises(TypeError, int, TruncReturnsNonInt())
421
422 class IntSubclass(int):
423 pass
424 class TruncReturnsNonInt(object):
425 def __trunc__(self):
426 return IntSubclass(42)
427 n = int(TruncReturnsNonInt())
428 assert n == 42
429 assert type(n) is int
430
431 def test_long_before_string(self):
432 class A(str):
433 def __int__(self):
434 return 42
435 assert int(A('abc')) == 42
436
437 def test_conjugate(self):
438 assert (self._long(7)).conjugate() == self._long(7)
439 assert (-self._long(7)).conjugate() == -self._long(7)
440
441 class L(int):
442 pass
443
444 assert type(L(7).conjugate()) is int
445
446 class L(int):
447 def __pos__(self):
448 return 43
449 assert L(7).conjugate() == self._long(7)
450
451 def test_bit_length(self):
452 assert self._long(8).bit_length() == 4
453 assert (-1<<40).bit_length() == 41
454 assert ((2**31)-1).bit_length() == 31
455
456 def test_from_bytes(self):
457 assert int.from_bytes(b'c', 'little') == 99
458 assert int.from_bytes(b'\x01\x01', 'little') == 257
459 assert int.from_bytes(b'\x01\x00', 'big') == 256
460 assert int.from_bytes(b'\x00\x80', 'little', signed=True) == -32768
461 assert int.from_bytes([255, 0, 0], 'big', signed=True) == -65536
462 raises(TypeError, int.from_bytes, 0, 'big')
463 raises(TypeError, int.from_bytes, '', 'big')
464 raises(ValueError, int.from_bytes, b'c', 'foo')
465
466 def test_to_bytes(self):
467 assert 65535 .to_bytes(2, 'big') == b'\xff\xff'
468 assert (-8388608).to_bytes(3, 'little', signed=True) == b'\x00\x00\x80'
469 raises(OverflowError, (-5).to_bytes, 1, 'big')
470 raises(ValueError, (-5).to_bytes, 1, 'foo')
471 assert 65535 .to_bytes(length=2, byteorder='big') == b'\xff\xff'
472
473 def test_to_bytes_default_length(self):
474 assert 11 .to_bytes() == b'\x0b'
475
476 def test_negative_zero(self):
477 x = eval("-self._long(0)")
478 assert x == self._long(0)
479
480 def test_long_real(self):
481 class A(int): pass
482 b = A(5).real
483 assert type(b) is int
484
485 @py.test.mark.skipif("not config.option.runappdirect and sys.maxunicode == 0xffff")
486 def test_long_from_unicode(self):
487 raises(ValueError, int, '123L')
488 assert int('L', 22) == 21
489 s = '\U0001D7CF\U0001D7CE' # 𝟏𝟎
490 assert int(s) == 10
491
492 def test_long_from_bytes(self):
493 assert int(b'1234') == 1234
494
495 def test_invalid_literal_message(self):
496 try:
497 int('hello àèìò')
498 except ValueError as e:
499 assert 'hello àèìò' in str(e)
500 else:
501 assert False, 'did not raise'
502
503 def test_base_overflow(self):
504 raises(ValueError, int, '42', 2**63)
505
506 def test_long_real(self):
507 class A(int): pass
508 b = A(5).real
509 assert type(b) is int
510
511 def test__int__(self):
512 class A(int):
513 def __int__(self):
514 return 42
515
516 assert int(int(3)) == int(3)
517 assert int(A(13)) == 42
518
519 def test_long_error_msg(self):
520 e = raises(TypeError, int, [])
521 assert str(e.value) == (
522 "int() argument must be a string, a bytes-like object "
523 "or a number, not 'list'")
524
525 def test_linear_long_base_16(self):
526 # never finishes if int(_, 16) is not linear-time
527 size = 100000
528 n = "a" * size
529 expected = (2 << (size * 4)) // 3
530 assert int(n, 16) == expected
531
532 def test_large_identity(self):
533 import sys
534 if '__pypy__' not in sys.builtin_module_names:
535 skip('PyPy only')
536 a = sys.maxsize + 1
537 b = sys.maxsize + 2
538 assert a is not b
539 b -= 1
540 assert a is b
541
542 def test_pow_negative_exponent_modulo(self):
543 import math
544 import sys
545 mod = 2 ** 100
546 res = 519502503658624787456021964081
547 assert pow(3**100, -1, mod) == res
548 assert pow(3**100, -2, mod) == (res ** 2) % mod
549 assert pow(12312388333333333333333333333333333333333333333333333333333, -sys.maxsize-1, 23) == 12
550 assert pow(13, -55555555555555555555555555555555555555555555555555555555551, 23) == 16
551
552 for i in range(1, 7):
553 i = self._long(i)
554 for j in range(2, 7):
555 j = self._long(j)
556 for sign in [1, -1]:
557 i = i * sign
558 if math.gcd(i, j) != 1:
559 with raises(ValueError):
560 pow(i, -1, j)
561 continue
562 x = pow(i, -1, j)
563 assert (x * i) % j == 1 % j
564
565 for k in range(2, 4):
566 y = pow(i, -k, j)
567 assert y == pow(x, k, j)
568
569 def test_repr(self):
570 import sys
571 x = self._long(1)
572 big = x << self._long(4000)
573 assert len(str(big)) == 1205
574 huge = x << self._long(40000)
575 with raises(ValueError) as exc:
576 str(huge)
577 assert str(exc.value).startswith('Exceeds the limit')
578 s = '%x' % huge
579 assert len(s) == 10001 # much longer that 4300 ...
580 maxdigits = sys.get_int_max_str_digits()
581 less_huge = 10 ** maxdigits
582 with raises(ValueError) as exc:
583 str(less_huge) # exactly on the cusp
584 sys.set_int_max_str_digits(0)
585 try:
586 str(huge) # succeeds
587 finally:
588 sys.set_int_max_str_digits(maxdigits)
589
590 def test_sign_not_counted_in_int_max(self):
591 import sys
592
593 previous_limit = sys.get_int_max_str_digits()
594 sys.set_int_max_str_digits(2048)
595 max_digits = sys.get_int_max_str_digits()
596 try:
597 s = '5' * max_digits
598 i = int(s)
599 pos_i = int(s)
600 assert i == pos_i
601 neg_i = int('-' + s)
602 assert -pos_i == neg_i
603 str(pos_i) # succeeds?
604 str(neg_i) # succeeds?
605 finally:
606 sys.set_int_max_str_digits(previous_limit)
607