Пример #1
0
    def test_get_edges(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertEqual(graph.numVertices, 3)
        self.assertEqual(graph.numEdges, 0)

        # Create edges
        edges = [
            ("a", "b", 10),
            ("b", "c", 10),
            ("c", "a", 4)
        ]

        # Iterate through edges
        for edge in edges:
            fromVert, toVert, weight = edge
            graph.add_edge(fromVert, toVert, weight)

        self.assertEqual(graph.numEdges, 3)
Пример #2
0
    def test_add_neighbor(self):
        v1 = "a"
        vertex = Vertex(v1)

        vertex.add_neighbor("b", 0)
        self.assertTrue(any(vertex.neighbors))
        self.assertDictEqual(vertex.neighbors, {"b":0})
Пример #3
0
    def text_get_verticies(self):
        graph = Graph()

        # Create test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add verticies
        graph.add_vertex(v1)
        graph.add_vertex(v2)
        graph.add_vertex(v3)

        self.assertListEqual(graph.get_vertices, ["a","b","c"])
Пример #4
0
    def test_init(self):
        # Tests the initialization of the Vertex class 
        v1 = "a"
        vertex = Vertex(v1)

        self.assertEqual(vertex.id, "a")
        self.assertDictEqual(vertex.neighbors, {})
Пример #5
0
    def test_add_vertex(self):
        graph = Graph()

        # Creates test Verticies
        v1,v2,v3 = Vertex("a"), Vertex("b"), Vertex("c")

        # Add vertex "a"
        graph.add_vertex(v1)
        self.assertEqual(graph.numVertices, 1)
        self.assertEqual(graph.numEdges, 0)

        # Add vertex "b"
        graph.add_vertex(v2)
        self.assertEqual(graph.numVertices, 2)
        self.assertEqual(graph.numEdges, 0)

        # Add vertex "c"
        graph.add_vertex(v3)
        self.assertEqual(graph.numVertices, 3)
        self.assertEqual(graph.numEdges, 0)
Пример #6
0
 def setUp(self):
     self.vertex = Vertex("A")
Пример #7
0
class VertexTests(unittest.TestCase):
    def setUp(self):
        self.vertex = Vertex("A")

    def test_init(self):
        assert self.vertex.data 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