Esempio n. 1
0
    def test_degree(self) -> None:
        g = Graph()
        v0 = g.add_vertex(0)
        assert v0.degree == 0, "vertex 0 should have degree 0"

        g.add_edge(1, 1)
        assert g[1].degree == 2, "vertex 1 with self loop should have degree 2"

        g.add_edge(1, 2)
        assert g[1].degree == 3, "vertex 1 should have degree 3"
Esempio n. 2
0
    def test_incident_edges_and_loop_edge(self) -> None:
        g = Graph()
        g.add_vertex(0)
        assert not g[0].loop_edge, "vertex 0 should not have a loop edge"

        g.add_edge(1, 1)
        assert g[1].incident_edges() == {g.get_edge(
            1, 1)}, "vertex 1 should have self loop as incident edge"
        assert g[1].loop_edge, "vertex 1 should have a self loop"

        g.add_edge(1, 2)
        assert len(g[1].incident_edges()
                   ) == 2, "vertex 1 should have two incident edges"
Esempio n. 3
0
 def test_repr_str_and_label(self) -> None:
     g = Graph([(2, 1)])
     assert g.get_edge(2,
                       1).label == "(1, 2)", "edge label should be (1, 2)"
     assert (g.get_edge(2, 1).__str__() == g.get_edge(
         2, 1).__repr__()), "edge __repr__ should equal __str__"
     assert g.get_edge(
         2, 1).__str__() == "(1, 2)", "edge __str__ should be (1, 2)"
     g.add_edge(3, 4, weight=1.5)
     assert g.get_edge(2,
                       1).label == "(1, 2)", "edge label should be (1, 2)"
     assert (
         g.get_edge(2, 1).__str__() == "(1, 2, 1.0)"
     ), "edge __str__ should be (1, 2, 1.0) after adding weighted edge to graph"
Esempio n. 4
0
    def test_adj_vertices(self) -> None:
        g = Graph()
        g.add_vertex(1)
        assert not g[1].adj_vertices(
        ), "vertex 1 should have no adjacent vertices"

        g.add_edge(1, 2)
        assert next(iter(
            g[1].adj_vertices())) == 2, "vertex 1 should adjacent to vertex 2"

        g.add_edge(1, 3)
        assert len(g[1].adj_vertices()
                   ) == 2, "vertex 1 should be adjacent to vertices 2 and 3"
        assert next(iter(g[3].adj_vertices())
                    ) == g[1], "vertex 3 should be adjacent to vertex 1"

        g.add_edge(2, 4)
        assert g[4] not in g[1].adj_vertices(
        ), "vertex 1 should not be adjacent to vertex 4"
        assert g[2] not in g[2].adj_vertices(
        ), "vertex 2 should not be adjacent to itself"

        g.add_edge(4, 4)
        assert g[4] in g[4].adj_vertices(
        ), "vertex 4 should be adjacent to itself"
