コード例 #1
0
 def test_get_edge_weight(self):
     v_1 = Vertex('A')
     # add a neighbor without weight
     v_2 = Vertex('B')
     v_1.add_neighbor(v_2, 3)
     assert v_2 in v_1.neighbors
     assert len(v_1.get_neighbors()) is 1
     # check the weight
     self.assertEqual(v_1.get_edge_weight(v_2), 3)
コード例 #2
0
    def test_add_neighbor(self):
        vertex_1 = Vertex('A')
        # add a neighbor without weight
        vertex_2 = 'B'
        vertex_1.add_neighbor(vertex_2)
        assert vertex_2 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 1
        assert vertex_1.get_edge_weight(vertex_2) is 0

        # add another vertex with weight
        vertex_3 = 'C'
        vertex_1.add_neighbor(vertex_3, 5)
        assert vertex_3 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 2
        assert vertex_1.get_edge_weight(vertex_3) is 5

        # adding duplicate neighbor
        vertex_1.add_neighbor(vertex_2, 3)
        assert vertex_2 in vertex_1.neighbors
        assert len(vertex_1.get_neighbors()) is 2
        assert vertex_1.get_edge_weight(vertex_2) is 3