-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for python interpreter/code_generator (#24)
- Loading branch information
1 parent
80dfb48
commit fbdee6d
Showing
7 changed files
with
131 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
from m2cgen import ast | ||
from m2cgen import interpreters | ||
from tests import utils | ||
|
||
|
||
def test_if_expr(): | ||
expr = ast.IfExpr( | ||
ast.CompExpr(ast.NumVal(1), ast.FeatureRef(0), ast.CompOpType.EQ), | ||
ast.NumVal(2), | ||
ast.NumVal(3)) | ||
|
||
interpreter = interpreters.PythonInterpreter() | ||
|
||
expected_code = """ | ||
def score(input): | ||
if (1) == (input[0]): | ||
var0 = 2 | ||
else: | ||
var0 = 3 | ||
return var0 | ||
""" | ||
|
||
utils.assert_code_equal(interpreter.interpret(expr)[0][1], expected_code) | ||
|
||
|
||
def test_bin_num_expr(): | ||
expr = ast.BinNumExpr( | ||
ast.BinNumExpr( | ||
ast.FeatureRef(0), ast.NumVal(-2), ast.BinNumOpType.DIV), | ||
ast.NumVal(2), | ||
ast.BinNumOpType.MUL) | ||
|
||
interpreter = interpreters.PythonInterpreter() | ||
|
||
expected_code = """ | ||
def score(input): | ||
return ((input[0]) / (-2)) * (2) | ||
""" | ||
|
||
utils.assert_code_equal(interpreter.interpret(expr)[0][1], expected_code) | ||
|
||
|
||
def test_dependable_condition(): | ||
left = ast.BinNumExpr( | ||
ast.IfExpr( | ||
ast.CompExpr(ast.NumVal(1), | ||
ast.NumVal(1), | ||
ast.CompOpType.EQ), | ||
ast.NumVal(1), | ||
ast.NumVal(2)), | ||
ast.NumVal(2), | ||
ast.BinNumOpType.ADD) | ||
|
||
right = ast.BinNumExpr(ast.NumVal(1), ast.NumVal(2), ast.BinNumOpType.DIV) | ||
bool_test = ast.CompExpr(left, right, ast.CompOpType.GTE) | ||
|
||
expr = ast.IfExpr(bool_test, ast.NumVal(1), ast.FeatureRef(0)) | ||
|
||
expected_code = """ | ||
def score(input): | ||
if (1) == (1): | ||
var1 = 1 | ||
else: | ||
var1 = 2 | ||
if ((var1) + (2)) >= ((1) / (2)): | ||
var0 = 1 | ||
else: | ||
var0 = input[0] | ||
return var0 | ||
""" | ||
|
||
interpreter = interpreters.PythonInterpreter() | ||
|
||
utils.assert_code_equal(interpreter.interpret(expr)[0][1], expected_code) | ||
|
||
|
||
def test_nested_condition(): | ||
left = ast.BinNumExpr( | ||
ast.IfExpr( | ||
ast.CompExpr(ast.NumVal(1), | ||
ast.NumVal(1), | ||
ast.CompOpType.EQ), | ||
ast.NumVal(1), | ||
ast.NumVal(2)), | ||
ast.NumVal(2), | ||
ast.BinNumOpType.ADD) | ||
|
||
bool_test = ast.CompExpr(ast.NumVal(1), left, ast.CompOpType.EQ) | ||
|
||
expr_nested = ast.IfExpr(bool_test, ast.FeatureRef(2), ast.NumVal(2)) | ||
|
||
expr = ast.IfExpr(bool_test, expr_nested, ast.NumVal(2)) | ||
|
||
expected_code = """ | ||
def score(input): | ||
if (1) == (1): | ||
var1 = 1 | ||
else: | ||
var1 = 2 | ||
if (1) == ((var1) + (2)): | ||
if (1) == (1): | ||
var2 = 1 | ||
else: | ||
var2 = 2 | ||
if (1) == ((var2) + (2)): | ||
var0 = input[2] | ||
else: | ||
var0 = 2 | ||
else: | ||
var0 = 2 | ||
return var0 | ||
""" | ||
|
||
interpreter = interpreters.PythonInterpreter() | ||
utils.assert_code_equal(interpreter.interpret(expr)[0][1], expected_code) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters