def test_get_nodes(): graph = Graph() shoe = graph.add_node('shoe') boot = graph.add_node('boot') sneaker = graph.add_node('sneaker') expected = 3 actual = len(graph.get_nodes()) assert actual == expected
def test_breadth_first_3_el(): graph = Graph() start = graph.add_node('spam') middle = graph.add_node('bacon') end = graph.add_node('eggs') graph.add_edge(start, end) graph.add_edge(start, middle, 2) assert graph.breadth_first(start) == ['spam', 'eggs', 'bacon']
def test_breadth_first_none(): g = Graph() apple = g.add_vertex('apple') banana = g.add_vertex('banana') orange = g.add_vertex('orange') kiwi = g.add_vertex('kiwi') potato = g.add_vertex('potato') assert g.depth_first(banana) == ['banana']
def test_add_edge_weight(): graph = Graph() start = graph.add_node('start') end = graph.add_node('end') graph.add_edge(start, end, 20) expected = (end, 20) actual = graph._adjancency_list[start][0] assert actual == expected
def graph(): graph = Graph() graph.adjacency_list.append(Node('a')) graph.adjacency_list.append(Node('b')) graph.adjacency_list.append(Node('c')) graph.adjacency_list.append(Node('d')) graph.addEdge('a', 'b', 1) graph.addEdge('a', 'c', 2) graph.addEdge('c', 'd', 3) graph.addEdge('d', 'b', 4) return graph
def test_get_neighbors(): graph = Graph() shoe = graph.add_node('shoe') boot = graph.add_node('boot') graph.add_edge(shoe, boot, 6) neighbors = graph.get_neighbors(shoe) expected = (boot, 6) assert len(neighbors) == 1 assert neighbors[0][0].value == 'boot' assert isinstance(neighbors[0][0], Vertex) assert neighbors[0][1] == 6
def test_breadth_first_one(): g = Graph() apple = g.add_vertex('apple') banana = g.add_vertex('banana') orange = g.add_vertex('orange') kiwi = g.add_vertex('kiwi') potato = g.add_vertex('potato') g.add_edge(apple, banana, 4) g.add_edge(apple, orange, 5) g.add_edge(orange, kiwi, 8) g.add_edge(banana, kiwi, 7) g.add_edge(kiwi, potato, 9) g.add_edge(banana, potato, 11) assert g.depth_first(banana) == [ 'banana', 'potato', 'kiwi', 'orange', 'apple' ]
def test_get_many_neighbors(): graph = Graph() shoe = graph.add_node('shoe') boot = graph.add_node('boot') sandal = graph.add_node('sandal') flipflop = graph.add_node('flipflop') graph.add_edge(shoe, boot, 6) graph.add_edge(shoe, sandal, 5) graph.add_edge(shoe, flipflop, 12) graph.add_edge(boot, sandal, 5) neighbors = graph.get_neighbors(shoe) expected = [boot, 6, flipflop, 12, sandal, 5] assert len(neighbors) == 3 assert neighbors[0][0].value == 'boot' assert neighbors[1][0].value == 'sandal' assert neighbors[2][0].value == 'flipflop' assert isinstance(neighbors[0][0], Vertex) assert neighbors[0][1] == 6 assert neighbors[1][1] == 5 assert neighbors[2][1] == 12
def test_add_node(): graph = Graph() expected = 'shoe' actual = graph.add_node('shoe').value assert actual == expected
def test_add_edge(): graph = Graph() start = graph.add_node('start') end = graph.add_node('end') graph.add_edge(start, end)
def test_size_many(): graph = Graph() graph.add_node('shoe') graph.add_node('boot') graph.add_node('sandal') graph.add_node('slipper') graph.add_node('flipflop') graph.add_node('sneaker') expected = 6 actual = graph.size() assert actual == expected
def test_size_empty(): graph = Graph() expected = 0 actual = graph.size() assert actual == expected
def test_breadth_1_edge(): graph = Graph() first = graph.add_node('spam') second = graph.add_node('bacon') graph.add_edge(first, second) assert graph.breadth_first(first) == ['spam', 'bacon']
def test_breadth_first_few_edges(): graph = Graph() first = graph.add_node('spam') second = graph.add_node('bacon') third = graph.add_node('eggs') fourth = graph.add_node('butter') graph.add_edge(first, second) graph.add_edge(first, third, 2) graph.add_edge(second, third, 3) graph.add_edge(third, fourth, 4) assert graph.breadth_first(first) == ['spam', 'bacon', 'eggs', 'butter']