- Tuple wasn't an applied type yet - phasm_type3 would re-order type IDs between prints - AppliedType3 wouldn't store the args for iterators -
141 lines
3.3 KiB
Python
141 lines
3.3 KiB
Python
import pytest
|
|
|
|
from phasm.type3.entry import Type3Exception
|
|
|
|
from ..constants import COMPLETE_NUMERIC_TYPES, TYPE_MAP
|
|
from ..helpers import Suite
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', ['u8', 'u32', 'u64', ])
|
|
def test_module_constant_1(type_):
|
|
code_py = f"""
|
|
CONSTANT: ({type_}, ) = (65, )
|
|
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return CONSTANT[0]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 65 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_module_constant_6():
|
|
code_py = """
|
|
CONSTANT: (u8, u8, u32, u32, u64, u64, ) = (11, 22, 3333, 4444, 555555, 666666, )
|
|
|
|
@exported
|
|
def testEntry() -> u32:
|
|
return CONSTANT[2]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 3333 == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.parametrize('type_', COMPLETE_NUMERIC_TYPES)
|
|
def test_tuple_simple_constructor(type_):
|
|
code_py = f"""
|
|
@exported
|
|
def testEntry() -> {type_}:
|
|
return helper((24, 57, 80, ))
|
|
|
|
def helper(vector: ({type_}, {type_}, {type_}, )) -> {type_}:
|
|
return vector[0] + vector[1] + vector[2]
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 161 == result.returned_value
|
|
assert TYPE_MAP[type_] == type(result.returned_value)
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_float():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> f32:
|
|
return helper((1.0, 2.0, 3.0, ))
|
|
|
|
def helper(v: (f32, f32, f32, )) -> f32:
|
|
return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2])
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert 3.74 < result.returned_value < 3.75
|
|
|
|
@pytest.mark.integration_test
|
|
@pytest.mark.skip('SIMD support is but a dream')
|
|
def test_tuple_i32x4():
|
|
code_py = """
|
|
@exported
|
|
def testEntry() -> i32x4:
|
|
return (51, 153, 204, 0, )
|
|
"""
|
|
|
|
result = Suite(code_py).run_code()
|
|
|
|
assert (1, 2, 3, 0) == result.returned_value
|
|
|
|
@pytest.mark.integration_test
|
|
def test_assign_to_tuple_with_tuple():
|
|
code_py = """
|
|
CONSTANT: (u32, ) = 0
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Must be tuple'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_too_few_values():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 57, )
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Tuple element count mismatch'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_too_many_values():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 57, 1, 1, )
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Tuple element count mismatch'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_constant_type_mismatch():
|
|
code_py = """
|
|
CONSTANT: (u32, u8, u8, ) = (24, 4000, 1, )
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Must fit in 1 byte(s)'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_must_use_literal_for_indexing():
|
|
code_py = """
|
|
CONSTANT: u32 = 0
|
|
|
|
@exported
|
|
def testEntry(x: (u8, u32, u64)) -> u64:
|
|
return x[CONSTANT]
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Tuples must be indexed with literals'):
|
|
Suite(code_py).run_code()
|
|
|
|
@pytest.mark.integration_test
|
|
def test_tuple_must_use_integer_for_indexing():
|
|
code_py = """
|
|
@exported
|
|
def testEntry(x: (u8, u32, u64)) -> u64:
|
|
return x[0.0]
|
|
"""
|
|
|
|
with pytest.raises(Type3Exception, match='Must be integer'):
|
|
Suite(code_py).run_code()
|