def test_get_edge_bad_input(self): g = Graph() assert g.get_edge(None, None) is None assert g.get_edge(None, v) is None assert g.get_edge(v, None) is None assert g.get_edge(v, 'fs') is None assert g.get_edge(v, 3) is None
def test_bfs_visit(self): def visit(node): visited.add(node) visited = set() g = Graph([v, w, x, y], [vw, vx, wx]) g.bfs(v, visit) assert set([v, w, x]) == visited
def test_add_regular_edges_invalid(self): g = Graph([v, w, x]) # degree > vertexcount-1 with pytest.raises(GraphException): g.add_regular_edges(3) # degree * vertexcount not even with pytest.raises(GraphException): g.add_regular_edges(1)
def test_add_regular_edges_0(self): g = Graph() g.add_regular_edges(0) assert g.vertices() == [] assert g.edges() == [] g = Graph([v, w, x]) g.add_regular_edges(0) assert sorted(g.vertices()) == sorted([v, x, w]) assert g.edges() == []
def test_size(self): assert 0 == Graph().size() assert 1 == Graph([v, w], [vw, ]).size() print('hi') for n in range(8): print(n) g = Graph(make_vertices(n)) g.add_all_edges() assert (n * (n - 1)) // 2 == g.size()
def test_del_vertex(self): g = Graph([v, w, x], [vw, vx, wx]) g.del_vertex(x) assert g == Graph([v, w], [vw]) g.del_vertex(v) assert g == Graph([w, ], []) g.del_vertex(w) assert g == Graph([], [])
def test_get_edge_false(self): g = Graph() assert None is g.get_edge(v, v) assert None is g.get_edge(w, w) assert None is g.get_edge(v, w) assert None is g.get_edge(w, v) g = Graph([v, w]) assert None is g.get_edge(v, v) assert None is g.get_edge(w, w) assert None is g.get_edge(v, w) assert None is g.get_edge(w, v) g = Graph([v, w, x], [vx, ]) assert None is g.get_edge(v, v) assert None is g.get_edge(w, w) assert None is g.get_edge(v, w) assert None is g.get_edge(w, v)
def test_add_all_edges(self): g = Graph([v, x, w]) g.add_all_edges() assert sorted(g.vertices()) == sorted([v, x, w]) assert len(g.edges()) == 3
def test_add_all_edges_empty(self): g = Graph() g.add_all_edges() assert g.vertices() == [] assert g.edges() == []
def test_add_regular_edges_odd(self): g = Graph([v, w, x, y]) g.add_regular_edges(1)
def test_bfs(self): g = Graph([v, w, x, y], [vw, vx, wx]) assert set([v, w, x]) == g.bfs(v) assert set([v, w, x]) == g.bfs(w) assert set([y, ]) == g.bfs(y)
def test_remove_edge_notthere(self): g = Graph([v, w, x], [vw, vx]) g.del_edge(wx) assert g == Graph([v, w, x], [vw, vx])
def test_del_edge(self): g = Graph([v, w, x], [vw, vx]) g.del_edge(vx) assert g == Graph([v, w, x], [vw]) g.del_edge(wv) # backwards vw assert g == Graph([v, w, x], [])
def test_get_edge_true(self): g = Graph([v, w], [vw, ]) assert vw == g.get_edge(v, w) assert vw == g.get_edge(w, v)
def test_add_regular_edges_even(self): g = Graph([v, w, x]) g.add_regular_edges(2) assert set(g.edges()) == set([vw, vx, wx])
def test_out_edges_none(self): g = Graph() assert [] == g.out_edges(v)
def test_edges(self): g = Graph([v, w], [vw, ]) assert g.edges() == [vw, ] g = Graph([v, w, x], [vw, vx]) assert set(g.edges()) == set([vx, vw])
def test_vertices(self): g = Graph([v, w], [vw, ]) assert sorted(g.vertices()) == sorted([v, w])
def test_order(self): assert 0 == Graph().order() assert 1 == Graph([v, ]).order() assert 4 == Graph([v, w, x, y]).order() g = Graph(make_vertices(10)) assert 10 == g.order()
def test_add_regular_edges_negative(self): g = Graph() with pytest.raises(ValueError): g.add_regular_edges(-1)
def test_out_edges(self): g = Graph([v, w, x], [vw, vx]) assert sorted(g.out_edges(v)) == sorted([vw, vx]) assert g.out_edges(w) == [vw, ] assert g.out_edges(x) == [vx, ]
def test_del_vertex_notthere(self): g = Graph([v, w, x], [vw, vx, wx]) g.del_vertex(y) assert g == Graph([v, w, x], [vw, vx, wx])