174 lines
4.2 KiB
Python
174 lines
4.2 KiB
Python
import pytest
|
|
|
|
from phasm.exceptions import StaticError, TypingError
|
|
|
|
from ..constants import (
|
|
ALL_FLOAT_TYPES, ALL_INT_TYPES, COMPLETE_INT_TYPES, COMPLETE_NUMERIC_TYPES, TYPE_MAP
|
|
)
|
|
from ..helpers import Suite
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ALL_INT_TYPES)
|
|
def test_module_constant(type_):
|
|
code_py = f"""
|
|
CONSTANT: {type_}[3] = (24, 57, 80, )
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return CONSTANT[0]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 24 == result.returned_value
|
|
assert TYPE_MAP[type_] == type(result.returned_value)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.skip('To decide: What to do on out of index?')
|
|
@pytest.mark.parametrize('type_', COMPLETE_NUMERIC_TYPES)
|
|
def test_static_array_indexed(type_):
|
|
code_py = f"""
|
|
CONSTANT: {type_}[3] = (24, 57, 80, )
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return helper(CONSTANT, 0, 1, 2)
|
|
|
|
def helper(array: {type_}[3], i0: u32, i1: u32, i2: u32) -> {type_}:
|
|
return array[i0] + array[i1] + array[i2]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 161 == result.returned_value
|
|
assert TYPE_MAP[type_] == type(result.returned_value)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', COMPLETE_INT_TYPES)
|
|
def test_function_call_int(type_):
|
|
code_py = f"""
|
|
CONSTANT: {type_}[3] = (24, 57, 80, )
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return helper(CONSTANT)
|
|
|
|
def helper(array: {type_}[3]) -> {type_}:
|
|
return array[0] + array[1] + array[2]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 161 == result.returned_value
|
|
assert TYPE_MAP[type_] == type(result.returned_value)
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ALL_FLOAT_TYPES)
|
|
def test_function_call_float(type_):
|
|
code_py = f"""
|
|
CONSTANT: {type_}[3] = (24.0, 57.5, 80.75, )
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return helper(CONSTANT)
|
|
|
|
def helper(array: {type_}[3]) -> {type_}:
|
|
return array[0] + array[1] + array[2]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 162.25 == result.returned_value
|
|
assert TYPE_MAP[type_] == type(result.returned_value)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_module_constant_type_mismatch_bitwidth():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 57, 280, )
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='u8.*280'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_return_as_int():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 57, 80, )
|
|
|
|
def testEntry() -> u32:
|
|
return CONSTANT
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match=r'u32.*u8\[3\]'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_module_constant_type_mismatch_not_subscriptable():
|
|
code_py = """
|
|
CONSTANT: u8 = 24
|
|
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return CONSTANT[0]
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='Type cannot be subscripted:'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_module_constant_type_mismatch_index_out_of_range():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 57, 80, )
|
|
|
|
@exported
|
|
def testEntry() -> u8:
|
|
return CONSTANT[3]
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='Type cannot be subscripted with index 3:'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_static_array_constant_too_few_values():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 57, )
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='Member count does not match'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_static_array_constant_too_many_values():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 57, 1, 1, )
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='Member count does not match'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_static_array_constant_type_mismatch():
|
|
code_py = """
|
|
CONSTANT: u8[3] = (24, 4000, 1, )
|
|
"""
|
|
|
|
with pytest.raises(TypingError, match='u8.*4000'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.skip('To decide: What to do on out of index?')
|
|
def test_static_array_index_out_of_bounds():
|
|
code_py = """
|
|
CONSTANT0: u32[3] = (24, 57, 80, )
|
|
|
|
CONSTANT1: u32[16] = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, )
|
|
|
|
@exported
|
|
def testEntry() -> u32:
|
|
return CONSTANT0[16]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 0 == result.returned_value
|