def test_full_2d(self): a = """void postcondition (int N, ref double[N * N] postvol, ref double[N * N] prevol, ref double[N * N] volfluxx, int k, int j, int k_p, int j_p, ref bit _out)/*postvol-check.sk:114*/ { _out = 0; _out = 1; if((k_p >= 0) && (k_p < k))/*postvol-check.sk:118*/ { if((j_p >= 0) && (j_p < j))/*postvol-check.sk:119*/ { double arracc_s27 = prevol[j_p + (N * (k_p + 1))]; double arracc_s27_0 = volfluxx[j_p + (N * k_p)]; double arracc_s27_1 = prevol[j_p + (N * k_p)]; double arracc_s27_2 = volfluxx[(j_p + 1) + (N * k_p)]; double arracc_s27_3 = prevol[j_p + (N * (k_p + 1))]; double genned_s29 = arracc_s27_0 + (arracc_s27_1 - (arracc_s27_2 + arracc_s27_3)); if(!((postvol[j_p + (N * k_p)]) == (arracc_s27 + genned_s29)))/*postvol-check.sk:121*/ { _out = 0; } } } return; }""" p = PostconditionParser(a).get_postcondition() print ag.tree_to_str(p)
def test_multiple_subst(self): clauses = block.parseString( """_pac_sc_s37_0 = volfluxx[(_j + 1) + (N * _k)] _pac_sc_s37_1 = prevol[_j + (N * _k)] _pac_sc_s37_2 = volfluxx[_j + (N * _k)] _out_s36 = _pac_sc_s37_0 - (_pac_sc_s37_1 - (_pac_sc_s37_2 + (prevol[0])))""" )[0] rhs = exp.parseString("""((prevol[0]) - _out_s36)))""")[0] a = Simplifier(ag.to_ag_tree(rhs)).simplify( [ag.to_ag_tree(x) for x in clauses.body]) print ag.tree_to_str(a)
def test_complicated_array_assignment(self): tst_ast = AssignExp(ArrExp(VarNode("x"), NumNode(0)), ArrExp(VarNode("x"), NumNode(3))) q = ag.BinExp(ag.ArrExp(ag.VarNode("x"), ag.NumNode(0)), '<', ag.NumNode(10)) self.assertEqual("(x{0:=x[3]}[0] < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get())) tst_ast = AssignExp(ArrExp(VarNode("x"), VarNode("i")), ArrExp(VarNode("x"), NumNode(3))) q = ag.BinExp(ag.ArrExp(ag.VarNode("x"), ag.NumNode(0)), '<', ag.NumNode(10)) self.assertEqual("(x{i:=x[3]}[0] < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get())) tst_ast = AssignExp(ArrExp(VarNode("x"), VarNode("i")), ArrExp(VarNode("y"), VarNode("i"))) q = ag.BinExp(ag.ArrExp(ag.VarNode("x"), ag.NumNode(0)), '<', ag.NumNode(10)) self.assertEqual("(x{i:=y[i]}[0] < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get()))
def test_loop(self): print "in test_loop." tst_ast = Block([AssignExp(VarNode("i"), NumNode(1)), WhileLoop(VarNode("i"), BinExp(VarNode("i"), '<', NumNode(3)), Block([AssignExp(ArrExp(VarNode("a"), VarNode("i")), ArrExp(VarNode("a"), BinExp(VarNode("i"), '+', NumNode(1)))), AssignExp(VarNode("i"), BinExp(VarNode("i"), '+', NumNode(1)))]))]) #print tree_to_str(tst_ast) q = ag.CallExp(ag.VarNode("postcondition"), [ag.VarNode("a")]) precon = WeakestPrecondition(tst_ast, q, []) print "HMMM:", precon.additional_conditions print "=====" print ag.tree_to_str(precon.get_with_additional_conditions()) print "====="
def test_subst(self): rhs = ag.to_ag_tree( (exp.parseString("""((prevol[0]) - _out_s36)))"""))[0]) clauses = [ ag.to_ag_tree((assignexp.parseString("""_out_s36 = 4\n"""))[0]) ] a = Simplifier(rhs).simplify(clauses) self.assertEqual(ag.tree_to_str(a), "(prevol[0] - 4)")
def test_conditional(self): tst_ast = IfExp(BinExp(VarNode("i"), '<', NumNode(1)), Block([AssignExp(VarNode("i"), NumNode(3))]), Block([NumNode(1)])) q = ag.BinExp(ag.VarNode("i"), '<', ag.NumNode(10)) wp = WeakestPrecondition(tst_ast, q, []) allcond = ag.tree_to_str(wp.get_with_additional_conditions()) print "COND", allcond self.assertEqual("((!((i < 1))) -> ((i < 10)) && ((i < 1)) -> ((3 < 10)))", allcond)
def test_conditional_in_loop(self): tst_ast = Block([AssignExp(VarNode("i"), NumNode(1)), WhileLoop(VarNode("i"), BinExp(VarNode("i"), '<', NumNode(3)), Block([IfExp(BinExp(VarNode("i"), '<', NumNode(2)), Block([AssignExp(ArrExp(VarNode("a"), VarNode("i")), ArrExp(VarNode("b"), BinExp(VarNode("i"), '+', NumNode(1))))]), Block([AssignExp(ArrExp(VarNode("a"), VarNode("i")), ArrExp(VarNode("b"), BinExp(VarNode("i"), '-', NumNode(1))))])), AssignExp(VarNode("i"), BinExp(VarNode("i"), '+', NumNode(1)))]))]) q = ag.BinExp(ag.VarNode("i"), '<', ag.NumNode(10)) wp = WeakestPrecondition(tst_ast, q, []) allcond = ag.tree_to_str(wp.get_with_additional_conditions()) print tree_to_str(tst_ast) print "BIGCOND", allcond
def test_block(self): tst_ast = Block(body=[AssignExp(VarNode("i"), NumNode(3)), VarNode("i")]) q = ag.BinExp(ag.VarNode("i"), '<', ag.NumNode(10)) self.assertEqual("(3 < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get()))
def test_sequence_array_assignment(self): tst_ast = Block([AssignExp(ArrExp(VarNode("x"), NumNode(0)), NumNode(3)), AssignExp(ArrExp(VarNode("y"), VarNode("i")), ArrExp(VarNode("x"), NumNode(0)))]) q = ag.BinExp(ag.ArrExp(ag.VarNode("y"), ag.NumNode(0)), '<', ag.NumNode(10)) self.assertEqual("(y{i:=x{0:=3}[0]}[0] < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get()))
def test_array_assignment(self): tst_ast = AssignExp(ArrExp(VarNode("i"), NumNode(0)), NumNode(3)) q = ag.BinExp(ag.ArrExp(ag.VarNode("i"), ag.NumNode(0)), '<', ag.NumNode(10)) self.assertEqual("(i{0:=3}[0] < 10)", ag.tree_to_str(WeakestPrecondition(tst_ast, q, []).get()))