Exemplo n.º 1
0
    def test_nodes(self):
        """Test addition/deletion of nodes."""
        g = utility.Graph()
        g.add_node(1)
        g.add_node(2)
        g.add_node(3)
        g.add_node(1)
        self.assertTrue(g.node_in(1))
        self.assertTrue(g.node_in(2))
        self.assertTrue(g.node_in(3))
        self.assertEqual(len(g), 3)

        g.delete_node(1)
        self.assertFalse(g.node_in(1))
        self.assertTrue(g.node_in(2))
        self.assertTrue(g.node_in(3))
        self.assertEqual(len(g), 2)

        g.delete_node(1)
        self.assertFalse(g.node_in(1))
        self.assertTrue(g.node_in(2))
        self.assertTrue(g.node_in(3))
        self.assertEqual(len(g), 2)

        g.add_node(1)
        self.assertTrue(g.node_in(1))
        self.assertTrue(g.node_in(2))
        self.assertTrue(g.node_in(3))
        self.assertEqual(len(g), 3)

        g.delete_node(2)
        self.assertTrue(g.node_in(1))
        self.assertFalse(g.node_in(2))
        self.assertTrue(g.node_in(3))
        self.assertEqual(len(g), 2)
Exemplo n.º 2
0
    def test_ior(self):
        g1 = utility.Graph()
        g1.add_edge(1, 2)
        g1.add_edge(2, 3)

        g2 = utility.Graph()
        g2.add_edge(2, 3)
        g2.add_edge(3, 4)

        g1 |= g2
        self.assertTrue(g1.edge_in(1, 2))
        self.assertTrue(g1.edge_in(2, 3))
        self.assertTrue(g1.edge_in(3, 4))
        self.assertFalse(g2.edge_in(1, 2))
        self.assertTrue(g2.edge_in(2, 3))
        self.assertTrue(g2.edge_in(3, 4))
Exemplo n.º 3
0
    def test_or(self):
        g1 = utility.Graph()
        g1.add_edge(1, 2)
        g1.add_edge(2, 3)

        g2 = utility.Graph()
        g2.add_edge(2, 3)
        g2.add_edge(3, 4)

        g = g1 | g2
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(2, 3))
        self.assertTrue(g.edge_in(3, 4))
        self.assertTrue(isinstance(g, utility.Graph))
        self.assertTrue(g1.edge_in(1, 2))
        self.assertTrue(g1.edge_in(2, 3))
        self.assertFalse(g1.edge_in(3, 4))
        self.assertFalse(g2.edge_in(1, 2))
        self.assertTrue(g2.edge_in(2, 3))
        self.assertTrue(g2.edge_in(3, 4))
Exemplo n.º 4
0
    def test_cycle(self):
        g1 = utility.Graph()
        g1.add_edge(1, 2)
        self.assertFalse(g1.has_cycle())
        g1.add_edge(2, 3)
        self.assertFalse(g1.has_cycle())
        g1.add_edge(2, 4)
        self.assertFalse(g1.has_cycle())
        g1.add_edge(4, 1)
        self.assertTrue(g1.has_cycle())
        g1.delete_edge(2, 3)
        self.assertTrue(g1.has_cycle())
        g1.delete_edge(2, 4)
        self.assertFalse(g1.has_cycle())

        actual_cycle_as_set = frozenset(utility.Cycle(['p', 'q', 't', 'p']))
        expected_cycle_as_set = frozenset([('p', 'q'), ('q', 't'), ('t', 'p')])
        self.assertEqual(actual_cycle_as_set, expected_cycle_as_set)

        g = utility.Graph()
        g.add_edge('p', 'q')
        g.add_edge('p', 'r')
        g.add_edge('q', 't')
        g.add_edge('q', 's')
        g.add_edge('t', 't')
        g.add_edge('t', 'p')
        g.add_edge('t', 'q')
        self.assertTrue(g.has_cycle())
        self.assertEqual(len(g.cycles()), 3)
        expected_cycle_set = set([
            utility.Cycle(['p', 'q', 't', 'p']),
            utility.Cycle(['q', 't', 'q']),
            utility.Cycle(['t', 't'])
        ])
        actual_cycle_set = set([
            utility.Cycle(g.cycles()[0]),
            utility.Cycle(g.cycles()[1]),
            utility.Cycle(g.cycles()[2])
        ])
        self.assertEqual(expected_cycle_set, actual_cycle_set)
Exemplo n.º 5
0
 def test_cycle(self):
     g1 = utility.Graph()
     g1.add_edge(1, 2)
     self.assertFalse(g1.has_cycle())
     g1.add_edge(2, 3)
     self.assertFalse(g1.has_cycle())
     g1.add_edge(2, 4)
     self.assertFalse(g1.has_cycle())
     g1.add_edge(4, 1)
     self.assertTrue(g1.has_cycle())
     g1.delete_edge(2, 3)
     self.assertTrue(g1.has_cycle())
     g1.delete_edge(2, 4)
     self.assertFalse(g1.has_cycle())
