def test_edge_labels(self):
        g = utility.BagGraph()

        g.add_edge(1, 2)
        g.add_edge(1, 2, "label1")
        g.add_edge(1, 2, "label1")
        g.add_edge(1, 2, "label2")
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(1, 2, "label1"))
        self.assertTrue(g.edge_in(1, 2, "label2"))
        self.assertFalse(g.edge_in(1, 2, "non-existent"))
        self.assertEqual(g.edge_count(1, 2, "label1"), 2)

        g.delete_edge(1, 2, "label1")
        self.assertTrue(g.edge_in(1, 2))
        self.assertTrue(g.edge_in(1, 2, "label1"))
        self.assertTrue(g.edge_in(1, 2, "label2"))
        self.assertFalse(g.edge_in(1, 2, "non-existent"))
        self.assertEqual(g.edge_count(1, 2, "label1"), 1)

        g.delete_edge(1, 2, "label1")
        self.assertTrue(g.edge_in(1, 2))
        self.assertFalse(g.edge_in(1, 2, "label1"))
        self.assertTrue(g.edge_in(1, 2, "label2"))
        self.assertFalse(g.edge_in(1, 2, "non-existent"))
        self.assertEqual(g.edge_count(1, 2, "label1"), 0)

        g.delete_edge(1, 2, "label2")
        self.assertTrue(g.edge_in(1, 2))
        self.assertFalse(g.edge_in(1, 2, "label1"))
        self.assertFalse(g.edge_in(1, 2, "label2"))
        self.assertFalse(g.edge_in(1, 2, "non-existent"))
        self.assertEqual(g.edge_count(1, 2, "label1"), 0)
    def test_nodes(self):
        """Test addition/deletion of nodes."""
        g = utility.BagGraph()
        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), 4)

        g.delete_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.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)
    def test_ior(self):
        g1 = utility.BagGraph()
        g1.add_edge(1, 2)
        g1.add_edge(2, 3)

        g2 = utility.BagGraph()
        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.assertTrue(isinstance(g1, utility.BagGraph))
        self.assertEqual(g1.edge_count(2, 3), 2)
        self.assertFalse(g2.edge_in(1, 2))
        self.assertTrue(g2.edge_in(2, 3))
        self.assertTrue(g2.edge_in(3, 4))
        self.assertEqual(g2.edge_count(2, 3), 1)
Exemple #4
0
    def test_or(self):
        g1 = utility.BagGraph()
        g1.add_edge(1, 2)
        g1.add_edge(2, 3)

        g2 = utility.BagGraph()
        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.assertEqual(g.edge_count(2, 3), 2)
        self.assertIsInstance(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.assertEqual(g1.edge_count(2, 3), 1)
        self.assertFalse(g2.edge_in(1, 2))
        self.assertTrue(g2.edge_in(2, 3))
        self.assertTrue(g2.edge_in(3, 4))
        self.assertEqual(g2.edge_count(2, 3), 1)
 def test_cycle(self):
     g1 = utility.BagGraph()
     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(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.assertTrue(g1.has_cycle())
     g1.delete_edge(2, 4)
     self.assertFalse(g1.has_cycle())
    def test_edges(self):
        """Test addition/deletion of edges."""
        g = utility.BagGraph()

        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), 12)

        g.delete_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), 9)

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

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

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

        g.add_node(1)
        self.assertEqual(g.node_count(1), 2)