def test_edge_orders(self):
        with self.assertRaises(Exception) as cm:
            self.graph.setEdgeOrder(tlp.node(NB_NODES), [])
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(tlp.node(NB_NODES),
                                     tlp.edge(), tlp.edge())
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.swapEdgeOrder(self.graph.getRandomNode(),
                                     tlp.edge(NB_EDGES), tlp.edge(NB_EDGES+1))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for n in self.graph.getNodes():
            edges = list(self.graph.getInOutEdges(n))
            edges = list(reversed(edges))
            self.graph.setEdgeOrder(n, edges)
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.incidence(n), edges)

            self.graph.swapEdgeOrder(n, edges[0], edges[-1])
            edges[0], edges[-1] = edges[-1], edges[0]
            self.assertEqual(list(self.graph.getInOutEdges(n)), edges)
            self.assertEqual(self.graph.incidence(n), edges)
    def test_add_then_del_edges(self):
        invalid_edges = [(tlp.node(NB_NODES), tlp.node(NB_NODES+1)),
                         (tlp.node(NB_NODES+1), tlp.node(NB_NODES+2))]

        with self.assertRaises(Exception) as cm:
            self.graph.addEdges(invalid_edges)
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        edges_to_add = []
        for i in range(NB_EDGES):
            edge = (self.graph.getRandomNode(), self.graph.getRandomNode())
            edges_to_add.append(edge)

        new_edges = self.graph.addEdges(edges_to_add)
        self.assertEqual(self.graph.numberOfEdges(), 2*NB_EDGES)

        for i, e in enumerate(new_edges):
            self.assertTrue(self.graph.isElement(e))
            self.assertEqual(self.graph.source(e), edges_to_add[i][0])
            self.assertEqual(self.graph.target(e), edges_to_add[i][1])

        self.graph.delEdges(new_edges)
        self.assertEqual(self.graph.numberOfEdges(), NB_EDGES)

        for n in new_edges:
            self.assertFalse(self.graph.isElement(e))
 def test_add_invalid_edge(self):
     with self.assertRaises(Exception) as cm:
         self.graph.addEdge(tlp.node(NB_NODES), tlp.node(NB_NODES+1))
         self.assertEquals(
             cm.exception.args[0],
             'Node with id %s does not belong to graph "%s" (id %s)' %
             (NB_NODES, self.graph.getName(), self.graph.getId()))
    def test_del_nodes_from_list(self):
        invalid_nodes = [tlp.node(NB_NODES), tlp.node(NB_NODES+1)]
        with self.assertRaises(Exception) as cm:
            self.graph.delNodes(invalid_nodes)
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        self.graph.delNodes(self.nodes)
        self.assertEqual(self.graph.numberOfNodes(), 0)
        self.assertEqual(self.graph.numberOfEdges(), 0)
Exemple #5
0
 def setUp(self):
     self.prop_name = 'test_prop'
     self.prop = None
     self.graph = tlp.importGraph('Grid')
     self.n = self.graph.getRandomNode()
     self.e = self.graph.getRandomEdge()
     sg_nodes = [
         tlp.node(i) for i in range(self.graph.numberOfNodes() // 2)
     ]
     self.sub_graph = self.graph.inducedSubGraph(sg_nodes)
    def test_is_element(self):
        for n in self.nodes:
            self.assertTrue(self.graph.isElement(n))
            self.assertTrue(n in self.graph)

        for e in self.edges:
            self.assertTrue(self.graph.isElement(e))
            self.assertTrue(e in self.graph)

        for n in self.graph.getNodes():
            self.assertIn(n, self.nodes)

        for n in self.graph.nodes():
            self.assertIn(n, self.nodes)

        for e in self.graph.getEdges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)
            src, tgt = self.graph.ends(e)
            self.assertIn(src, self.nodes)
            self.assertIn(tgt, self.nodes)

        for e in self.graph.edges():
            self.assertIn(e, self.edges)
            self.assertIn(self.graph.source(e), self.nodes)
            self.assertIn(self.graph.target(e), self.nodes)

        for i in range(NB_NODES - 1):
            self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[i+1]))
            self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[i+1]),
                          self.edges)
            self.assertTrue((self.nodes[i], self.nodes[i+1]) in self.graph)
            self.assertTrue(self.graph.hasEdge(self.nodes[i], self.nodes[-1]))
            self.assertIn(self.graph.existEdge(self.nodes[i], self.nodes[-1]),
                          self.edges)
            self.assertTrue((self.nodes[i], self.nodes[-1]) in self.graph)

        self.assertFalse(self.graph.isElement(tlp.node(NB_NODES)))
        self.assertTrue(tlp.node(NB_NODES) not in self.graph)
        self.assertFalse(self.graph.isElement(tlp.edge(NB_EDGES)))
        self.assertTrue(tlp.edge(NB_EDGES) not in self.graph)
    def test_del_elements(self):
        with self.assertRaises(Exception) as cm:
            self.graph.delNode(tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.delEdge(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        for i, e in enumerate(self.edges):
            self.graph.delEdge(e)
            self.assertEqual(self.graph.numberOfEdges(), NB_EDGES - (i+1))
            self.assertFalse(self.graph.isElement(e))

        for i, n in enumerate(self.nodes):
            self.graph.delNode(n)
            self.assertEqual(self.graph.numberOfNodes(), NB_NODES - (i+1))
            self.assertFalse(self.graph.isElement(n))
    def test_edge_extremities_modification(self):

        with self.assertRaises(Exception) as cm:
            self.graph.ends(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.reverse(tlp.edge(NB_EDGES))
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(tlp.edge(NB_EDGES), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setSource(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setTarget(self.graph.getRandomEdge(),
                                 tlp.node(NB_NODES))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(tlp.edge(NB_EDGES), tlp.node(), tlp.node())
            self.assertEquals(
                cm.exception.args[0],
                'Edge with id %s does not belong to graph "%s" (id %s)' %
                (NB_EDGES, self.graph.getName(), self.graph.getId()))

        with self.assertRaises(Exception) as cm:
            self.graph.setEnds(self.graph.getRandomEdge(),
                               tlp.node(NB_NODES), tlp.node(NB_NODES+1))
            self.assertEquals(
                cm.exception.args[0],
                'Node with id %s does not belong to graph "%s" (id %s)' %
                (NB_NODES, self.graph.getName(), self.graph.getId()))

        for e in self.graph.edges():
            src, tgt = self.graph.ends(e)
            self.graph.reverse(e)
            self.assertEqual(self.graph.source(e), tgt)
            self.assertEqual(self.graph.target(e), src)

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setSource(e, new_src)
            self.graph.setTarget(e, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))

            new_src = self.graph.getRandomNode()
            new_tgt = self.graph.getRandomNode()
            self.graph.setEnds(e, new_src, new_tgt)
            self.assertEqual(self.graph.source(e), new_src)
            self.assertEqual(self.graph.target(e), new_tgt)
            self.assertEqual(self.graph.ends(e), (new_src, new_tgt))