Example #1
0
class VertexTests(unittest.TestCase):
    def setUp(self):
        self.vertex = Vertex("A")

    def test_init(self):
        assert self.vertex.id is 'A'
        assert not self.vertex.neighbors

    def test_add_neighbor(self):
        # Adding a neighbor
        neighbor_one = "B"
        self.vertex.add_neighbor(neighbor_one)
        assert len(self.vertex.get_neighbors()) is 1
        assert neighbor_one in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_one) is 0

        # Adding another neighbor
        neighbor_two = 'C'
        self.vertex.add_neighbor(neighbor_two, 4)
        assert len(self.vertex.get_neighbors()) is 2
        assert neighbor_two in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_two) is 4

        # Handling duplicate vertex
        self.vertex.add_neighbor(neighbor_one, 8)
        assert len(self.vertex.get_neighbors()) is 2
        assert neighbor_one in self.vertex.neighbors
        assert self.vertex.get_edge_weight(neighbor_one) is 8

    def test_negatives(self):
        # Requesting a weight from a non existing edge
        assert self.vertex.get_edge_weight("B") is None
Example #2
0
    def test_get_neighbors(self):
        ramon = Vertex('Ramon Geronimo')
        jessie = Vertex('Jessie Pichardo')
        joel = Vertex('Joel Pichardo')

        ramon.add_neighbor(jessie)
        ramon.add_neighbor(joel)

        self.assertCountEqual(ramon.get_neighbors(), [jessie, joel])
        self.assertCountEqual(jessie.get_neighbors(), [])
Example #3
0
 def test_get_neighbors(self):
     v1 = Vertex(1)
     v2 = Vertex(2)
     v3 = Vertex(3)
     # Test getting neighbors
     v1.add_neighbor(v2)
     v1.add_neighbor(v3)
     self.assertCountEqual(v1.get_neighbors(), [v2, v3])
     v2.add_neighbor(v1, 2)
     v2.add_neighbor(v3, 2)
     self.assertCountEqual(v2.get_neighbors(), [v1, v3])
     v3.add_neighbor(v1, 3)
     v3.add_neighbor(v2, 3)
     self.assertCountEqual(v3.get_neighbors(), [v1, v2])
 def test_add_edge(self):
     graph = Graph()
     a = Vertex('A')
     b = Vertex('B')
     graph.add_vertex(a)
     graph.add_vertex(b)
     graph.add_edge(a, b)
     a.add_neighbor(b)
     assert b in a.get_neighbors() 
    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

        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

        # 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
Example #6
0
    def test_get_neighbors(self):
        label = "Sugar"
        vertex = Vertex(label)

        vertex.add_neighbor("Kevin", weight = 0)
        vertex.add_neighbor("Chewie", weight = 4)
        vertex.add_neighbor("Maggie", weight = 8)
        vertex.add_neighbor("Ducky", weight = 2)

        # Should tell us Sugar has 4 neighbors
        assert len(vertex.neighbors) == 4
        # Should tell us Sugar's neighbors are Kevin, Chewie, Maggie, Ducky
        assert vertex.get_neighbors() == {"Kevin", "Chewie", "Maggie", "Ducky"}
 def test_get_neighbors(self):
     test_vertex = Vertex(1)
     test_vertex.add_neighbor(2, 1)
     assert 2 in test_vertex.get_neighbors()