Prior to this PR, each type would have its own handwritten test suite. The end result was that not all types were tested for all situations. This PR adds a framework based on a Markdown file, which generates the basic tests for the types defined in json files. These are auto generated and updated by the Makefile before the test suite is run. Also, a number of unsupported type combinations are now supported. Also, we now support negative literals. Also, allocation calculation fixes for nested types. Also, the test helpers can now properly import and export typed variables such as bytes, static arrays and tuples. This may come in handy when it comes to phasm platform wanting to route data. Also, adds better support for i8 type. Also, started on a runtime.py, since there's quite some code now that deals with compile time handling of WebAssembly stuff. Also, minor improvement to the type constrains, namely we better match 'tuple' literals with static array types. Also, reduced spam when printing the type analysis results; constraints that go back on the backlog are now no longer printed one by one. It now also prints the end results of the typing analysis. Also, reorganized the big test_primitives test into type classes. Also, replaced pylint with ruff.
116 lines
2.7 KiB
Python
116 lines
2.7 KiB
Python
import pytest
|
|
|
|
from phasm.type3.entry import Type3Exception
|
|
|
|
from ..helpers import Suite
|
|
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u32', 'u64']) # FIXME: Support u8, requires an extra AND operation
|
|
def test_logical_left_shift(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return 10 << 3
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 80 == result.returned_value
|
|
assert isinstance(result.returned_value, int)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u32', 'u64'])
|
|
def test_logical_right_shift_left_bit_zero(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return 10 >> 3
|
|
"""
|
|
|
|
# Check with wasmtime, as other engines don't mind if the type
|
|
# doesn't match. They'll complain when: (>>) : u32 -> u64 -> u32
|
|
result = Suite(code_py).run_code(runtime='wasmtime')
|
|
|
|
assert 1 == result.returned_value
|
|
assert isinstance(result.returned_value, int)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_logical_right_shift_left_bit_one():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> u32:
|
|
return 4294967295 >> 16
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 0xFFFF == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u8', 'u32', 'u64'])
|
|
def test_bitwise_or_uint(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return 10 | 3
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 11 == result.returned_value
|
|
assert isinstance(result.returned_value, int)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bitwise_or_inv_type():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> f64:
|
|
return 10.0 | 3.0
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='f64 does not implement the BitWiseOperation type class'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bitwise_or_type_mismatch():
|
|
code_py = """
|
|
CONSTANT1: u32 = 3
|
|
CONSTANT2: u64 = 3
|
|
|
|
@exported
|
|
def testEntry() -> u64:
|
|
return CONSTANT1 | CONSTANT2
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='u64 must be u32 instead'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u8', 'u32', 'u64'])
|
|
def test_bitwise_xor(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return 10 ^ 3
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 9 == result.returned_value
|
|
assert isinstance(result.returned_value, int)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u8', 'u32', 'u64'])
|
|
def test_bitwise_and(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return 10 & 3
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 2 == result.returned_value
|
|
assert isinstance(result.returned_value, int)
|