def test_no_lvalue(): """Test that expressions that have no lvalue are invalid in assignment and increment expressions.""" decl = emptyfn("1 = 2;") with raises(CompileException): compile(decl) decl = emptyfn("1++;") with raises(CompileException): compile(decl)
def test_numeric_literals(): """Test various numeric literals.""" decl = emptyfn("var a := 1;") compile(decl) decl = emptyfn("var a := 1/u1;") compile(decl) decl = emptyfn("var a := 1/s1;") compile(decl) decl = emptyfn("var a := 1/u8;") compile(decl)
def test_var_decl(): """Test various variable declarations.""" decl = emptyfn("var a: u1;") compile(decl) decl = emptyfn("var a: u1 = 3;") compile(decl) decl = emptyfn("var a: [u1] = {1, 2, 3};") compile(decl) decl = emptyfn("var a: [u1@4] = {1, 2, 3, 4};") compile(decl) decl = emptyfn("var a: [u1@4] = {1, 2, 3};") compile(decl) decl = emptyfn("var a: [*u1] = \"test\";") with raises(CompileException): compile(decl) decl = emptyfn("var a: [*u1] = {1, 2};") with raises(CompileException): compile(decl) decl = emptyfn("var a: [u1@4] = {1, 2, 3, 4, 5};") with raises(CompileException): compile(decl) decl = emptyfn("var a: [u1] = 3;") with raises(CompileException): compile(decl)
def test_array_decl(): """Tests array declaration.""" decl = emptyfn("var a: [u1];") # this should error, no size information with raises(CompileException): compile(decl) decl = emptyfn("var a: [u1@5];") compile(decl) decl = emptyfn("var a: [u1@-4];") with raises(CompileException): compile(decl)
def test_types_to_binary_add_op(): """Test types to binary add operation.""" tests = ("1 + 1", "1 - 1", "1 + 1::*u4", "1::u2 + 1::u8", "1::u2 + 1::s8", "1::*u4 + 1", "1::*u4 - 1::*u4") for i in tests: compile(emptyfn(i + ";"))
def test_incompatible_types_to_binary_add_op(): """Test incorrect types to binary add operation.""" tests = ("1::*u4 + 1::*u4", "1::*u4 - 1::*u8") for i in tests: with raises(CompileException): compile(emptyfn(i + ";"))
def test_incompatible_types_to_mul_op(): """Test incorrect types to binary multiply operation.""" tests = ("1 * 1::*u4", "1::*u4 * 1::*u4") for i in tests: with raises(CompileException): compile(emptyfn(i + ";"))
def test_incompatible_types_to_bitwise_op(): """Test incorrect types to binary bitwise operation.""" tests = ("1 | 1::*u4", "1::*u4 | 1::*u4") for i in tests: with raises(CompileException): compile(emptyfn(i + ";"))
def test_incompatible_types_to_shift_op(): """Test incorrect types to binary shift operation.""" tests = ("1 << 1::*u4", "1::*u4 >> 1::*u4") for i in tests: with raises(CompileException): compile(emptyfn(i + ";"))
def test_return_parse(): """Test that the return statement is parsed correctly.""" decl = emptyfn("return 1;") fn, = parse_source(decl) rtn_stmt = fn.body[0] assert isinstance(rtn_stmt, objects.statements.ReturnStmt)
def test_if_stmt(): """Test the functionality of an if statement.""" decl = emptyfn("var a := 1;" "var b := 2;" "if a < b {" " return a;" "} elif a > b {" " return b;" "} elif a == b {" " return a+b;" "} else {" " return (a + b) / 2;" "}") compile(decl)
def test_deref_nonptr(): """Make sure we cannot dereference non-pointer types.""" decl = emptyfn("*(3::u8);") with raises(CompileException): compile(decl)
def test_call_nonfn(): """Make sure we can't call non-function types.""" decl = emptyfn("(3::u8)();") with raises(CompileException): compile(decl)
def test_while_loop(): """Test the functionality of a while loop.""" decl = emptyfn("var a := 2;" "while a {" " a = a * 2;" "}") compile(decl)
def test_unary_negate_on_unsigned(): """Make sure we cannot use unary negate on unsigned integers.""" decl = emptyfn("var a := 1;" "var b := -a;") with raises(CompileException): compile(decl)
def test_types_to_binary_shift_op(): """Test types to binary shift operation.""" tests = ("1 << 1", "1 >> 1") for i in tests: compile(emptyfn(i + ";"))
def test_array_vars_second(): """Test array initialisation where a variable isn't the inspected type.""" decl = emptyfn("var b := 2;" "var a := {1, b, 3};") compile(decl)
def test_array_indexes_no_void(): """Assert that it is not possible to use void pointers inside array indexing operations.""" decl = emptyfn("(0::*())[1];") with raises(CompileException): compile(decl)
def test_array_init_str(): """Test array initialisation.""" decl = emptyfn( "var a: [|*u8|] = {\"string\", \"morestring\", \"lessstring\"};") compile(decl)
def test_array_vars_first(): """Test array initialisation where a variable is the inspected type.""" decl = emptyfn("var b := 1;" "var a := {b, 2, 3};") compile(decl)
def test_array_lit_no_const(): """Test that non-constant expressions work in array lits.""" decl = emptyfn("var a := 3;" "{a, a * 2};") compile(decl)
def test_array_lit_num(): """Test array literals with numbers.""" decl = emptyfn("{1, 2, 3};") compile(decl)
def test_array_init_expr(): """Test that expressions in an array initialisation are valid.""" decl = emptyfn("var b := 4;" "var a := {b, b * 2};") compile(decl)
def test_array_init_invalid(): """Test array initialisation with conflicting types.""" decl = emptyfn("var a := {1, 2::*u2};") with raises(CompileException): compile(decl)
def test_types_to_binary_mul_op(): """Test types to binary multiply operation.""" tests = ("1 * 1", "1 / 1") for i in tests: compile(emptyfn(i + ";"))
def test_array_init_num(): """Test array initialisation.""" decl = emptyfn("var a := {1, 2, 3};") compile(decl)
def test_memory_reference_op(): """Test the memory-location-of operator.""" decl = emptyfn("var a: u1;" "return &a;", "*u1") compile(decl)
def test_void_dereference(): """Ensure that void pointers cannot be dereferenced.""" decl = emptyfn("*(0::*());") with raises(CompileException): compile(decl)
def test_array_lit_str(): """Test array literals with strings.""" decl = emptyfn("{\"string\", \"morestring\", \"lessstring\"};") compile(decl)
def test_var_multiple_different_newscope(): """Test that declarations of a variable with the same name as an existing variable of a different type in an enclosing type is valid.""" decl = emptyfn("var a:u4;" "{ var a:*u4; }") compile(decl)