99 lines
1.9 KiB
Python
99 lines
1.9 KiB
Python
import pytest
|
|
|
|
from phasm.type3.entry import Type3Exception
|
|
|
|
from ..helpers import Suite
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bytes_address():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> bytes:
|
|
return f
|
|
"""
|
|
|
|
result = Suite(code_py).run_code(b'This is a test')
|
|
|
|
# THIS DEPENDS ON THE ALLOCATOR
|
|
# A different allocator will return a different value
|
|
assert 20 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bytes_length():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> u32:
|
|
return len(f)
|
|
"""
|
|
|
|
result = Suite(code_py).run_code(b'This yet is another test')
|
|
|
|
assert 24 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bytes_index():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> u8:
|
|
return f[8]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code(b'This is another test')
|
|
|
|
assert 0x61 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_constant():
|
|
code_py = """
|
|
CONSTANT: bytes = b'ABCDEF'
|
|
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return CONSTANT[0]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 0x41 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_bytes_index_out_of_bounds():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> u8:
|
|
return f[50]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code(b'Short', b'Long' * 100)
|
|
|
|
assert 0 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_function_call_element_ok():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> u8:
|
|
return helper(f[0])
|
|
|
|
def helper(x: u8) -> u8:
|
|
return x
|
|
"""
|
|
|
|
result = Suite(code_py).run_code(b'Short')
|
|
|
|
assert 83 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_function_call_element_type_mismatch():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(f: bytes) -> u64:
|
|
return helper(f[0])
|
|
|
|
def helper(x: u64) -> u64:
|
|
return x
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match=r'u64 must be u8 instead'):
|
|
Suite(code_py).run_code()
|