|
| 1 | +"""Tests for constant folding of huge operands.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from mypy.constant_fold import constant_fold_binary_float_op, constant_fold_binary_int_op |
| 6 | +from mypy.test.helpers import Suite |
| 7 | + |
| 8 | +BIG = 2**2000 |
| 9 | + |
| 10 | + |
| 11 | +class ConstantFoldOverflowSuite(Suite): |
| 12 | + """Folding is an optimization: when a result cannot be built, it must yield None.""" |
| 13 | + |
| 14 | + def test_int_div_overflow(self) -> None: |
| 15 | + assert constant_fold_binary_int_op("/", BIG, 3) is None |
| 16 | + |
| 17 | + def test_int_lshift_huge_count(self) -> None: |
| 18 | + assert constant_fold_binary_int_op("<<", 1, 2**70) is None |
| 19 | + |
| 20 | + def test_float_ops_with_huge_int(self) -> None: |
| 21 | + for op in ("+", "-", "*", "/", "//", "%"): |
| 22 | + assert constant_fold_binary_float_op(op, BIG, 1.0) is None, op |
| 23 | + |
| 24 | + def test_small_operands_still_fold(self) -> None: |
| 25 | + assert constant_fold_binary_int_op("/", 6, 3) == 2.0 |
| 26 | + assert constant_fold_binary_int_op("<<", 1, 4) == 16 |
| 27 | + assert constant_fold_binary_float_op("+", 1, 2.5) == 3.5 |
| 28 | + assert constant_fold_binary_float_op("**", 2.0, 3) == 8.0 |
0 commit comments