195
complex_shortcut = {'d':[NULL_REVISION],
196
'x':['d'], 'y':['x'],
197
'e':['y'], 'f':['d'], 'g':['f', 'i'], 'h':['f'],
198
'i':['e'], 'j':['g'], 'k':['j'],
199
'l':['k'], 'm':['i', 's'], 'n':['s', 'h'],
200
'o':['l'], 'p':['o'], 'q':['p'],
201
'r':['q'], 's':['r'],
193
complex_shortcut = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
194
'e':['d'], 'f':['d'], 'g':['f'], 'h':['f'],
195
'i':['e', 'g'], 'j':['g'], 'k':['j'],
196
'l':['k'], 'm':['i', 'l'], 'n':['l', 'h']}
236
complex_shortcut2 = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
237
'e':['d'], 'f':['e'], 'g':['f'], 'h':['d'], 'i':['g'],
238
'j':['h'], 'k':['h', 'i'], 'l':['k'], 'm':['l'], 'n':['m'],
239
'o':['n'], 'p':['o'], 'q':['p'], 'r':['q'], 's':['r'],
240
't':['i', 's'], 'u':['s', 'j'],
243
# Graph where different walkers will race to find the common and uncommon
286
# y is found to be common right away, but is the start of a long series of
288
# o is actually common, but the i-j shortcut makes it look like it is actually
289
# unique to j at first, you have to traverse all of y->o to find it.
290
# q,n give the walker from j a common point to stop searching, as does p,f.
291
# k-n exists so that the second pass still has nodes that are worth searching,
292
# rather than instantly cancelling the extra walker.
294
racing_shortcuts = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'], 'd':['c'],
295
'e':['d'], 'f':['e'], 'g':['f'], 'h':['g'], 'i':['h', 'o'], 'j':['i', 'y'],
296
'k':['d'], 'l':['k'], 'm':['l'], 'n':['m'], 'o':['n', 'g'], 'p':['f'],
297
'q':['p', 'm'], 'r':['o'], 's':['r'], 't':['s'], 'u':['t'], 'v':['u'],
298
'w':['v'], 'x':['w'], 'y':['x'], 'z':['x', 'q']}
301
# A graph with multiple nodes unique to one side.
340
multiple_interesting_unique = {'a':[NULL_REVISION], 'b':['a'], 'c':['b'],
341
'd':['c'], 'e':['d'], 'f':['d'], 'g':['e'], 'h':['e'], 'i':['f'],
342
'j':['g'], 'k':['g'], 'l':['h'], 'm':['i'], 'n':['k', 'l'],
343
'o':['m'], 'p':['m', 'l'], 'q':['n', 'o'], 'r':['q'], 's':['r'],
344
't':['s'], 'u':['t'], 'v':['u'], 'w':['v'], 'x':['w'],
345
'y':['j', 'x'], 'z':['x', 'p']}
204
348
# Shortcut with extra root
205
349
# We have a long history shortcut, and an extra root, which is why we can't
206
350
# stop searchers based on seeing NULL_REVISION
357
501
graph.find_unique_lca('rev2a', 'rev2b',
358
502
count_steps=True))
504
def assertRemoveDescendants(self, expected, graph, revisions):
505
parents = graph.get_parent_map(revisions)
506
self.assertEqual(expected,
507
graph._remove_simple_descendants(revisions, parents))
509
def test__remove_simple_descendants(self):
510
graph = self.make_graph(ancestry_1)
511
self.assertRemoveDescendants(set(['rev1']), graph,
512
set(['rev1', 'rev2a', 'rev2b', 'rev3', 'rev4']))
514
def test__remove_simple_descendants_disjoint(self):
515
graph = self.make_graph(ancestry_1)
516
self.assertRemoveDescendants(set(['rev1', 'rev3']), graph,
517
set(['rev1', 'rev3']))
519
def test__remove_simple_descendants_chain(self):
520
graph = self.make_graph(ancestry_1)
521
self.assertRemoveDescendants(set(['rev1']), graph,
522
set(['rev1', 'rev2a', 'rev3']))
524
def test__remove_simple_descendants_siblings(self):
525
graph = self.make_graph(ancestry_1)
526
self.assertRemoveDescendants(set(['rev2a', 'rev2b']), graph,
527
set(['rev2a', 'rev2b', 'rev3']))
360
529
def test_unique_lca_criss_cross(self):
361
530
"""Ensure we don't pick non-unique lcas in a criss-cross"""
362
531
graph = self.make_graph(criss_cross)
443
609
def test_graph_difference_complex_shortcut(self):
444
610
graph = self.make_graph(complex_shortcut)
445
self.expectFailure('find_difference cannot handle shortcuts',
446
self.assertEqual, (set(['m']), set(['h', 'n'])),
447
graph.find_difference('m', 'n'))
448
self.assertEqual((set(['m']), set(['h', 'n'])),
611
self.assertEqual((set(['m', 'i', 'e']), set(['n', 'h'])),
449
612
graph.find_difference('m', 'n'))
614
def test_graph_difference_complex_shortcut2(self):
615
graph = self.make_graph(complex_shortcut2)
616
self.assertEqual((set(['t']), set(['j', 'u'])),
617
graph.find_difference('t', 'u'))
451
619
def test_graph_difference_shortcut_extra_root(self):
452
620
graph = self.make_graph(shortcut_extra_root)
453
self.expectFailure('find_difference cannot handle shortcuts',
454
self.assertEqual, (set(['e']), set(['f', 'g'])),
455
graph.find_difference('e', 'f'))
456
621
self.assertEqual((set(['e']), set(['f', 'g'])),
457
622
graph.find_difference('e', 'f'))
973
1138
self.assertEqual(set(['head', NULL_REVISION]), result.get_keys())
1141
class TestFindUniqueAncestors(tests.TestCase):
1143
def make_graph(self, ancestors):
1144
return _mod_graph.Graph(_mod_graph.DictParentsProvider(ancestors))
1146
def make_breaking_graph(self, ancestors, break_on):
1147
"""Make a Graph that raises an exception if we hit a node."""
1148
g = self.make_graph(ancestors)
1149
orig_parent_map = g.get_parent_map
1150
def get_parent_map(keys):
1151
bad_keys = set(keys).intersection(break_on)
1153
self.fail('key(s) %s was accessed' % (sorted(bad_keys),))
1154
return orig_parent_map(keys)
1155
g.get_parent_map = get_parent_map
1158
def assertFindUniqueAncestors(self, graph, expected, node, common):
1159
actual = graph.find_unique_ancestors(node, common)
1160
self.assertEqual(expected, sorted(actual))
1162
def test_empty_set(self):
1163
graph = self.make_graph(ancestry_1)
1164
self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev1'])
1165
self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev2b'])
1166
self.assertFindUniqueAncestors(graph, [], 'rev3', ['rev1', 'rev3'])
1168
def test_single_node(self):
1169
graph = self.make_graph(ancestry_1)
1170
self.assertFindUniqueAncestors(graph, ['rev2a'], 'rev2a', ['rev1'])
1171
self.assertFindUniqueAncestors(graph, ['rev2b'], 'rev2b', ['rev1'])
1172
self.assertFindUniqueAncestors(graph, ['rev3'], 'rev3', ['rev2a'])
1174
def test_minimal_ancestry(self):
1175
graph = self.make_breaking_graph(extended_history_shortcut,
1176
[NULL_REVISION, 'a', 'b'])
1177
self.assertFindUniqueAncestors(graph, ['e'], 'e', ['d'])
1179
graph = self.make_breaking_graph(extended_history_shortcut,
1181
self.assertFindUniqueAncestors(graph, ['f'], 'f', ['a', 'd'])
1183
graph = self.make_breaking_graph(complex_shortcut,
1185
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['i'])
1186
self.assertFindUniqueAncestors(graph, ['e', 'g', 'i'], 'i', ['h'])
1187
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['g'])
1188
self.assertFindUniqueAncestors(graph, ['h'], 'h', ['j'])
1190
def test_in_ancestry(self):
1191
graph = self.make_graph(ancestry_1)
1192
self.assertFindUniqueAncestors(graph, [], 'rev1', ['rev3'])
1193
self.assertFindUniqueAncestors(graph, [], 'rev2b', ['rev4'])
1195
def test_multiple_revisions(self):
1196
graph = self.make_graph(ancestry_1)
1197
self.assertFindUniqueAncestors(graph,
1198
['rev4'], 'rev4', ['rev3', 'rev2b'])
1199
self.assertFindUniqueAncestors(graph,
1200
['rev2a', 'rev3', 'rev4'], 'rev4', ['rev2b'])
1202
def test_complex_shortcut(self):
1203
graph = self.make_graph(complex_shortcut)
1204
self.assertFindUniqueAncestors(graph,
1205
['h', 'n'], 'n', ['m'])
1206
self.assertFindUniqueAncestors(graph,
1207
['e', 'i', 'm'], 'm', ['n'])
1209
def test_complex_shortcut2(self):
1210
graph = self.make_graph(complex_shortcut2)
1211
self.assertFindUniqueAncestors(graph,
1212
['j', 'u'], 'u', ['t'])
1213
self.assertFindUniqueAncestors(graph,
1216
def test_multiple_interesting_unique(self):
1217
graph = self.make_graph(multiple_interesting_unique)
1218
self.assertFindUniqueAncestors(graph,
1219
['j', 'y'], 'y', ['z'])
1220
self.assertFindUniqueAncestors(graph,
1221
['p', 'z'], 'z', ['y'])
1223
def test_racing_shortcuts(self):
1224
graph = self.make_graph(racing_shortcuts)
1225
self.assertFindUniqueAncestors(graph,
1226
['p', 'q', 'z'], 'z', ['y'])
1227
self.assertFindUniqueAncestors(graph,
1228
['h', 'i', 'j', 'y'], 'j', ['z'])
976
1231
class TestCachingParentsProvider(tests.TestCase):
978
1233
def setUp(self):