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, [])
def test__inward__1(self): cb = Callbacks() search = dfs_.Dfs(direction='inward', **cb.callbacks) trail = search(self.graph1(), ['p', 'x']) self.assertIsInstance(trail, trail_.Trail) self.assertIsNone(trail.result) self.assertEqual(trail.nodes, ['p', 's', 'q', 'x']) self.assertEqual(trail.edges, [('s', 'p'), ('q', 's')]) self.assertEqual(trail.backedges, [('p', 'q')]) self.assertEqual(cb.ingress_edges, trail.edges) self.assertEqual(cb.ingress_nodes, trail.nodes) self.assertEqual(cb.egress_nodes, ['q', 's', 'p', 'x']) self.assertEqual(cb.egress_edges, [('q', 's'), ('s', 'p')]) self.assertEqual(cb.backedges, trail.backedges)
def test__stop_on_backedge(self): cb = Callbacks(backedge_func=lambda g, e: e == ('s', 'p')) search = dfs_.Dfs(direction='outward', **cb.callbacks) trail = search(self.graph2(), ['p', 'x']) self.assertIsInstance(trail, trail_.Trail) self.assertEqual(trail.result, ('s', 'p')) self.assertEqual(trail.backedges, [('s', 'p')]) if 't' in trail.nodes: self.assertIn( trail.nodes, [['p', 'q', 'r', 'u', 's', 't'], ['p', 'q', 's', 't']]) if trail.nodes == ['p', 'q', 'r', 'u', 's', 't']: self.assertEqual(trail.edges, [('p', 'q'), ('q', 'r'), ('r', 'u'), ('q', 's'), ('s', 't')]) self.assertEqual(cb.egress_nodes, ['u', 'r', 't', 's', 'q', 'p']) self.assertEqual(cb.egress_edges, [('r', 'u'), ('q', 'r'), ('s', 't'), ('q', 's'), ('p', 'q')]) else: self.assertEqual(trail.edges, [('p', 'q'), ('q', 's'), ('s', 't')]) self.assertEqual(cb.egress_nodes, ['t', 's', 'q', 'p']) self.assertEqual(cb.egress_edges, [('s', 't'), ('q', 's'), ('p', 'q')]) else: self.assertIn(trail.nodes, [['p', 'q', 'r', 'u', 's'], ['p', 'q', 's']]) if trail.nodes == ['p', 'q', 'r', 'u', 's']: self.assertEqual(trail.edges, [('p', 'q'), ('q', 'r'), ('r', 'u'), ('q', 's')]) self.assertEqual(cb.egress_nodes, ['u', 'r', 's', 'q', 'p']) self.assertEqual(cb.egress_edges, [('r', 'u'), ('q', 'r'), ('q', 's'), ('p', 'q')]) else: self.assertEqual(trail.edges, [('p', 'q'), ('q', 's')]) self.assertEqual(cb.egress_nodes, ['s', 'q', 'p']) self.assertEqual(cb.egress_edges, [('q', 's'), ('p', 'q')]) self.assertEqual(cb.ingress_nodes, trail.nodes) self.assertEqual(cb.ingress_edges, trail.edges) self.assertEqual(cb.backedges, trail.backedges)
def test__stop_on_ingress(self): cb = Callbacks(ingress_func=lambda g, n, e: n == 's') search = dfs_.Dfs(direction='outward', **cb.callbacks) trail = search(self.graph1(), ['p', 'x']) self.assertIsInstance(trail, trail_.Trail) self.assertEqual(trail.result, 's') self.assertEqual(trail.backedges, []) self.assertIn( trail.nodes, [['p', 'q', 'r', 's'], ['p', 'q', 's']]) # 'x' is never visited if trail.nodes == ['p', 'q', 'r', 's']: self.assertEqual(trail.edges, [('p', 'q'), ('q', 'r'), ('q', 's')]) self.assertEqual(cb.egress_nodes, ['r', 's', 'q', 'p']) self.assertEqual(cb.egress_edges, [('q', 'r'), ('q', 's'), ('p', 'q')]) else: self.assertEqual(trail.edges, [('p', 'q'), ('q', 's')]) self.assertEqual(cb.egress_nodes, ['s', 'q', 'p']) self.assertEqual(cb.egress_edges, [('q', 's'), ('p', 'q')]) self.assertEqual(cb.ingress_nodes, trail.nodes) self.assertEqual(cb.ingress_edges, trail.edges) self.assertEqual(cb.backedges, trail.backedges)
def test__incident__1(self): cb = Callbacks() search = dfs_.Dfs(direction='incident', **cb.callbacks) trail = search(self.graph1(), ['p', 'x']) self.assertIsInstance(trail, trail_.Trail) self.assertIsNone(trail.result) self.assertIn(trail.backedges, [[('p', 'q')], [('s', 'p')]]) if trail.backedges == [('s', 'p')]: self.assertIn( trail.nodes, [['p', 'q', 'r', 's', 'x'], ['p', 'q', 's', 'r', 'x']]) if trail.nodes == ['p', 'q', 'r', 's', 'x']: self.assertEqual(cb.egress_nodes, ['r', 's', 'q', 'p', 'x']) self.assertEqual(cb.egress_edges, [('q', 'r'), ('q', 's'), ('p', 'q')]) else: self.assertEqual(cb.egress_nodes, ['s', 'r', 'q', 'p', 'x']) self.assertEqual(cb.egress_edges, [('q', 's'), ('q', 'r'), ('p', 'q')]) else: self.assertEqual(trail.nodes, ['p', 's', 'q', 'r', 'x']) self.assertEqual(cb.ingress_edges, trail.edges) self.assertEqual(cb.ingress_nodes, trail.nodes) self.assertEqual(cb.backedges, trail.backedges)
def test__isinstance_Traversal(self): self.assertIsInstance(dfs_.Dfs(), traversal_.Traversal)