def test_has_node(): g = Graph() assert g.has_node('a') is False g.add_node('a') assert g.has_node('a') is True g.add_edge('b', 'c') assert g.has_node('b') is True g.del_edge('b', 'c') assert g.has_node('c') is True
def test_weighted_edges(): g = Graph() g.add_edge('a', 'b') g.add_edge('a', 'd') g.add_edge('a', 'g') g.add_edge('b', 'e') g.add_edge('e', 'g') g.add_edge('b', 'f') g.add_edge('f', 'd') g.add_edge('f', 'c') g.add_edge('c', 'h') assert g.adjacent('a', 'b') is True assert g.adjacent('a', 'c') is False g.del_edge('a', 'b') assert g.neighbors('a') == ['d', 'g']
def test_del_edge(): g = Graph() e1 = Edge('a', 'b') e2 = Edge('b', 'c') e3 = Edge('a', 'c') edges = [e1, e2, e3] g.add_edge('a', 'b') g.add_edge('b', 'c') g.add_edge('a', 'c') for edge in edges: assert edge in g._edges g.del_edge('b', 'c') assert len(g._edges) == 2 assert e2 not in g._edges assert e1 in g._edges and e3 in g._edges try: g.del_edge(1, 2) except IndexError: assert True else: assert False g.del_edge('b', 'a') assert len(g._edges) == 1