示例#1
0
def test_cppgen_if():
    exp = """
if(x==y){
  x=1;\n
}else if(x>y){
  x=2;\n
}else{
  x=3;\n
}""".strip() + "\n"
    n = If(cond=BinOp(x=Var(name="x"), op="==", y=Var(name="y")),\
           body=[ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="1")))],\
           elifs=[(BinOp(x=Var(name="x"), op=">", y=Var(name="y")), [ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="2")))])],\
           el=ExprStmt(child=Assign(target=Var(name="x"), value=Raw(code="3"))))
    obs = CPPGEN.visit(n)
    assert_equal(exp, obs)
示例#2
0
def test_cppgen_for():
    exp = """
for(int i=0;i<5;i++){
  a++;
  b++;
  c[i]=a+b;\n
}""".strip() + "\n"
    n = For(adecl=DeclAssign(type=Type(cpp="int"), target=Var(name="i"), value=Raw(code="0")),\
            cond=BinOp(x=Var(name="i"), op="<", y=Raw(code="5")),\
            incr=RightUnaryOp(name=Var(name="i"), op="++"),\
            body=[ExprStmt(child=RightUnaryOp(name=Var(name="a"), op="++")),
                  ExprStmt(child=RightUnaryOp(name=Var(name="b"), op="++")),
                  ExprStmt(child=Assign(target=RightUnaryOp(name=Var(name="c"), op="[i]"),
                         value=BinOp(x=Var(name="a"), op="+", y=Var(name="b"))))])
    obs = CPPGEN.visit(n)
    assert_equal(exp, obs)
示例#3
0
def test_cppgen_assign():
    exp = "x=y"
    n = Assign(target=Var(name="x"), value=Var(name="y"))
    obs = CPPGEN.visit(n)
    assert_equal(exp, obs)