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_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 test_ast_IntVar(self): my_astvisitor = MyAstVisitor() intvar = ast.IntVar('a := 1') my_astvisitor.visit(intvar) repr(intvar) str(intvar) hash(intvar)
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 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 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 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 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 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 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 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 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 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_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_invar(self): node = ast.IntVar('x') print(str(node), repr(node), hash(node))