def test_vertex_not_found2(): graph = Graph() graph.create_vertex_by_id('B') with pytest.raises(NotContainsElementException) as exception_info: graph.add_edge('A', 'B') assert str(exception_info.value) == not_contains_vertex_message('A')
def test_edges(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.add_edge('A', 'B', 10) graph.add_edge('A', 'C', 20) graph.add_edge('B', 'C', 20) edges = graph.edges assert len(edges) == 3 first = edges[0] assert first.first.identifier == 'A' assert first.second.identifier == 'B' assert first.weight == 10 second = edges[1] assert second.first.identifier == 'A' assert second.second.identifier == 'C' assert second.weight == 20 third = edges[2] assert third.first.identifier == 'B' assert third.second.identifier == 'C' assert third.weight == 20
def test_contains_positive(): graph = Graph() graph.create_vertex_by_id('K') graph.create_vertex_by_id('L') graph.create_vertex_by_id('M') assert True is ('L' in graph)
def test_contains_negative(): graph = Graph() graph.create_vertex_by_id('K') graph.create_vertex_by_id('L') graph.create_vertex_by_id('M') assert False is ('F' in graph)
def test_bfs_positive1(): graph = Graph() graph.create_vertex_by_id('A') distance = bfs(graph, graph.get_vertex_by_id('A')) assert distance['A'] == 0
def test_topological_sort3(sort): graph = Graph() graph.create_vertex_by_id('A') result = sort(graph) assert result[0].identifier == 'A'
def test_add_vertex_second_time(): graph = Graph() graph.create_vertex_by_id('A') with pytest.raises(GraphContainsVertexExemption) as exception_info: graph.create_vertex_by_id('A') assert str(exception_info.value) == graph_contains_vertex_message('A')
def test_dijkstra_negative2(f): graph = Graph() graph.create_vertex_by_id('A') with pytest.raises(NotContainsElementException) as exception_info: f(graph, Vertex('B')) assert str(exception_info.value) == not_contains_vertex_message('B')
def test_get_vertex_by_id_negative1(): graph = Graph() graph.create_vertex_by_id('B') with pytest.raises(NotContainsElementException) as exception_info: vertex = graph.get_vertex_by_id('A') assert str(exception_info.value) == not_contains_vertex_message('A')
def test_get_graph_iterator(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') for vertex in graph: pass
def test_add_vertices(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') assert graph.size == 3 assert len(graph) == 3 assert graph.is_empty == False
def test_get_vertex_by_id_positive(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') length = len(graph) vertex = graph.get_vertex_by_id('A') assert vertex.identifier == 'A' assert length == len(graph)
def test_four_component(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') components = connected_components(graph) assert components['A'] == 1 assert components['B'] == 2 assert components['C'] == 3 assert components['D'] == 4
def test_all_vertex_visited_dfs_recursion1(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.add_edge('A', 'B') graph.add_edge('A', 'C') graph.add_edge('C', 'C') visited = dfs_recursion(graph, pre_visit, post_visit) assert all(value == True for value in visited.values())
def test_one_component(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.add_edge('A', 'B') graph.add_edge('B', 'C') components = connected_components(graph) assert components['A'] == 1 assert components['B'] == 1 assert components['C'] == 1
def test_kruskal3(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.add_edge('A', 'C', 5) graph.add_edge('B', 'C', 2) graph.add_edge('C', 'A', 5) graph.add_edge('C', 'B', 2) edges = kruskal(graph) assert len(edges) == 2 assert find_edge('A', 'C', edges) assert find_edge('B', 'C', edges)
def test_bfs_positive2(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.add_edge('A', 'B') graph.add_edge('A', 'C') distance = bfs(graph, graph.get_vertex_by_id('A')) assert distance['A'] == 0 assert distance['B'] == 1 assert distance['C'] == 1 assert distance['D'] is None
def test_sorted_edges(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.add_edge('A', 'B', 15) graph.add_edge('A', 'C', 10) graph.add_edge('B', 'C', 20) edges = graph.get_sorted_edges_by(lambda edge: edge.weight) first = edges[0] assert first.weight == 10 second = edges[1] assert second.weight == 15 third = edges[2] assert third.weight == 20
def test_dijkstra_positive2(f): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.add_edge('A', 'C', 2) graph.add_edge('A', 'B', 4) distance = f(graph, graph.get_vertex_by_id('A')) distance_to_b = get_distance(graph.get_vertex_by_id('B'), distance) assert distance_to_b == 4 distance_to_c = get_distance(graph.get_vertex_by_id('C'), distance) assert distance_to_c == 2 distance_to_d = get_distance(graph.get_vertex_by_id('D'), distance) assert distance_to_d is None
def test_positive1(f): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.create_vertex_by_id('E') graph.add_edge('A', 'C', 2) graph.add_edge('A', 'B', 4) graph.add_edge('B', 'C', 3) graph.add_edge('B', 'D', 2) graph.add_edge('B', 'E', 3) graph.add_edge('C', 'B', 1) graph.add_edge('C', 'D', 4) graph.add_edge('C', 'E', 5) graph.add_edge('E', 'D', 1) distance = f(graph, graph.get_vertex_by_id('A')) distance_to_c = get_distance(graph.get_vertex_by_id('C'), distance) assert distance_to_c == 2 distance_to_b = get_distance(graph.get_vertex_by_id('B'), distance) assert distance_to_b == 3 distance_to_e = get_distance(graph.get_vertex_by_id('E'), distance) assert distance_to_e == 6 distance_to_d = get_distance(graph.get_vertex_by_id('D'), distance) assert distance_to_d == 5
def test_topological_sort2(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('G') graph.create_vertex_by_id('D') graph.create_vertex_by_id('E') graph.add_edge('A', 'B') graph.add_edge('A', 'G') graph.add_edge('B', 'C') graph.add_edge('C', 'D') graph.add_edge('B', 'D') graph.add_edge('G', 'D') graph.add_edge('D', 'E') result = topological_sort(graph) assert result[0].identifier == 'A' assert result[1].identifier == 'B' assert result[2].identifier == 'G' assert result[3].identifier == 'C' assert result[4].identifier == 'D' assert result[5].identifier == 'E'
def test_all_vertex_visited_dfs_iterative2(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.create_vertex_by_id('E') visited = dfs_iterative(graph) assert all(value == True for value in visited.values())
def test_all_vertex_visited_dfs_iterative1(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.create_vertex_by_id('E') # first component of connectivity graph.add_edge('A', 'B') graph.add_edge('B', 'C') graph.add_edge('C', 'A') # second component of connectivity graph.add_edge('D', 'E') # third component of connectivity graph.create_vertex_by_id('K') visited = dfs_iterative(graph) assert all(value == True for value in visited.values())
def test_kruskal1(): graph = Graph() graph.create_vertex_by_id('A') graph.create_vertex_by_id('B') graph.create_vertex_by_id('C') graph.create_vertex_by_id('D') graph.create_vertex_by_id('E') graph.create_vertex_by_id('F') graph.add_edge('A', 'B', 2) graph.add_edge('A', 'C', 1) graph.add_edge('B', 'A', 2) graph.add_edge('B', 'C', 2) graph.add_edge('B', 'D', 1) graph.add_edge('D', 'C', 2) graph.add_edge('D', 'E', 3) graph.add_edge('D', 'F', 4) graph.add_edge('C', 'D', 2) graph.add_edge('C', 'E', 3) graph.add_edge('C', 'A', 1) graph.add_edge('C', 'B', 2) graph.add_edge('E', 'D', 3) graph.add_edge('E', 'C', 3) graph.add_edge('E', 'F', 2) graph.add_edge('F', 'E', 2) graph.add_edge('F', 'D', 4) edges = kruskal(graph) assert len(edges) == 5 assert find_edge('A', 'B', edges) is not None assert find_edge('A', 'C', edges) is not None assert find_edge('B', 'D', edges) is not None assert find_edge('D', 'E', edges) is not None assert find_edge('E', 'F', edges) is not None