コード例 #1
0
 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
コード例 #2
0
 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
コード例 #3
0
 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)
コード例 #4
0
 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() == []
コード例 #5
0
 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()
コード例 #6
0
 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([], [])
コード例 #7
0
 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)
コード例 #8
0
 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
コード例 #9
0
 def test_add_all_edges_empty(self):
     g = Graph()
     g.add_all_edges()
     assert g.vertices() == []
     assert g.edges() == []
コード例 #10
0
 def test_add_regular_edges_odd(self):
     g = Graph([v, w, x, y])
     g.add_regular_edges(1)
コード例 #11
0
 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)
コード例 #12
0
 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])
コード例 #13
0
 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], [])
コード例 #14
0
 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)
コード例 #15
0
 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])
コード例 #16
0
 def test_out_edges_none(self):
     g = Graph()
     assert [] == g.out_edges(v)
コード例 #17
0
 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])
コード例 #18
0
 def test_vertices(self):
     g = Graph([v, w], [vw, ])
     assert sorted(g.vertices()) == sorted([v, w])
コード例 #19
0
 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()
コード例 #20
0
 def test_add_regular_edges_negative(self):
     g = Graph()
     with pytest.raises(ValueError):
         g.add_regular_edges(-1)
コード例 #21
0
 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, ]
コード例 #22
0
 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])