Skip to content

Commit

Permalink
update tests wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tserg committed Apr 7, 2024
1 parent d45ec62 commit 67d0209
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 49 deletions.
5 changes: 2 additions & 3 deletions tests/functional/builtins/codegen/test_as_wei_value.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import pytest
from decimal import Decimal

from vyper.semantics.types.primitives import DecimalT
from tests.utils import decimal_to_int
from vyper.semantics.types.primitives import DecimalT

wei_denoms = {
"femtoether": 3,
Expand Down Expand Up @@ -71,7 +70,7 @@ def foo(a: decimal) -> uint256:
denom = 10**multiplier
value = Decimal((2**127 - 1) / denom)

assert c.foo(decimal_to_int(value)) == decimal_to_int(value * denom)
assert c.foo(decimal_to_int(value)) == int(value * denom)


@pytest.mark.parametrize("value", (-1, -(2**127)))
Expand Down
22 changes: 10 additions & 12 deletions tests/functional/codegen/types/numbers/test_constants.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import itertools
from decimal import Decimal

import pytest

from tests.utils import decimal_to_int
from vyper.compiler import compile_code
from vyper.exceptions import TypeMismatch
from vyper.utils import MemoryPositions
Expand Down Expand Up @@ -59,15 +59,13 @@ def test_arithmetic(a: int128) -> int128:
assert c.test_int128(-(2**127)) == [False, True]
assert c.test_int128(0) == [False, False]

assert c.test_decimal(Decimal("18707220957835557353007165858768422651595.9365500927")) == [
True,
False,
]
assert c.test_decimal(Decimal("-18707220957835557353007165858768422651595.9365500928")) == [
False,
True,
]
assert c.test_decimal(Decimal("0.1")) == [False, False]
assert c.test_decimal(
decimal_to_int("18707220957835557353007165858768422651595.9365500927")
) == [True, False]
assert c.test_decimal(
decimal_to_int("-18707220957835557353007165858768422651595.9365500928")
) == [False, True]
assert c.test_decimal(decimal_to_int("0.1")) == [False, False]

assert c.test_uint256(2**256 - 1) is True

Expand Down Expand Up @@ -121,8 +119,8 @@ def zoo() -> uint256:

assert c.joo() is None

assert c.koo() == Decimal(2**167 - 1) / 10**10
assert c.loo() == Decimal(-(2**167)) / 10**10
assert c.koo() == (2**167 - 1)
assert c.loo() == -(2**167)

assert c.zoo() == 2**256 - 1

Expand Down
46 changes: 27 additions & 19 deletions tests/functional/codegen/types/numbers/test_decimals.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def iarg() -> uint256:
assert c.arg(decimal_to_int("3.7")) == decimal_to_int("3.7")
assert c.garg() == decimal_to_int("6.75")
assert c.harg() == decimal_to_int("9.0")
assert c.iarg() == decimal_to_int("14")
assert c.iarg() == 14

print("Passed fractional multiplication test")

Expand All @@ -182,14 +182,18 @@ def _num_mul(x: decimal, y: decimal) -> decimal:
y = 1 + DECIMAL_EPSILON

with tx_failed():
c._num_mul(x, y)
c._num_mul(decimal_to_int(x), decimal_to_int(y))

assert c._num_mul(x, decimal_to_int(1)) == x
assert c._num_mul(decimal_to_int(x), decimal_to_int(1)) == decimal_to_int(x)

assert c._num_mul(x, 1 - DECIMAL_EPSILON) == quantize(x * (1 - DECIMAL_EPSILON))
assert c._num_mul(decimal_to_int(x), decimal_to_int(1 - DECIMAL_EPSILON)) == decimal_to_int(
quantize(x * (1 - DECIMAL_EPSILON))
)

x = SizeLimits.MIN_AST_DECIMAL
assert c._num_mul(x, 1 - DECIMAL_EPSILON) == quantize(x * (1 - DECIMAL_EPSILON))
assert c._num_mul(decimal_to_int(x), decimal_to_int(1 - DECIMAL_EPSILON)) == decimal_to_int(
quantize(x * (1 - DECIMAL_EPSILON))
)


# division failure modes(!)
Expand All @@ -206,35 +210,39 @@ def foo(x: decimal, y: decimal) -> decimal:
y = -DECIMAL_EPSILON

with tx_failed():
c.foo(x, y)
c.foo(decimal_to_int(x), decimal_to_int(y))
with tx_failed():
c.foo(x, decimal_to_int(0))
c.foo(decimal_to_int(x), 0)
with tx_failed():
c.foo(y, decimal_to_int(0))
c.foo(decimal_to_int(y), 0)

y = decimal_to_int(1) - DECIMAL_EPSILON # 0.999999999
y = 1 - DECIMAL_EPSILON # 0.999999999
with tx_failed():
c.foo(x, y)
c.foo(decimal_to_int(x), decimal_to_int(y))

y = decimal_to_int(-1)
y = -1
with tx_failed():
c.foo(x, y)
c.foo(decimal_to_int(x), decimal_to_int(y))

assert c.foo(x, decimal_to_int(1)) == x
assert c.foo(x, 1 + DECIMAL_EPSILON) == quantize(x / (1 + DECIMAL_EPSILON))
assert c.foo(decimal_to_int(x), decimal_to_int(1)) == decimal_to_int(x)
assert c.foo(decimal_to_int(x), decimal_to_int(1 + DECIMAL_EPSILON)) == decimal_to_int(
quantize(x / (1 + DECIMAL_EPSILON))
)

x = SizeLimits.MAX_AST_DECIMAL

with tx_failed():
c.foo(x, DECIMAL_EPSILON)
c.foo(decimal_to_int(x), decimal_to_int(DECIMAL_EPSILON))

y = decimal_to_int(1) - DECIMAL_EPSILON
y = 1 - DECIMAL_EPSILON
with tx_failed():
c.foo(x, y)
c.foo(decimal_to_int(x), decimal_to_int(y))

assert c.foo(x, decimal_to_int(1)) == x
assert c.foo(decimal_to_int(x), decimal_to_int(1)) == decimal_to_int(x)

assert c.foo(x, 1 + DECIMAL_EPSILON) == quantize(x / (1 + DECIMAL_EPSILON))
assert c.foo(decimal_to_int(x), decimal_to_int(1 + DECIMAL_EPSILON)) == decimal_to_int(
quantize(x / (1 + DECIMAL_EPSILON))
)


def test_decimal_min_max_literals(tx_failed, get_contract_with_gas_estimation):
Expand Down
11 changes: 5 additions & 6 deletions tests/functional/codegen/types/numbers/test_modulo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from decimal import Decimal

import pytest

from tests.utils import decimal_to_int
from vyper.exceptions import ZeroDivisionException


Expand All @@ -26,9 +25,9 @@ def num_modulo_decimal() -> decimal:
"""
c = get_contract_with_gas_estimation(code)
assert c.num_modulo_num() == 1
assert c.decimal_modulo_decimal() == Decimal(".18")
assert c.decimal_modulo_num() == Decimal(".5")
assert c.num_modulo_decimal() == Decimal(".5")
assert c.decimal_modulo_decimal() == decimal_to_int(".18")
assert c.decimal_modulo_num() == decimal_to_int(".5")
assert c.num_modulo_decimal() == decimal_to_int(".5")


def test_modulo_with_input_of_zero(tx_failed, get_contract_with_gas_estimation):
Expand All @@ -39,7 +38,7 @@ def foo(a: decimal, b: decimal) -> decimal:
"""
c = get_contract_with_gas_estimation(code)
with tx_failed():
c.foo(Decimal("1"), Decimal("0"))
c.foo(decimal_to_int("1"), decimal_to_int("0"))


def test_literals_vs_evm(get_contract):
Expand Down
20 changes: 15 additions & 5 deletions tests/functional/codegen/types/test_dynamic_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import pytest

from tests.utils import decimal_to_int
from vyper.compiler import compile_code
from vyper.exceptions import (
ArgumentException,
Expand Down Expand Up @@ -220,7 +221,10 @@ def uoo(inp: DynArray[Foobar, 2]) -> DynArray[DynArray[Foobar, 2], 2]:
assert c.poo([]) == []
assert c.poo([[1, 2], [3, 4]]) == [[1, 2], [3, 4]]
assert c.qoo([1, 2]) == [[1, 2], [3, 4]]
assert c.roo([1, 2]) == [[1.0, 2.0], [3.0, 4.0]]
assert c.roo([decimal_to_int(1), decimal_to_int(2)]) == [
[decimal_to_int(1), decimal_to_int(2)],
[decimal_to_int(3), decimal_to_int(4)],
]
assert c.soo() == [1, 2]
assert c.too() == [2, 1]
assert c.uoo([1, 2]) == [[1, 2], [2, 1]]
Expand Down Expand Up @@ -729,9 +733,15 @@ def test_array_decimal_return3() -> DynArray[DynArray[decimal, 2], 2]:

c = get_contract_with_gas_estimation(code)
assert c.test_array_num_return() == [[], [3, 4]]
assert c.test_array_decimal_return1() == [[1.0], [3.0, 4.0]]
assert c.test_array_decimal_return2() == [[1.0, 2.0]]
assert c.test_array_decimal_return3() == [[1.0, 2.0], [3.0]]
assert c.test_array_decimal_return1() == [
[decimal_to_int(1)],
[decimal_to_int(3), decimal_to_int(4)],
]
assert c.test_array_decimal_return2() == [[decimal_to_int(1), decimal_to_int(2)]]
assert c.test_array_decimal_return3() == [
[decimal_to_int(1), decimal_to_int(2)],
[decimal_to_int(3)],
]


@pytest.mark.venom_xfail(raises=StackTooDeep, reason="stack scheduler regression")
Expand Down Expand Up @@ -1660,7 +1670,7 @@ def ix(i: uint256) -> decimal:
"""
c = get_contract(code)
for i, p in enumerate(some_good_primes):
assert c.ix(i) == p
assert c.ix(i) == decimal_to_int(p)
# assert oob
with tx_failed():
c.ix(len(some_good_primes) + 1)
Expand Down
9 changes: 5 additions & 4 deletions tests/functional/codegen/types/test_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import pytest

from vyper.exceptions import ArrayIndexException, OverflowException, TypeMismatch
from tests.utils import decimal_to_int
from vyper.exceptions import ArrayIndexException, OverflowException, TypeMismatch


def _map_nested(f, xs):
Expand Down Expand Up @@ -129,8 +129,9 @@ def roo(inp: decimal[2]) -> decimal[2][2]:
assert c.noo([3, 5]) == [3, 5]
assert c.poo([[1, 2], [3, 4]]) == [[1, 2], [3, 4]]
assert c.qoo([1, 2]) == [[1, 2], [3, 4]]
assert c.roo(_map_nested(decimal_to_int, [1.0, 2.0])) == _map_nested(decimal_to_int, [[1.0, 2.0], [3.0, 4.0]])

assert c.roo(_map_nested(decimal_to_int, [1.0, 2.0])) == _map_nested(
decimal_to_int, [[1.0, 2.0], [3.0, 4.0]]
)


def test_array_accessor(get_contract_with_gas_estimation):
Expand Down Expand Up @@ -335,7 +336,7 @@ def test_array_decimal_return3() -> decimal[2][2]:

c = get_contract_with_gas_estimation(code)
assert c.test_array_num_return() == [[1, 2], [3, 4]]
assert c.test_array_decimal_return1() == _map_nested(decimal_to_int,[[1.0, 2.0], [3.0, 4.0]])
assert c.test_array_decimal_return1() == _map_nested(decimal_to_int, [[1.0, 2.0], [3.0, 4.0]])
assert c.test_array_decimal_return2() == _map_nested(decimal_to_int, [[1.0, 2.0], [3.0, 4.0]])
assert c.test_array_decimal_return3() == _map_nested(decimal_to_int, [[1.0, 2.0], [3.0, 4.0]])

Expand Down

0 comments on commit 67d0209

Please sign in to comment.