def test_no_body_no_return(self): expression = FunctionDeclarationExpression(0, "func2", "func2", Void, [], []) stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents( ) == "@runtime_function(\"func2\")\ndef func2():\n pass\n"
def test_no_body(self): expression = FunctionDeclarationExpression(0, "func2", "func2", List(Integer), [], []) stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents() == "@runtime_function(\"func2\")\n" \ "def func2():\n return list()\n"
def test_elseif(self): expr = ConditionExpression( 0, [(Expression(0, Bool, ["a"]), [Expression(0, Integer, "b")]), (Expression(0, Bool, ["c"]), [Expression(0, Integer, "d")])], [Expression(0, Real, "e")]) pool = VariablesPool() stream = CodeStream() expr.generate(pool, stream, CodeMap(), None) res = stream.get_contents() assert res == "if a:\n b\nelse:\n if c:\n d\n else:\n e\n"
def test_declaration(self): expression = FunctionDeclarationExpression( 0, "func_name", "func", Integer, ["x", "y", "z"], [Expression(0, Void, "a"), Expression(0, Void, "b")]) stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents() == "@runtime_function(\"func\")\n" \ "def func_name(x, y, z):\n" \ " a\n b\n" \ " return 0\n"
def test_loop(self): conditon = Expression(0, Bool, ["a ", 0]) conditon.add_intermediate(Expression(0, Void, ["b"])) expr = DoWhileLoopExpression( 0, conditon, [Expression(0, Void, "a"), Expression(0, Void, "b")]) stream = CodeStream() expr.generate(VariablesPool(), stream, CodeMap(), None) res = stream.get_contents() assert res == "while True:\n a\n b\n tmp_0 = b\n if not (a tmp_0):\n break\n"
def test_simple(self): expr = ConditionExpression( 0, [(Expression(0, Bool, ["a"]), [Expression(0, Integer, "b"), Expression(0, Integer, "c")])], []) pool = VariablesPool() stream = CodeStream() expr.generate(pool, stream, CodeMap(), None) res = stream.get_contents() assert res == "if a:\n b\n c\n" assert expr.type == Void
def test_loop(self): range_expression = Expression(0, Set(Integer), ["range(", 0, ")"]) range_expression.add_intermediate(Expression(0, Bool, ["x"])) expr = ForLoopExpression( 0, "i", range_expression, [Expression(0, Void, "x"), Expression(0, Integer, "y")]) stream = CodeStream() expr.generate(VariablesPool(), stream, CodeMap(), None) res = stream.get_contents() assert res == "tmp_0 = x\nfor i in range(tmp_0):\n x\n y\n"
def test_loop(self): iterations = Expression(0, Bool, ["a ", 0]) iterations.add_intermediate(Expression(0, Void, ["b"])) expr = RepeatLoopExpression( 0, iterations, [Expression(0, Void, "a"), Expression(0, Void, "b")]) stream = CodeStream() expr.generate(VariablesPool(), stream, CodeMap(), None) res = stream.get_contents() assert res == "tmp_1 = b\nfor tmp_0 in range(a tmp_1):\n a\n b\n"
def test_expression(self): expr = ConditionExpression( 0, [(Expression(0, Bool, ["a"]), [Expression(0, Integer, "b")])], [Expression(0, Real, "c"), Expression(0, Integer, "d")]) pool = VariablesPool() stream = CodeStream() expr.generate(pool, stream, CodeMap(), "res") res = stream.get_contents() assert res == "if a:\n res = b\nelse:\n c\n res = d\n" assert expr.type == Integer
def test_generating(): expression = Expression(0, Integer) t1 = expression.add_intermediate(Expression(0, Integer, ["2 + 3"])) t2 = expression.add_intermediate(Expression(0, Integer, ["4 + 6"])) expression.expression = [t1, " * ", t2] stream = CodeStream() pool = VariablesPool() pool.allocate(2) expression.generate(pool, stream, CodeMap(), None) assert stream.get_contents( ) == "tmp_2 = 2 + 3\ntmp_3 = 4 + 6\ntmp_2 * tmp_3\n"
def test_loop_variable(self): condition = Expression(0, Bool, ["a ", 0]) condition.add_intermediate(Expression(0, Integer, "b")) expr = WhileLoopExpression( 0, condition, [Expression(0, Integer, "b"), Expression(0, Integer, "c")]) stream = CodeStream() expr.generate(VariablesPool(), stream, CodeMap(), None) res = stream.get_contents() assert res == "tmp_0 = b\nwhile a tmp_0:\n b\n c\n tmp_0 = b\n"
def test_merge(self): expr1 = Expression(0, Integer, ["a"]) expr2 = ConditionExpression( 0, [(Expression(0, Bool, ["a"]), [Expression(0, Integer, "b")])], [Expression(0, Integer, "d")]) merged = Expression.merge(Bool, [0, " -- ", 1], expr1, expr2) pool = VariablesPool() stream = CodeStream() merged.generate(pool, stream, CodeMap(), "x") res = stream.get_contents() assert res == "if a:\n tmp_0 = b\nelse:\n tmp_0 = d\nx = a -- tmp_0\n"
def test_cube_rotation(): expression = CubeRotationExpression(0, "front", False) stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents() == "cube_rotate(front, False)\n"
def test_indices(self): expression = CubeTurningExpression(0, "left", 2) expression.indices = [Expression(0, Integer, x) for x in "abc"] stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents() == "cube_turn(left, 2, [a, b, c])\n"
def test_double(self): expression = CubeTurningExpression(0, "left", 2) stream = CodeStream() expression.generate(VariablesPool(), stream, CodeMap(), None) assert stream.get_contents() == "cube_turn(left, 2, [1])\n"