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.
82 lines
1.9 KiB
Python
82 lines
1.9 KiB
Python
import sys
|
|
|
|
import pytest
|
|
|
|
from ..helpers import Suite, write_header
|
|
from ..runners import RunnerPywasm
|
|
|
|
|
|
def setup_interpreter(phash_code: str) -> RunnerPywasm:
|
|
runner = RunnerPywasm(phash_code)
|
|
|
|
runner.parse()
|
|
runner.compile_ast()
|
|
runner.compile_wat()
|
|
runner.compile_wasm()
|
|
runner.interpreter_setup()
|
|
runner.interpreter_load()
|
|
|
|
write_header(sys.stderr, 'Phasm')
|
|
runner.dump_phasm_code(sys.stderr)
|
|
write_header(sys.stderr, 'Assembly')
|
|
runner.dump_wasm_wat(sys.stderr)
|
|
|
|
return runner
|
|
|
|
@pytest.mark.integration_test
|
|
def test_foldl_1():
|
|
code_py = """
|
|
def u8_or(l: u8, r: u8) -> u8:
|
|
return l | r
|
|
|
|
@exported
|
|
def testEntry(b: bytes) -> u8:
|
|
return foldl(u8_or, 128, b)
|
|
"""
|
|
suite = Suite(code_py)
|
|
|
|
result = suite.run_code(b'')
|
|
assert 128 == result.returned_value
|
|
|
|
result = suite.run_code(b'\x80', runtime='pywasm')
|
|
assert 128 == result.returned_value
|
|
|
|
result = suite.run_code(b'\x80\x40', runtime='pywasm')
|
|
assert 192 == result.returned_value
|
|
|
|
result = suite.run_code(b'\x80\x40\x20\x10', runtime='pywasm')
|
|
assert 240 == result.returned_value
|
|
|
|
result = suite.run_code(b'\x80\x40\x20\x10\x08\x04\x02\x01', runtime='pywasm')
|
|
assert 255 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_foldl_2():
|
|
code_py = """
|
|
def xor(l: u8, r: u8) -> u8:
|
|
return l ^ r
|
|
|
|
@exported
|
|
def testEntry(a: bytes, b: bytes) -> u8:
|
|
return foldl(xor, 0, a) ^ foldl(xor, 0, b)
|
|
"""
|
|
suite = Suite(code_py)
|
|
|
|
result = suite.run_code(b'\x55\x0F', b'\x33\x80')
|
|
assert 233 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_foldl_3():
|
|
code_py = """
|
|
def xor(l: u32, r: u8) -> u32:
|
|
return l ^ u32(r)
|
|
|
|
@exported
|
|
def testEntry(a: bytes) -> u32:
|
|
return foldl(xor, 0, a)
|
|
"""
|
|
suite = Suite(code_py)
|
|
|
|
result = suite.run_code(b'\x55\x0F\x33\x80')
|
|
assert 233 == result.returned_value
|