def test3 (self): """Defined only on the then branch of the if statement""" prg1 = "havoc x ; if x > 10 then { x := x + 1; z := 10} else y := x + 1 ; x := z + 1" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1)
def test_havoc(self): prg2 = "havoc x, y, z" ast2 = ast.parse_string(prg2) uv = undef_visitor.UndefVisitor() uv.check(ast2) #check if there is an empty set self.assertEquals(0, len(uv.get_undefs()))
def test2 (self): """"Variable defined only on the else branch of the if-statement""" prg1 = "havoc x ; if x > 10 then y := x + 1 else { x := x+1 ; z := 10 }; x := z + 1" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1)
def test_skip(self): prg3 = "skip" ast3 = ast.parse_string(prg3) uv = undef_visitor.UndefVisitor() uv.check(ast3) #check if there is an empty set self.assertEquals(0, len(uv.get_undefs()))
def test10 (self): """havoc is a definition""" prg1 = "havoc x ; y := x" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1)
def test1 (self): prg1 = "x := 10; y := x + z" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('z')]), uv.get_undefs ())
def test_while(self): prg7 = "while true do x := k +3 " ast7 = ast.parse_string(prg7) uv = undef_visitor.UndefVisitor() uv.check(ast7) #check if there is an empty set self.assertEquals(set([ast.IntVar('k')]), uv.get_undefs())
def test_if(self): prg6 = "if true then x := z + 5 else x := 6" ast6 = ast.parse_string(prg6) uv = undef_visitor.UndefVisitor() uv.check(ast6) #check if there is an empty set self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
def test3(self): """Defined only on the then branch of the if statement""" prg1 = "havoc x ; if x > 10 then { x := x + 1; z := 10} else y := x + 1 ; x := z + 1" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
def test5 (self): """Defined inside body of a loop""" prg1 = "while true do x := 1 ; y := x" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
def test6 (self): """Undefined use in a conditional""" prg1 = "if x > 0 then skip else skip" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
def test7 (self): """Undefined use in a loop condition""" prg1 = "while x > 0 do skip" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
def test10(self): """havoc is a definition""" prg1 = "havoc x ; y := x" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals(set(), uv.get_undefs())
def test4 (self): """Use an undefined variable to re-define itself""" prg1 = "x:=x+1" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
def test9 (self): """Undefined use in assert""" prg1 = "assert x > 0" ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) self.assertEquals (set ([ast.IntVar('x')]), uv.get_undefs ())
def test_assume(self): prg5 = "assume x < 3" ast5 = ast.parse_string(prg5) print ast5 uv = undef_visitor.UndefVisitor() uv.check(ast5) #check if there is an empty set self.assertEquals(set([ast.IntVar('x')]), uv.get_undefs())
def test_assert(self): prg4 = "assert x < y" ast4 = ast.parse_string(prg4) uv = undef_visitor.UndefVisitor() uv.check(ast4) #check if there is an empty set self.assertEquals(set([ast.IntVar('x'), ast.IntVar('y')]), uv.get_undefs())
def test_one(self): prg1 = "x := 10; y := x + z;havoc a;a:=b+c" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals( set([ast.IntVar('z'), ast.IntVar('b'), ast.IntVar('c')]), uv.get_undefs())
def test_while_if(self): prg1 = "x := 5; " \ "while x >= 0 do {" \ " if false then {" \ " if x > 1 then m := 0" \ " else m := 1" \ " } " \ "};" \ "y := m + k" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals({ast.IntVar('m'), ast.IntVar('k')}, uv.get_undefs())
def test_two(self): prg1 = """havoc x; if x > 10 then y:=x+1 else z := 10 ; x := z + 1""" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals(set([ast.IntVar('z')]), uv.get_undefs())
def test_three(self): prg1 = """havoc x; if x > 10 then z:=11 else z := 10 ; x := z + 1""" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals(set(), uv.get_undefs())
def test_while (self): prg1 = """ x := 10; assume x > 0; while x > 0 do x := x -1; print_state """ ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) # UNCOMMENT to run the test self.assertEquals (set ([]), uv.get_undefs ())
def test_havoc (self): prg1 = """ havoc x, y; if x > y then x := y else y := x; assert x = z; print_state """ ast1 = ast.parse_string (prg1) uv = undef_visitor.UndefVisitor () uv.check (ast1) # UNCOMMENT to run the test self.assertEquals (set ([ast.IntVar('z')]), uv.get_undefs ())
def test_undef_visitor(self): prg1 = """ a := 1; b := -1; if a > 1 and a >= b then b := 3; if a <= 1 or b < 3 then a := c; if false and not a = 1 then a := 1 else e := 2; skip; print_state; while (a < 3) and true do {a := a + 1}; assume b = -1; havoc c,d; assert a = 3; d := a + e """ ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1) self.assertEquals(set([ast.IntVar('c'), ast.IntVar('e')]), uv.get_undefs())
def visit_WhileStmt_inv(self, node, *args, **kwargs): """" Symbolic execution of while loops with invariants """ uv = undef_visitor.UndefVisitor() uv.check(node.body) vars = uv.get_undefs() c = node.inv.args[0] vars = list(vars) st = kwargs['state'] for i in node.body.stmts: c = i prg = "assert " + str(node.inv) + ";havoc " + str(vars[0]) + "," + str( vars[1]) + "; assume " + str(node.inv.args[0]) + "; if " + str( node.cond) + "then {" + str(node.body.stmts[0]) + ";" + str( node.body.stmts[1]) + ";assert " + str( node.inv) + "; assume false}" #prg = "assert "+str(node.inv.args[0]); ast1 = ast.parse_string(prg) for i in self._run_Stmts(ast1.stmts, st): yield i
def test_one(self): prg1 = "x := 10; y := x + z" ast1 = ast.parse_string(prg1) uv = undef_visitor.UndefVisitor() uv.check(ast1)