def test_add_vertex(self): g = Graph() g.add_vertex('A') g.add_vertex('B') g.add_vertex('C') g.add_vertex('D') assert g.has_vertex('A') is True assert g.has_vertex('B') is True assert g.has_vertex('C') is True assert g.has_vertex('D') is True assert g.has_vertex('E') is False assert g.has_vertex('F') is False assert g.has_vertex('G') is False assert g.has_vertex('H') is False g.add_edge('A', 'B') g.add_edge('C', 'D') self.assertCountEqual(g.get_neighbors('A'), ['B']) self.assertCountEqual(g.get_neighbors('B'), []) self.assertCountEqual(g.get_neighbors('C'), ['D']) self.assertCountEqual(g.get_neighbors('D'), []) g.add_edge('A', 'F') g.add_edge('B', 'E') self.assertCountEqual(g.get_neighbors('A'), ['B', 'F']) self.assertCountEqual(g.get_neighbors('B'), ['E'])
def test_get_neighbors(self): g = Graph() g.add_edge('A', 'B') g.add_edge('C', 'D') g.add_edge('E', 'F') g.add_edge('G', 'H') self.assertCountEqual(g.get_neighbors('A'), ['B']) self.assertCountEqual(g.get_neighbors('B'), []) self.assertCountEqual(g.get_neighbors('C'), ['D']) self.assertCountEqual(g.get_neighbors('D'), []) with self.assertRaises(KeyError): g.get_neighbors('Z')