def all_definitions(CG, T): """The arguments are a control graph (CG), and a data set of variables(T). The list of elements named vert_to_visit is the vertices where there is an assignement of a variables. This test checks if there are used at least once. It checks if the criteria all_definitions is validated.""" for var in CG.var: vert_var_in_defv = set() for vert in CG.vertices: if var in vert.defv: vert_var_in_defv.add(vert.label) vert_to_visit = set(vert_var_in_defv) vert_not_visited = set(vert_var_in_defv) for t in T: path, variables = apply_path(CG, t) for definition_vert in vert_var_in_defv: def_vert_visited = False for label in path: cur_vertice = find_vertice_with_label(CG, label) if label == definition_vert: def_vert_visited = True continue if var in cur_vertice.refv and def_vert_visited: vert_not_visited.remove(definition_vert) break Graph.coverage_criteria2(vert_to_visit, "not_visited", vert_not_visited) if vert_not_visited: print('Test failed') return False print('Test passed') return True
def all_DU_paths(CG, T): """The arguments are a control graph (CG), and a data set of variables(T). The list of elements named path_to_visit is a list of simple partial path where there is a definition vertice and reference vertice of a variable without redefinition between both vertices. This test checks if each path is visited at least once. It checks if the criteria all_DU_paths is validated.""" path_to_visit = [] path_not_visited = [] for var in CG.var: vert_var_in_defv = set() vert_var_in_refv = set() for vert in CG.vertices: if var in vert.defv: vert_var_in_defv.add(vert.label) elif var in vert.refv: vert_var_in_refv.add(vert.label) couples = product(vert_var_in_defv, vert_var_in_refv) for vert_def, vert_ref in couples: #paths_remaining = list(CG.simple_partial_paths[(vert_def, vert_ref)]) path_to_visit += list(CG.simple_partial_paths[(vert_def, vert_ref)]) path_not_visited += list(CG.simple_partial_paths[(vert_def, vert_ref)]) for t in T: path, variables = apply_path(CG, t) vert_found = False for label in path: cur_vertice = find_vertice_with_label(CG, label) if vert_def == label: vert_found = True elif vert_ref == label and vert_found: if path in path_not_visited: path_not_visited.remove(path) break elif vert_ref != label and var in cur_vertice.refv: break #print('Test failed') #print(paths_remaining, var,vert_def, vert_ref, path) #return False #if paths_remaining: # print('Test failed') # print(paths_remaining, var, vert_def, vert_ref, path) # return False Graph.coverage_criteria2(path_to_visit, "not_visited", path_not_visited) if path_not_visited: print('Test failed') else: print('Test passed') return True
def all_conditions(CG, L, T): """The arguments are a control graph (CG), the list of paths that should be visited to validate the criteria (L),and a data set of variables(T). It checks if the criteria all_conditions is validated.""" to_visit = list(L) not_visited = list(L) for t in T: p = apply_path(CG, t)[0] if p in not_visited: not_visited.remove(p) Graph.coverage_criteria2(to_visit, "not_visited", not_visited) if not_visited: print('test failed') else: print('test passed')
def all_decisions(CG, L, T): """The arguments are a control graph (CG), the list of edges that should be visited to validate the criteria (L),and a data set of variables(T). It checks if the criteria all_decisions is validated.""" to_visit = list(L) visited = set() for t in T: path_visited = apply_path(CG, t)[0] edge_visited = set((path_visited[i], path_visited[i + 1]) for i in range(len(path_visited) - 1)) visited |= edge_visited Graph.coverage_criteria2(to_visit, "visited", visited.intersection(to_visit)) if L - visited: print('test failed') else: print('test passed')
def all_usages(CG, T): """The arguments are a control graph (CG), and a data set of variables(T). The list of elements named couples_to_visit is a list of tuple (def,ref) with def a vertice with assignement of a variable and ref a vertice with a reference of this variable . This test checks if each tuple are visited without a redefinition between both. It checks if the criteria all_usages is validated.""" couples_to_visit = list() couples_not_visited = list() for var in CG.var: vert_var_in_defv = set() vert_var_in_refv = set() for vert in CG.vertices: if var in vert.defv: vert_var_in_defv.add(vert.label) elif var in vert.refv: vert_var_in_refv.add(vert.label) couples = product(vert_var_in_defv, vert_var_in_refv) couples_to_visit += list(couples) couples_not_visited += list(couples_to_visit) for t in T: path, variables = apply_path(CG, t) for vert_def, vert_ref in couples_not_visited: vert_def_found = False for vert_label in path: vert = find_vertice_with_label(CG, vert_label) if vert_def == vert.label: vert_def_found = True elif vert_ref == vert.label and vert_def_found: couples_not_visited.remove((vert_def, vert_ref)) break elif vert_ref != vert.label and var in vert.refv and vert_def_found: break Graph.coverage_criteria2(couples_to_visit, "not_visited", couples_not_visited) if couples_not_visited: print('Test failed') return False else: print('Test passed') return True
def all_assigned(CG, L, T): """The arguments are a control graph (CG), the list of vertices that should be visited to validate the criteria (L),and a data set of variables(T). It checks if the criteria all_assigned is validated.""" to_visit = L not_visited = list(L) for t in T: p = apply_path(CG, t) for label in p[0]: if label in not_visited: not_visited.remove(label) Graph.coverage_criteria2(to_visit, "not_visited", not_visited) if not_visited: print('test failed') return False else: print('test passed') return True
V5.add_next_edge(Edge(B_True, C5, "exit")) V6 = Vertice(6) def fun_6(variables): variables['X'] += 1 return variables def reverse_fun_6(variables): variables['X'] -= 1 return variables C6 = Command_Exp(fun_6, "X:=X+1", type="assign") C6.reverse_fun(reverse_fun_6) V6.add_next_edge(Edge(B_True, C6, "exit")) Vexit = Vertice("exit") Control_Graph = Graph([V1, V2, V3, V4, V5, V6, Vexit]) return Control_Graph if __name__ == "__main__": CG = CG_Project_Example() variables = {"X": 2} print(apply_path(CG, variables)) print(CG.vertices[4].__dict__) print(CG.vertices[5].__dict__) print(CG.__dict__)