Debian Codegraph @

vcsgraph/tests/test_tsort.py

raw symbols SCIP
1 # Copyright (C) 2005-2009, 2016 Canonical Ltd
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
17
18 """Tests for topological sort."""
19
20 import pprint
21 from unittest import TestCase
22
23 from ..errors import GraphCycleError
24 from ..graph import NULL_REVISION
25 from ..tsort import MergeSorter, TopoSorter, merge_sort, topo_sort
26
27
28 class TopoSortTests(TestCase):
29 def assertSortAndIterate(self, graph, result_list):
30 """Check that sorting and iter_topo_order on graph works."""
31 self.assertEqual(result_list, topo_sort(graph))
32 self.assertEqual(result_list, list(TopoSorter(graph).iter_topo_order()))
33
34 def assertSortAndIterateRaise(self, exception_type, graph):
35 """Try iterating and topo_sorting graph and expect an exception."""
36 self.assertRaises(exception_type, topo_sort, graph)
37 self.assertRaises(exception_type, list, TopoSorter(graph).iter_topo_order())
38
39 def assertSortAndIterateOrder(self, graph):
40 """Check topo_sort and iter_topo_order is genuinely topological order.
41
42 For every child in the graph, check if it comes after all of it's
43 parents.
44 """
45 sort_result = topo_sort(graph)
46 iter_result = list(TopoSorter(graph).iter_topo_order())
47 for node, parents in graph:
48 for parent in parents:
49 if sort_result.index(node) < sort_result.index(parent):
50 self.fail(
51 f"parent {parent} must come before child {node}:\n{sort_result}"
52 )
53 if iter_result.index(node) < iter_result.index(parent):
54 self.fail(
55 f"parent {parent} must come before child {node}:\n{iter_result}"
56 )
57
58 def test_tsort_empty(self):
59 """TopoSort empty list."""
60 self.assertSortAndIterate([], [])
61
62 def test_tsort_easy(self):
63 """TopoSort list with one node."""
64 self.assertSortAndIterate({0: []}.items(), [0])
65
66 def test_tsort_cycle(self):
67 """TopoSort traps graph with cycles."""
68 self.assertSortAndIterateRaise(GraphCycleError, {0: [1], 1: [0]}.items())
69
70 def test_tsort_cycle_2(self):
71 """TopoSort traps graph with longer cycle."""
72 self.assertSortAndIterateRaise(
73 GraphCycleError, {0: [1], 1: [2], 2: [0]}.items()
74 )
75
76 def test_topo_sort_cycle_with_tail(self):
77 """TopoSort traps graph with longer cycle."""
78 self.assertSortAndIterateRaise(
79 GraphCycleError, {0: [1], 1: [2], 2: [3, 4], 3: [0], 4: []}.items()
80 )
81
82 def test_tsort_1(self):
83 """TopoSort simple nontrivial graph."""
84 self.assertSortAndIterate(
85 {0: [3], 1: [4], 2: [1, 4], 3: [], 4: [0, 3]}.items(), [3, 0, 4, 1, 2]
86 )
87
88 def test_tsort_partial(self):
89 """Topological sort with partial ordering.
90
91 Multiple correct orderings are possible, so test for
92 correctness, not for exact match on the resulting list.
93 """
94 self.assertSortAndIterateOrder(
95 [
96 (0, []),
97 (1, [0]),
98 (2, [0]),
99 (3, [0]),
100 (4, [1, 2, 3]),
101 (5, [1, 2]),
102 (6, [1, 2]),
103 (7, [2, 3]),
104 (8, [0, 1, 4, 5, 6]),
105 ]
106 )
107
108 def test_tsort_unincluded_parent(self):
109 """Sort nodes, but don't include some parents in the output."""
110 self.assertSortAndIterate([(0, [1]), (1, [2])], [1, 0])
111
112
113 class MergeSortTests(TestCase):
114 def assertSortAndIterate(
115 self, graph, branch_tip, result_list, generate_revno, mainline_revisions=None
116 ):
117 """Check that merge based sort and iter_topo_order on graph works."""
118 value = merge_sort(
119 graph,
120 branch_tip,
121 mainline_revisions=mainline_revisions,
122 generate_revno=generate_revno,
123 )
124 if result_list != value:
125 self.assertEqualDiff(pprint.pformat(result_list), pprint.pformat(value))
126 self.assertEqual(
127 result_list,
128 list(
129 MergeSorter(
130 graph,
131 branch_tip,
132 mainline_revisions=mainline_revisions,
133 generate_revno=generate_revno,
134 ).iter_topo_order()
135 ),
136 )
137
138 def test_merge_sort_empty(self):
139 # sorting of an emptygraph does not error
140 self.assertSortAndIterate({}, None, [], False)
141 self.assertSortAndIterate({}, None, [], True)
142 self.assertSortAndIterate({}, NULL_REVISION, [], False)
143 self.assertSortAndIterate({}, NULL_REVISION, [], True)
144
145 def test_merge_sort_not_empty_no_tip(self):
146 # merge sorting of a branch starting with None should result
147 # in an empty list: no revisions are dragged in.
148 self.assertSortAndIterate({0: []}.items(), None, [], False)
149 self.assertSortAndIterate({0: []}.items(), None, [], True)
150
151 def test_merge_sort_one_revision(self):
152 # sorting with one revision as the tip returns the correct fields:
153 # sequence - 0, revision id, merge depth - 0, end_of_merge
154 self.assertSortAndIterate({"id": []}.items(), "id", [(0, "id", 0, True)], False)
155 self.assertSortAndIterate(
156 {"id": []}.items(), "id", [(0, "id", 0, (1,), True)], True
157 )
158
159 def test_sequence_numbers_increase_no_merges(self):
160 # emit a few revisions with no merges to check the sequence
161 # numbering works in trivial cases
162 self.assertSortAndIterate(
163 {"A": [], "B": ["A"], "C": ["B"]}.items(),
164 "C",
165 [
166 (0, "C", 0, False),
167 (1, "B", 0, False),
168 (2, "A", 0, True),
169 ],
170 False,
171 )
172 self.assertSortAndIterate(
173 {"A": [], "B": ["A"], "C": ["B"]}.items(),
174 "C",
175 [
176 (0, "C", 0, (3,), False),
177 (1, "B", 0, (2,), False),
178 (2, "A", 0, (1,), True),
179 ],
180 True,
181 )
182
183 def test_sequence_numbers_increase_with_merges(self):
184 # test that sequence numbers increase across merges
185 self.assertSortAndIterate(
186 {"A": [], "B": ["A"], "C": ["A", "B"]}.items(),
187 "C",
188 [
189 (0, "C", 0, False),
190 (1, "B", 1, True),
191 (2, "A", 0, True),
192 ],
193 False,
194 )
195 self.assertSortAndIterate(
196 {"A": [], "B": ["A"], "C": ["A", "B"]}.items(),
197 "C",
198 [
199 (0, "C", 0, (2,), False),
200 (1, "B", 1, (1, 1, 1), True),
201 (2, "A", 0, (1,), True),
202 ],
203 True,
204 )
205
206 def test_merge_sort_race(self):
207 # A
208 # |
209 # B-.
210 # |\ \
211 # | | C
212 # | |/
213 # | D
214 # |/
215 # F
216 graph = {
217 "A": [],
218 "B": ["A"],
219 "C": ["B"],
220 "D": ["B", "C"],
221 "F": ["B", "D"],
222 }
223 self.assertSortAndIterate(
224 graph,
225 "F",
226 [
227 (0, "F", 0, (3,), False),
228 (1, "D", 1, (2, 2, 1), False),
229 (2, "C", 2, (2, 1, 1), True),
230 (3, "B", 0, (2,), False),
231 (4, "A", 0, (1,), True),
232 ],
233 True,
234 )
235 # A
236 # |
237 # B-.
238 # |\ \
239 # | X C
240 # | |/
241 # | D
242 # |/
243 # F
244 graph = {
245 "A": [],
246 "B": ["A"],
247 "C": ["B"],
248 "X": ["B"],
249 "D": ["X", "C"],
250 "F": ["B", "D"],
251 }
252 self.assertSortAndIterate(
253 graph,
254 "F",
255 [
256 (0, "F", 0, (3,), False),
257 (1, "D", 1, (2, 1, 2), False),
258 (2, "C", 2, (2, 2, 1), True),
259 (3, "X", 1, (2, 1, 1), True),
260 (4, "B", 0, (2,), False),
261 (5, "A", 0, (1,), True),
262 ],
263 True,
264 )
265
266 def test_merge_depth_with_nested_merges(self):
267 # the merge depth marker should reflect the depth of the revision
268 # in terms of merges out from the mainline
269 # revid, depth, parents:
270 # A 0 [D, B]
271 # B 1 [C, F]
272 # C 1 [H]
273 # D 0 [H, E]
274 # E 1 [G, F]
275 # F 2 [G]
276 # G 1 [H]
277 # H 0
278 self.assertSortAndIterate(
279 {
280 "A": ["D", "B"],
281 "B": ["C", "F"],
282 "C": ["H"],
283 "D": ["H", "E"],
284 "E": ["G", "F"],
285 "F": ["G"],
286 "G": ["H"],
287 "H": [],
288 }.items(),
289 "A",
290 [
291 (0, "A", 0, False),
292 (1, "B", 1, False),
293 (2, "C", 1, True),
294 (3, "D", 0, False),
295 (4, "E", 1, False),
296 (5, "F", 2, True),
297 (6, "G", 1, True),
298 (7, "H", 0, True),
299 ],
300 False,
301 )
302 self.assertSortAndIterate(
303 {
304 "A": ["D", "B"],
305 "B": ["C", "F"],
306 "C": ["H"],
307 "D": ["H", "E"],
308 "E": ["G", "F"],
309 "F": ["G"],
310 "G": ["H"],
311 "H": [],
312 }.items(),
313 "A",
314 [
315 (0, "A", 0, (3,), False),
316 (1, "B", 1, (1, 3, 2), False),
317 (2, "C", 1, (1, 3, 1), True),
318 (3, "D", 0, (2,), False),
319 (4, "E", 1, (1, 1, 2), False),
320 (5, "F", 2, (1, 2, 1), True),
321 (6, "G", 1, (1, 1, 1), True),
322 (7, "H", 0, (1,), True),
323 ],
324 True,
325 )
326
327 def test_dotted_revnos_with_simple_merges(self):
328 # A 1
329 # |\
330 # B C 2, 1.1.1
331 # | |\
332 # D E F 3, 1.1.2, 1.2.1
333 # |/ /|
334 # G H I 4, 1.2.2, 1.3.1
335 # |/ /
336 # J K 5, 1.3.2
337 # |/
338 # L 6
339 self.assertSortAndIterate(
340 {
341 "A": [],
342 "B": ["A"],
343 "C": ["A"],
344 "D": ["B"],
345 "E": ["C"],
346 "F": ["C"],
347 "G": ["D", "E"],
348 "H": ["F"],
349 "I": ["F"],
350 "J": ["G", "H"],
351 "K": ["I"],
352 "L": ["J", "K"],
353 }.items(),
354 "L",
355 [
356 (0, "L", 0, (6,), False),
357 (1, "K", 1, (1, 3, 2), False),
358 (2, "I", 1, (1, 3, 1), True),
359 (3, "J", 0, (5,), False),
360 (4, "H", 1, (1, 2, 2), False),
361 (5, "F", 1, (1, 2, 1), True),
362 (6, "G", 0, (4,), False),
363 (7, "E", 1, (1, 1, 2), False),
364 (8, "C", 1, (1, 1, 1), True),
365 (9, "D", 0, (3,), False),
366 (10, "B", 0, (2,), False),
367 (11, "A", 0, (1,), True),
368 ],
369 True,
370 )
371 # Adding a shortcut from the first revision should not change any of
372 # the existing numbers
373 self.assertSortAndIterate(
374 {
375 "A": [],
376 "B": ["A"],
377 "C": ["A"],
378 "D": ["B"],
379 "E": ["C"],
380 "F": ["C"],
381 "G": ["D", "E"],
382 "H": ["F"],
383 "I": ["F"],
384 "J": ["G", "H"],
385 "K": ["I"],
386 "L": ["J", "K"],
387 "M": ["A"],
388 "N": ["L", "M"],
389 }.items(),
390 "N",
391 [
392 (0, "N", 0, (7,), False),
393 (1, "M", 1, (1, 4, 1), True),
394 (2, "L", 0, (6,), False),
395 (3, "K", 1, (1, 3, 2), False),
396 (4, "I", 1, (1, 3, 1), True),
397 (5, "J", 0, (5,), False),
398 (6, "H", 1, (1, 2, 2), False),
399 (7, "F", 1, (1, 2, 1), True),
400 (8, "G", 0, (4,), False),
401 (9, "E", 1, (1, 1, 2), False),
402 (10, "C", 1, (1, 1, 1), True),
403 (11, "D", 0, (3,), False),
404 (12, "B", 0, (2,), False),
405 (13, "A", 0, (1,), True),
406 ],
407 True,
408 )
409
410 def test_end_of_merge_not_last_revision_in_branch(self):
411 # within a branch only the last revision gets an
412 # end of merge marker.
413 self.assertSortAndIterate(
414 {
415 "A": ["B"],
416 "B": [],
417 },
418 "A",
419 [(0, "A", 0, False), (1, "B", 0, True)],
420 False,
421 )
422 self.assertSortAndIterate(
423 {
424 "A": ["B"],
425 "B": [],
426 },
427 "A",
428 [(0, "A", 0, (2,), False), (1, "B", 0, (1,), True)],
429 True,
430 )
431
432 def test_end_of_merge_multiple_revisions_merged_at_once(self):
433 # when multiple branches are merged at once, both of their
434 # branch-endpoints should be listed as end-of-merge.
435 # Also, the order of the multiple merges should be
436 # left-right shown top to bottom.
437 # * means end of merge
438 # A 0 [H, B, E]
439 # B 1 [D, C]
440 # C 2 [D] *
441 # D 1 [H] *
442 # E 1 [G, F]
443 # F 2 [G] *
444 # G 1 [H] *
445 # H 0 [] *
446 self.assertSortAndIterate(
447 {
448 "A": ["H", "B", "E"],
449 "B": ["D", "C"],
450 "C": ["D"],
451 "D": ["H"],
452 "E": ["G", "F"],
453 "F": ["G"],
454 "G": ["H"],
455 "H": [],
456 },
457 "A",
458 [
459 (0, "A", 0, False),
460 (1, "B", 1, False),
461 (2, "C", 2, True),
462 (3, "D", 1, True),
463 (4, "E", 1, False),
464 (5, "F", 2, True),
465 (6, "G", 1, True),
466 (7, "H", 0, True),
467 ],
468 False,
469 )
470 self.assertSortAndIterate(
471 {
472 "A": ["H", "B", "E"],
473 "B": ["D", "C"],
474 "C": ["D"],
475 "D": ["H"],
476 "E": ["G", "F"],
477 "F": ["G"],
478 "G": ["H"],
479 "H": [],
480 },
481 "A",
482 [
483 (0, "A", 0, (2,), False),
484 (1, "B", 1, (1, 3, 2), False),
485 (2, "C", 2, (1, 4, 1), True),
486 (3, "D", 1, (1, 3, 1), True),
487 (4, "E", 1, (1, 1, 2), False),
488 (5, "F", 2, (1, 2, 1), True),
489 (6, "G", 1, (1, 1, 1), True),
490 (7, "H", 0, (1,), True),
491 ],
492 True,
493 )
494
495 def test_mainline_revs_partial(self):
496 # when a mainline_revisions list is passed this must
497 # override the graphs idea of mainline, and must also
498 # truncate the output to the specified range, if needed.
499 # so we test both at once: a mainline_revisions list that
500 # disagrees with the graph about which revs are 'mainline'
501 # and also truncates the output.
502 # graph:
503 # A 0 [E, B]
504 # B 1 [D, C]
505 # C 2 [D]
506 # D 1 [E]
507 # E 0
508 # with a mainline of NONE,E,A (the inferred one) this will show the
509 # merge depths above.
510 # with a overriden mainline of NONE,E,D,B,A it should show:
511 # A 0
512 # B 0
513 # C 1
514 # D 0
515 # E 0
516 # and thus when truncated to D,B,A it should show
517 # A 0
518 # B 0
519 # C 1
520 # because C is brought in by B in this view and D
521 # is the terminating revision id
522 # this should also preserve revision numbers: C should still be 2.1.1
523 self.assertSortAndIterate(
524 {"A": ["E", "B"], "B": ["D", "C"], "C": ["D"], "D": ["E"], "E": []},
525 "A",
526 [
527 (0, "A", 0, False),
528 (1, "B", 0, False),
529 (2, "C", 1, True),
530 ],
531 False,
532 mainline_revisions=["D", "B", "A"],
533 )
534 self.assertSortAndIterate(
535 {"A": ["E", "B"], "B": ["D", "C"], "C": ["D"], "D": ["E"], "E": []},
536 "A",
537 [
538 (0, "A", 0, (4,), False),
539 (1, "B", 0, (3,), False),
540 (2, "C", 1, (2, 1, 1), True),
541 ],
542 True,
543 mainline_revisions=["D", "B", "A"],
544 )
545
546 def test_mainline_revs_with_none(self):
547 # a simple test to ensure that a mainline_revs
548 # list which goes all the way to None works
549 self.assertSortAndIterate(
550 {
551 "A": [],
552 },
553 "A",
554 [
555 (0, "A", 0, True),
556 ],
557 False,
558 mainline_revisions=[None, "A"],
559 )
560 self.assertSortAndIterate(
561 {
562 "A": [],
563 },
564 "A",
565 [
566 (0, "A", 0, (1,), True),
567 ],
568 True,
569 mainline_revisions=[None, "A"],
570 )
571
572 def test_mainline_revs_with_ghost(self):
573 # We have a mainline, but the end of it is actually a ghost
574 # The graph that is passed to tsort has had ghosts filtered out, but
575 # the mainline history has not.
576 self.assertSortAndIterate(
577 {"B": [], "C": ["B"]}.items(),
578 "C",
579 [
580 (0, "C", 0, (2,), False),
581 (1, "B", 0, (1,), True),
582 ],
583 True,
584 mainline_revisions=["A", "B", "C"],
585 )
586
587 def test_parallel_root_sequence_numbers_increase_with_merges(self):
588 """When there are parallel roots, check their revnos."""
589 self.assertSortAndIterate(
590 {"A": [], "B": [], "C": ["A", "B"]}.items(),
591 "C",
592 [
593 (0, "C", 0, (2,), False),
594 (1, "B", 1, (0, 1, 1), True),
595 (2, "A", 0, (1,), True),
596 ],
597 True,
598 )
599
600 def test_revnos_are_globally_assigned(self):
601 """Revnos are assigned according to the revision they derive from."""
602 # in this test we setup a number of branches that all derive from
603 # the first revision, and then merge them one at a time, which
604 # should give the revisions as they merge numbers still deriving from
605 # the revision were based on.
606 # merge 3: J: ['G', 'I']
607 # branch 3:
608 # I: ['H']
609 # H: ['A']
610 # merge 2: G: ['D', 'F']
611 # branch 2:
612 # F: ['E']
613 # E: ['A']
614 # merge 1: D: ['A', 'C']
615 # branch 1:
616 # C: ['B']
617 # B: ['A']
618 # root: A: []
619 self.assertSortAndIterate(
620 {
621 "J": ["G", "I"],
622 "I": [
623 "H",
624 ],
625 "H": ["A"],
626 "G": ["D", "F"],
627 "F": ["E"],
628 "E": ["A"],
629 "D": ["A", "C"],
630 "C": ["B"],
631 "B": ["A"],
632 "A": [],
633 }.items(),
634 "J",
635 [
636 (0, "J", 0, (4,), False),
637 (1, "I", 1, (1, 3, 2), False),
638 (2, "H", 1, (1, 3, 1), True),
639 (3, "G", 0, (3,), False),
640 (4, "F", 1, (1, 2, 2), False),
641 (5, "E", 1, (1, 2, 1), True),
642 (6, "D", 0, (2,), False),
643 (7, "C", 1, (1, 1, 2), False),
644 (8, "B", 1, (1, 1, 1), True),
645 (9, "A", 0, (1,), True),
646 ],
647 True,
648 )
649
650 def test_roots_and_sub_branches_versus_ghosts(self):
651 """Extra roots and their mini branches use the same numbering.
652
653 All of them use the 0-node numbering.
654 """
655 # A D K
656 # | |\ |\
657 # B E F L M
658 # | |/ |/
659 # C G N
660 # |/ |\
661 # H I O P
662 # |/ |/
663 # J Q
664 # |.---'
665 # R
666 self.assertSortAndIterate(
667 {
668 "A": [],
669 "B": ["A"],
670 "C": ["B"],
671 "D": [],
672 "E": ["D"],
673 "F": ["D"],
674 "G": ["E", "F"],
675 "H": ["C", "G"],
676 "I": [],
677 "J": ["H", "I"],
678 "K": [],
679 "L": ["K"],
680 "M": ["K"],
681 "N": ["L", "M"],
682 "O": ["N"],
683 "P": ["N"],
684 "Q": ["O", "P"],
685 "R": ["J", "Q"],
686 }.items(),
687 "R",
688 [
689 (0, "R", 0, (6,), False),
690 (1, "Q", 1, (0, 4, 5), False),
691 (2, "P", 2, (0, 6, 1), True),
692 (3, "O", 1, (0, 4, 4), False),
693 (4, "N", 1, (0, 4, 3), False),
694 (5, "M", 2, (0, 5, 1), True),
695 (6, "L", 1, (0, 4, 2), False),
696 (7, "K", 1, (0, 4, 1), True),
697 (8, "J", 0, (5,), False),
698 (9, "I", 1, (0, 3, 1), True),
699 (10, "H", 0, (4,), False),
700 (11, "G", 1, (0, 1, 3), False),
701 (12, "F", 2, (0, 2, 1), True),
702 (13, "E", 1, (0, 1, 2), False),
703 (14, "D", 1, (0, 1, 1), True),
704 (15, "C", 0, (3,), False),
705 (16, "B", 0, (2,), False),
706 (17, "A", 0, (1,), True),
707 ],
708 True,
709 )