Debian Codegraph @

vcsgraph/tests/test_known_graph.py

raw symbols SCIP
1 # Copyright (C) 2009, 2010, 2011 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 """Tests for the python and pyrex extensions of KnownGraph."""
18
19 import pprint
20 from unittest import TestCase
21
22 from .. import errors
23 from ..graph import NULL_REVISION
24 from ..known_graph import KnownGraph
25 from . import test_graph
26
27 # a
28 # |\
29 # b |
30 # | |
31 # c |
32 # \|
33 # d
34 alt_merge = {b"a": [], b"b": [b"a"], b"c": [b"b"], b"d": [b"a", b"c"]}
35
36
37 class TestCaseWithKnownGraph(TestCase):
38 def make_known_graph(self, ancestry):
39 return KnownGraph(ancestry)
40
41
42 class TestKnownGraph(TestCaseWithKnownGraph):
43 def assertGDFO(self, graph, rev, gdfo):
44 node = graph._nodes[rev]
45 self.assertEqual(gdfo, node.gdfo)
46
47 def test_children_ancestry1(self):
48 graph = self.make_known_graph(test_graph.ancestry_1)
49 self.assertEqual([b"rev1"], graph.get_child_keys(NULL_REVISION))
50 self.assertEqual([b"rev2a", b"rev2b"], sorted(graph.get_child_keys(b"rev1")))
51 self.assertEqual([b"rev3"], graph.get_child_keys(b"rev2a"))
52 self.assertEqual([b"rev4"], graph.get_child_keys(b"rev3"))
53 self.assertEqual([b"rev4"], graph.get_child_keys(b"rev2b"))
54 self.assertRaises(KeyError, graph.get_child_keys, b"not_in_graph")
55
56 def test_parent_ancestry1(self):
57 graph = self.make_known_graph(test_graph.ancestry_1)
58 self.assertEqual((NULL_REVISION,), tuple(graph.get_parent_keys(b"rev1")))
59 self.assertEqual((b"rev1",), tuple(graph.get_parent_keys(b"rev2a")))
60 self.assertEqual((b"rev1",), tuple(graph.get_parent_keys(b"rev2b")))
61 self.assertEqual((b"rev2a",), tuple(graph.get_parent_keys(b"rev3")))
62 self.assertEqual([b"rev2b", b"rev3"], sorted(graph.get_parent_keys(b"rev4")))
63 self.assertRaises(KeyError, graph.get_child_keys, b"not_in_graph")
64
65 def test_parent_with_ghost(self):
66 graph = self.make_known_graph(test_graph.with_ghost)
67 self.assertEqual(None, graph.get_parent_keys(b"g"))
68
69 def test_gdfo_ancestry_1(self):
70 graph = self.make_known_graph(test_graph.ancestry_1)
71 self.assertGDFO(graph, b"rev1", 2)
72 self.assertGDFO(graph, b"rev2b", 3)
73 self.assertGDFO(graph, b"rev2a", 3)
74 self.assertGDFO(graph, b"rev3", 4)
75 self.assertGDFO(graph, b"rev4", 5)
76
77 def test_gdfo_feature_branch(self):
78 graph = self.make_known_graph(test_graph.feature_branch)
79 self.assertGDFO(graph, b"rev1", 2)
80 self.assertGDFO(graph, b"rev2b", 3)
81 self.assertGDFO(graph, b"rev3b", 4)
82
83 def test_gdfo_extended_history_shortcut(self):
84 graph = self.make_known_graph(test_graph.extended_history_shortcut)
85 self.assertGDFO(graph, b"a", 2)
86 self.assertGDFO(graph, b"b", 3)
87 self.assertGDFO(graph, b"c", 4)
88 self.assertGDFO(graph, b"d", 5)
89 self.assertGDFO(graph, b"e", 6)
90 self.assertGDFO(graph, b"f", 6)
91
92 def test_gdfo_with_ghost(self):
93 graph = self.make_known_graph(test_graph.with_ghost)
94 self.assertGDFO(graph, b"f", 2)
95 self.assertGDFO(graph, b"e", 3)
96 self.assertGDFO(graph, b"g", 1)
97 self.assertGDFO(graph, b"b", 4)
98 self.assertGDFO(graph, b"d", 4)
99 self.assertGDFO(graph, b"a", 5)
100 self.assertGDFO(graph, b"c", 5)
101
102 def test_add_existing_node(self):
103 graph = self.make_known_graph(test_graph.ancestry_1)
104 # Add a node that already exists with identical content
105 # This is a 'no-op'
106 self.assertGDFO(graph, b"rev4", 5)
107 graph.add_node(b"rev4", [b"rev3", b"rev2b"])
108 self.assertGDFO(graph, b"rev4", 5)
109 # This also works if we use a tuple rather than a list
110 graph.add_node(b"rev4", (b"rev3", b"rev2b"))
111
112 def test_add_existing_node_mismatched_parents(self):
113 graph = self.make_known_graph(test_graph.ancestry_1)
114 self.assertRaises(ValueError, graph.add_node, b"rev4", [b"rev2b", b"rev3"])
115
116 def test_add_node_with_ghost_parent(self):
117 graph = self.make_known_graph(test_graph.ancestry_1)
118 graph.add_node(b"rev5", [b"rev2b", b"revGhost"])
119 self.assertGDFO(graph, b"rev5", 4)
120 self.assertGDFO(graph, b"revGhost", 1)
121
122 def test_add_new_root(self):
123 graph = self.make_known_graph(test_graph.ancestry_1)
124 graph.add_node(b"rev5", [])
125 self.assertGDFO(graph, b"rev5", 1)
126
127 def test_add_with_all_ghost_parents(self):
128 graph = self.make_known_graph(test_graph.ancestry_1)
129 graph.add_node(b"rev5", [b"ghost"])
130 self.assertGDFO(graph, b"rev5", 2)
131 self.assertGDFO(graph, b"ghost", 1)
132
133 def test_gdfo_after_add_node(self):
134 graph = self.make_known_graph(test_graph.ancestry_1)
135 self.assertEqual([], graph.get_child_keys(b"rev4"))
136 graph.add_node(b"rev5", [b"rev4"])
137 self.assertEqual([b"rev4"], graph.get_parent_keys(b"rev5"))
138 self.assertEqual([b"rev5"], graph.get_child_keys(b"rev4"))
139 self.assertEqual([], graph.get_child_keys(b"rev5"))
140 self.assertGDFO(graph, b"rev5", 6)
141 graph.add_node(b"rev6", [b"rev2b"])
142 graph.add_node(b"rev7", [b"rev6"])
143 graph.add_node(b"rev8", [b"rev7", b"rev5"])
144 self.assertGDFO(graph, b"rev5", 6)
145 self.assertGDFO(graph, b"rev6", 4)
146 self.assertGDFO(graph, b"rev7", 5)
147 self.assertGDFO(graph, b"rev8", 7)
148
149 def test_fill_in_ghost(self):
150 graph = self.make_known_graph(test_graph.with_ghost)
151 # Add in a couple nodes and then fill in the 'ghost' so that it should
152 # cause renumbering of children nodes
153 graph.add_node(b"x", [])
154 graph.add_node(b"y", [b"x"])
155 graph.add_node(b"z", [b"y"])
156 graph.add_node(b"g", [b"z"])
157 self.assertGDFO(graph, b"f", 2)
158 self.assertGDFO(graph, b"e", 3)
159 self.assertGDFO(graph, b"x", 1)
160 self.assertGDFO(graph, b"y", 2)
161 self.assertGDFO(graph, b"z", 3)
162 self.assertGDFO(graph, b"g", 4)
163 self.assertGDFO(graph, b"b", 4)
164 self.assertGDFO(graph, b"d", 5)
165 self.assertGDFO(graph, b"a", 5)
166 self.assertGDFO(graph, b"c", 6)
167
168
169 class TestKnownGraphHeads(TestCaseWithKnownGraph):
170 def test_heads_null(self):
171 graph = self.make_known_graph(test_graph.ancestry_1)
172 self.assertEqual({b"null:"}, graph.heads([b"null:"]))
173 self.assertEqual({b"rev1"}, graph.heads([b"null:", b"rev1"]))
174 self.assertEqual({b"rev1"}, graph.heads([b"rev1", b"null:"]))
175 self.assertEqual({b"rev1"}, graph.heads({b"rev1", b"null:"}))
176 self.assertEqual({b"rev1"}, graph.heads((b"rev1", b"null:")))
177
178 def test_heads_one(self):
179 # A single node will always be a head
180 graph = self.make_known_graph(test_graph.ancestry_1)
181 self.assertEqual({b"null:"}, graph.heads([b"null:"]))
182 self.assertEqual({b"rev1"}, graph.heads([b"rev1"]))
183 self.assertEqual({b"rev2a"}, graph.heads([b"rev2a"]))
184 self.assertEqual({b"rev2b"}, graph.heads([b"rev2b"]))
185 self.assertEqual({b"rev3"}, graph.heads([b"rev3"]))
186 self.assertEqual({b"rev4"}, graph.heads([b"rev4"]))
187
188 def test_heads_single(self):
189 graph = self.make_known_graph(test_graph.ancestry_1)
190 self.assertEqual({b"rev4"}, graph.heads([b"null:", b"rev4"]))
191 self.assertEqual({b"rev2a"}, graph.heads([b"rev1", b"rev2a"]))
192 self.assertEqual({b"rev2b"}, graph.heads([b"rev1", b"rev2b"]))
193 self.assertEqual({b"rev3"}, graph.heads([b"rev1", b"rev3"]))
194 self.assertEqual({b"rev3"}, graph.heads([b"rev3", b"rev2a"]))
195 self.assertEqual({b"rev4"}, graph.heads([b"rev1", b"rev4"]))
196 self.assertEqual({b"rev4"}, graph.heads([b"rev2a", b"rev4"]))
197 self.assertEqual({b"rev4"}, graph.heads([b"rev2b", b"rev4"]))
198 self.assertEqual({b"rev4"}, graph.heads([b"rev3", b"rev4"]))
199
200 def test_heads_two_heads(self):
201 graph = self.make_known_graph(test_graph.ancestry_1)
202 self.assertEqual({b"rev2a", b"rev2b"}, graph.heads([b"rev2a", b"rev2b"]))
203 self.assertEqual({b"rev3", b"rev2b"}, graph.heads([b"rev3", b"rev2b"]))
204
205 def test_heads_criss_cross(self):
206 graph = self.make_known_graph(test_graph.criss_cross)
207 self.assertEqual({b"rev2a"}, graph.heads([b"rev2a", b"rev1"]))
208 self.assertEqual({b"rev2b"}, graph.heads([b"rev2b", b"rev1"]))
209 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev1"]))
210 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev1"]))
211 self.assertEqual({b"rev2a", b"rev2b"}, graph.heads([b"rev2a", b"rev2b"]))
212 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2a"]))
213 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2b"]))
214 self.assertEqual({b"rev3a"}, graph.heads([b"rev3a", b"rev2a", b"rev2b"]))
215 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2a"]))
216 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2b"]))
217 self.assertEqual({b"rev3b"}, graph.heads([b"rev3b", b"rev2a", b"rev2b"]))
218 self.assertEqual({b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b"]))
219 self.assertEqual(
220 {b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b", b"rev2a", b"rev2b"])
221 )
222
223 def test_heads_shortcut(self):
224 graph = self.make_known_graph(test_graph.history_shortcut)
225 self.assertEqual(
226 {b"rev2a", b"rev2b", b"rev2c"}, graph.heads([b"rev2a", b"rev2b", b"rev2c"])
227 )
228 self.assertEqual({b"rev3a", b"rev3b"}, graph.heads([b"rev3a", b"rev3b"]))
229 self.assertEqual(
230 {b"rev3a", b"rev3b"}, graph.heads([b"rev2a", b"rev3a", b"rev3b"])
231 )
232 self.assertEqual({b"rev2a", b"rev3b"}, graph.heads([b"rev2a", b"rev3b"]))
233 self.assertEqual({b"rev2c", b"rev3a"}, graph.heads([b"rev2c", b"rev3a"]))
234
235 def test_heads_linear(self):
236 graph = self.make_known_graph(test_graph.racing_shortcuts)
237 self.assertEqual({b"w"}, graph.heads([b"w", b"s"]))
238 self.assertEqual({b"z"}, graph.heads([b"w", b"s", b"z"]))
239 self.assertEqual({b"w", b"q"}, graph.heads([b"w", b"s", b"q"]))
240 self.assertEqual({b"z"}, graph.heads([b"s", b"z"]))
241
242 def test_heads_alt_merge(self):
243 graph = self.make_known_graph(alt_merge)
244 self.assertEqual({b"c"}, graph.heads([b"a", b"c"]))
245
246 def test_heads_with_ghost(self):
247 graph = self.make_known_graph(test_graph.with_ghost)
248 self.assertEqual({b"e", b"g"}, graph.heads([b"e", b"g"]))
249 self.assertEqual({b"a", b"c"}, graph.heads([b"a", b"c"]))
250 self.assertEqual({b"a", b"g"}, graph.heads([b"a", b"g"]))
251 self.assertEqual({b"f", b"g"}, graph.heads([b"f", b"g"]))
252 self.assertEqual({b"c"}, graph.heads([b"c", b"g"]))
253 self.assertEqual({b"c"}, graph.heads([b"c", b"b", b"d", b"g"]))
254 self.assertEqual({b"a", b"c"}, graph.heads([b"a", b"c", b"e", b"g"]))
255 self.assertEqual({b"a", b"c"}, graph.heads([b"a", b"c", b"f"]))
256
257 def test_filling_in_ghosts_resets_head_cache(self):
258 graph = self.make_known_graph(test_graph.with_ghost)
259 self.assertEqual({b"e", b"g"}, graph.heads([b"e", b"g"]))
260 # 'g' is filled in, and decends from 'e', so the heads result is now
261 # different
262 graph.add_node(b"g", [b"e"])
263 self.assertEqual({b"g"}, graph.heads([b"e", b"g"]))
264
265
266 class TestKnownGraphTopoSort(TestCaseWithKnownGraph):
267 def assertTopoSortOrder(self, ancestry):
268 """Check topo_sort and iter_topo_order is genuinely topological order.
269
270 For every child in the graph, check if it comes after all of it's
271 parents.
272 """
273 graph = self.make_known_graph(ancestry)
274 sort_result = graph.topo_sort()
275 # We should have an entry in sort_result for every entry present in the
276 # graph.
277 self.assertEqual(len(ancestry), len(sort_result))
278 node_idx = {node: idx for idx, node in enumerate(sort_result)}
279 for node in sort_result:
280 parents = ancestry[node]
281 for parent in parents:
282 if parent not in ancestry:
283 # ghost
284 continue
285 if node_idx[node] <= node_idx[parent]:
286 self.fail(
287 f"parent {parent} must come before child {node}:\n{sort_result}"
288 )
289
290 def test_topo_sort_empty(self):
291 """TopoSort empty list."""
292 self.assertTopoSortOrder({})
293
294 def test_topo_sort_easy(self):
295 """TopoSort list with one node."""
296 self.assertTopoSortOrder({0: []})
297
298 def test_topo_sort_cycle(self):
299 """TopoSort traps graph with cycles."""
300 g = self.make_known_graph({0: [1], 1: [0]})
301 self.assertRaises(errors.GraphCycleError, g.topo_sort)
302
303 def test_topo_sort_cycle_2(self):
304 """TopoSort traps graph with longer cycle."""
305 g = self.make_known_graph({0: [1], 1: [2], 2: [0]})
306 self.assertRaises(errors.GraphCycleError, g.topo_sort)
307
308 def test_topo_sort_cycle_with_tail(self):
309 """TopoSort traps graph with longer cycle."""
310 g = self.make_known_graph({0: [1], 1: [2], 2: [3, 4], 3: [0], 4: []})
311 self.assertRaises(errors.GraphCycleError, g.topo_sort)
312
313 def test_topo_sort_1(self):
314 """TopoSort simple nontrivial graph."""
315 self.assertTopoSortOrder({0: [3], 1: [4], 2: [1, 4], 3: [], 4: [0, 3]})
316
317 def test_topo_sort_partial(self):
318 """Topological sort with partial ordering.
319
320 Multiple correct orderings are possible, so test for
321 correctness, not for exact match on the resulting list.
322 """
323 self.assertTopoSortOrder(
324 {
325 0: [],
326 1: [0],
327 2: [0],
328 3: [0],
329 4: [1, 2, 3],
330 5: [1, 2],
331 6: [1, 2],
332 7: [2, 3],
333 8: [0, 1, 4, 5, 6],
334 }
335 )
336
337 def test_topo_sort_ghost_parent(self):
338 """Sort nodes, but don't include some parents in the output."""
339 self.assertTopoSortOrder({0: [1], 1: [2]})
340
341
342 class TestKnownGraphMergeSort(TestCaseWithKnownGraph):
343 def assertSortAndIterate(self, ancestry, branch_tip, result_list):
344 """Check that merge based sorting and iter_topo_order on graph works."""
345 graph = self.make_known_graph(ancestry)
346 value = graph.merge_sort(branch_tip)
347 value = [(n.key, n.merge_depth, n.revno, n.end_of_merge) for n in value]
348 if result_list != value:
349 self.assertEqualDiff(pprint.pformat(result_list), pprint.pformat(value))
350
351 def test_merge_sort_empty(self):
352 # sorting of an emptygraph does not error
353 self.assertSortAndIterate({}, None, [])
354 self.assertSortAndIterate({}, NULL_REVISION, [])
355 self.assertSortAndIterate({}, (NULL_REVISION,), [])
356
357 def test_merge_sort_not_empty_no_tip(self):
358 # merge sorting of a branch starting with None should result
359 # in an empty list: no revisions are dragged in.
360 self.assertSortAndIterate({0: []}, None, [])
361 self.assertSortAndIterate({0: []}, NULL_REVISION, [])
362 self.assertSortAndIterate({0: []}, (NULL_REVISION,), [])
363
364 def test_merge_sort_one_revision(self):
365 # sorting with one revision as the tip returns the correct fields:
366 # sequence - 0, revision id, merge depth - 0, end_of_merge
367 self.assertSortAndIterate({"id": []}, "id", [("id", 0, (1,), True)])
368
369 def test_sequence_numbers_increase_no_merges(self):
370 # emit a few revisions with no merges to check the sequence
371 # numbering works in trivial cases
372 self.assertSortAndIterate(
373 {"A": [], "B": ["A"], "C": ["B"]},
374 "C",
375 [
376 ("C", 0, (3,), False),
377 ("B", 0, (2,), False),
378 ("A", 0, (1,), True),
379 ],
380 )
381
382 def test_sequence_numbers_increase_with_merges(self):
383 # test that sequence numbers increase across merges
384 self.assertSortAndIterate(
385 {"A": [], "B": ["A"], "C": ["A", "B"]},
386 "C",
387 [
388 ("C", 0, (2,), False),
389 ("B", 1, (1, 1, 1), True),
390 ("A", 0, (1,), True),
391 ],
392 )
393
394 def test_merge_sort_race(self):
395 # A
396 # |
397 # B-.
398 # |\ \
399 # | | C
400 # | |/
401 # | D
402 # |/
403 # F
404 graph = {
405 "A": [],
406 "B": ["A"],
407 "C": ["B"],
408 "D": ["B", "C"],
409 "F": ["B", "D"],
410 }
411 self.assertSortAndIterate(
412 graph,
413 "F",
414 [
415 ("F", 0, (3,), False),
416 ("D", 1, (2, 2, 1), False),
417 ("C", 2, (2, 1, 1), True),
418 ("B", 0, (2,), False),
419 ("A", 0, (1,), True),
420 ],
421 )
422 # A
423 # |
424 # B-.
425 # |\ \
426 # | X C
427 # | |/
428 # | D
429 # |/
430 # F
431 graph = {
432 "A": [],
433 "B": ["A"],
434 "C": ["B"],
435 "X": ["B"],
436 "D": ["X", "C"],
437 "F": ["B", "D"],
438 }
439 self.assertSortAndIterate(
440 graph,
441 "F",
442 [
443 ("F", 0, (3,), False),
444 ("D", 1, (2, 1, 2), False),
445 ("C", 2, (2, 2, 1), True),
446 ("X", 1, (2, 1, 1), True),
447 ("B", 0, (2,), False),
448 ("A", 0, (1,), True),
449 ],
450 )
451
452 def test_merge_depth_with_nested_merges(self):
453 # the merge depth marker should reflect the depth of the revision
454 # in terms of merges out from the mainline
455 # revid, depth, parents:
456 # A 0 [D, B]
457 # B 1 [C, F]
458 # C 1 [H]
459 # D 0 [H, E]
460 # E 1 [G, F]
461 # F 2 [G]
462 # G 1 [H]
463 # H 0
464 self.assertSortAndIterate(
465 {
466 "A": ["D", "B"],
467 "B": ["C", "F"],
468 "C": ["H"],
469 "D": ["H", "E"],
470 "E": ["G", "F"],
471 "F": ["G"],
472 "G": ["H"],
473 "H": [],
474 },
475 "A",
476 [
477 ("A", 0, (3,), False),
478 ("B", 1, (1, 3, 2), False),
479 ("C", 1, (1, 3, 1), True),
480 ("D", 0, (2,), False),
481 ("E", 1, (1, 1, 2), False),
482 ("F", 2, (1, 2, 1), True),
483 ("G", 1, (1, 1, 1), True),
484 ("H", 0, (1,), True),
485 ],
486 )
487
488 def test_dotted_revnos_with_simple_merges(self):
489 # A 1
490 # |\
491 # B C 2, 1.1.1
492 # | |\
493 # D E F 3, 1.1.2, 1.2.1
494 # |/ /|
495 # G H I 4, 1.2.2, 1.3.1
496 # |/ /
497 # J K 5, 1.3.2
498 # |/
499 # L 6
500 self.assertSortAndIterate(
501 {
502 "A": [],
503 "B": ["A"],
504 "C": ["A"],
505 "D": ["B"],
506 "E": ["C"],
507 "F": ["C"],
508 "G": ["D", "E"],
509 "H": ["F"],
510 "I": ["F"],
511 "J": ["G", "H"],
512 "K": ["I"],
513 "L": ["J", "K"],
514 },
515 "L",
516 [
517 ("L", 0, (6,), False),
518 ("K", 1, (1, 3, 2), False),
519 ("I", 1, (1, 3, 1), True),
520 ("J", 0, (5,), False),
521 ("H", 1, (1, 2, 2), False),
522 ("F", 1, (1, 2, 1), True),
523 ("G", 0, (4,), False),
524 ("E", 1, (1, 1, 2), False),
525 ("C", 1, (1, 1, 1), True),
526 ("D", 0, (3,), False),
527 ("B", 0, (2,), False),
528 ("A", 0, (1,), True),
529 ],
530 )
531 # Adding a shortcut from the first revision should not change any of
532 # the existing numbers
533 self.assertSortAndIterate(
534 {
535 "A": [],
536 "B": ["A"],
537 "C": ["A"],
538 "D": ["B"],
539 "E": ["C"],
540 "F": ["C"],
541 "G": ["D", "E"],
542 "H": ["F"],
543 "I": ["F"],
544 "J": ["G", "H"],
545 "K": ["I"],
546 "L": ["J", "K"],
547 "M": ["A"],
548 "N": ["L", "M"],
549 },
550 "N",
551 [
552 ("N", 0, (7,), False),
553 ("M", 1, (1, 4, 1), True),
554 ("L", 0, (6,), False),
555 ("K", 1, (1, 3, 2), False),
556 ("I", 1, (1, 3, 1), True),
557 ("J", 0, (5,), False),
558 ("H", 1, (1, 2, 2), False),
559 ("F", 1, (1, 2, 1), True),
560 ("G", 0, (4,), False),
561 ("E", 1, (1, 1, 2), False),
562 ("C", 1, (1, 1, 1), True),
563 ("D", 0, (3,), False),
564 ("B", 0, (2,), False),
565 ("A", 0, (1,), True),
566 ],
567 )
568
569 def test_end_of_merge_not_last_revision_in_branch(self):
570 # within a branch only the last revision gets an
571 # end of merge marker.
572 self.assertSortAndIterate(
573 {
574 "A": ["B"],
575 "B": [],
576 },
577 "A",
578 [("A", 0, (2,), False), ("B", 0, (1,), True)],
579 )
580
581 def test_end_of_merge_multiple_revisions_merged_at_once(self):
582 # when multiple branches are merged at once, both of their
583 # branch-endpoints should be listed as end-of-merge.
584 # Also, the order of the multiple merges should be
585 # left-right shown top to bottom.
586 # * means end of merge
587 # A 0 [H, B, E]
588 # B 1 [D, C]
589 # C 2 [D] *
590 # D 1 [H] *
591 # E 1 [G, F]
592 # F 2 [G] *
593 # G 1 [H] *
594 # H 0 [] *
595 self.assertSortAndIterate(
596 {
597 "A": ["H", "B", "E"],
598 "B": ["D", "C"],
599 "C": ["D"],
600 "D": ["H"],
601 "E": ["G", "F"],
602 "F": ["G"],
603 "G": ["H"],
604 "H": [],
605 },
606 "A",
607 [
608 ("A", 0, (2,), False),
609 ("B", 1, (1, 3, 2), False),
610 ("C", 2, (1, 4, 1), True),
611 ("D", 1, (1, 3, 1), True),
612 ("E", 1, (1, 1, 2), False),
613 ("F", 2, (1, 2, 1), True),
614 ("G", 1, (1, 1, 1), True),
615 ("H", 0, (1,), True),
616 ],
617 )
618
619 def test_parallel_root_sequence_numbers_increase_with_merges(self):
620 """When there are parallel roots, check their revnos."""
621 self.assertSortAndIterate(
622 {"A": [], "B": [], "C": ["A", "B"]},
623 "C",
624 [
625 ("C", 0, (2,), False),
626 ("B", 1, (0, 1, 1), True),
627 ("A", 0, (1,), True),
628 ],
629 )
630
631 def test_revnos_are_globally_assigned(self):
632 """Revnos are assigned according to the revision they derive from."""
633 # in this test we setup a number of branches that all derive from
634 # the first revision, and then merge them one at a time, which
635 # should give the revisions as they merge numbers still deriving from
636 # the revision were based on.
637 # merge 3: J: ['G', 'I']
638 # branch 3:
639 # I: ['H']
640 # H: ['A']
641 # merge 2: G: ['D', 'F']
642 # branch 2:
643 # F: ['E']
644 # E: ['A']
645 # merge 1: D: ['A', 'C']
646 # branch 1:
647 # C: ['B']
648 # B: ['A']
649 # root: A: []
650 self.assertSortAndIterate(
651 {
652 "J": ["G", "I"],
653 "I": [
654 "H",
655 ],
656 "H": ["A"],
657 "G": ["D", "F"],
658 "F": ["E"],
659 "E": ["A"],
660 "D": ["A", "C"],
661 "C": ["B"],
662 "B": ["A"],
663 "A": [],
664 },
665 "J",
666 [
667 ("J", 0, (4,), False),
668 ("I", 1, (1, 3, 2), False),
669 ("H", 1, (1, 3, 1), True),
670 ("G", 0, (3,), False),
671 ("F", 1, (1, 2, 2), False),
672 ("E", 1, (1, 2, 1), True),
673 ("D", 0, (2,), False),
674 ("C", 1, (1, 1, 2), False),
675 ("B", 1, (1, 1, 1), True),
676 ("A", 0, (1,), True),
677 ],
678 )
679
680 def test_roots_and_sub_branches_versus_ghosts(self):
681 """Extra roots and their mini branches use the same numbering.
682
683 All of them use the 0-node numbering.
684 """
685 # A D K
686 # | |\ |\
687 # B E F L M
688 # | |/ |/
689 # C G N
690 # |/ |\
691 # H I O P
692 # |/ |/
693 # J Q
694 # |.---'
695 # R
696 self.assertSortAndIterate(
697 {
698 "A": [],
699 "B": ["A"],
700 "C": ["B"],
701 "D": [],
702 "E": ["D"],
703 "F": ["D"],
704 "G": ["E", "F"],
705 "H": ["C", "G"],
706 "I": [],
707 "J": ["H", "I"],
708 "K": [],
709 "L": ["K"],
710 "M": ["K"],
711 "N": ["L", "M"],
712 "O": ["N"],
713 "P": ["N"],
714 "Q": ["O", "P"],
715 "R": ["J", "Q"],
716 },
717 "R",
718 [
719 ("R", 0, (6,), False),
720 ("Q", 1, (0, 4, 5), False),
721 ("P", 2, (0, 6, 1), True),
722 ("O", 1, (0, 4, 4), False),
723 ("N", 1, (0, 4, 3), False),
724 ("M", 2, (0, 5, 1), True),
725 ("L", 1, (0, 4, 2), False),
726 ("K", 1, (0, 4, 1), True),
727 ("J", 0, (5,), False),
728 ("I", 1, (0, 3, 1), True),
729 ("H", 0, (4,), False),
730 ("G", 1, (0, 1, 3), False),
731 ("F", 2, (0, 2, 1), True),
732 ("E", 1, (0, 1, 2), False),
733 ("D", 1, (0, 1, 1), True),
734 ("C", 0, (3,), False),
735 ("B", 0, (2,), False),
736 ("A", 0, (1,), True),
737 ],
738 )
739
740 def test_ghost(self):
741 # merge_sort should be able to ignore ghosts
742 # A
743 # |
744 # B ghost
745 # |/
746 # C
747 self.assertSortAndIterate(
748 {
749 "A": [],
750 "B": ["A"],
751 "C": ["B", "ghost"],
752 },
753 "C",
754 [
755 ("C", 0, (3,), False),
756 ("B", 0, (2,), False),
757 ("A", 0, (1,), True),
758 ],
759 )
760
761 def test_lefthand_ghost(self):
762 # ghost
763 # |
764 # A
765 # |
766 # B
767 self.assertSortAndIterate(
768 {
769 "A": ["ghost"],
770 "B": ["A"],
771 },
772 "B",
773 [
774 ("B", 0, (2,), False),
775 ("A", 0, (1,), True),
776 ],
777 )
778
779 def test_graph_cycle(self):
780 # merge_sort should fail with a simple error when a graph cycle is
781 # encountered.
782 #
783 # A
784 # |,-.
785 # B |
786 # | |
787 # C ^
788 # | |
789 # D |
790 # |'-'
791 # E
792 self.assertRaises(
793 errors.GraphCycleError,
794 self.assertSortAndIterate,
795 {
796 "A": [],
797 "B": ["D"],
798 "C": ["B"],
799 "D": ["C"],
800 "E": ["D"],
801 },
802 "E",
803 [],
804 )
805
806
807 class TestKnownGraphStableReverseTopoSort(TestCaseWithKnownGraph):
808 """Test the sort order returned by gc_sort."""
809
810 def assertSorted(self, expected, parent_map):
811 graph = self.make_known_graph(parent_map)
812 value = graph.gc_sort()
813 if expected != value:
814 self.assertEqualDiff(pprint.pformat(expected), pprint.pformat(value))
815
816 def test_empty(self):
817 self.assertSorted([], {})
818
819 def test_single(self):
820 self.assertSorted(["a"], {"a": ()})
821 self.assertSorted([("a",)], {("a",): ()})
822 self.assertSorted([("F", "a")], {("F", "a"): ()})
823
824 def test_linear(self):
825 self.assertSorted(["c", "b", "a"], {"a": (), "b": ("a",), "c": ("b",)})
826 self.assertSorted(
827 [("c",), ("b",), ("a",)], {("a",): (), ("b",): (("a",),), ("c",): (("b",),)}
828 )
829 self.assertSorted(
830 [("F", "c"), ("F", "b"), ("F", "a")],
831 {("F", "a"): (), ("F", "b"): (("F", "a"),), ("F", "c"): (("F", "b"),)},
832 )
833
834 def test_mixed_ancestries(self):
835 # Each prefix should be sorted separately
836 self.assertSorted(
837 [
838 ("F", "c"),
839 ("F", "b"),
840 ("F", "a"),
841 ("G", "c"),
842 ("G", "b"),
843 ("G", "a"),
844 ("Q", "c"),
845 ("Q", "b"),
846 ("Q", "a"),
847 ],
848 {
849 ("F", "a"): (),
850 ("F", "b"): (("F", "a"),),
851 ("F", "c"): (("F", "b"),),
852 ("G", "a"): (),
853 ("G", "b"): (("G", "a"),),
854 ("G", "c"): (("G", "b"),),
855 ("Q", "a"): (),
856 ("Q", "b"): (("Q", "a"),),
857 ("Q", "c"): (("Q", "b"),),
858 },
859 )
860
861 def test_stable_sorting(self):
862 # the sort order should be stable even when extra nodes are added
863 self.assertSorted(["b", "c", "a"], {"a": (), "b": ("a",), "c": ("a",)})
864 self.assertSorted(
865 ["b", "c", "d", "a"], {"a": (), "b": ("a",), "c": ("a",), "d": ("a",)}
866 )
867 self.assertSorted(
868 ["b", "c", "d", "a"], {"a": (), "b": ("a",), "c": ("a",), "d": ("a",)}
869 )
870 self.assertSorted(
871 ["Z", "b", "c", "d", "a"],
872 {"a": (), "b": ("a",), "c": ("a",), "d": ("a",), "Z": ("a",)},
873 )
874 self.assertSorted(
875 ["e", "b", "c", "f", "Z", "d", "a"],
876 {
877 "a": (),
878 "b": ("a",),
879 "c": ("a",),
880 "d": ("a",),
881 "Z": ("a",),
882 "e": ("b", "c", "d"),
883 "f": ("d", "Z"),
884 },
885 )
886
887 def test_skip_ghost(self):
888 self.assertSorted(["b", "c", "a"], {"a": (), "b": ("a", "ghost"), "c": ("a",)})
889
890 def test_skip_mainline_ghost(self):
891 self.assertSorted(["b", "c", "a"], {"a": (), "b": ("ghost", "a"), "c": ("a",)})