Debian Codegraph @

mypyc/test-data/run-integers.test

raw
1 # Test cases for integers (compile and run)
2
3 [case testInc]
4 def inc(x: int) -> int:
5 return x + 1
6 [file driver.py]
7 from native import inc
8 print(inc(3))
9 print(inc(-5))
10 print(inc(10**20))
11 [out]
12 4
13 -4
14 100000000000000000001
15
16 [case testCount]
17 def count(n: int) -> int:
18 i = 1
19 while i <= n:
20 i = i + 1
21 return i
22 [file driver.py]
23 from native import count
24 print(count(0))
25 print(count(1))
26 print(count(5))
27 [out]
28 1
29 2
30 6
31
32 [case testIntMathOps]
33 # This tests integer math things that are either easier to test in Python than
34 # in our C tests or are tested here because (for annoying reasons) we don't run
35 # the C unit tests in our 32-bit CI.
36 def multiply(x: int, y: int) -> int:
37 return x * y
38
39 # these stringify their outputs because that will catch if exceptions are mishandled
40 def floor_div(x: int, y: int) -> str:
41 return str(x // y)
42 def remainder(x: int, y: int) -> str:
43 return str(x % y)
44
45 [file driver.py]
46 from native import multiply, floor_div, remainder
47
48 def test_multiply(x, y):
49 assert multiply(x, y) == x * y
50 def test_floor_div(x, y):
51 assert floor_div(x, y) == str(x // y)
52 def test_remainder(x, y):
53 assert remainder(x, y) == str(x % y)
54
55 test_multiply(10**6, 10**6)
56 test_multiply(2**15, 2**15-1)
57 test_multiply(2**14, 2**14)
58
59 test_multiply(10**12, 10**12)
60 test_multiply(2**30, 2**30-1)
61 test_multiply(2**29, 2**29)
62
63 test_floor_div(-2**62, -1)
64 test_floor_div(-2**30, -1)
65 try:
66 floor_div(10, 0)
67 except ZeroDivisionError:
68 pass
69 else:
70 assert False, "Expected ZeroDivisionError"
71
72 test_remainder(-2**62, -1)
73 test_remainder(-2**30, -1)
74 try:
75 remainder(10, 0)
76 except ZeroDivisionError:
77 pass
78 else:
79 assert False, "Expected ZeroDivisionError"
80
81 [case testBigIntLiteral]
82 def big_int() -> None:
83 a_62_bit = 4611686018427387902
84 max_62_bit = 4611686018427387903
85 b_63_bit = 4611686018427387904
86 c_63_bit = 9223372036854775806
87 max_63_bit = 9223372036854775807
88 d_64_bit = 9223372036854775808
89 max_32_bit = 2147483647
90 max_32_bit_plus1 = 2147483648
91 max_31_bit = 1073741823
92 max_31_bit_plus1 = 1073741824
93 neg = -1234567
94 min_signed_63_bit = -4611686018427387904
95 underflow = -4611686018427387905
96 min_signed_64_bit = -9223372036854775808
97 min_signed_31_bit = -1073741824
98 min_signed_31_bit_plus1 = -1073741823
99 min_signed_31_bit_minus1 = -1073741825
100 min_signed_32_bit = -2147483648
101 print(a_62_bit)
102 print(max_62_bit)
103 print(b_63_bit)
104 print(c_63_bit)
105 print(max_63_bit)
106 print(d_64_bit)
107 print('==')
108 print(max_32_bit)
109 print(max_32_bit_plus1)
110 print(max_31_bit)
111 print(max_31_bit_plus1)
112 print(neg)
113 print(min_signed_63_bit)
114 print(underflow)
115 print(min_signed_64_bit)
116 print(min_signed_31_bit)
117 print(min_signed_31_bit_plus1)
118 print(min_signed_31_bit_minus1)
119 print(min_signed_32_bit)
120 [file driver.py]
121 from native import big_int
122 big_int()
123 [out]
124 4611686018427387902
125 4611686018427387903
126 4611686018427387904
127 9223372036854775806
128 9223372036854775807
129 9223372036854775808
130 ==
131 2147483647
132 2147483648
133 1073741823
134 1073741824
135 -1234567
136 -4611686018427387904
137 -4611686018427387905
138 -9223372036854775808
139 -1073741824
140 -1073741823
141 -1073741825
142 -2147483648
143
144 [case testNeg]
145 def neg(x: int) -> int:
146 return -x
147 [file driver.py]
148 from native import neg
149 assert neg(5) == -5
150 assert neg(-5) == 5
151 assert neg(1073741823) == -1073741823
152 assert neg(-1073741823) == 1073741823
153 assert neg(1073741824) == -1073741824
154 assert neg(-1073741824) == 1073741824
155 assert neg(2147483647) == -2147483647
156 assert neg(-2147483647) == 2147483647
157 assert neg(2147483648) == -2147483648
158 assert neg(-2147483648) == 2147483648
159 assert neg(4611686018427387904) == -4611686018427387904
160 assert neg(-4611686018427387904) == 4611686018427387904
161 assert neg(9223372036854775807) == -9223372036854775807
162 assert neg(-9223372036854775807) == 9223372036854775807
163 assert neg(9223372036854775808) == -9223372036854775808
164 assert neg(-9223372036854775808) == 9223372036854775808
165
166 [case testIsinstanceIntAndNotBool]
167 def test_isinstance_int_and_not_bool(value: object) -> bool:
168 return isinstance(value, int) and not isinstance(value, bool)
169 [file driver.py]
170 from native import test_isinstance_int_and_not_bool
171 assert test_isinstance_int_and_not_bool(True) == False
172 assert test_isinstance_int_and_not_bool(1) == True
173
174 [case testIntOps]
175 from typing import Any
176 from testutil import assertRaises
177
178 def check_and(x: int, y: int) -> None:
179 # eval() can be trusted to calculate expected result
180 expected = eval('{} & {}'.format(x, y))
181 actual = x & y
182 assert actual == expected, '{} & {}: got {}, expected {}'.format(x, y, actual, expected)
183
184 def check_or(x: int, y: int) -> None:
185 # eval() can be trusted to calculate expected result
186 expected = eval('{} | {}'.format(x, y))
187 actual = x | y
188 assert actual == expected, '{} | {}: got {}, expected {}'.format(x, y, actual, expected)
189
190 def check_xor(x: int, y: int) -> None:
191 # eval() can be trusted to calculate expected result
192 expected = eval('{} ^ {}'.format(x, y))
193 actual = x ^ y
194 assert actual == expected, '{} ^ {}: got {}, expected {}'.format(x, y, actual, expected)
195
196 def check_bitwise(x: int, y: int) -> None:
197 for l, r in (x, y), (y, x):
198 for ll, rr in (l, r), (-l, r), (l, -r), (-l, -r):
199 check_and(ll, rr)
200 check_or(ll, rr)
201 check_xor(ll, rr)
202
203 SHIFT = 30
204 DIGIT0a = 615729753
205 DIGIT0b = 832796681
206 DIGIT1a = 744342356 << SHIFT
207 DIGIT1b = 321006080 << SHIFT
208 DIGIT2a = 643582106 << (SHIFT * 2)
209 DIGIT2b = 656420725 << (SHIFT * 2)
210 DIGIT50 = 315723472 << (SHIFT * 50)
211 DIGIT100a = 1020652627 << (SHIFT * 100)
212 DIGIT100b = 923752451 << (SHIFT * 100)
213 BIG_SHORT = 3491190729721336556
214 MAX_SHORT = (1 << 62) - 1
215 MIN_SHORT = -(1 << 62)
216 MAX_SHORT_32 = (1 << 30) - 1
217 MIN_SHORT_32 = -(1 << 30)
218
219 def test_and_or_xor() -> None:
220 check_bitwise(0, 0)
221 check_bitwise(0, 1)
222 check_bitwise(1, 1)
223 check_bitwise(DIGIT0a, DIGIT0b)
224 check_bitwise(DIGIT1a, DIGIT1b)
225 check_bitwise(DIGIT2a, DIGIT2b)
226 check_bitwise(DIGIT100a, DIGIT100b)
227 check_bitwise(DIGIT0a, DIGIT0b + DIGIT2a)
228 check_bitwise(DIGIT0a, DIGIT0b + DIGIT50)
229 check_bitwise(DIGIT50 + DIGIT1a, DIGIT100a + DIGIT2b)
230 check_bitwise(BIG_SHORT, DIGIT0a)
231 check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a)
232 check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a + DIGIT2a)
233 check_bitwise(BIG_SHORT, DIGIT0a + DIGIT1a + DIGIT2a + DIGIT50)
234
235 for x in range(-25, 25):
236 for y in range(-25, 25):
237 check_bitwise(x, y)
238
239 def test_bitwise_inplace() -> None:
240 # Basic sanity checks; these should use the same code as the non-in-place variants
241 for x, y in (DIGIT0a, DIGIT1a), (DIGIT2a, DIGIT0a + DIGIT2b):
242 n = x
243 n &= y
244 assert n == x & y
245 n = x
246 n |= y
247 assert n == x | y
248 n = x
249 n ^= y
250 assert n == x ^ y
251
252 def check_invert(x: int) -> None:
253 # Use eval() as the source of truth
254 assert ~x == eval('~{}'.format(x))
255 assert ~(-x) == eval('~({})'.format(-x))
256
257 def test_invert() -> None:
258 check_invert(0)
259 check_invert(1)
260 check_invert(DIGIT0a)
261 check_invert(DIGIT0a + DIGIT1a)
262 check_invert(DIGIT0a + DIGIT1a + DIGIT2a)
263 check_invert(DIGIT0a + DIGIT1a + DIGIT2a + DIGIT50)
264 check_invert(BIG_SHORT)
265 for delta in -1, 0, 1:
266 check_invert(MAX_SHORT + delta)
267 check_invert(MIN_SHORT + delta)
268 check_invert(MAX_SHORT_32 + delta)
269 check_invert(MIN_SHORT_32 + delta)
270
271 def check_right_shift(x: int, n: int) -> None:
272 if n < 0:
273 try:
274 x >> n
275 except ValueError:
276 return
277 assert False, "no exception raised"
278 # Use eval() as the source of truth
279 expected = eval('{} >> {}'.format(x, n))
280 actual = x >> n
281 assert actual == expected, "{} >> {}: got {}, expected {}".format(x, n, actual, expected)
282
283 def test_right_shift() -> None:
284 for x in 0, 1, 1235, DIGIT0a, DIGIT0a + DIGIT1a, DIGIT0a + DIGIT50:
285 for n in 0, 1, 2, 3, 4, 10, 40, 10000, DIGIT1a, -1, -1334444, -DIGIT1a:
286 check_right_shift(x, n)
287 check_right_shift(-x, n)
288 x = DIGIT0a
289 x >>= 1
290 assert x == DIGIT0a >> 1
291 x = DIGIT50
292 x >>= 5
293 assert x == DIGIT50 >> 5
294 for i in range(256):
295 check_right_shift(1, i)
296 check_right_shift(137, i)
297 check_right_shift(MAX_SHORT, i)
298 check_right_shift(MAX_SHORT_32, i)
299 check_right_shift(MAX_SHORT + 1, i)
300 check_right_shift(MAX_SHORT_32 + 1, i)
301 for x in 1, DIGIT50:
302 try:
303 # It's okay if this raises an exception
304 assert x >> DIGIT2a == 0
305 except Exception:
306 pass
307 try:
308 x >> -DIGIT2a
309 assert False
310 except Exception:
311 pass
312
313 def check_left_shift(x: int, n: int) -> None:
314 if n < 0:
315 try:
316 x << n
317 except ValueError:
318 return
319 assert False, "no exception raised"
320 # Use eval() as the source of truth
321 expected = eval('{} << {}'.format(x, n))
322 actual = x << n
323 assert actual == expected, "{} << {}: got {}, expected {}".format(x, n, actual, expected)
324
325 def test_left_shift() -> None:
326 for x in 0, 1, 1235, DIGIT0a, DIGIT0a + DIGIT1a, DIGIT0a + DIGIT50:
327 for n in 0, 1, 2, 10, 40, 10000, -1, -1334444:
328 check_left_shift(x, n)
329 check_left_shift(-x, n)
330 x = DIGIT0a
331 x <<= 1
332 assert x == DIGIT0a << 1
333 x = DIGIT50
334 x <<= 5
335 assert x == DIGIT50 << 5
336 for shift in range(256):
337 check_left_shift(1, shift)
338 check_left_shift(137, shift)
339 for x in 1, DIGIT50:
340 try:
341 x << DIGIT50
342 assert False
343 except Exception:
344 pass
345 try:
346 x << -DIGIT50
347 assert False
348 except Exception:
349 pass
350
351 def is_true(x: int) -> bool:
352 if x:
353 return True
354 else:
355 return False
356
357 def is_true2(x: int) -> bool:
358 return bool(x)
359
360 def is_false(x: int) -> bool:
361 if not x:
362 return True
363 else:
364 return False
365
366 def test_int_as_bool() -> None:
367 assert not is_true(0)
368 assert not is_true2(0)
369 assert is_false(0)
370 for x in 1, 55, -1, -7, 1 << 50, 1 << 101, -(1 << 50), -(1 << 101):
371 assert is_true(x)
372 assert is_true2(x)
373 assert not is_false(x)
374
375 def bool_as_int(b: bool) -> int:
376 return b
377
378 def bool_as_int2(b: bool) -> int:
379 return int(b)
380
381 def test_bool_as_int() -> None:
382 assert bool_as_int(False) == 0
383 assert bool_as_int(True) == 1
384 assert bool_as_int2(False) == 0
385 assert bool_as_int2(True) == 1
386
387 def no_op_conversion(n: int) -> int:
388 return int(n)
389
390 def test_no_op_conversion() -> None:
391 for x in 1, 55, -1, -7, 1 << 50, 1 << 101, -(1 << 50), -(1 << 101):
392 assert no_op_conversion(x) == x
393
394 def test_floor_divide() -> None:
395 for x in range(-100, 100):
396 for y in range(-100, 100):
397 if y != 0:
398 assert x // y == getattr(x, "__floordiv__")(y)
399
400 def test_mod() -> None:
401 for x in range(-100, 100):
402 for y in range(-100, 100):
403 if y != 0:
404 assert x % y == getattr(x, "__mod__")(y)
405
406 def test_constant_fold() -> None:
407 assert str(-5 + 3) == "-2"
408 assert str(15 - 3) == "12"
409 assert str(1000 * 1000) == "1000000"
410 assert str(12325 // 12 ) == "1027"
411 assert str(87645 % 321) == "12"
412 assert str(674253 | 76544) == "748493"
413 assert str(765 ^ 82) == "687"
414 assert str(6546 << 3) == "52368"
415 assert str(6546 >> 7) == "51"
416 assert str(3**5) == "243"
417 assert str(~76) == "-77"
418 try:
419 2 / 0
420 except ZeroDivisionError:
421 pass
422 else:
423 assert False, "no exception raised"
424
425 x = int()
426 y = int() - 1
427 assert x == -1 or y != -3
428 assert -1 <= x
429 assert -1 == y
430
431 # Use int() to avoid constant propagation
432 i30 = (1 << 30) + int()
433 assert i30 == 1 << 30
434 i31 = (1 << 31) + int()
435 assert i31 == 1 << 31
436 i32 = (1 << 32) + int()
437 assert i32 == 1 << 32
438 i62 = (1 << 62) + int()
439 assert i62 == 1 << 62
440 i63 = (1 << 63) + int()
441 assert i63 == 1 << 63
442 i64 = (1 << 64) + int()
443 assert i64 == 1 << 64
444
445 n30 = -(1 << 30) + int()
446 assert n30 == -(1 << 30)
447 n31 = -(1 << 31) + int()
448 assert n31 == -(1 << 31)
449 n32 = -(1 << 32) + int()
450 assert n32 == -(1 << 32)
451 n62 = -(1 << 62) + int()
452 assert n62 == -(1 << 62)
453 n63 = -(1 << 63) + int()
454 assert n63 == -(1 << 63)
455 n64 = -(1 << 64) + int()
456 assert n64 == -(1 << 64)
457
458 def div_by_2(x: int) -> int:
459 return x // 2
460
461 def div_by_3(x: int) -> int:
462 return x // 3
463
464 def div_by_4(x: int) -> int:
465 return x // 4
466
467 def test_floor_divide_by_literal() -> None:
468 for i in range(-100, 100):
469 i_boxed: Any = i
470 assert div_by_2(i) == i_boxed // int('2')
471 assert div_by_3(i) == i_boxed // int('3')
472 assert div_by_4(i) == i_boxed // int('4')
473
474 def test_true_divide() -> None:
475 for x in range(-150, 100):
476 for y in range(-150, 100):
477 if y != 0:
478 assert x / y == getattr(x, "__truediv__")(y)
479 large1 = (123 + int())**123
480 large2 = (121 + int())**121
481 assert large1 / large2 == getattr(large1, "__truediv__")(large2)
482 assert large1 / 135 == getattr(large1, "__truediv__")(135)
483 assert large1 / -2 == getattr(large1, "__truediv__")(-2)
484 assert 17 / large2 == getattr(17, "__truediv__")(large2)
485
486 huge = 10**1000 + int()
487 with assertRaises(OverflowError, "integer division result too large for a float"):
488 huge / 2
489 with assertRaises(OverflowError, "integer division result too large for a float"):
490 huge / -2
491 assert 1 / huge == 0.0
492
493 [case testIntMinMax]
494 def test_int_min_max() -> None:
495 x: int = 200
496 y: int = 30
497 assert min(x, y) == 30
498 assert max(x, y) == 200
499 assert min(y, x) == 30
500 assert max(y, x) == 200
501
502 def test_int_hybrid_min_max() -> None:
503 from typing import Any
504
505 x: object = 30
506 y: Any = 20.0
507 assert min(x, y) == 20.0
508 assert max(x, y) == 30
509
510 u: object = 20
511 v: float = 30.0
512 assert min(u, v) == 20
513 assert max(u, v) == 30.0
514
515 def test_int_incompatible_min_max() -> None:
516 x: int = 2
517 y: str = 'aaa'
518 try:
519 print(min(x, y))
520 except TypeError as e:
521 assert str(e) == "'<' not supported between instances of 'str' and 'int'"
522 try:
523 print(max(x, y))
524 except TypeError as e:
525 assert str(e) == "'>' not supported between instances of 'str' and 'int'"
526
527 def test_int_bool_min_max() -> None:
528 x: int = 2
529 y: bool = False
530 z: bool = True
531 assert min(x, y) == False
532 assert min(x, z) == True
533 assert max(x, y) == 2
534 assert max(x, z) == 2
535
536 u: int = -10
537 assert min(u, y) == -10
538 assert min(u, z) == -10
539 assert max(u, y) == False
540 assert max(u, z) == True
541
542 [case testIsInstance]
543 from copysubclass import subc
544 from typing import Any
545 def test_built_in() -> None:
546 i: Any = 0
547 assert isinstance(i + 0, int)
548 assert isinstance(i + 9223372036854775808, int)
549 assert isinstance(i + -9223372036854775808, int)
550 assert isinstance(subc(), int)
551 assert isinstance(subc(9223372036854775808), int)
552 assert isinstance(subc(-9223372036854775808), int)
553
554 assert not isinstance(set(), int)
555 assert not isinstance((), int)
556 assert not isinstance((1,2,3), int)
557 assert not isinstance({1,2}, int)
558 assert not isinstance(float(0) + 1.0, int)
559 assert not isinstance(str() + '1', int)
560
561 def test_user_defined() -> None:
562 from userdefinedint import int
563
564 i: Any = 42
565 assert isinstance(int(), int)
566 assert not isinstance(i, int)
567
568 [file copysubclass.py]
569 class subc(int):
570 pass
571
572 [file userdefinedint.py]
573 class int:
574 pass
575
576 [case testIntToBytes]
577 from testutil import assertRaises
578 def to_bytes(n: int, length: int, byteorder: str, signed: bool = False) -> bytes:
579 return n.to_bytes(length, byteorder, signed=signed)
580 def test_to_bytes() -> None:
581 assert to_bytes(255, 2, "big") == b'\x00\xff', to_bytes(255, 2, "big")
582 assert to_bytes(255, 2, "little") == b'\xff\x00', to_bytes(255, 2, "little")
583 assert to_bytes(-1, 2, "big", True) == b'\xff\xff', to_bytes(-1, 2, "big", True)
584 assert to_bytes(0, 1, "big") == b'\x00', to_bytes(0, 1, "big")
585 # test with a value that does not fit in 64 bits
586 assert to_bytes(10**30, 16, "big") == b'\x00\x00\x00\x0c\x9f,\x9c\xd0Ft\xed\xea@\x00\x00\x00', to_bytes(10**30, 16, "big")
587 # unsigned, too large for 1 byte
588 with assertRaises(OverflowError):
589 to_bytes(256, 1, "big")
590 # signed, too small for 1 byte
591 with assertRaises(OverflowError):
592 to_bytes(-129, 1, "big", True)
593 # signed, too large for 1 byte
594 with assertRaises(OverflowError):
595 to_bytes(128, 1, "big", True)
596
597 [case testBitLength]
598 def bit_length(n: int) -> int:
599 return n.bit_length()
600 def bit_length_python(n: int) -> int:
601 return getattr(n, "bit_length")()
602 def test_bit_length() -> None:
603 for n in range(256):
604 i = 1 << n
605 assert bit_length(i) == bit_length_python(i)
606 assert bit_length(-(i)) == bit_length_python(-(i))
607 i -= 1
608 assert bit_length(i) == bit_length_python(i)
609 assert bit_length(-(i)) == bit_length_python(-(i))