Esempio n. 1
0
 def test__edge__1(self):
     graph = graph_.Graph(['p', 'q', 'r'], {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     self.assertEqual(graph.edge(('p', 'q')), '1')
     self.assertEqual(graph.edge(('q', 'r')), '2')
Esempio n. 2
0
 def test__has_successors(self):
     graph = graph_.Graph(['p', 'q', 'r', 's', 't'],
                          [('p', 'q'), ('q', 'r'), ('q', 's')])
     self.assertTrue(graph.has_successors('p'))
     self.assertTrue(graph.has_successors('q'))
     self.assertFalse(graph.has_successors('r'))
     self.assertFalse(graph.has_successors('t'))
Esempio n. 3
0
 def test__neighbors__2(self):
     graph = graph_.Graph(['p', 'q', 'r', 's', 't'],
                          [('p', 'q'), ('q', 'r'), ('s', 'r')])
     self.assertEqual(graph.neighbors('p'), set(['q']))
     self.assertEqual(graph.neighbors('q'), set(['p', 'r']))
     self.assertEqual(graph.neighbors('r'), set(['q', 's']))
     self.assertEqual(graph.neighbors('s'), set(['r']))
     self.assertEqual(graph.neighbors('t'),
                      set())  # isolated node, must not throw
Esempio n. 4
0
 def test__incident__2(self):
     graph = graph_.Graph(['p', 'q', 'r', 's', 't'],
                          [('p', 'q'), ('p', 'r'), ('s', 'r')])
     self.assertEqual(graph.incident('p'), set([('p', 'q'), ('p', 'r')]))
     self.assertEqual(graph.incident('q'), set([('p', 'q')]))
     self.assertEqual(graph.incident('r'), set([('p', 'r'), ('s', 'r')]))
     self.assertEqual(graph.incident('s'), set([('s', 'r')]))
     self.assertEqual(graph.incident('t'),
                      set())  # isolated node, must not throw
Esempio n. 5
0
 def test__init__consistency_false(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q'), ('q', 'r')],
                          consistency=False)
     self.assertIsInstance(graph.nodes, nodes_.Nodes)
     self.assertEqual(graph.nodes.data, {'p': None, 'q': None})
     self.assertIsInstance(graph.edges, edges_.Edges)
     self.assertEqual(graph.edges.data, {
         ('p', 'q'): None,
         ('q', 'r'): None
     })
Esempio n. 6
0
 def test__has_edge__1(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     self.assertTrue(graph.has_edge(('p', 'q')))
     self.assertTrue(graph.has_edge(('q', 'r')))
Esempio n. 7
0
 def test__discard_edge__2(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.discard_edge(('p', 'r'))  # must not throw
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 8
0
 def test__del_edge(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.del_edge(('q', 'r'))
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertEqual(graph.edges.data, {('p', 'q'): '1'})
Esempio n. 9
0
 def test__discard_node__3(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.discard_node('x')
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 10
0
 def test__has_edge__3(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     self.assertFalse(graph.has_edge(('x', 'y')))
     self.assertFalse(graph.has_edge(('x', )))
     self.assertFalse(graph.has_edge(('x', 'y', 'z')))
Esempio n. 11
0
 def test__add_node__2(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.add_node('r', None)
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': None})
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 12
0
 def test__repeated_start_nodes_1(self):
     cb = Callbacks()
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     search = dfs_.Dfs(direction='outward', **cb.callbacks)
     trail = search(graph, ['p', 'p'])
     self.assertEqual(trail.nodes, ['p', 'q'])
     self.assertEqual(trail.edges, [('p', 'q')])
     self.assertEqual(trail.backedges, [])
     self.assertEqual(cb.ingress_nodes, ['p', 'q'])
     self.assertEqual(cb.egress_nodes, ['q', 'p'])
     self.assertEqual(cb.ingress_edges, [('p', 'q')])
     self.assertEqual(cb.egress_edges, [('p', 'q')])
     self.assertEqual(cb.backedges, [])
Esempio n. 13
0
 def test__init__2(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     self.assertIsInstance(graph.nodes, nodes_.Nodes)
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertIsInstance(graph.edges, edges_.Edges)
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 14
0
 def test__init__1(self):
     graph = graph_.Graph(['p', 'q', 'r', 's'], [('p', 'q'), ('q', 'r')])
     self.assertIsInstance(graph.nodes, nodes_.Nodes)
     self.assertEqual(graph.nodes.data, {
         'p': None,
         'q': None,
         'r': None,
         's': None
     })
     self.assertIsInstance(graph.edges, edges_.Edges)
     self.assertEqual(graph.edges.data, {
         ('p', 'q'): None,
         ('q', 'r'): None
     })
Esempio n. 15
0
 def test__del_edge__KeyError(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     with self.assertRaises(KeyError) as context:
         graph.del_edge(('p', 'r'))
     self.assertEqual(repr(('p', 'r')), str(context.exception))
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 16
0
 def test__add_edge__2(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.add_edge(('p', 'r'), '3')
     self.assertEqual(graph.nodes.data, {'p': 'P', 'q': 'Q', 'r': 'R'})
     self.assertEqual(graph.edges.data, {
         ('p', 'q'): '1',
         ('q', 'r'): '2',
         ('p', 'r'): '3'
     })
Esempio n. 17
0
 def test__add_node__4(self):
     graph = graph_.Graph({
         'p': 'P',
         'q': 'Q',
         'r': 'R'
     }, {
         ('p', 'q'): '1',
         ('q', 'r'): '2'
     })
     graph.add_node('s', 'S')
     self.assertEqual(graph.nodes.data, {
         'p': 'P',
         'q': 'Q',
         'r': 'R',
         's': 'S'
     })
     self.assertEqual(graph.edges.data, {('p', 'q'): '1', ('q', 'r'): '2'})
Esempio n. 18
0
 def test__repr__(self):
     graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')])
     self.assertEqual(
         repr(graph),
         "Graph(%s, %s)" % (repr(graph.nodes), repr(graph.edges)))
Esempio n. 19
0
 def test__init__inconsistent_2(self):
     with self.assertRaises(KeyError) as context:
         graph_.Graph(['q', 'r'], [('p', 'q'), ('q', 'r')])
     self.assertEqual(repr('p'), str(context.exception))
Esempio n. 20
0
 def test__leafs(self):
     graph = graph_.Graph(['p', 'q', 'r', 's', 't'],
                          [('p', 'q'), ('p', 'r'), ('s', 'r')])
     self.assertEqual(graph.leafs(), set(['q', 'r', 't']))
Esempio n. 21
0
 def test__isolated(self):
     graph = graph_.Graph(['p', 'q', 'r', 's', 't'],
                          [('p', 'q'), ('p', 'r'), ('s', 'r')])
     self.assertEqual(graph.isolated(), set(['t']))
Esempio n. 22
0
 def test__incident__KeyError(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     with self.assertRaises(KeyError) as context:
         graph.incident('x')
     self.assertEqual(repr('x'), str(context.exception))
Esempio n. 23
0
 def test__edge__KeyError(self):
     graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')])
     with self.assertRaises(KeyError) as context:
         graph.edge(('x', 'y'))
     self.assertEqual(repr(('x', 'y')), str(context.exception))
Esempio n. 24
0
 def test__add_edge__ValueError_2(self):
     graph = graph_.Graph({'p': 'P', 'q': 'Q'}, {('p', 'q'): '1'})
     with self.assertRaises(ValueError) as context:
         graph.add_edge(('q', 'r', 's'))
Esempio n. 25
0
 def test__incident__1(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     self.assertEqual(type(graph.incident('q')), set)
Esempio n. 26
0
 def test__edge__2(self):
     graph = graph_.Graph(['p', 'q', 'r'], [('p', 'q'), ('q', 'r')])
     self.assertIsNone(graph.edge(('p', 'q')))
     self.assertIsNone(graph.edge(('q', 'r')))
Esempio n. 27
0
 def test__outward__1(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     self.assertEqual(type(graph.outward('p')), set)
Esempio n. 28
0
 def test__neighbors__1(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     self.assertEqual(type(graph.neighbors('p')), set)
Esempio n. 29
0
 def graph2(self):
     nodes = ['p', 'q', 'r', 's', 't', 'u', 'x']
     edges = [('p', 'q'), ('q', 'r'), ('q', 's'), ('s', 'p'), ('s', 't'),
              ('r', 'u')]
     return graph_.Graph(nodes, edges)
Esempio n. 30
0
 def test__predecessors__1(self):
     graph = graph_.Graph(['p', 'q'], [('p', 'q')])
     self.assertEqual(type(graph.predecessors('p')), set)