phasm/tests/integration/test_lang/test_integral.py
Johan B.W. de Vries da381e4a48 Made Integral use Python's operators
Rather than Haskell's div and rem methods, we use the
Python operators since these are often used. And we have
as goal to make it 'look like Python'
2025-04-09 13:16:50 +02:00

62 lines
1.6 KiB
Python

import pytest
from ..helpers import Suite
TYPE_LIST = ['u32', 'u64', 'i32', 'i64']
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
def test_integral_div_ok(type_):
code_py = f"""
@exported
def testEntry() -> {type_}:
return 10 // 3
"""
result = Suite(code_py).run_code()
assert 3 == result.returned_value
assert isinstance(result.returned_value, int)
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
def test_integral_div_zero_let_it_crash_int(type_):
code_py = f"""
@exported
def testEntry() -> {type_}:
return 10 // 0
"""
# WebAssembly dictates that integer division is a partial operator (e.g. unreachable for 0)
# https://www.w3.org/TR/wasm-core-1/#-hrefop-idiv-umathrmidiv_u_n-i_1-i_2
with pytest.raises(Exception):
Suite(code_py).run_code()
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
def test_integral_rem_ok(type_):
code_py = f"""
@exported
def testEntry() -> {type_}:
return 10 % 3
"""
result = Suite(code_py).run_code()
assert 1 == result.returned_value
assert isinstance(result.returned_value, int)
@pytest.mark.integration_test
@pytest.mark.parametrize('type_', TYPE_LIST)
def test_integral_rem_zero_let_it_crash_int(type_):
code_py = f"""
@exported
def testEntry() -> {type_}:
return 10 % 0
"""
# WebAssembly dictates that integer division is a partial operator (e.g. unreachable for 0)
# https://www.w3.org/TR/wasm-core-1/#-hrefop-idiv-umathrmidiv_u_n-i_1-i_2
with pytest.raises(Exception):
Suite(code_py).run_code()