Esempio n. 5
0
    def test_weight(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        edge_data1 = pp_module.EdgeData.from_edge_obj(g.get_edge(1, 2))
        assert edge_data1.weight == edge_module.DEFAULT_WEIGHT, "weight should be default"

        g.add_edge(3, 4, 9.5)
        edge_data2 = pp_module.EdgeData.from_edge_obj(g.get_edge(3, 4))
        assert edge_data2.weight == 9.5, "weight should be 9.5"

        v5 = pp_module.VertexData("5")
        v6 = pp_module.VertexData("6")
        edge_data3 = pp_module.EdgeData(v5, v6)
        assert edge_data3.weight == edge_module.DEFAULT_WEIGHT, "weight should be default"
Esempio n. 6
0
    def test_attr(self) -> None:
        v1 = pp_module.VertexData("1")
        v2 = pp_module.VertexData("2")
        edge_data = pp_module.EdgeData(v1, v2)
        assert not edge_data.attr, "there should be no attributes by default"
        assert not edge_data.edge_object, "there should be no edge object reference"
        edge_data.attr["color"] = "blue"
        assert edge_data.attr[
            "color"] == "blue", "should have 'color' attribute set to 'blue'"

        g = Graph()
        g.add_edge(1, 2)
        edge_data2 = pp_module.EdgeData.from_edge_obj(g.get_edge(1, 2))
        assert not edge_data2.attr, "there should be no attributes from edge (1, 2)"
        edge_data2.attr["color"] = "blue"
        assert edge_data2.attr[
            "color"] == "blue", "should have 'color' attribute set to 'blue'"
Esempio n. 7
0
    def test_is_isolated(self) -> None:
        g = Graph()
        v1 = g.add_vertex(1)
        assert v1.is_isolated(), "v1 should be isolated"

        g.add_edge(v1, v1)
        assert v1.loop_edge, "v1 should have a self loop"
        assert not v1.is_isolated(
        ), "vertex with self-loop should not be considered isolated"
        assert v1.is_isolated(
            ignore_self_loops=True
        ), "vertex with self-loop should be considered semi-isolated"

        g.add_edge(v1, 2)
        assert not v1.is_isolated(
        ), "vertex connected to a different vertex should not be isolated"

        v1.remove_incident_edges()
        assert v1.is_isolated(
        ), "vertex should be isolated after removing incident edges"
Esempio n. 8
0
    def test_remove_and_edge_removal(self) -> None:
        g = Graph()
        v1 = g.add_vertex("a")
        assert g.vertex_count == 1, "graph should have one vertex"
        v1.remove()
        assert g.vertex_count == 0, "graph should have zero vertices after vertex self removal"
        v2 = g.add_vertex("b")
        g.add_edge("b", "c")
        g.add_edge("b", "b")

        with pytest.raises(VertizeeException):
            v2.remove()

        assert len(
            v2.incident_edges()) == 2, "v2 should have two incident edges"
        v2.remove_loops()
        assert v2.loop_edge is None, "v2 should not have a loop edge after removal"
        assert len(v2.incident_edges()
                   ) == 1, "v2 should have one incident after removing loop"
        v2.remove_incident_edges()
        assert len(v2.incident_edges(
        )) == 0, "v2 should not have any incident edges after removal"
Esempio n. 9
0
    def test_is_vertex_type(self) -> None:
        g = Graph()
        v: Vertex = g.add_vertex(1)
        assert vertex_module.is_vertex_type(
            v), "Vertex object should be a VertexType"

        g2 = DiGraph()
        di_v: DiVertex = g2.add_vertex(1)
        assert vertex_module.is_vertex_type(
            di_v), "DiVertex object should be a VertexType"

        g3 = MultiGraph()
        multi_v: MultiVertex = g3.add_vertex(1)
        assert vertex_module.is_vertex_type(
            multi_v), "MultiVertex object should be a VertexType"

        g4 = MultiDiGraph()
        multi_di_v: MultiDiVertex = g4.add_vertex(1)
        assert vertex_module.is_vertex_type(
            multi_di_v), "MultiDiVertex should be a VertexType"

        assert vertex_module.is_vertex_type(
            10), "int vertex label should be a VertexType"
        assert vertex_module.is_vertex_type(
            "s"), "str vertex label should be a VertexType"
        assert vertex_module.is_vertex_type(("s", {
            "color": "blue"
        })), "vertex tuple should be a VertexType"
        assert not vertex_module.is_vertex_type(
            10.99), "float should not be a VertexType"
        assert not vertex_module.is_vertex_type(
            ("s", "t")), "edge tuple should not be a VertexType"
        assert not vertex_module.is_vertex_type(
            ("s", "t",
             4.5)), "edge tuple with edge weight should not be a VertexType"
        g.add_edge("s", "t")
        assert not vertex_module.is_vertex_type(g.get_edge(
            "s", "t")), "edge object should not be a VertexType"
Esempio n. 10
0
 def test_issubclass_and_isinstance(self) -> None:
     g = Graph()
     edge: Edge = g.add_edge(1, 2)
     assert isinstance(
         edge, edge_module.EdgeBase
     ), "edge should be an instance of superclass EdgeBase"
     assert isinstance(
         edge, edge_module.MutableEdgeBase
     ), "edge should be an instance of superclass MutableEdgeBase"
     assert isinstance(edge, Edge), "edge should be an Edge instance"
     assert issubclass(
         Edge, edge_module.EdgeBase), "Edge should be EdgeBase subclass"
     assert issubclass(Edge, edge_module.MutableEdgeBase
                       ), "Edge should be MutableEdgeBase subclass"
Esempio n. 11
0
    def test_parse_edge_type(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        edge_data1 = pp_module.parse_edge_type(g.get_edge(1, 2))
        assert edge_data1.vertex1.label == "1"
        assert edge_data1.vertex2.label == "2"

        mg = MultiGraph([(3, 4), (3, 4)])
        edge_data2 = pp_module.parse_edge_type(mg.get_edge(3, 4))
        assert edge_data2.vertex1.label == "3"
        assert edge_data2.vertex2.label == "4"

        edge_tuple = (1, 2)
        edge_data3 = pp_module.parse_edge_type(edge_tuple)
        assert edge_data3.vertex1.label == "1"
        assert edge_data3.vertex2.label == "2"

        edge_tuple_weighted = (3, 4, 3.5)
        edge_data4 = pp_module.parse_edge_type(edge_tuple_weighted)
        assert edge_data4.vertex1.label == "3"
        assert edge_data4.vertex2.label == "4"
        assert edge_data4.weight == 3.5

        edge_tuple_attr = (4, 5, {"color": "blue"})
        edge_data5 = pp_module.parse_edge_type(edge_tuple_attr)
        assert edge_data5.vertex1.label == "4"
        assert edge_data5.vertex2.label == "5"
        assert edge_data5.weight == edge_module.DEFAULT_WEIGHT
        assert edge_data5.attr["color"] == "blue"

        edge_tuple_weighted_attr = (6, 7, 9.5, {"k": "v"})
        edge_data6 = pp_module.parse_edge_type(edge_tuple_weighted_attr)
        assert edge_data6.vertex1.label == "6"
        assert edge_data6.vertex2.label == "7"
        assert edge_data6.weight == 9.5
        assert edge_data6.attr["k"] == "v"
Esempio n. 12
0
 def test_vertex1_vertex2(self) -> None:
     g = Graph()
     g.add_edge(2, 1)
     assert g.get_edge(1, 2).vertex1 == 2, "vertex1 should be 2"
     assert g.get_edge(1, 2).vertex2 == 1, "vertex2 should be 1"