def test_add_edge(self): o, p = object(), object() g = c.DiGraph((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.assertFalse(g.has_edge(p, o)) self.assertEqual(g.size, 1) e = g.edges self.assertIn((o, p), e) self.assertNotIn((p, o), e) self.assertIn(p, g[o]) self.assertNotIn(o, g[p]) g.add_edge(p, o) self.assertTrue(g.has_edge(o, p)) self.assertTrue(g.has_edge(p, o)) self.assertEqual(g.size, 2) e = g.edges self.assertIn((o, p), e) self.assertIn((p, o), e) self.assertIn(p, g[o]) self.assertIn(o, g[p])
def test_degree(self): e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)} g = c.DiGraph(range(6), e) for i, v in enumerate((1, 3, 2, 2, 2, 0)): self.assertEqual(g.degree(i), g.in_degree(i) + g.out_degree(i)) self.assertEqual(g.degree(i), v)
def test_neighbors(self): e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)} g = c.DiGraph(range(6), e) self.assertEqual(g.neighbors([1]), {1: {2, 4}}) self.assertEqual(g.neighbors([1])[1], g[1]) self.assertEqual(g.neighbors([2, 4]), {2: {3}, 4: set()})
def test_adjacency_map(self): v, e = set(range(1, 4)), {(1, 3), (1, 4), (2, 4), (3, 2)} g = c.DiGraph(v, e) m = g.adjacency_map self.assertEqual(m, {1: {3, 4}, 2: {4}, 3: {2}, 4: set()}) m[1].add(2) self.assertFalse(g.has_edge(1, 2), "Adjacency map mutated the graph.")
def test_add_edges_nonexisting_vertices(self): e = {(0, 1), (1, 2), (2, 3), (3, 4), (4, 1)} g = c.DiGraph(edges=e) self.assertEqual(g.vertices, set(range(5))) for u, v in e: self.assertTrue(g.has_edge(u, v)) self.assertFalse(g.has_edge(v, u))
def test_add_edges(self): e = {(0, 1), (1, 2), (2, 3), (3, 4), (1, 4)} g = c.DiGraph(range(6), e) for u, v in e: self.assertTrue(g.has_edge(u, v)) self.assertFalse(g.has_edge(v, u)) 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)) self.assertFalse(g.has_edge(v, u))
def test_edges(self): e = {(1, 5), (2, 5), (5, 2), (5, 1), (3, 4)} g = c.DiGraph(range(1, 6), e) self.assertEqual(g.edges, e)
def test_add_path(self): g = c.DiGraph(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)
def test_add_edge_cant_add_loop(self): g = c.DiGraph({1}) self.assertRaises(exc.EdgeCreatesLoop, g.add_edge, 1, 1)
def test_is_simple(self): g = c.DiGraph() self.assertFalse(g.is_directed) self.assertFalse(g.is_multigraph)