def test_list_elem_assign_with_wrong_index_should_give_error(self): types = [ Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int())) ] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} a; list[int] b; b[a] <- 1; }} """.format(type)) self.assertTrue( "Expression {} should have type int, but is {}".format( Expression(type, type.default_value, ident="a"), type.type_name) in str(context.exception))
def test_list_insert_with_wrong_type_value_should_give_error(self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int())) ] for t1 in types: for t2 in types: if t1 == t2: continue with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} a; list[{}] b; b.insert(a); }} """.format(t1, t2)) self.assertTrue( "List {} has been declared as {}, but inserted with element type {}" .format(Expression(Type.list_of(t2), [], ident="b"), Type.list_of(t2), t1) in str(context.exception))
def test_to_expression_should_equal_self(self): expressions = [ Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False), Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]), Expression(Type.list_of(Type.int()), []), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]), Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"), Expression(Type.boolean(), False, "a"), Expression(Type.vector(), [1.1, 2.2, -1.1], "a"), Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"), Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"), Expression(Type.list_of(Type.int()), [], "a"), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a") ] for expression in expressions: self.assertEqual(expression, expression.to_expression())
def test_to_expression_should_return_correct_value(self): expression = Expression(Type.vector(), [1.0, 2.0, 3.0], "a") expected = Expression(Type.decimal(), 1.0) self.assertEqual(expected, VectorElem("a", expression, 0).to_expression()) expression = Expression(Type.vector(), [1.0, 2.0, 3.0], "b[1]") expected = Expression(Type.decimal(), 3.0) self.assertEqual(expected, VectorElem("b[1]", expression, 2).to_expression())
def test_return_in_main_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} a; return a; }} """.format(type)) self.assertTrue("Cannot return in the Main function" in str(context.exception))
def test_str(self): self.assertEqual("Expression: { type: int, value: 1, ident: None }", str(Expression(Type.int(), 1))) self.assertEqual( "Expression: { type: decimal, value: 1.1, ident: None }", str(Expression(Type.decimal(), 1.1))) self.assertEqual( "Expression: { type: boolean, value: False, ident: None }", str(Expression(Type.boolean(), False))) self.assertEqual( "Expression: { type: vector, value: [1.1, 2.2, -1.1], ident: None }", str(Expression(Type.vector(), [1.1, 2.2, -1.1]))) self.assertEqual( "Expression: { type: list[int], value: [1, 2, 3, 4], ident: None }", str(Expression(Type.list_of(Type.int()), [1, 2, 3, 4]))) self.assertEqual( "Expression: { type: list[int], value: [], ident: None }", str(Expression(Type.list_of(Type.int()), []))) self.assertEqual( "Expression: { type: list[list[vector]], value: [[[1.1, 2.2, -1.1], [1, 2, -1]]], ident: None }", str( Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]))) self.assertEqual("Expression: { type: int, value: 1, ident: a }", str(Expression(Type.int(), 1, "a"))) self.assertEqual("Expression: { type: decimal, value: 1.1, ident: a }", str(Expression(Type.decimal(), 1.1, "a"))) self.assertEqual( "Expression: { type: boolean, value: False, ident: a }", str(Expression(Type.boolean(), False, "a"))) self.assertEqual( "Expression: { type: vector, value: [1.1, 2.2, -1.1], ident: a }", str(Expression(Type.vector(), [1.1, 2.2, -1.1], "a"))) self.assertEqual( "Expression: { type: drone, value: Drone: { name: DRONE, config: DroneConfig: " "{ init_position: (0, 0, 0), speed_mps: 1, rotate_speed_dps: 90, takeoff_height_meters: 1 } " "}, ident: a }", str( Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"))) self.assertEqual( "Expression: { type: list[int], value: [1, 2, 3, 4], ident: a }", str(Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"))) self.assertEqual( "Expression: { type: list[int], value: [], ident: a }", str(Expression(Type.list_of(Type.int()), [], "a"))) self.assertEqual( "Expression: { type: list[list[vector]], value: [[[1.1, 2.2, -1.1], [1, 2, -1]]], ident: a }", str( Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a")))
def test_define_functions_should_change_function_table(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: actual = FunctionTable() generate_commands(""" function func() return {} {{}} procedure proc() {{}} main () {{}} """.format(type), function_table=actual) expected = FunctionTable() expected.store("func", Function("func", [], type, [])) expected.store("proc", Function("proc", [], None, [])) self.assertEqual(expected, actual)
def test_define_functions_with_parameter_should_change_function_table(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type1 in types: for type2 in types: actual = FunctionTable() generate_commands(""" function func({} a, {} b) return int {{}} procedure proc({} a, {} b) {{}} main () {{}} """.format(type1, type2, type1, type2), function_table=actual) expected = FunctionTable() expected.store("func", Function("func", [Parameter("a", type1), Parameter("b", type2)], Type.int(), [])) expected.store("proc", Function("proc", [Parameter("a", type1), Parameter("b", type2)], None, [])) self.assertEqual(expected, actual)
def visitAssignVectorElem( self, ctx: xDroneParser.AssignVectorElemContext) -> None: vector_elem, expr = self.visit(ctx.vectorElem()), self.visit( ctx.expr()) ident = vector_elem.ident vector = vector_elem.container index = vector_elem.index if expr.type != Type.int() and expr.type != Type.decimal(): raise CompileError( "Assigned value {} should have type int or decimal, but is {}". format(expr, expr.type)) decimal_expr = Expression(Type.decimal(), float(expr.value), ident=expr.ident) self._update_nested_ident(ident, decimal_expr, index)
def test_return_not_exist_in_function_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" function func() return {} {{ }} main () {{ {} a <- func(); }} """.format(type, type)) self.assertTrue("Function func has returned type {}, but nothing is returned" .format(type) in str(context.exception))
def test_container_not_vector_should_give_error(self): none_vector_exprs = [ Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.0, "a"), Expression(Type.boolean(), True, "a"), Expression(Type.string(), "abc", "a"), Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"), Expression(Type.list_of(Type.decimal()), [1.0, 1.0, 1.0], "a"), Expression(Type.list_of(Type.list_of(Type.decimal())), [[1.0, 1.0, 1.0]], "a") ] for none_vector_expr in none_vector_exprs: with self.assertRaises(AssertionError) as context: VectorElem("a", none_vector_expr, 0)
def test_declare_and_assign_with_different_type_should_give_error(self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.list_of(Type.int())) ] for t1 in types: for t2 in types: if t1 == t2: continue with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} a; {} b <- a; }} """.format(t1, t2)) self.assertTrue( "Identifier b has been declared as {}, but assigned as {}". format(t2.type_name, t1.type_name) in str( context.exception))
def test_assign_list_elem_with_different_type_should_give_error(self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.list_of(Type.int())) ] for t1 in types: for t2 in types: if t1 == t2: continue with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} a; {} b; list[{}] c <- [a]; c[0] <- b; }} """.format(t1, t2, t1)) self.assertTrue( "Assigned value {} should have type {}, but is {}".format( Expression(t2, t2.default_value, ident="b"), t1.type_name, t2.type_name) in str(context.exception))
def test_for_with_wrong_type_ident_should_give_error(self): types = [Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ {} i; for i from 0 to 9 step 2 {{ }} }} """.format(type)) self.assertTrue("Identifier i has been declared as {}, but assigned as int" .format(type) in str(context.exception))
def test_repeated_declare_and_assign_drone_constant_should_give_error( self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.list_of(Type.int())) ] for type in types: with self.assertRaises(CompileError) as context: generate_commands( """ main () {{ {} a; {} DRONE <- a; }} """.format(type.type_name, type.type_name), drone_config_map={"DRONE": DefaultDroneConfig()}) self.assertTrue( "Identifier DRONE already declared" in str(context.exception))
def test_return_correct_type_in_function_should_return_correct_value(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: actual = SymbolTable() generate_commands(""" function func() return {} {{ {} a; return a; }} main () {{ {} a <- func(); }} """.format(type, type, type), symbol_table=actual) expected = SymbolTable() expected.store("a", Expression(type, type.default_value, ident="a")) self.assertEqual(expected, actual)
def test_for_with_wrong_type_step_expr_should_give_error(self): types = [Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ int i; {} a; for i from 0 to 9 step a {{ }} }} """.format(type)) self.assertTrue("Expression {} should have type int, but is {}" .format(Expression(type, type.default_value, ident="a"), type.type_name) in str(context.exception))
def test_return_value_in_procedure_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" procedure proc() {{ {} a; return a; }} main () {{ proc(); }} """.format(type)) self.assertTrue("Procedure proc should not return anything, but {} is returned" .format(Expression(type, type.default_value, ident=None)) in str(context.exception))
def test_eq(self): expressions1 = [ None, Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False), Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]), Expression(Type.list_of(Type.int()), []), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]), Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"), Expression(Type.boolean(), False, "a"), Expression(Type.vector(), [1.1, 2.2, -1.1], "a"), Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"), Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"), Expression(Type.list_of(Type.int()), [], "a"), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a") ] expressions2 = [ None, Expression(Type.int(), 1), Expression(Type.decimal(), 1.1), Expression(Type.boolean(), False), Expression(Type.vector(), [1.1, 2.2, -1.1]), Expression(Type.list_of(Type.int()), [1, 2, 3, 4]), Expression(Type.list_of(Type.int()), []), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0]), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]]), Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"), Expression(Type.boolean(), False, "a"), Expression(Type.vector(), [1.1, 2.2, -1.1], "a"), Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"), Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"), Expression(Type.list_of(Type.int()), [], "a"), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a") ] for i in range(len(expressions1)): for j in range(len(expressions2)): if i == j: self.assertEqual(expressions1[i], expressions2[j]) else: self.assertNotEqual(expressions1[i], expressions2[j])
def test_define_functions_with_duplicated_parameter_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for type1 in types: for type2 in types: with self.assertRaises(CompileError) as context: generate_commands(""" function func({} a, {} a) return int {{}} main () {{}} """.format(type1, type2, type1, type2)) self.assertTrue("Parameter names are duplicated in ['a', 'a']" in str(context.exception)) with self.assertRaises(CompileError) as context: generate_commands(""" procedure proc({} a, {} a) {{}} main () {{}} """.format(type1, type2, type1, type2)) self.assertTrue("Parameter names are duplicated in ['a', 'a']" in str(context.exception))
def test_vector_elem_expr_should_return_correct_value(self): actual = SymbolTable() generate_commands(""" main () { vector a <- (1, -2, -3.0); decimal b <- a.x; decimal c <- a.y; decimal d <- a.z; } """, symbol_table=actual) expected = SymbolTable() expected.store("a", Expression(Type.vector(), [1.0, -2.0, -3.0], ident="a")) expected.store("b", Expression(Type.decimal(), 1.0, ident="b")) expected.store("c", Expression(Type.decimal(), -2.0, ident="c")) expected.store("d", Expression(Type.decimal(), -3.0, ident="d")) self.assertEqual(expected, actual)
def test_declare_and_assign_decimal_should_change_symbol_table(self): actual = SymbolTable() generate_commands(""" main () { decimal a <- 1.0; } """, symbol_table=actual) expected = SymbolTable() expected.store("a", Expression(Type.decimal(), 1.0, ident="a")) self.assertEqual(expected, actual)
def test_to_expression_should_return_correct_value(self): expressions = [ None, Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.1, "a"), Expression(Type.boolean(), False, "a"), Expression(Type.vector(), [1.1, 2.2, -1.1], "a"), Expression(Type.drone(), Drone("DRONE", DefaultDroneConfig()), "a"), Expression(Type.list_of(Type.int()), [1, 2, 3, 4], "a"), Expression(Type.list_of(Type.int()), [], "a"), Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a") ] for expression in expressions: self.assertEqual(expression, Identifier("a", expression).to_expression())
def test_to_expression_should_return_correct_value(self): expression = Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a") expected = Expression(Type.decimal(), 1.0, "a[0]") self.assertEqual(expected, ListElem("a", expression, 0).to_expression()) expression = Expression(Type.list_of(Type.list_of(Type.vector())), [[[1.1, 2.2, -1.1], [1, 2, -1]]], "a") expected = Expression(Type.list_of(Type.vector()), [[1.1, 2.2, -1.1], [1, 2, -1]], "a[0]") self.assertEqual(expected, ListElem("a", expression, 0).to_expression()) expression = Expression(Type.list_of(Type.boolean()), [True, False], "b[1]") expected = Expression(Type.boolean(), False, "b[1][1]") self.assertEqual(expected, ListElem("b[1]", expression, 1).to_expression())
def visitCompare(self, ctx: xDroneParser.CompareContext) -> Expression: expr1, expr2 = self.visit(ctx.expr(0)), self.visit(ctx.expr(1)) if expr1.type != Type.int() and expr1.type != Type.decimal(): raise CompileError( "Expression {} should have type int or decimal, but is {}". format(expr1, expr1.type)) if expr2.type != Type.int() and expr2.type != Type.decimal(): raise CompileError( "Expression {} should have type int or decimal, but is {}". format(expr2, expr2.type)) if ctx.GREATER(): result_value = expr1.value > expr2.value elif ctx.GREATER_EQ(): result_value = expr1.value >= expr2.value elif ctx.LESS(): result_value = expr1.value < expr2.value else: # LESS_EQ result_value = expr1.value <= expr2.value return Expression(Type.boolean(), result_value)
def test_call_function_with_wrong_type_argument_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for t1 in types: for t2 in types: if t1 == t2: continue with self.assertRaises(CompileError) as context: generate_commands(""" function func({} i) return {} {{ return(i); }} main () {{ {} a; {} b <- func(a); }} """.format(t1, t1, t2, t1)) self.assertTrue("Arguments when calling function or procedure func should have types ['{}'], " "but is ['{}']".format(t1, t2) in str(context.exception))
def test_return_wrong_type_in_function_should_give_error(self): types = [Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.drone(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int()))] for t1 in types: for t2 in types: if t1 == t2: continue with self.assertRaises(CompileError) as context: generate_commands(""" function func() return {} {{ {} a; return a; }} main () {{ {} a <- func(); }} """.format(t1, t2, t1)) self.assertTrue("Function func has returned type {}, but {} is returned" .format(t1, t2) in str(context.exception))
def test_movement_with_other_type_should_give_error(self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.list_of(Type.int()), Type.list_of(Type.decimal()), Type.list_of(Type.list_of(Type.int())) ] for type in types: with self.assertRaises(CompileError) as context: generate_commands(""" main() {{ {} a; a.takeoff(); a.land(); }} """.format(type)) self.assertTrue( "Expression {} should have type drone, but is {}".format( Expression(type, type.default_value, ident="a"), type.type_name) in str(context.exception))
def test_assign_ident_decimal_should_update_symbol_table(self): actual = SymbolTable() generate_commands(""" main () { decimal a; a <- -1.5e10; } """, symbol_table=actual) expected = SymbolTable() expected.store("a", Expression(Type.decimal(), -1.5e10, ident="a")) self.assertEqual(expected, actual)
def test_list_with_inconsistent_type_should_give_error(self): with self.assertRaises(CompileError) as context: generate_commands(""" main () { list[int] a <- [1, 1.0]; } """) self.assertTrue( "Elements in list {} should have the same type".format([ str(Expression(Type.int(), 1, None)), str(Expression(Type.decimal(), 1.0, None)) ]) in str(context.exception))