Beispiel #1
0
    def test_neighbors(self):
        e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)}
        g = c.Graph(range(6), e)

        self.assertEqual(g.neighbors([1]), {1: {0, 2, 4}})
        self.assertEqual(g.neighbors([1])[1], g[1])

        self.assertEqual(g.neighbors([2, 4]), {2: {1, 3}, 4: {1, 3}})
Beispiel #2
0
    def test_adjacency_map(self):
        v, e = set(range(1, 4)), {(1, 3), (1, 4), (2, 4), (2, 3)}
        g = c.Graph(v, e)

        m = g.adjacency_map
        self.assertEqual(m, {1: {3, 4}, 2: {3, 4}, 3: {1, 2}, 4: {1, 2}})

        m[1].add(2)
        self.assertFalse(g.has_edge(1, 2), "Adjacency map mutated the graph.")
Beispiel #3
0
    def test_add_edges_nonexisting_vertices(self):
        e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)}
        g = c.Graph(edges=e)

        self.assertEqual(g.vertices, set(range(5)))

        for u, v in e:
            self.assertTrue(g.has_edge(u, v))
            self.assertTrue(g.has_edge(v, u))
Beispiel #4
0
    def test_add_edges(self):
        e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)}
        g = c.Graph(range(6), e)

        for u, v in e:
            self.assertTrue(g.has_edge(u, v))
            self.assertTrue(g.has_edge(v, u), "Didn't add an undirected edge!")

        e = {(3, 1), (5, 3), (5, 4), (4, 2)}
        g.add_edges(e)

        for u, v in e:
            self.assertTrue(g.has_edge(u, v), "Didn't add an undirected edge!")
            self.assertTrue(g.has_edge(v, u))
Beispiel #5
0
    def test_add_edge(self):
        o, p = object(), object()
        g = c.Graph((o, p))

        self.assertEqual(g.size, 0)
        self.assertFalse(g.has_edge(o, p))
        self.assertFalse(g.has_edge(p, o))

        g.add_edge(o, p)
        self.assertTrue(g.has_edge(o, p))
        self.assertTrue(g.has_edge(p, o))

        self.assertEqual(g.size, 1)

        e = g.edges
        self.assertTrue(
            (o, p) in e or (p, o) in e, "Undirected should have "
            "edge in both directions but only appear once in .edges")

        self.assertIn(p, g[o])
Beispiel #6
0
 def test_edges(self):
     e = {(1, 5), (2, 5), (3, 4)}
     g = c.Graph(range(1, 6), e)
     self.assertEqual(g.edges, e)
Beispiel #7
0
    def test_degree(self):
        e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)}
        g = c.Graph(range(6), e)

        for i, v in enumerate((1, 3, 2, 2, 2, 0)):
            self.assertEqual(g.degree(i), v)
Beispiel #8
0
    def test_add_path(self):
        g = c.Graph(range(6), {(1, 4)})

        e = {(0, 1), (1, 2), (2, 3), (3, 4), (4, 5), (1, 4)}
        g.add_path(range(6))
        self.assertEqual(g.edges, e)
Beispiel #9
0
 def test_add_edge_cant_add_loop(self):
     g = c.Graph({1})
     self.assertRaises(exc.EdgeCreatesLoop, g.add_edge, 1, 1)
Beispiel #10
0
 def test_is_simple(self):
     g = c.Graph()
     self.assertFalse(g.is_directed)
     self.assertFalse(g.is_multigraph)