import pytest from phasm.type3.entry import Type3Exception 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 def test_module_constant_def(): code_py = """ CONSTANT: u8[3] = (24, 57, 80, ) @exported def testEntry() -> i32: return 0 """ result = Suite(code_py).run_code() assert 0 == result.returned_value @pytest.mark.integration_test @pytest.mark.parametrize('type_', ALL_INT_TYPES) def test_module_constant_3(type_): code_py = f""" CONSTANT: {type_}[3] = (24, 57, 80, ) @exported def testEntry() -> {type_}: return CONSTANT[1] """ result = Suite(code_py).run_code() assert 57 == 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_function_call_element_ok(): code_py = """ CONSTANT: u64[3] = (250, 250000, 250000000, ) @exported def testEntry() -> u64: return helper(CONSTANT[0]) def helper(x: u64) -> u64: return x """ result = Suite(code_py).run_code() assert 250 == result.returned_value @pytest.mark.integration_test def test_function_call_element_type_mismatch(): code_py = """ CONSTANT: u64[3] = (250, 250000, 250000000, ) @exported def testEntry() -> u8: return helper(CONSTANT[0]) def helper(x: u8) -> u8: return x """ with pytest.raises(Type3Exception, match=r'u8 must be u64 instead'): Suite(code_py).run_code() @pytest.mark.integration_test def test_module_constant_type_mismatch_bitwidth(): code_py = """ CONSTANT: u8[3] = (24, 57, 280, ) """ with pytest.raises(Type3Exception, match=r'Must fit in 1 byte\(s\)'): 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(Type3Exception, match=r'static_array \(u8\) \(3\) must be u32 instead'): 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(Type3Exception, match='u8 cannot be subscripted'): Suite(code_py).run_code() @pytest.mark.integration_test def test_module_constant_type_mismatch_index_out_of_range_constant(): code_py = """ CONSTANT: u8[3] = (24, 57, 80, ) @exported def testEntry() -> u8: return CONSTANT[3] """ with pytest.raises(Type3Exception, match='3 must be less or equal than 2'): Suite(code_py).run_code() @pytest.mark.integration_test def test_module_constant_type_mismatch_index_out_of_range_variable(): code_py = """ CONSTANT: u8[3] = (24, 57, 80, ) @exported def testEntry(x: u32) -> u8: return CONSTANT[x] """ with pytest.raises(RuntimeError): Suite(code_py).run_code(3) @pytest.mark.integration_test def test_static_array_constant_too_few_values(): code_py = """ CONSTANT: u8[4] = (24, 57, ) @exported def testEntry() -> i32: return 0 """ with pytest.raises(Type3Exception, match='Member count mismatch'): 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, ) @exported def testEntry() -> i32: return 0 """ with pytest.raises(Type3Exception, match='Member count mismatch'): Suite(code_py).run_code()