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
"""Tests for topological sort.
17
"""Tests for topological sort."""
20
from bzrlib.reconcile import topological_sort
23
21
from bzrlib.tests import TestCase
24
22
from bzrlib.tsort import topo_sort
25
23
from bzrlib.errors import GraphCycleError
27
26
class TopoSortTests(TestCase):
29
28
def test_tsort_empty(self):
76
75
(8, [0, 1, 4, 5, 6])]),
77
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, ])