~bzr-pqm/bzr/bzr.dev

2052.3.2 by John Arbash Meinel
Change Copyright .. by Canonical to Copyright ... Canonical
1
# Copyright (C) 2005 Canonical Ltd
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
17
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
18
"""Tests for topological sort."""
19
20
1185.31.25 by John Arbash Meinel
Renamed all of the tests from selftest/foo.py to tests/test_foo.py
21
from bzrlib.tests import TestCase
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
22
from bzrlib.tsort import topo_sort, TopoSorter, MergeSorter, merge_sort
1185.16.114 by mbp at sourcefrog
Improved topological sort
23
from bzrlib.errors import GraphCycleError
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
24
1570.1.6 by Robert Collins
Update fast topological_sort to be a function and to have the topo_sort tests run against it.
25
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
26
class TopoSortTests(TestCase):
27
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
28
    def assertSortAndIterate(self, graph, result_list):
29
        """Check that sorting and iter_topo_order on graph works."""
30
        self.assertEquals(result_list, topo_sort(graph))
31
        self.assertEqual(result_list,
32
                         list(TopoSorter(graph).iter_topo_order()))
33
34
    def assertSortAndIterateRaise(self, exception_type, graph):
35
        """Try both iterating and topo_sorting graph and expect an exception."""
36
        self.assertRaises(exception_type, topo_sort, graph)
37
        self.assertRaises(exception_type,
38
                          list,
39
                          TopoSorter(graph).iter_topo_order())
40
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
41
    def test_tsort_empty(self):
42
        """TopoSort empty list"""
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
43
        self.assertSortAndIterate([], [])
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
44
45
    def test_tsort_easy(self):
1185.16.114 by mbp at sourcefrog
Improved topological sort
46
        """TopoSort list with one node"""
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
47
        self.assertSortAndIterate({0: []}.items(), [0])
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
48
49
    def test_tsort_cycle(self):
1185.16.114 by mbp at sourcefrog
Improved topological sort
50
        """TopoSort traps graph with cycles"""
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
51
        self.assertSortAndIterateRaise(GraphCycleError,
52
                                       {0: [1], 
53
                                        1: [0]}.items())
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
54
1185.16.114 by mbp at sourcefrog
Improved topological sort
55
    def test_tsort_cycle_2(self):
56
        """TopoSort traps graph with longer cycle"""
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
57
        self.assertSortAndIterateRaise(GraphCycleError,
58
                                       {0: [1], 
59
                                        1: [2], 
60
                                        2: [0]}.items())
1185.16.114 by mbp at sourcefrog
Improved topological sort
61
                 
1185.16.113 by mbp at sourcefrog
Add topo_sort utility function
62
    def test_tsort_1(self):
63
        """TopoSort simple nontrivial graph"""
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
64
        self.assertSortAndIterate({0: [3], 
65
                                   1: [4],
66
                                   2: [1, 4],
67
                                   3: [], 
68
                                   4: [0, 3]}.items(),
69
                                  [3, 0, 4, 1, 2])
1185.16.115 by mbp at sourcefrog
Another topo_sort test
70
71
    def test_tsort_partial(self):
72
        """Topological sort with partial ordering.
73
74
        If the graph does not give an order between two nodes, they are 
75
        returned in lexicographical order.
76
        """
1570.1.7 by Robert Collins
Replace the slow topo_sort routine with a much faster one for non trivial datasets.
77
        self.assertSortAndIterate(([(0, []),
78
                                   (1, [0]),
79
                                   (2, [0]),
80
                                   (3, [0]),
81
                                   (4, [1, 2, 3]),
82
                                   (5, [1, 2]),
83
                                   (6, [1, 2]),
84
                                   (7, [2, 3]),
85
                                   (8, [0, 1, 4, 5, 6])]),
86
                                  [0, 1, 2, 3, 4, 5, 6, 7, 8])
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
87
2490.2.31 by Aaron Bentley
Fix iter_topo_order to permit un-included parents
88
    def test_tsort_unincluded_parent(self):
89
        """Sort nodes, but don't include some parents in the output"""
90
        self.assertSortAndIterate([(0, [1]),
91
                                   (1, [2])],
92
                                   [1, 0])
