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.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_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\) 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(): code_py = """ CONSTANT: u8[3] = (24, 57, 80, ) @exported def testEntry() -> u8: return CONSTANT[3] """ with pytest.raises(Type3Exception, 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, ) @exported def testEntry() -> i32: return 0 """ with pytest.raises(Type3Exception, 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, ) @exported def testEntry() -> i32: return 0 """ with pytest.raises(Type3Exception, match='Member count does not match'): Suite(code_py).run_code() @pytest.mark.integration_test @pytest.mark.skip('To decide: What to do on out of index? Should be a panic.') 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