Exemplo n.º 1
0
    def test_remove_isolated_vertices(self) -> None:
        g = Graph([(1, 2), (2, 3), (5, 5)])
        g.remove_edge(1, 2, remove_semi_isolated_vertices=False)
        g.add_vertex(4)
        assert set(g.vertices()) == {g[1], g[2], g[3], g[4], g[5]}

        g.remove_isolated_vertices(ignore_self_loops=False)
        assert set(g.vertices()) == {
            g[2],
            g[3],
            g[5],
        }, "isolated vertices 1 and 4 should have been removed"

        g.remove_isolated_vertices(ignore_self_loops=True)
        assert set(g.vertices()) == {
            g[2], g[3]
        }, "semi-isolated vertex 5 should have been removed"
Exemplo n.º 2
0
 def test_vertices(self) -> None:
     g = Graph([(1, 2), (2, 3), (3, 4)])
     assert set(g.vertices()) == {
         g[1],
         g[2],
         g[3],
         g[4],
     }, "graph should have vertices 1, 2, 3, 4"
Exemplo n.º 3
0
    def test_deepcopy(self) -> None:
        g = Graph([(1, 2, {"color": "blue"}), (3, 4)])
        g.add_vertex(42)
        g_copy = g.deepcopy()

        assert set(g.vertices()) == set(
            g_copy.vertices()), "graph copy should have same vertices"
        assert (
            g[1] is not g_copy[1]
        ), "graph copy vertex objects should be distinct from original graph"

        assert set(g.edges()) == set(
            g_copy.edges()), "graph copy should have same edges"
        assert g.get_edge(1, 2) is not g_copy.get_edge(
            1, 2
        ), "graph copy edge objects should be distinct from original graph"
        assert (
            g.get_edge(1, 2)._attr == g_copy.get_edge(1, 2)._attr
        ), "graph copy edge object `_attr` dictionary should contain logically equal contents"
        assert (
            g.get_edge(1, 2)._attr is not g_copy.get_edge(1, 2)._attr
        ), "graph copy edge object `_attr` dictionary should be distinct from original graph"