コード例 #1
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
 def test_is_weighted(self) -> None:
     g = Graph()
     g.add_edge(1, 2)
     assert (
         not g.is_weighted()
     ), "graph without custom edge weights should not identify as weighted"
     g.add_edge(3, 4, weight=9.5)
     assert g.is_weighted(
     ), "graph with custom edge weights should identify as weighted"
コード例 #2
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
 def test_has_vertex(self) -> None:
     g = Graph()
     g.add_edge(1, 2)
     assert g.has_vertex(1), "vertex specified as int should be in graph"
     assert g.has_vertex("1"), "vertex specified as str should be in graph"
     v1 = g[1]
     assert g.has_vertex(
         v1), "vertex specified as object should be in graph"
     assert not g.has_vertex(3), "vertex 3 should not be in the graph"
コード例 #3
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
    def test_allows_self_loops(self) -> None:
        g = Graph()
        assert g.allows_self_loops(
        ), "graph should default to allowing self loops"
        g.add_edge(1, 1)  # graph should allow adding self loop

        g2 = Graph(allow_self_loops=False)
        assert not g2.allows_self_loops(
        ), "graph 2 should not allow self loops"
        with pytest.raises(exception.SelfLoopsNotAllowed):
            g2.add_edge(1, 1)
コード例 #4
0
    def test_kruskal_optimum_forest_multiple_trees(self) -> None:
        g = Graph(test_edges)
        g.add_edge("x", "y", weight=22)
        g.add_edge("y", "z", weight=20)
        g.add_vertex("isolated")

        count = 0
        total_weight = 0.0
        for tree in undirected.kruskal_optimum_forest(g):
            count += 1
            total_weight += tree.weight

        assert count == 3, "there should be 3 trees in the spanning forest"
        assert total_weight == 70, "total weight of trees should be 70"
コード例 #5
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
    def test__contains__(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        assert g[1] in g, "vertex 1 should be in graph"
        assert 1 in g, "vertex specified as int should be in graph"
        assert "1" in g, "vertex specified as str should be in graph"
        assert 3 not in g, "vertex 3 should not be in graph"

        assert g.get_edge(1, 2) in g, "edge (1, 2) should be in graph"
        assert (1, 2) in g, "edge specified as tuple should be in graph"
        assert ("1", "2") in g, "edge specified as tuple should be in graph"
        assert (1, 3) not in g, "edge (1, 3) should not be in graph"

        with pytest.raises(TypeError):
            _ = 4.5 not in g  # type: ignore
        with pytest.raises(TypeError):
            _ = (1, 2, 3, 4) not in g  # type: ignore
コード例 #6
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
    def test_add_edge(self) -> None:
        g = Graph()
        edge = g.add_edge(1, 2, weight=4.5, color="blue", mass=42)
        assert isinstance(edge, Edge), "new edge should an Edge object"
        assert g.get_edge(
            1, 2).weight == 4.5, "edge (1, 2) should have weight 4.5"
        assert edge[
            "color"] == "blue", "edge should have 'color' attribute set to 'blue'"
        assert edge[
            "mass"] == 42, "edge should have 'mass' attribute set to 42"

        edge_dup = g.add_edge(1, 2, weight=1.5, color="red", mass=57)
        assert (
            edge is edge_dup
        ), "adding an edge with same vertices as existing edge should return existing edge"

        g.add_edge(3, 4)
        assert (g.get_edge(3, 4).weight == edge_module.DEFAULT_WEIGHT
                ), "edge should have default weight"
        assert not g.get_edge(3, 4).has_attributes_dict(
        ), "edge should not have attributes dictionary"
コード例 #7
0
ファイル: test_graph.py プロジェクト: cpeisert/vertizee
    def test__getitem__(self) -> None:
        g = Graph()
        g.add_edge(1, 2)
        assert isinstance(g[1], Vertex), "graph should have vertex 1"
        assert isinstance(g["1"], Vertex), "graph should have vertex 1"
        assert isinstance(g[(1, {})], Vertex), "graph should have vertex 1"
        assert isinstance(g[1, {}], Vertex), "graph should have vertex 1"
        v1 = g[1]
        assert isinstance(g[v1], Vertex), "graph should have vertex 1"
        with pytest.raises(KeyError):
            _ = g.get_edge(1, 3)
        with pytest.raises(KeyError):
            _ = g[3]

        assert isinstance(g.get_edge(1, 2),
                          Edge), "graph should have edge (1, 2)"
        assert isinstance(g.get_edge("1", "2"),
                          Edge), "graph should have edge (1, 2)"
        _ = g.get_edge(1, 2)
        with pytest.raises(KeyError):
            _ = g.get_edge(1.0, 2.0)  # type: ignore
        with pytest.raises(KeyError):
            _ = g.get_edge(1, 3)