93
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
94
95
class MergeSortTests(TestCase):
96
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
97
    def assertSortAndIterate(self, graph, branch_tip, result_list,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
98
            generate_revno, mainline_revisions=None):
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
99
        """Check that merge based sorting and iter_topo_order on graph works."""
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
100
        self.assertEquals(result_list,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
101
            merge_sort(graph, branch_tip, mainline_revisions=mainline_revisions,
102
                generate_revno=generate_revno))
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
103
        self.assertEqual(result_list,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
104
            list(MergeSorter(
105
                graph,
106
                branch_tip,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
107
                mainline_revisions=mainline_revisions,
108
                generate_revno=generate_revno,
109
                ).iter_topo_order()))
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
110
111
    def test_merge_sort_empty(self):
112
        # sorting of an emptygraph does not error
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
113
        self.assertSortAndIterate({}, None, [], False)
114
        self.assertSortAndIterate({}, None, [], True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
115
116
    def test_merge_sort_not_empty_no_tip(self):
117
        # merge sorting of a branch starting with None should result
118
        # in an empty list: no revisions are dragged in.
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
119
        self.assertSortAndIterate({0: []}.items(), None, [], False)
120
        self.assertSortAndIterate({0: []}.items(), None, [], True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
121
122
    def test_merge_sort_one_revision(self):
123
        # sorting with one revision as the tip returns the correct fields:
124
        # sequence - 0, revision id, merge depth - 0, end_of_merge
125
        self.assertSortAndIterate({'id': []}.items(),
126
                                  'id',
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
127
                                  [(0, 'id', 0, True)],
128
                                  False)
129
        self.assertSortAndIterate({'id': []}.items(),
130
                                  'id',
131
                                  [(0, 'id', 0, (1,), True)],
132
                                  True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
133
    
134
    def test_sequence_numbers_increase_no_merges(self):
135
        # emit a few revisions with no merges to check the sequence
136
        # numbering works in trivial cases
137
        self.assertSortAndIterate(
138
            {'A': [],
139
             'B': ['A'],
140
             'C': ['B']}.items(),
141
            'C',
142
            [(0, 'C', 0, False),
143
             (1, 'B', 0, False),
144
             (2, 'A', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
145
             ],
146
            False
147
            )
148
        self.assertSortAndIterate(
149
            {'A': [],
150
             'B': ['A'],
151
             'C': ['B']}.items(),
152
            'C',
153
            [(0, 'C', 0, (3,), False),
154
             (1, 'B', 0, (2,), False),
155
             (2, 'A', 0, (1,), True),
156
             ],
157
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
158
            )
159
160
    def test_sequence_numbers_increase_with_merges(self):
161
        # test that sequence numbers increase across merges
162
        self.assertSortAndIterate(
163
            {'A': [],
164
             'B': ['A'],
165
             'C': ['A', 'B']}.items(),
166
            'C',
167
            [(0, 'C', 0, False),
168
             (1, 'B', 1, True),
169
             (2, 'A', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
170
             ],
171
            False
172
            )
173
        self.assertSortAndIterate(
174
            {'A': [],
175
             'B': ['A'],
176
             'C': ['A', 'B']}.items(),
177
            'C',
178
            [(0, 'C', 0, (2,), False),
179
             (1, 'B', 1, (1,1,1), True),
180
             (2, 'A', 0, (1,), True),
181
             ],
182
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
183
            )
184
        
185
    def test_merge_depth_with_nested_merges(self):
186
        # the merge depth marker should reflect the depth of the revision
187
        # in terms of merges out from the mainline
188
        # revid, depth, parents:
189
        #  A 0   [D, B]   
190
        #  B  1  [C, F]   
191
        #  C  1  [H] 
192
        #  D 0   [H, E]
193
        #  E  1  [G, F]
194
        #  F   2 [G]
195
        #  G  1  [H]
196
        #  H 0
197
        self.assertSortAndIterate(
198
            {'A': ['D', 'B'],
199
             'B': ['C', 'F'],
200
             'C': ['H'],
201
             'D': ['H', 'E'],
202
             'E': ['G', 'F'],
203
             'F': ['G'],
204
             'G': ['H'],
205
             'H': []
206
             }.items(),
207
            'A',
208
            [(0, 'A', 0, False),
209
             (1, 'B', 1, False),
210
             (2, 'C', 1, True),
211
             (3, 'D', 0, False),
212
             (4, 'E', 1, False),
213
             (5, 'F', 2, True),
214
             (6, 'G', 1, True),
215
             (7, 'H', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
216
             ],
217
            False
218
            )
219
        self.assertSortAndIterate(
220
            {'A': ['D', 'B'],
221
             'B': ['C', 'F'],
222
             'C': ['H'],
223
             'D': ['H', 'E'],
224
             'E': ['G', 'F'],
225
             'F': ['G'],
226
             'G': ['H'],
227
             'H': []
228
             }.items(),
229
            'A',
230
            [(0, 'A', 0, (3,),  False),
231
             (1, 'B', 1, (1,2,2), False),
232
             (2, 'C', 1, (1,2,1), True),
233
             (3, 'D', 0, (2,), False),
234
             (4, 'E', 1, (1,1,2), False),
235
             (5, 'F', 2, (1,1,1,1,1), True),
236
             (6, 'G', 1, (1,1,1), True),
237
             (7, 'H', 0, (1,), True),
238
             ],
239
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
240
            )
241
 
242
    def test_end_of_merge_not_last_revision_in_branch(self):
243
        # within a branch only the last revision gets an
244
        # end of merge marker.
245
        self.assertSortAndIterate(
246
            {'A': ['B'],
247
             'B': [],
248
             },
249
            'A',
250
            [(0, 'A', 0, False),
251
             (1, 'B', 0, True)
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
252
             ],
253
            False
254
            )
255
        self.assertSortAndIterate(
256
            {'A': ['B'],
257
             'B': [],
258
             },
259
            'A',
260
            [(0, 'A', 0, (2,), False),
261
             (1, 'B', 0, (1,), True)
262
             ],
263
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
264
            )
265
266
    def test_end_of_merge_multiple_revisions_merged_at_once(self):
267
        # when multiple branches are merged at once, both of their
268
        # branch-endpoints should be listed as end-of-merge.
269
        # Also, the order of the multiple merges should be 
270
        # left-right shown top to bottom.
271
        # * means end of merge
272
        # A 0    [H, B, E] 
273
        # B  1   [D, C]
274
        # C   2  [D]       *
275
        # D  1   [H]       *
276
        # E  1   [G, F]
277
        # F   2  [G]       *
278
        # G  1   [H]       *
279
        # H 0    []        *
280
        self.assertSortAndIterate(
281
            {'A': ['H', 'B', 'E'],
282
             'B': ['D', 'C'],
283
             'C': ['D'],
284
             'D': ['H'],
285
             'E': ['G', 'F'],
286
             'F': ['G'],
287
             'G': ['H'],
288
             'H': [],
289
             },
290
            'A',
291
            [(0, 'A', 0, False),
292
             (1, 'B', 1, False),
293
             (2, 'C', 2, True),
294
             (3, 'D', 1, True),
295
             (4, 'E', 1, False),
296
             (5, 'F', 2, True),
297
             (6, 'G', 1, True),
298
             (7, 'H', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
299
             ],
300
            False
301
            )
302
        self.assertSortAndIterate(
303
            {'A': ['H', 'B', 'E'],
304
             'B': ['D', 'C'],
305
             'C': ['D'],
306
             'D': ['H'],
307
             'E': ['G', 'F'],
308
             'F': ['G'],
309
             'G': ['H'],
310
             'H': [],
311
             },
312
            'A',
313
            [(0, 'A', 0, (2,), False),
314
             (1, 'B', 1, (1,2,2), False),
315
             (2, 'C', 2, (1,2,1,1,1), True),
316
             (3, 'D', 1, (1,2,1), True),
317
             (4, 'E', 1, (1,1,2), False),
318
             (5, 'F', 2, (1,1,1,1,1), True),
319
             (6, 'G', 1, (1,1,1), True),
320
             (7, 'H', 0, (1,), True),
321
             ],
322
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
323
            )
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
324
325
    def test_mainline_revs_partial(self):
326
        # when a mainline_revisions list is passed this must
327
        # override the graphs idea of mainline, and must also
328
        # truncate the output to the specified range, if needed.
329
        # so we test both at once: a mainline_revisions list that
330
        # disagrees with the graph about which revs are 'mainline'
331
        # and also truncates the output.
332
        # graph:
333
        # A 0 [E, B]
334
        # B 1 [D, C]
335
        # C 2 [D]
336
        # D 1 [E]
337
        # E 0
338
        # with a mainline of NONE,E,A (the inferred one) this will show the merge
339
        # depths above.
340
        # with a overriden mainline of NONE,E,D,B,A it should show:
341
        # A 0
342
        # B 0
343
        # C 1
344
        # D 0
345
        # E 0
346
        # and thus when truncated to D,B,A it should show
347
        # A 0
348
        # B 0
349
        # C 1 
350
        # because C is brought in by B in this view and D
351
        # is the terminating revision id
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
352
        # this should also preserve revision numbers: C should still be 2.1.1
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
353
        self.assertSortAndIterate(
354
            {'A': ['E', 'B'],
355
             'B': ['D', 'C'],
356
             'C': ['D'],
357
             'D': ['E'],
358
             'E': []
359
             },
360
            'A',
361
            [(0, 'A', 0, False),
362
             (1, 'B', 0, False),
363
             (2, 'C', 1, True),
364
             ],
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
365
            False,
366
            mainline_revisions=['D', 'B', 'A']
367
            )
368
        self.assertSortAndIterate(
369
            {'A': ['E', 'B'],
370
             'B': ['D', 'C'],
371
             'C': ['D'],
372
             'D': ['E'],
373
             'E': []
374
             },
375
            'A',
376
            [(0, 'A', 0, (4,), False),
377
             (1, 'B', 0, (3,), False),
378
             (2, 'C', 1, (2,1,1), True),
379
             ],
380
            True,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
381
            mainline_revisions=['D', 'B', 'A']
382
            )
383
384
    def test_mainline_revs_with_none(self):
385
        # a simple test to ensure that a mainline_revs
386
        # list which goes all the way to None works
387
        self.assertSortAndIterate(
388
            {'A': [],
389
             },
390
            'A',
391
            [(0, 'A', 0, True),
392
             ],
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
393
            False,
394
            mainline_revisions=[None, 'A']
395
            )
396
        self.assertSortAndIterate(
397
            {'A': [],
398
             },
399
            'A',
400
            [(0, 'A', 0, (1,), True),
401
             ],
402
            True,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
403
            mainline_revisions=[None, 'A']
404
            )
405
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
406
    def test_parallel_root_sequence_numbers_increase_with_merges(self):
407
        """When there are parallel roots, check their revnos."""
408
        self.assertSortAndIterate(
409
            {'A': [],
410
             'B': [],
411
             'C': ['A', 'B']}.items(),
412
            'C',
413
            [(0, 'C', 0, (2,), False),
414
             (1, 'B', 1, (0,1,1), True),
415
             (2, 'A', 0, (1,), True),
416
             ],
417
            True
418
            )
419
        
420
    def test_revnos_are_globally_assigned(self):
421
        """revnos are assigned according to the revision they derive from."""
422
        # in this test we setup a number of branches that all derive from 
423
        # the first revision, and then merge them one at a time, which 
424
        # should give the revisions as they merge numbers still deriving from
425
        # the revision were based on.
426
        # merge 3: J: ['G', 'I']
427
        # branch 3:
428
        #  I: ['H']
429
        #  H: ['A']
430
        # merge 2: G: ['D', 'F']
431
        # branch 2:
432
        #  F: ['E']
433
        #  E: ['A']
434
        # merge 1: D: ['A', 'C']
435
        # branch 1:
436
        #  C: ['B']
437
        #  B: ['A']
438
        # root: A: []
439
        self.assertSortAndIterate(
440
            {'J': ['G', 'I'],
441
             'I': ['H',],
442
             'H': ['A'],
443
             'G': ['D', 'F'],
444
             'F': ['E'],
445
             'E': ['A'],
446
             'D': ['A', 'C'],
447
             'C': ['B'],
448
             'B': ['A'],
449
             'A': [],
450
             }.items(),
451
            'J',
452
            [(0, 'J', 0, (4,), False),
453
             (1, 'I', 1, (1,3,2), False),
454
             (2, 'H', 1, (1,3,1), True),
455
             (3, 'G', 0, (3,), False),
456
             (4, 'F', 1, (1,2,2), False),
457
             (5, 'E', 1, (1,2,1), True),
458
             (6, 'D', 0, (2,), False),
459
             (7, 'C', 1, (1,1,2), False),
460
             (8, 'B', 1, (1,1,1), True),
461
             (9, 'A', 0, (1,), True),
462
             ],
463
            True
464
            )