def test_division_by_zero() -> None: basic = Basic() run_test(basic, " let _ = 42/0", "test_integer division by zero", None, RTError) run_test(basic, " let _ = 42.5/0", "test_float division by zero", None, RTError)
def test_float_assignment() -> None: basic = Basic() run_test(basic, "let a1 = 0.7", "test_positive_float_assignment", LangNumber, None, 0.7) run_test(basic, "let a2 = -0.7", "test_negative_float_assignment", LangNumber, None, -0.7)
def test_fib_naive() -> None: basic = Basic() run_test( basic, """ let fib = let fib_pom a b n = if n == 0 then a else fib_pom b (a+b) (n-1) in fib_pom 0 1 """, "test_fib_part1", LangFunction, None, ) for i in range(42): run_test( basic, f"let _ = fib {i}", f"test_fib_in_part{i + 1}", LangNumber, None, fib(i), )
def test_wrong_syntax_division() -> None: basic = Basic() run_test(basic, " let _ = /42", "test_wrong_order_division", None, InvalidSyntaxError) run_test( basic, " let _ = /42.5", "test_wrong_order_div_float", None, InvalidSyntaxError, )
def test_integer_equals() -> None: basic = Basic() run_test( basic, " let _ = 4 != +2", "test_positive_integer_equals", LangBool, None, 4 != 2, ) run_test( basic, " let _ = 4 != 4", "test_positive_integer_not_equals", LangBool, None, (4 != 4), ) run_test( basic, " let _ = -4 != -2", "test_negative_integer_not_equals", LangBool, None, (-4 != -2), ) run_test( basic, " let _ = 4 != -2", "test_mixed_integer_not_equals", LangBool, None, (4 != -2), )
def test_parentheses() -> None: basic = Basic() run_test( basic, " let _ = (4 + 2)", "test_single_parentheses", LangNumber, None, (4 + 2) ) run_test( basic, " let _ = (4) + 2", "test_single_parentheses", LangNumber, None, (4) + 2 ) run_test( basic, " let _ = ((4 + 2) + 6)", "test_double_parentheses", LangNumber, None, ((4 + 2) + 6), ) run_test( basic, " let _ = -((4 + 2) + 6)", "test_double_parentheses_negation", LangNumber, None, -((4 + 2) + 6), )
def test_wrong_logic_operator() -> None: basic = Basic() for name, operator in OPERATORS.items(): run_test( basic, " let _ = 42" + operator, "test_wrong_order_" + name, None, InvalidSyntaxError, ) run_test( basic, " let _ = 42.5" + operator, "test_wrong_order_" + name + "_float", None, InvalidSyntaxError, ) run_test( basic, " let _ = " + operator + "42", "test_wrong_order_" + name, None, InvalidSyntaxError, ) run_test( basic, " let _ = " + operator + "42.5", "test_wrong_order_" + name + "_float", None, InvalidSyntaxError, )
def test_tuple_correct_syntax() -> None: basic = Basic() run_test(basic, "let _ = 1", "test_tuple_valid_syntax1", LangNumber, None) run_test(basic, "let _ = 1, 2", "test_tuple_valid_syntax2", LangTuple, None) run_test(basic, "let _ = 1, 2, 3", "test_tuple_valid_syntax3", LangTuple, None) run_test(basic, "let _ = (1, 2)", "test_tuple_valid_syntax4", LangTuple, None)
def test_wrong_syntax_multiplication() -> None: basic = Basic() run_test( basic, " let _ = *42", "test_wrong_order_multiplication", None, InvalidSyntaxError, ) run_test( basic, " let _ = *42.5", "test_wrong_order_multiplication_float", None, InvalidSyntaxError, )
def test_wrong_syntax_raise_to_power() -> None: basic = Basic() run_test( basic, " let _ = ^42", "test_wrong_order_raise_to_power", None, InvalidSyntaxError, ) run_test( basic, " let _ = ^42.5", "test_wrong_order_raise_to_power_float", None, InvalidSyntaxError, )
def test_wrong_syntax_assignment() -> None: basic = Basic() run_test(basic, "a let 0 =", "test_wrong_order_assignment", None, InvalidSyntaxError) run_test(basic, "a let = 0", "test_wrong_order_assignment_2", None, InvalidSyntaxError) run_test(basic, "0 = let a", "test_wrong_order_assignment_3", None, InvalidSyntaxError) run_test(basic, "= 0 let a", "test_wrong_order_assignment_4", None, InvalidSyntaxError) run_test( basic, "1 + let a = 0", "test_wrong_order_assignment_5", None, InvalidSyntaxError, )
def test_list_invalid_syntax() -> None: basic = Basic() run_test(basic, "let _ = ][", "test_list_invalid_syntax1", None, InvalidSyntaxError) run_test(basic, "let _ = [1 2]", "test_list_invalid_syntax2", None, InvalidSyntaxError) run_test(basic, "let _ = [1, 2,,]", "test_list_invalid_syntax3", None, InvalidSyntaxError) run_test(basic, "let _ = [,]", "test_list_valid_syntax4", None, InvalidSyntaxError)
def test_list_correct_syntax() -> None: basic = Basic() run_test(basic, "let _ = []", "test_list_valid_syntax1", LangVariantType, None) run_test(basic, "let _ = [1,2,3]", "test_list_valid_syntax2", LangVariantType, None) run_test(basic, "let _ = [1]", "test_list_valid_syntax3", LangVariantType, None) run_test(basic, "let _ = [1,]", "test_list_valid_syntax4", LangVariantType, None)
def test_tuple_invalid_syntax() -> None: basic = Basic() run_test(basic, "let _ = ,", "test_tuple_invalid_syntax1", None, InvalidSyntaxError) run_test( basic, "let _ = ,1", "test_tuple_invalid_syntax2", None, InvalidSyntaxError ) run_test( basic, "let _ = 1,,1", "test_tuple_invalid_syntax3", None, InvalidSyntaxError ) run_test(basic, "let _ = 1,,", "test_tuple_valid_syntax4", None, InvalidSyntaxError)
def test_negation() -> None: basic = Basic() run_test(basic, " let _ = -42", "test_single_negation", LangNumber, None, -42) run_test(basic, " let _ = --42", "test_double_negation", LangNumber, None, 42) run_test(basic, " let _ = -42.5", "test_single_negation_float", LangNumber, None, -42.5) run_test(basic, " let _ = --42.5", "test_double_negation_float", LangNumber, None, 42.5)
def test_ack() -> None: basic = Basic() run_test( basic, "let ack m n = if m ==0 then n+1 elif n ==0 and m>0 then ack (m-1) 1 elif m>0 and n>0 then ack (m-1) (ack m (n-1)) else 0", "test_ack_decl", LangFunction, None, ) for i in range(1, 4): for j in range(1, 4): run_test( basic, f"let _ = ack {i} {j}", f"test_ack.{i}.{j}", LangNumber, None, ack(i, j), )
def test_mixed_logic_operator() -> None: basic = Basic() for name, operator in OPERATORS.items(): run_test( basic, " let _ = 4.5 " + operator + " 5", "test_positive_mixed_" + name, LangBool, None, (eval("4.5 " + operator + " 5")), ) run_test( basic, " let _ = -4.5 " + operator + " -2", "test_negative_mixed_" + name, LangBool, None, (eval("-4.5 " + operator + " -2")), ) run_test( basic, " let _ = -4.0 " + operator + " -4", "test_negative_mixed_" + name, LangBool, None, (eval("-4.0 " + operator + " -4")), )
def test_integer_arithmetic_operator() -> None: basic = Basic() for name, operator in OPERATORS.items(): run_test( basic, " let _ = 4 " + operator + " +2", "test_positive_integer" + name, LangNumber, None, (eval("4 " + py_op(operator) + " +2")), ) run_test( basic, " let _ = -4 " + operator + " -2", "test_positive_integer_" + name, LangNumber, None, (eval("-4 " + py_op(operator) + " -2")), ) run_test( basic, " let _ = 4 " + operator + " -2", "test_negative_" + name, LangNumber, None, (eval("4 " + py_op(operator) + " -2")), )
def test_mixed_arithmetic_operator() -> None: basic = Basic() for name, operator in OPERATORS.items(): run_test( basic, " let _ = 4.5 " + operator + " +5", "test_positive_mixed_" + name, LangNumber, None, (eval("4.5 " + py_op(operator) + " +5")), ) run_test( basic, " let _ = -4.5 " + operator + " -2", "test_negative_mixed_" + name, LangNumber, None, (eval("-4.5 " + py_op(operator) + " -2")), ) run_test( basic, " let _ = -4.0 " + operator + " -4", "test_negative_mixed_" + name, LangNumber, None, (eval("-4.0 " + py_op(operator) + " -4")), )
def test_pass_to_function() -> None: basic = Basic() run_test(basic, "let test list = []", "test_lists_in_functions1", LangFunction, None) run_test(basic, "test []", "test_lists_in_functions2", LangVariantType, None) run_test(basic, "test ([])", "test_lists_in_functions2", LangVariantType, None)
def test_parentheses_error() -> None: basic = Basic() run_test( basic, " let _ = (4 + 2", "test_unclosed_parentheses_error", None, InvalidSyntaxError, ) run_test( basic, " let _ = 4 + 2)", "test_unopened_parentheses_error", None, InvalidSyntaxError, ) run_test( basic, " let _ = (4 +) 2", "test_wrong_parentheses_placement", None, InvalidSyntaxError, )
def test_if() -> None: basic = Basic() run_test( basic, " let _ = if false then 1 else 2", "test_if_just_else1", LangNumber, None, 2, ) run_test( basic, " let _ = if false then 1 elif true then 2 else 3", "test_if_elif_1", LangNumber, None, 2, ) run_test( basic, " let _ = if false then 1 elif false then 2 else 3", "test_if_elif_2", LangNumber, None, 3, ) run_test( basic, "let _ = if false then 1 elif false then 2 elif true then 42 else 69", "test_if_multi_elif_1", LangNumber, None, 42, ) run_test( basic, "let _ = if false then 1 elif false then 2 elif false then 42 else 69", "test_if_multi_elif_2", LangNumber, None, 69, ) run_test( basic, "let _ = if false then 1 elif false then 2 else if true then 42 else 69", "test_if_nested_if_1", LangNumber, None, 42, ) run_test( basic, "let _ = if false then 1 elif false then 2 else if false then 42 else 69", "test_if_nested_if_2", LangNumber, None, 69, )
def test_unpacking() -> None: basic = Basic() run_test(basic, "let f, s = (1, 2)", "test_tuple_unpack1", LangTuple, None) run_test(basic, "f", "test_tuple_unpack2", LangNumber, None, 1) run_test(basic, "s", "test_tuple_unpack3", LangNumber, None, 2)
def test_pass_to_function() -> None: basic = Basic() run_test( basic, "let test tuple = []", "test_tuples_in_functions1", LangFunction, None ) run_test(basic, "test (1, 2)", "test_tuples_in_functions2", LangVariantType, None) run_test(basic, "test (1 )", "test_tuples_in_functions3", LangVariantType, None) run_test( basic, "let fst (f, s) = f", "test_tuples_in_functions_destruct1", LangFunction, None, ) run_test(basic, "fst (1 )", "test_tuples_in_functions_destruct2", None, RTError) run_test( basic, "fst (1, 2)", "test_tuples_in_functions_destruct3", LangNumber, None, 1 ) run_test( basic, "fst (2, 1)", "test_tuples_in_functions_destruct4", LangNumber, None, 2 ) run_test( basic, "let snd (f, s) = s", "test_tuples_in_functions_destruct2.1", LangFunction, None, ) run_test( basic, "snd (1,2,3)", "test_tuples_in_functions_destruct2.2", None, RTError ) run_test( basic, "snd (1, 2)", "test_tuples_in_functions_destruct2.3", LangNumber, None, 2 ) run_test( basic, "snd (2, 1)", "test_tuples_in_functions_destruct2.4", LangNumber, None, 1 )
def test_ack() -> None: basic = Basic() run_test( basic, "let add x y = x + y", "test_partial_part0", LangFunction, None, ) run_test( basic, "let inc = add 1", "test_partial_part1", LangFunction, None, ) for i in range(10): for j in range(10): run_test( basic, f"let _ = add {i} {j}", f"test_partials_part2.{i}.{j}", LangNumber, None, add(i, j), ) for i in range(21): run_test( basic, f"let _ = inc {i}", f"test_partials_part3.{i}", LangNumber, None, inc(i), ) for i in range(10): for j in range(10): run_test( basic, f"let _ = add {i} {j}", f"test_partials_part4.{i}.{j}", LangNumber, None, add(i, j), ) for i in range(21): run_test( basic, f"let _ = inc {i}", f"test_partials_part5.{i}", LangNumber, None, inc(i), )
def test_integer_assignment() -> None: basic = Basic() run_test(basic, "let a1 = 0", "test_positive_integer_assignment", LangNumber, None, 0) run_test(basic, "let a2 = -2", "test_negative_integer_assignment", LangNumber, None, -2) run_test( basic, "let a3 = 2 + 2", "test_addition_integer_assignment_part1", LangNumber, None, 4, ) run_test( basic, "let _ = a3", "test_addition_integer_assignment_part2", LangNumber, None, 4, ) run_test( basic, "let a4 = 2 + 2", "test_multiplication_integer_assignment_part1", LangNumber, None, 4, ) run_test( basic, "let _ = a4", "test_multiplication_integer_assignment_part2", LangNumber, None, 4, ) run_test( basic, "let a5 = 2 ^ 2", "test_raised_to_power_integer_assignment_part1", LangNumber, None, 4, ) run_test( basic, "let _ = a5", "test_raised_to_power_integer_assignment_part2", LangNumber, None, 4, ) run_test( basic, "let a6 = (let a7 = 2 in a7) * (let a8 = 1 in a8) * (let a9 = 3 in a9) * (let a10 = 7 in a10)", "test_assignement_output_part1", LangNumber, None, 2 * 1 * 3 * 7, ) run_test( basic, "let _ = a6", "test_assignement_output_part2", LangNumber, None, 2 * 1 * 3 * 7, ) run_test(basic, "let _ = a7", "test_assignement_output_part3", LangNumber, None, 2) run_test(basic, "let _ = a8", "test_assignement_output_part4", LangNumber, None, 1) run_test(basic, "let _ = a9", "test_assignement_output_part5", LangNumber, None, 3) run_test(basic, "let _ = a10", "test_assignement_output_part6", LangNumber, None, 7) run_test( basic, "let a11 = (let a12 = 2 in a12)", "test_multi_assignement_part1", LangNumber, None, 2, ) run_test(basic, "let _ = a11", "test_multi_assignement_part2", LangNumber, None, 2) run_test(basic, "let _ = a12", "test_multi_assignement_part3", LangNumber, None, 2)
def test_underscore_assignment() -> None: basic = Basic() run_test(basic, "let _ = 0.7", "test_underscore_assignment_part1", LangNumber, None, 0.7) run_test(basic, "_", "test_underscore_assignment_part2", None, RTError)
def test_order() -> None: basic = Basic() run_test( basic, " let _ = 42 - 6 * 7", "test_order_-2", LangNumber, None, 42 - 6 * 7 ) run_test( basic, " let _ = 7 + 2 * (6 + 3) / 3 - 7", "test_order_-1", LangNumber, None, 7 + 2 * (6 + 3) / 3 - 7, ) run_test(basic, " let _ = 42 - 6 * 7", "test_order_0", LangNumber, None, 42 - 6 * 7) run_test( basic, " let _ = 7 + 2 * (6 + 3) / 3 - 7", "test_order_1", LangNumber, None, 7 + 2 * (6 + 3) / 3 - 7, ) run_test(basic, " let _ = 42 - 6 * 7", "test_order_2", LangNumber, None, 42 - 6 * 7) run_test( basic, " let _ = 11 + 19 * 2", "test_order_3", LangNumber, None, 11 + 19 * 2 ) run_test(basic, " let _ = 42 - 6 * 7", "test_order_4", LangNumber, None, 42 - 6 * 7) run_test( basic, " let _ = 7 + 2 * (6 + 3) / 3 - 7", "test_order_5", LangNumber, None, 7 + 2 * (6 + 3) / 3 - 7, ) run_test( basic, " let _ = (14 + 2) * 2 + 3", "test_order_6", LangNumber, None, (14 + 2) * 2 + 3, ) run_test( basic, " let _ = 120 / (6 + 12 * 2)", "test_order_7", LangNumber, None, 120 / (6 + 12 * 2), ) run_test( basic, " let _ = 12 + 2 * 44", "test_order_8", LangNumber, None, 12 + 2 * 44 ) run_test( basic, " let _ = 10 * 2 - (7 + 9)", "test_order_9", LangNumber, None, 10 * 2 - (7 + 9), ) run_test(basic, " let _ = 2 * 2 ^ 3", "test_order_10", LangNumber, None, 2 * 2 ** 3) run_test( basic, " let _ = 2 * 2 ^ 3 * 2", "test_order_11", LangNumber, None, 2 * 2 ** 3 * 2, )
def test_wrong_syntax_if() -> None: basic = Basic() run_test(basic, " let _ = if", "test_wrong_syntax_if", None, InvalidSyntaxError) run_test( basic, " let _ = if 1 then 2", "test_wrong_syntax_if_float", None, InvalidSyntaxError, ) run_test( basic, " let _ = if 1 then 2 elif 3 then 2", "test_wrong_syntax_if", None, InvalidSyntaxError, ) run_test( basic, " let _ = if 1 2 else 4", "test_wrong_syntax_if_float", None, InvalidSyntaxError, ) run_test( basic, "let _ = if 1 then 2 else 4 elif 4 then 9", "test_wrong_syntax_if_float", None, InvalidSyntaxError, ) run_test( basic, " let _ = 1 then 2 else", "test_wrong_syntax_if_float", None, InvalidSyntaxError, )