14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
18
"""Tests for topological sort."""
20
from bzrlib.reconcile import topological_sort
21
21
from bzrlib.tests import TestCase
22
from bzrlib.tsort import topo_sort
22
from bzrlib.tsort import topo_sort, TopoSorter
23
23
from bzrlib.errors import GraphCycleError
26
26
class TopoSortTests(TestCase):
28
def assertSortAndIterate(self, graph, result_list):
29
"""Check that sorting and iter_topo_order on graph works."""
30
self.assertEquals(result_list, topo_sort(graph))
31
self.assertEqual(result_list,
32
list(TopoSorter(graph).iter_topo_order()))
34
def assertSortAndIterateRaise(self, exception_type, graph):
35
"""Try both iterating and topo_sorting graph and expect an exception."""
36
self.assertRaises(exception_type, topo_sort, graph)
37
self.assertRaises(exception_type,
39
TopoSorter(graph).iter_topo_order())
28
41
def test_tsort_empty(self):
29
42
"""TopoSort empty list"""
30
self.assertEquals(topo_sort([]), [])
43
self.assertSortAndIterate([], [])
32
45
def test_tsort_easy(self):
33
46
"""TopoSort list with one node"""
34
self.assertEquals(topo_sort({0: []}.items()),
47
self.assertSortAndIterate({0: []}.items(), [0])
37
49
def test_tsort_cycle(self):
38
50
"""TopoSort traps graph with cycles"""
39
self.assertRaises(GraphCycleError,
51
self.assertSortAndIterateRaise(GraphCycleError,
44
55
def test_tsort_cycle_2(self):
45
56
"""TopoSort traps graph with longer cycle"""
46
self.assertRaises(GraphCycleError,
57
self.assertSortAndIterateRaise(GraphCycleError,
52
62
def test_tsort_1(self):
53
63
"""TopoSort simple nontrivial graph"""
54
self.assertEquals(topo_sort({0: [3],
58
4: [0, 3]}.iteritems()),
64
self.assertSortAndIterate({0: [3],
61
71
def test_tsort_partial(self):
62
72
"""Topological sort with partial ordering.
64
74
If the graph does not give an order between two nodes, they are
65
75
returned in lexicographical order.
67
self.assertEquals(topo_sort([(0, []),
75
(8, [0, 1, 4, 5, 6])]),
76
[0, 1, 2, 3, 4, 5, 6, 7, 8, ])
78
def test_new_tsort_empty(self):
79
"""TopoSort empty list"""
80
self.assertEquals(topological_sort([]), [])
82
def test_new_tsort_easy(self):
83
"""TopoSort list with one node"""
84
self.assertEquals(topological_sort({0: []}.items()),
87
def test_new_tsort_cycle(self):
88
"""TopoSort traps graph with cycles"""
89
self.assertRaises(GraphCycleError,
94
def test_new_tsort_cycle_2(self):
95
"""TopoSort traps graph with longer cycle"""
96
self.assertRaises(GraphCycleError,
102
def test_new_tsort_1(self):
103
"""TopoSort simple nontrivial graph"""
104
self.assertEquals(topological_sort({0: [3],
108
4: [0, 3]}.iteritems()),
111
def test_new_tsort_partial(self):
112
"""Topological sort with partial ordering.
114
If the graph does not give an order between two nodes, they are
115
returned in lexicographical order.
117
self.assertEquals(topological_sort([(0, []),
125
(8, [0, 1, 4, 5, 6])]),
126
[0, 1, 2, 3, 4, 5, 6, 7, 8, ])
77
self.assertSortAndIterate(([(0, []),
85
(8, [0, 1, 4, 5, 6])]),
86
[0, 1, 2, 3, 4, 5, 6, 7, 8])