def test_df_two_nodes(): from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b') assert graph.depth_first('a') == ['a', 'b']
def test_adjacent_false(): """Test if returns false.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') assert graph.adjacent('egg', 'chicken') is False
def test_nodes(node, result): """Test to check if all nodes are there.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) assert g.nodes() == result
def test_delete_edge_key_error(): """Test if KeyError exception is raised.""" from simple_graph import Graph graph = Graph() graph.add_node('egg') with pytest.raises(KeyError): graph.delete_edge('dog', 'egg')
def test_adjacent(key, value): from simple_graph import Graph new_graph = Graph() node1 = new_graph.add_node(key) node2 = new_graph.add_node(value) new_graph.add_edge(node1, node2) assert new_graph.adjacent(node1, node2) is True
def test_has_node(node, n, result): """Test to check if a node exists in graph.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) assert g.has_node(n) == result
def test_add_edge(): test = Graph() test.add_node(5) test.add_node(42) test.add_edge(5, 42) assert 42 in test.graph_dict[5]
def test_del_nodes(node, result): """Test to check the deleted nodes aren't there.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) assert g.del_node(1) == result
def test_adjacent_true(): """Test if node returns true.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert graph.adjacent('egg', 'chicken') is True
def test_neighbors(): """Return the list of all nodes connect to node.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert graph.get_neighbors('egg') == {'chicken': 0}
def test_delete_node(): """Delete selected node.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.delete_node('chicken') assert 'chicken' not in graph.container
def test_add_edge_destination_node_not_exist(): """Test if a node is added if it is the destination and does not exist.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_edge('chicken', 'egg') assert 'egg' in graph.container assert 'egg' in graph.container['chicken']
def test_add_edge(): """Test if there is an edge between two nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') assert 'chicken' in graph.container['egg']
def test_bf_two_nodes(): """Test breadth first with two nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_edge('a', 'b') assert graph.breadth_first('a') == ['a', 'b']
def populated(): g = Graph() g.add_node(5) g.add_node(7) g.add_node("Hello") g.add_edge(5, 7, 30) g.add_edge(5, "Hello", 10) return g
def test_delete_edge_in_graph(): """Test if an edge between two nodes is removed.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') graph.add_node('egg') graph.add_edge('egg', 'chicken') graph.delete_edge('egg', 'chicken') assert graph.container['egg'] == {}
def test_edge(node, result): """Test to check if all edges are there.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) g.add_edge(2, 3) g.add_edge(1, 4) assert g.edges() == result
def test_neighbor(node, n, result): """Test to check that the correct node have the right edge.""" from simple_graph import Graph g = Graph() for idx in node: g.add_node(idx) g.add_edge(1, 1) g.add_edge(1, 4) g.add_edge(4, 2) g.add_edge(3, 5) assert g.neighbors(n) == result
def test_bf_three_nodes(): """Test breadth first with three nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_edge('a', 'b') graph.add_edge('a', 'c') print(graph.container) assert graph.breadth_first('a') == ['a', 'b', 'c']
def test_depth_multiple_edges(): test = Graph() for i in range(5): test.add_node(i) test.add_edge(0, 1) test.add_edge(0, 2) test.add_edge(1, 2) test.add_edge(1, 3) test.add_edge(2, 3) test.add_edge(3, 4) assert test.depth_first_traversal(0) == [0, 1, 2, 3, 4]
def test_shortest_path_Bellman(): """Return the shortest path.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_edge('a', 'b', 5) graph.add_edge('a', 'c', 10) graph.add_edge('b', 'd', 8) graph.add_edge('c', 'd', 4) assert graph.shortest_path('a') == ['a', 'b', 'd']
def test_df_three_nodes_complex(): """Test depth first with even more nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_edge('a', 'c') graph.add_edge('a', 'd') graph.add_edge('b', 'c') graph.add_edge('b', 'a') graph.add_edge('d', 'b') assert graph.depth_first('a') == ['a', 'c', 'd', 'b']
def test_breadth_traversal_graph(): test_graph = Graph() for i in range(1, 10): test_graph.add_node(i) test_graph.add_edge(1, 2) test_graph.add_edge(1, 3) test_graph.add_edge(1, 4) test_graph.add_edge(2, 5) test_graph.add_edge(2, 6) test_graph.add_edge(4, 7) test_graph.add_edge(4, 8) test_graph.add_edge(5, 9) return test_graph
def test_depth_traversal_graph(): test_graph = Graph() for i in range(10): test_graph.add_node(i) test_graph.add_edge(0, 1) test_graph.add_edge(0, 2) test_graph.add_edge(0, 3) test_graph.add_edge(1, 4) test_graph.add_edge(1, 5) test_graph.add_edge(1, 8) test_graph.add_edge(5, 6) test_graph.add_edge(6, 7) test_graph.add_edge(2, 9) return test_graph
def full_graph(): g = Graph() g.add_node(3) g.add_node(10) g.add_node(15) g.add_node(7) g.add_node(14) g.add_edge(3, 7, 1) g.add_edge(15, 10, 2) g.add_edge(14, 3, 3) g.add_edge(3, 10, 4) g.add_edge(7, 15, 5) return g
def cyclic_graph(): g = Graph() g.add_node(3) g.add_node(10) g.add_node(5) g.add_node(2) g.add_node(7) g.add_edge(3, 10, 1) g.add_edge(3, 5, 2) g.add_edge(10, 5, 3) g.add_edge(5, 2, 4) g.add_edge(2, 7, 5) g.add_edge(5, 3, 5) return g
def cyclic(): g = Graph() g.add_node(1) g.add_node(2) g.add_node(3) g.add_node(4) g.add_node(5) g.add_node(6) g.add_node(7) g.add_node(8) g.add_node(9) g.add_edge(1, 2, 1) g.add_edge(1, 3, 3) g.add_edge(1, 6, 3) g.add_edge(5, 1, 10) g.add_edge(2, 8, 1000) g.add_edge(2, 9, 1) g.add_edge(2, 4, 4) g.add_edge(2, 3, 50) g.add_edge(3, 7, 4) g.add_edge(7, 1, 5) g.add_edge(7, 6, 10) return g
def test_graph(): test_graph = Graph() test_graph.add_node(5) test_graph.add_node(42) test_graph.add_node("test") test_graph.add_edge(5, 42) test_graph.add_edge(42, "test", 1000) test_graph.add_edge("test", 5) return test_graph
def test_df_three_nodes(): """Test if depth first with four nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('d') graph.add_edge('a', 'b') graph.add_node('c') graph.add_edge('b', 'c') graph.add_edge('b', 'd') assert graph.depth_first('a') == ['a', 'b', 'c', 'd']
def test_bf_five_nodes(): """Test breadth first with 5 nodes.""" from simple_graph import Graph graph = Graph() graph.add_node('a') graph.add_node('b') graph.add_node('c') graph.add_node('d') graph.add_node('e') graph.add_edge('a', 'b') graph.add_edge('a', 'c') graph.add_edge('a', 'd') graph.add_edge('b', 'e') assert graph.breadth_first('a') == ['a', 'b', 'c', 'd', 'e']
def test_add_edge(): g = Graph() g.add_node(5) g.add_node(10) g.add_edge(5, 10, 6) assert g.gdict[5] == {10: 6}
def test_add_node(): g = Graph() g.add_node(5) assert g.gdict == {5: {}}
def test_add_node(): g = Graph() g.add_node(70) assert 70 in g.graph
def test_depth_first_no_edges(): test = Graph() test.add_node(55) test.add_node("test") test.add_node(2) assert test.depth_first_traversal(55) == [55]
def test_in_graph(): """Test if Node exists in Graph.""" from simple_graph import Graph graph = Graph() graph.add_node('monkey') assert graph.has_node('monkey') is True
def test_add_edge_second_node_new(): test = Graph() test.add_node(55) test.add_edge(55, "test") assert "test" in test.graph_dict assert "test" in test.graph_dict[55]
def test_add_node(): """Test if node added to graph.""" from simple_graph import Graph graph = Graph() graph.add_node('chicken') assert 'chicken' in graph.container