Esempio n. 1
0
 def setUp(self):
     self.g = ListGraph()
Esempio n. 2
0
class ListGraphTest(GraphTest):
    def setUp(self):
        self.g = ListGraph()

    def test_add_vertex_once(self):
        self.g.add_vertex(self.v1)
        self.assertListEqual([self.v1], self.g.vertices)
        self.assertListEqual([[]], self.g.adj_list)

    def test_add_vertex_twice(self):
        self.g.add_vertex(self.v1)
        self.g.add_vertex(self.v2)
        self.assertListEqual([self.v1, self.v2], self.g.vertices)
        self.assertListEqual([[], []], self.g.adj_list)

    def test_delete_vertex(self):
        self.g.add_vertex(self.v1)
        self.g.add_vertex(self.v2)
        self.g.delete_vertex(self.v1)
        self.assertListEqual(self.g.vertices, [self.v2])
        self.assertListEqual(self.g.adj_list, [[]])

    def test_add_edge(self):
        self.g.add_edge(self.e1)
        self.assertListEqual(self.g.vertices, [self.v1, self.v2])
        self.assertListEqual(self.g.adj_list, [[self.e1], []])

    def test_delete_edge(self):
        self.g.add_edge(self.e1)
        self.g.delete_edge(self.e1)
        self.assertListEqual(self.g.vertices, [self.v1, self.v2])
        self.assertListEqual(self.g.adj_list, [[], []])

    def test_delete_edge_one_remaining(self):
        self.g.add_edge(self.e1)
        self.g.add_edge(self.e2)
        self.g.delete_edge(self.e2)
        self.assertListEqual(self.g.vertices, [self.v1, self.v2])
        self.assertListEqual(self.g.adj_list, [[self.e1], []])

    def test_get_neighbors(self):
        self.g.add_edge(self.e1)
        self.g.add_vertex(self.v3)
        self.assertListEqual(self.g.get_neighbors(self.v1), [self.v2])
        self.assertListEqual(self.g.get_neighbors(self.v3), [])

    def test_get_neighbors_exhaustive(self):
        self.g.add_edge(self.e1)
        self.g.add_edge(self.e2)
        self.g.add_edge(self.e3)
        self.g.add_vertex(self.v3)
        self.assertListEqual(self.g.get_neighbors(self.v1), [self.v2])
        self.assertListEqual(self.g.get_neighbors(self.v2), [self.v1])
        self.assertListEqual(self.g.get_neighbors(self.v3), [])
        self.assertListEqual(self.g.get_neighbors(self.v4), [self.v5])
        self.assertListEqual(self.g.get_neighbors(self.v5), [self.v4])

    def test_get_incoming_edges(self):
        self.g.add_edge(self.e1)
        self.g.add_edge(self.e2)
        self.assertListEqual(self.g.get_incoming_edges(self.v2), [self.e1])

    def test_get_outgoing_edges(self):
        self.g.add_edge(self.e1)
        self.g.add_edge(self.e2)
        self.assertListEqual(self.g.get_outgoing_edges(self.v2), [self.e2])

    def test_get_edge(self):
        self.g.add_edge(self.e1)
        self.assertEqual(self.g.get_edge(self.E1_LABEL), self.e1)

    def test_get_vertices_count(self):
        self.assertEqual(self.g.get_vertices_count(), 0)
        self.g.add_vertex(self.v1)
        self.assertEqual(self.g.get_vertices_count(), 1)
        self.g.add_vertex(self.v2)
        self.assertEqual(self.g.get_vertices_count(), 2)

    def test_get_edges_count(self):
        self.assertEqual(self.g.get_edges_count(), 0)
        self.g.add_edge(self.e1)
        self.assertEqual(self.g.get_edges_count(), 1)
        self.g.add_edge(self.e2)
        self.assertEqual(self.g.get_edges_count(), 2)

    def test_is_neighbors(self):
        self.g.add_edge(self.e1)
        self.g.add_vertex(self.v3)
        self.assertTrue(self.g.is_neighbors(self.v1, self.v2))
        self.assertTrue(self.g.is_neighbors(self.v2, self.v1))
        self.assertFalse(self.g.is_neighbors(self.v3, self.v1))
        self.assertFalse(self.g.is_neighbors(self.v3, self.v2))

    def test_acceptance(self):
        read_graph('../../data/graf.txt', self.g)
        control_sum = sum([e.weight for e in self.g.get_edges() if e.vertex_from < e.vertex_to])
        self.assertEqual(control_sum, 4790)