Skip to content

Commit

Permalink
remove folding
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Dec 28, 2023
1 parent 5cffa31 commit aff2fc1
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 820 deletions.
43 changes: 36 additions & 7 deletions tests/functional/codegen/types/numbers/test_decimals.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@

import pytest

from vyper.exceptions import DecimalOverrideException, InvalidOperation, TypeMismatch
from vyper import compile_code
from vyper.exceptions import (
DecimalOverrideException,
InvalidOperation,
OverflowException,
TypeMismatch,
)
from vyper.utils import DECIMAL_EPSILON, SizeLimits


Expand All @@ -24,23 +30,25 @@ def test_decimal_override():


@pytest.mark.parametrize("op", ["**", "&", "|", "^"])
def test_invalid_ops(get_contract, assert_compile_failed, op):
def test_invalid_ops(op):
code = f"""
@external
def foo(x: decimal, y: decimal) -> decimal:
return x {op} y
"""
assert_compile_failed(lambda: get_contract(code), InvalidOperation)
with pytest.raises(InvalidOperation):
compile_code(code)


@pytest.mark.parametrize("op", ["not"])
def test_invalid_unary_ops(get_contract, assert_compile_failed, op):
def test_invalid_unary_ops(op):
code = f"""
@external
def foo(x: decimal) -> decimal:
return {op} x
"""
assert_compile_failed(lambda: get_contract(code), InvalidOperation)
with pytest.raises(InvalidOperation):
compile_code(code)


def quantize(x: Decimal) -> Decimal:
Expand Down Expand Up @@ -263,11 +271,32 @@ def bar(num: decimal) -> decimal:
assert c.bar(Decimal("1e37")) == Decimal("-9e37") # Math lines up


def test_exponents(assert_compile_failed, get_contract):
def test_exponents():
code = """
@external
def foo() -> decimal:
return 2.2 ** 2.0
"""

assert_compile_failed(lambda: get_contract(code), TypeMismatch)
with pytest.raises(TypeMismatch):
compile_code(code)


def test_decimal_nested_intermediate_overflow():
code = """
@external
def foo():
a: decimal = 18707220957835557353007165858768422651595.9365500927 + 1e-10 - 1e-10
"""
with pytest.raises(OverflowException):
compile_code(code)


def test_replace_decimal_nested_intermediate_underflow(dummy_input_bundle):
code = """
@external
def foo():
a: decimal = -18707220957835557353007165858768422651595.9365500928 - 1e-10 + 1e-10
"""
with pytest.raises(OverflowException):
compile_code(code)
38 changes: 25 additions & 13 deletions tests/functional/codegen/types/numbers/test_signed_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from vyper import compile_code
from vyper.exceptions import InvalidOperation, InvalidType, OverflowException, ZeroDivisionException
from vyper.semantics.types import IntegerT
from vyper.utils import evm_div, evm_mod
Expand Down Expand Up @@ -206,17 +207,16 @@ def _num_min() -> {typ}:


@pytest.mark.parametrize("typ", types)
def test_overflow_out_of_range(get_contract, assert_compile_failed, typ):
def test_overflow_out_of_range(get_contract, typ):
code = f"""
@external
def num_sub() -> {typ}:
return 1-2**{typ.bits}
"""

if typ.bits == 256:
assert_compile_failed(lambda: get_contract(code), OverflowException)
else:
assert_compile_failed(lambda: get_contract(code), InvalidType)
exc = OverflowException if typ.bits == 256 else InvalidType
with pytest.raises(exc):
compile_code(code)


