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.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.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_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: 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_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 test_assign_list_elem_with_different_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.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( t2.default_value, t1.type_name, t2.type_name) in str( context.exception))
def test_container_not_list_should_give_error(self): none_list_exprs = [Expression(Type.int(), 1, "a"), Expression(Type.decimal(), 1.0, "a"), Expression(Type.boolean(), True, "a"), Expression(Type.string(), "abc", "a"), Expression(Type.vector(), [1.0, 1.0, 1.0], "a")] for none_list_expr in none_list_exprs: with self.assertRaises(AssertionError) as context: ListElem("a", none_list_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.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_declare_and_assign_boolean_should_change_symbol_table(self): actual = SymbolTable() generate_commands( """ main () { boolean a <- true; } """, actual) expected = SymbolTable() expected.store("a", Expression(Type.boolean(), True, ident="a")) self.assertEqual(expected, actual)
def test_str(self): st = SymbolTable() st.store("a", Expression(Type.int(), 0)) st.store("b", Expression(Type.list_of(Type.boolean()), [True])) expected = "SymbolTable: {\n" + \ " a -> Expression: { type: int, value: 0, ident: None }\n" + \ " b -> Expression: { type: list[boolean], value: [True], ident: None }\n" + \ "}" self.assertEqual(expected, str(st))
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.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_eq(self): list_elems1 = [None, ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 0), ListElem("b", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "b"), 0), ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0], "a"), 0), ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 1), ListElem("a", Expression(Type.list_of(Type.list_of(Type.int())), [[[1], [1]]], "a"), 0), ListElem("b[1]", Expression(Type.list_of(Type.boolean()), [True, False], "b[1]"), 0)] list_elems2 = [None, ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 0), ListElem("b", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "b"), 0), ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0], "a"), 0), ListElem("a", Expression(Type.list_of(Type.decimal()), [1.0, 2.0, 3.0, 4.0], "a"), 1), ListElem("a", Expression(Type.list_of(Type.list_of(Type.int())), [[[1], [1]]], "a"), 0), ListElem("b[1]", Expression(Type.list_of(Type.boolean()), [True, False], "b[1]"), 0)] for i in range(len(list_elems1)): for j in range(len(list_elems2)): if i == j: self.assertEqual(list_elems1[i], list_elems2[j]) else: self.assertNotEqual(list_elems1[i], list_elems2[j])
def test_eq(self): types1 = [ None, 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())), Type.list_of(Type.list_of(Type.decimal())) ] types2 = [ None, 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())), Type.list_of(Type.list_of(Type.decimal())) ] for i in range(len(types1)): for j in range(len(types2)): if i == j: self.assertEqual(types1[i], types2[j]) else: self.assertNotEqual(types1[i], types2[j]) self.assertEqual(Type.empty_list(), Type.empty_list()) for type in types1: if isinstance(type, ListType): self.assertEqual(Type.empty_list(), type) self.assertEqual(type, Type.empty_list()) else: self.assertNotEqual(Type.empty_list(), type) self.assertNotEqual(type, Type.empty_list())
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.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_declare_and_assign_ident_empty_list_should_update_symbol_table( self): types = [ Type.int(), Type.decimal(), Type.string(), Type.boolean(), Type.vector(), Type.list_of(Type.int()), Type.list_of(Type.list_of(Type.int())) ] for type in types: actual = SymbolTable() generate_commands( """ main () {{ list[{}] a <- []; }} """.format(type.type_name), actual) expected = SymbolTable() expected.store("a", Expression(Type.list_of(type), [], ident="a")) self.assertEqual(expected, actual)
def test_declare_and_then_assign_vector_elem_with_different_type_should_give_error( self): for type in [ Type.int(), Type.string(), Type.boolean(), Type.vector(), Type.list_of(Type.int()), Type.list_of(Type.list_of(Type.int())) ]: with self.assertRaises(CompileError) as context: generate_commands(""" main () {{ vector a; {} b; a.x <- b; }} """.format(type.type_name)) self.assertTrue( "Assigned value {} should have type decimal, but is {}".format( type.default_value, type.type_name) in str( context.exception))
def test_boolean(self): self.assertEqual(Type.boolean(), Type.boolean()) self.assertEqual("boolean", Type.boolean().type_name) self.assertEqual(False, Type.boolean().default_value) self.assertEqual("boolean", str(Type.boolean()))
def boolean_type(self, children) -> Type: return Type.boolean()
def true_expr(self, children) -> Expression: return Expression(Type.boolean(), True)
def false_expr(self, children) -> Expression: return Expression(Type.boolean(), False)