773
class TestKnownGraphStableReverseTopoSort(TestCaseWithKnownGraph):
774
"""Test the sort order returned by gc_sort."""
776
def assertSorted(self, expected, parent_map):
777
graph = self.make_known_graph(parent_map)
778
value = graph.gc_sort()
779
if expected != value:
780
self.assertEqualDiff(pprint.pformat(expected),
781
pprint.pformat(value))
783
def test_empty(self):
784
self.assertSorted([], {})
786
def test_single(self):
787
self.assertSorted(['a'], {'a':()})
788
self.assertSorted([('a',)], {('a',):()})
789
self.assertSorted([('F', 'a')], {('F', 'a'):()})
791
def test_linear(self):
792
self.assertSorted(['c', 'b', 'a'], {'a':(), 'b':('a',), 'c':('b',)})
793
self.assertSorted([('c',), ('b',), ('a',)],
794
{('a',):(), ('b',): (('a',),), ('c',): (('b',),)})
795
self.assertSorted([('F', 'c'), ('F', 'b'), ('F', 'a')],
796
{('F', 'a'):(), ('F', 'b'): (('F', 'a'),),
797
('F', 'c'): (('F', 'b'),)})
799
def test_mixed_ancestries(self):
800
# Each prefix should be sorted separately
801
self.assertSorted([('F', 'c'), ('F', 'b'), ('F', 'a'),
802
('G', 'c'), ('G', 'b'), ('G', 'a'),
803
('Q', 'c'), ('Q', 'b'), ('Q', 'a'),
805
{('F', 'a'):(), ('F', 'b'): (('F', 'a'),),
806
('F', 'c'): (('F', 'b'),),
807
('G', 'a'):(), ('G', 'b'): (('G', 'a'),),
808
('G', 'c'): (('G', 'b'),),
809
('Q', 'a'):(), ('Q', 'b'): (('Q', 'a'),),
810
('Q', 'c'): (('Q', 'b'),),
813
def test_stable_sorting(self):
814
# the sort order should be stable even when extra nodes are added
815
self.assertSorted(['b', 'c', 'a'],
816
{'a':(), 'b':('a',), 'c':('a',)})
817
self.assertSorted(['b', 'c', 'd', 'a'],
818
{'a':(), 'b':('a',), 'c':('a',), 'd':('a',)})
819
self.assertSorted(['b', 'c', 'd', 'a'],
820
{'a':(), 'b':('a',), 'c':('a',), 'd':('a',)})
821
self.assertSorted(['Z', 'b', 'c', 'd', 'a'],
822
{'a':(), 'b':('a',), 'c':('a',), 'd':('a',),
824
self.assertSorted(['e', 'b', 'c', 'f', 'Z', 'd', 'a'],
825
{'a':(), 'b':('a',), 'c':('a',), 'd':('a',),
831
def test_skip_ghost(self):
832
self.assertSorted(['b', 'c', 'a'],
833
{'a':(), 'b':('a', 'ghost'), 'c':('a',)})
835
def test_skip_mainline_ghost(self):
836
self.assertSorted(['b', 'c', 'a'],
837
{'a':(), 'b':('ghost', 'a'), 'c':('a',)})