ARITHMETIC_OPS = {
Expand All @@ -231,7 +231,7 @@ def num_sub() -> {typ}:
@pytest.mark.parametrize("op", sorted(ARITHMETIC_OPS.keys()))
@pytest.mark.parametrize("typ", types)
@pytest.mark.fuzzing
def test_arithmetic_thorough(get_contract, tx_failed, assert_compile_failed, op, typ):
def test_arithmetic_thorough(get_contract, tx_failed, op, typ):
# both variables
code_1 = f"""
@external
Expand Down Expand Up @@ -318,20 +318,21 @@ def foo() -> {typ}:
elif div_by_zero:
with tx_failed():
c.foo(x, y)
assert_compile_failed(lambda code=code_2: get_contract(code), ZeroDivisionException)
with pytest.raises(ZeroDivisionException):
compile_code(code_2)
with tx_failed():
get_contract(code_3).foo(y)
assert_compile_failed(lambda code=code_4: get_contract(code), ZeroDivisionException)
with pytest.raises(ZeroDivisionException):
compile_code(code_4)
else:
with tx_failed():
c.foo(x, y)
with tx_failed():
get_contract(code_2).foo(x)
with tx_failed():
get_contract(code_3).foo(y)
assert_compile_failed(
lambda code=code_4: get_contract(code), (InvalidType, OverflowException)
)
with pytest.raises((InvalidType, OverflowException)):
compile_code(code_4)


COMPARISON_OPS = {
Expand Down Expand Up @@ -413,10 +414,21 @@ def foo(a: {typ}) -> {typ}:

@pytest.mark.parametrize("typ", types)
@pytest.mark.parametrize("op", ["not"])
def test_invalid_unary_ops(get_contract, assert_compile_failed, typ, op):
def test_invalid_unary_ops(typ, op):
code = f"""
@external
def foo(a: {typ}) -> {typ}:
return {op} a
"""
assert_compile_failed(lambda: get_contract(code), InvalidOperation)
with pytest.raises(InvalidOperation):
compile_code(code)


def test_binop_nested_intermediate_underflow():
code = """
@external
def foo():
a: int256 = -2**255 * 2 - 10 + 100
"""
with pytest.raises(InvalidType):
compile_code(code)
21 changes: 17 additions & 4 deletions tests/functional/codegen/types/numbers/test_unsigned_ints.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytest

from vyper import compile_code
from vyper.exceptions import InvalidOperation, InvalidType, OverflowException, ZeroDivisionException
from vyper.semantics.types import IntegerT
from vyper.utils import evm_div, evm_mod
Expand Down Expand Up @@ -85,7 +86,7 @@ def foo(x: {typ}) -> {typ}:
@pytest.mark.parametrize("op", sorted(ARITHMETIC_OPS.keys()))
@pytest.mark.parametrize("typ", types)
@pytest.mark.fuzzing
def test_arithmetic_thorough(get_contract, tx_failed, assert_compile_failed, op, typ):
def test_arithmetic_thorough(get_contract, tx_failed, op, typ):
# both variables
code_1 = f"""
@external
Expand Down Expand Up @@ -204,7 +205,7 @@ def foo(x: {typ}, y: {typ}) -> bool:


@pytest.mark.parametrize("typ", types)
def test_uint_literal(get_contract, assert_compile_failed, typ):
def test_uint_literal(get_contract, typ):
lo, hi = typ.ast_bounds

good_cases = [0, 1, 2, 3, hi // 2 - 1, hi // 2, hi // 2 + 1, hi - 1, hi]
Expand All @@ -221,7 +222,8 @@ def test() -> {typ}:
assert c.test() == val

for val in bad_cases:
assert_compile_failed(lambda v=val: get_contract(code_template.format(typ=typ, val=v)))
with pytest.raises():
compile_code(code_template.format(typ=typ, val=val))


@pytest.mark.parametrize("typ", types)
Expand All @@ -232,4 +234,15 @@ def test_invalid_unary_ops(get_contract, assert_compile_failed, typ, op):
def foo(a: {typ}) -> {typ}:
return {op} a
"""
assert_compile_failed(lambda: get_contract(code), InvalidOperation)
with pytest.raises(InvalidOperation):
compile_code(code)


def test_binop_nested_intermediate_overflow():
code = """
@external
def foo():
a: uint256 = 2**255 * 2 / 10
"""
with pytest.raises(OverflowException):
compile_code(code)
4 changes: 2 additions & 2 deletions tests/unit/ast/nodes/test_fold_binop_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def foo({input_value}) -> decimal:
literal_op = literal_op.rsplit(maxsplit=1)[0]
vyper_ast = vy_ast.parse_to_ast(literal_op)
try:
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
new_node = vyper_ast.body[0].value.fold()
expected = new_node.value
is_valid = -(2**127) <= expected < 2**127
except (OverflowException, ZeroDivisionException):
# for overflow or division/modulus by 0, expect the contract call to revert
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ast/nodes/test_fold_binop_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ def foo({input_value}) -> int128:
vyper_ast = vy_ast.parse_to_ast(literal_op)

try:
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
new_node = vyper_ast.body[0].value.fold()
expected = new_node.value
is_valid = True
except ZeroDivisionException:
is_valid = False
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/ast/nodes/test_fold_boolop.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def foo({input_value}) -> bool:
literal_op = literal_op.rsplit(maxsplit=1)[0]

vyper_ast = vy_ast.parse_to_ast(literal_op)
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
new_node = vyper_ast.body[0].value.fold()
expected = new_node.value

assert contract.foo(*values) == expected
4 changes: 2 additions & 2 deletions tests/unit/ast/nodes/test_fold_unaryop.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def foo(a: bool) -> bool:

literal_op = f"{'not ' * count}{bool_cond}"
vyper_ast = vy_ast.parse_to_ast(literal_op)
vy_ast.folding.replace_literal_ops(vyper_ast)
expected = vyper_ast.body[0].value.value
new_node = vyper_ast.body[0].value.fold()
expected = new_node.value

assert contract.foo(bool_cond) == expected
Loading

0 comments on commit aff2fc1

Please sign in to comment.