Exemplo n.º 6
0
 def test_dependencies(self):
     g1 = utility.Graph()
     self.assertIsNone(g1.dependencies(1))
     g1.add_edge(0, 1)
     g1.add_edge(1, 2)
     g1.add_edge(2, 3)
     g1.add_edge(2, 4)
     g1.add_edge(3, 5)
     g1.add_edge(0, 6)
     g1.add_edge(7, 8)
     g1.add_edge(8, 9)
     g1.add_edge(10, 5)
     g1.add_edge(11, 12)
     self.assertTrue(g1.dependencies(1), set([1, 2, 3, 4, 5]))
     self.assertTrue(g1.dependencies(10), set([10, 5]))
     self.assertTrue(g1.dependencies(5), set([5]))
     self.assertTrue(g1.dependencies(11), set([11, 12]))
Exemplo n.º 7
0
 def test_cyclic_dependencies(self):
     g1 = utility.Graph()
     self.assertIsNone(g1.dependencies(1))
     g1.add_edge(0, 1)
     g1.add_edge(1, 2)
     g1.add_edge(2, 3)
     g1.add_edge(2, 4)
     g1.add_edge(3, 5)
     g1.add_edge(0, 6)
     g1.add_edge(7, 8)
     g1.add_edge(8, 9)
     g1.add_edge(10, 5)
     g1.add_edge(11, 12)
     g1.add_edge(5, 2)  # create cycle
     self.assertEqual(g1.dependencies(1), set([1, 2, 3, 4, 5]))
     self.assertEqual(g1.dependencies(10), set([10, 5, 2, 3, 4]))
     self.assertEqual(g1.dependencies(5), set([5, 2, 3, 4]))
     self.assertEqual(g1.dependencies(11), set([11, 12]))
Exemplo n.º 8
0
 def test_find_reachable_nodes(self):
     g1 = utility.Graph()
     self.assertEqual(g1.find_reachable_nodes([1]), set())
     g1.add_edge(0, 1)
     g1.add_edge(1, 2)
     g1.add_edge(2, 3)
     g1.add_edge(2, 4)
     g1.add_edge(3, 5)
     g1.add_edge(0, 6)
     g1.add_edge(7, 8)
     g1.add_edge(8, 9)
     g1.add_edge(10, 5)
     g1.add_edge(11, 12)
     self.assertEqual(g1.find_reachable_nodes([]), set())
     self.assertEqual(g1.find_reachable_nodes([1]), set([1, 2, 3, 4, 5]))
     self.assertEqual(g1.find_reachable_nodes([10]), set([10, 5]))
     self.assertEqual(g1.find_reachable_nodes([1, 10]),
                      set([1, 2, 3, 4, 5, 10]))
     self.assertEqual(g1.find_reachable_nodes([5]), set([5]))
     self.assertEqual(g1.find_reachable_nodes([11]), set([11, 12]))
     g1.add_edge(5, 2)
     self.assertEqual(g1.find_reachable_nodes([10]), set([10, 5, 2, 3, 4]))
Exemplo n.º 9
0
 def test_find_dependent_nodes(self):
     g1 = utility.Graph()
     self.assertEqual(g1.find_dependent_nodes([1]), set([1]))
     g1.add_edge(0, 1)
     g1.add_edge(1, 2)
     g1.add_edge(2, 3)
     g1.add_edge(2, 4)
     g1.add_edge(3, 5)
     g1.add_edge(0, 6)
     g1.add_edge(7, 8)
     g1.add_edge(8, 9)
     g1.add_edge(10, 5)
     g1.add_edge(11, 12)
     self.assertEqual(g1.find_dependent_nodes([0]), set([0]))
     self.assertEqual(g1.find_dependent_nodes([2]), set([2, 1, 0]))
     self.assertEqual(g1.find_dependent_nodes([5]), set([5, 0, 1, 2, 3,
                                                         10]))
     self.assertEqual(g1.find_dependent_nodes([12]), set([11, 12]))
     self.assertEqual(g1.find_dependent_nodes([5, 6]),
                      set([5, 0, 1, 2, 3, 10, 6]))
     g1.add_edge(5, 2)  # add cycle
     self.assertEqual(g1.find_dependent_nodes([2]), set([5, 0, 1, 2, 3,
                                                         10]))
Exemplo n.º 10
0
    def test_edges(self):
        """Test addition/deletion of edges."""
        g = utility.Graph()

        g.add_edge(1, 2)
        g.add_edge(2, 3)
        g.add_edge(2, 4)
        g.add_edge(1, 2)
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(2, 3))
        self.assertTrue(g.edge_in(2, 4))
        self.assertEqual(len(g), 7)

        g.delete_edge(2, 4)
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(2, 3))
        self.assertFalse(g.edge_in(2, 4))
        self.assertEqual(len(g), 6)

        g.delete_edge(2, 3)
        self.assertTrue(g.edge_in(1, 2))
        self.assertFalse(g.edge_in(2, 3))
        self.assertFalse(g.edge_in(2, 4))
        self.assertEqual(len(g), 5)

        g.delete_edge(2, 3)
        self.assertTrue(g.edge_in(1, 2))
        self.assertFalse(g.edge_in(2, 3))
        self.assertFalse(g.edge_in(2, 4))
        self.assertEqual(len(g), 5)

        g.add_edge(2, 3)
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(2, 3))
        self.assertFalse(g.edge_in(2, 4))
        self.assertEqual(len(g), 6)