|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | +# not use this file except in compliance with the License. You may obtain |
| 3 | +# a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | +# License for the specific language governing permissions and limitations |
| 11 | +# under the License. |
| 12 | + |
| 13 | +import unittest |
| 14 | + |
| 15 | +import rustworkx as rx |
| 16 | +import networkx as nx |
| 17 | + |
| 18 | + |
| 19 | +class TestImmediateDominators(unittest.TestCase): |
| 20 | + """Test `rustworkx.immediate_dominators`. |
| 21 | +
|
| 22 | + Test cases adapted from `networkx`: |
| 23 | + https://github.com/networkx/networkx/blob/9c5ca54b7e5310a21568bb2e0104f8c87bf74ff7/networkx/algorithms/tests/test_dominance.py |
| 24 | + (Copyright 2004-2024 NetworkX Developers, 3-clause BSD License) |
| 25 | + """ |
| 26 | + |
| 27 | + def test_empty(self): |
| 28 | + """ |
| 29 | + Edge case: empty graph. |
| 30 | + """ |
| 31 | + graph = rx.PyDiGraph() |
| 32 | + |
| 33 | + with self.assertRaises(rx.NullGraph): |
| 34 | + rx.immediate_dominators(graph, 0) |
| 35 | + |
| 36 | + def test_start_node_not_in_graph(self): |
| 37 | + """ |
| 38 | + Edge case: start_node is not in the graph. |
| 39 | + """ |
| 40 | + graph = rx.PyDiGraph() |
| 41 | + graph.add_node(0) |
| 42 | + |
| 43 | + self.assertEqual(list(graph.node_indices()), [0]) |
| 44 | + |
| 45 | + with self.assertRaises(rx.InvalidNode): |
| 46 | + rx.immediate_dominators(graph, 1) |
| 47 | + |
| 48 | + def test_singleton(self): |
| 49 | + """ |
| 50 | + Edge cases: single node, optionally cyclic. |
| 51 | + """ |
| 52 | + graph = rx.PyDiGraph() |
| 53 | + graph.add_node(0) |
| 54 | + self.assertDictEqual(rx.immediate_dominators(graph, 0), {0: 0}) |
| 55 | + graph.add_edge(0, 0, None) |
| 56 | + self.assertDictEqual(rx.immediate_dominators(graph, 0), {0: 0}) |
| 57 | + |
| 58 | + nx_graph = nx.DiGraph() |
| 59 | + nx_graph.add_edges_from(graph.edge_list()) |
| 60 | + self.assertDictEqual(nx.immediate_dominators(nx_graph, 0), {0: 0}) |
| 61 | + |
| 62 | + def test_irreducible1(self): |
| 63 | + """ |
| 64 | + Graph taken from figure 2 of "A simple, fast dominance algorithm." (2006). |
| 65 | + https://hdl.handle.net/1911/96345 |
| 66 | + """ |
| 67 | + edges = [(1, 2), (2, 1), (3, 2), (4, 1), (5, 3), (5, 4)] |
| 68 | + graph = rx.PyDiGraph() |
| 69 | + graph.add_node(0) |
| 70 | + graph.extend_from_edge_list(edges) |
| 71 | + |
| 72 | + result = rx.immediate_dominators(graph, 5) |
| 73 | + self.assertDictEqual(result, {i: 5 for i in range(1, 6)}) |
| 74 | + |
| 75 | + nx_graph = nx.DiGraph() |
| 76 | + nx_graph.add_edges_from(graph.edge_list()) |
| 77 | + self.assertDictEqual(nx.immediate_dominators(nx_graph, 5), result) |
| 78 | + |
| 79 | + def test_irreducible2(self): |
| 80 | + """ |
| 81 | + Graph taken from figure 4 of "A simple, fast dominance algorithm." (2006). |
| 82 | + https://hdl.handle.net/1911/96345 |
| 83 | + """ |
| 84 | + edges = [(1, 2), (2, 1), (2, 3), (3, 2), (4, 2), (4, 3), (5, 1), (6, 4), (6, 5)] |
| 85 | + graph = rx.PyDiGraph() |
| 86 | + graph.add_node(0) |
| 87 | + graph.extend_from_edge_list(edges) |
| 88 | + |
| 89 | + result = rx.immediate_dominators(graph, 6) |
| 90 | + self.assertDictEqual(result, {i: 6 for i in range(1, 7)}) |
| 91 | + |
| 92 | + nx_graph = nx.DiGraph() |
| 93 | + nx_graph.add_edges_from(graph.edge_list()) |
| 94 | + self.assertDictEqual(nx.immediate_dominators(nx_graph, 6), result) |
| 95 | + |
| 96 | + def test_domrel_png(self): |
| 97 | + """ |
| 98 | + Graph taken from https://commons.wikipedia.org/wiki/File:Domrel.png |
| 99 | + """ |
| 100 | + edges = [(1, 2), (2, 3), (2, 4), (2, 6), (3, 5), (4, 5), (5, 2)] |
| 101 | + graph = rx.PyDiGraph() |
| 102 | + graph.add_node(0) |
| 103 | + graph.extend_from_edge_list(edges) |
| 104 | + |
| 105 | + result = rx.immediate_dominators(graph, 1) |
| 106 | + self.assertDictEqual(result, {1: 1, 2: 1, 3: 2, 4: 2, 5: 2, 6: 2}) |
| 107 | + |
| 108 | + nx_graph = nx.DiGraph() |
| 109 | + nx_graph.add_edges_from(graph.edge_list()) |
| 110 | + self.assertDictEqual(nx.immediate_dominators(nx_graph, 1), result) |
| 111 | + |
| 112 | + # Test postdominance. |
| 113 | + graph.reverse() |
| 114 | + result = rx.immediate_dominators(graph, 6) |
| 115 | + self.assertDictEqual(result, {1: 2, 2: 6, 3: 5, 4: 5, 5: 2, 6: 6}) |
| 116 | + |
| 117 | + self.assertDictEqual(nx.immediate_dominators(nx_graph.reverse(copy=False), 6), result) |
| 118 | + |
| 119 | + def test_boost_example(self): |
| 120 | + """ |
| 121 | + Graph taken from Figure 1 of |
| 122 | + http://www.boost.org/doc/libs/1_56_0/libs/graph/doc/lengauer_tarjan_dominator.htm |
| 123 | + """ |
| 124 | + edges = [(0, 1), (1, 2), (1, 3), (2, 7), (3, 4), (4, 5), (4, 6), (5, 7), (6, 4)] |
| 125 | + graph = rx.PyDiGraph() |
| 126 | + graph.extend_from_edge_list(edges) |
| 127 | + result = rx.immediate_dominators(graph, 0) |
| 128 | + self.assertDictEqual(result, {0: 0, 1: 0, 2: 1, 3: 1, 4: 3, 5: 4, 6: 4, 7: 1}) |
| 129 | + |
| 130 | + nx_graph = nx.DiGraph() |
| 131 | + nx_graph.add_edges_from(graph.edge_list()) |
| 132 | + self.assertDictEqual(nx.immediate_dominators(nx_graph, 0), result) |
| 133 | + |
| 134 | + # Test postdominance. |
| 135 | + graph.reverse() |
| 136 | + result = rx.immediate_dominators(graph, 7) |
| 137 | + self.assertDictEqual(result, {0: 1, 1: 7, 2: 7, 3: 4, 4: 5, 5: 7, 6: 4, 7: 7}) |
| 138 | + |
| 139 | + self.assertDictEqual(nx.immediate_dominators(nx_graph.reverse(copy=False), 7), result) |
0 commit comments