~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
88
89
class MergeSortTests(TestCase):
90
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
91
    def assertSortAndIterate(self, graph, branch_tip, result_list,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
92
            generate_revno, mainline_revisions=None):
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
93
        """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.
94
        self.assertEquals(result_list,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
95
            merge_sort(graph, branch_tip, mainline_revisions=mainline_revisions,
96
                generate_revno=generate_revno))
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
97
        self.assertEqual(result_list,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
98
            list(MergeSorter(
99
                graph,
100
                branch_tip,
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
101
                mainline_revisions=mainline_revisions,
102
                generate_revno=generate_revno,
103
                ).iter_topo_order()))
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
104
105
    def test_merge_sort_empty(self):
106
        # sorting of an emptygraph does not error
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
107
        self.assertSortAndIterate({}, None, [], False)
108
        self.assertSortAndIterate({}, None, [], True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
109
110
    def test_merge_sort_not_empty_no_tip(self):
111
        # merge sorting of a branch starting with None should result
112
        # 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
113
        self.assertSortAndIterate({0: []}.items(), None, [], False)
114
        self.assertSortAndIterate({0: []}.items(), None, [], True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
115
116
    def test_merge_sort_one_revision(self):
117
        # sorting with one revision as the tip returns the correct fields:
118
        # sequence - 0, revision id, merge depth - 0, end_of_merge
119
        self.assertSortAndIterate({'id': []}.items(),
120
                                  'id',
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
121
                                  [(0, 'id', 0, True)],
122
                                  False)
123
        self.assertSortAndIterate({'id': []}.items(),
124
                                  'id',
125
                                  [(0, 'id', 0, (1,), True)],
126
                                  True)
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
127
    
128
    def test_sequence_numbers_increase_no_merges(self):
129
        # emit a few revisions with no merges to check the sequence
130
        # numbering works in trivial cases
131
        self.assertSortAndIterate(
132
            {'A': [],
133
             'B': ['A'],
134
             'C': ['B']}.items(),
135
            'C',
136
            [(0, 'C', 0, False),
137
             (1, 'B', 0, False),
138
             (2, 'A', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
139
             ],
140
            False
141
            )
142
        self.assertSortAndIterate(
143
            {'A': [],
144
             'B': ['A'],
145
             'C': ['B']}.items(),
146
            'C',
147
            [(0, 'C', 0, (3,), False),
148
             (1, 'B', 0, (2,), False),
149
             (2, 'A', 0, (1,), True),
150
             ],
151
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
152
            )
153
154
    def test_sequence_numbers_increase_with_merges(self):
155
        # test that sequence numbers increase across merges
156
        self.assertSortAndIterate(
157
            {'A': [],
158
             'B': ['A'],
159
             'C': ['A', 'B']}.items(),
160
            'C',
161
            [(0, 'C', 0, False),
162
             (1, 'B', 1, True),
163
             (2, 'A', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
164
             ],
165
            False
166
            )
167
        self.assertSortAndIterate(
168
            {'A': [],
169
             'B': ['A'],
170
             'C': ['A', 'B']}.items(),
171
            'C',
172
            [(0, 'C', 0, (2,), False),
173
             (1, 'B', 1, (1,1,1), True),
174
             (2, 'A', 0, (1,), True),
175
             ],
176
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
177
            )
178
        
179
    def test_merge_depth_with_nested_merges(self):
180
        # the merge depth marker should reflect the depth of the revision
181
        # in terms of merges out from the mainline
182
        # revid, depth, parents:
183
        #  A 0   [D, B]   
184
        #  B  1  [C, F]   
185
        #  C  1  [H] 
186
        #  D 0   [H, E]
187
        #  E  1  [G, F]
188
        #  F   2 [G]
189
        #  G  1  [H]
190
        #  H 0
191
        self.assertSortAndIterate(
192
            {'A': ['D', 'B'],
193
             'B': ['C', 'F'],
194
             'C': ['H'],
195
             'D': ['H', 'E'],
196
             'E': ['G', 'F'],
197
             'F': ['G'],
198
             'G': ['H'],
199
             'H': []
200
             }.items(),
201
            'A',
202
            [(0, 'A', 0, False),
203
             (1, 'B', 1, False),
204
             (2, 'C', 1, True),
205
             (3, 'D', 0, False),
206
             (4, 'E', 1, False),
207
             (5, 'F', 2, True),
208
             (6, 'G', 1, True),
209
             (7, 'H', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
210
             ],
211
            False
212
            )
213
        self.assertSortAndIterate(
214
            {'A': ['D', 'B'],
215
             'B': ['C', 'F'],
216
             'C': ['H'],
217
             'D': ['H', 'E'],
218
             'E': ['G', 'F'],
219
             'F': ['G'],
220
             'G': ['H'],
221
             'H': []
222
             }.items(),
223
            'A',
224
            [(0, 'A', 0, (3,),  False),
225
             (1, 'B', 1, (1,2,2), False),
226
             (2, 'C', 1, (1,2,1), True),
227
             (3, 'D', 0, (2,), False),
228
             (4, 'E', 1, (1,1,2), False),
229
             (5, 'F', 2, (1,1,1,1,1), True),
230
             (6, 'G', 1, (1,1,1), True),
231
             (7, 'H', 0, (1,), True),
232
             ],
233
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
234
            )
235
 
236
    def test_end_of_merge_not_last_revision_in_branch(self):
237
        # within a branch only the last revision gets an
238
        # end of merge marker.
239
        self.assertSortAndIterate(
240
            {'A': ['B'],
241
             'B': [],
242
             },
243
            'A',
244
            [(0, 'A', 0, False),
245
             (1, 'B', 0, True)
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
246
             ],
247
            False
248
            )
249
        self.assertSortAndIterate(
250
            {'A': ['B'],
251
             'B': [],
252
             },
253
            'A',
254
            [(0, 'A', 0, (2,), False),
255
             (1, 'B', 0, (1,), True)
256
             ],
257
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
258
            )
259
260
    def test_end_of_merge_multiple_revisions_merged_at_once(self):
261
        # when multiple branches are merged at once, both of their
262
        # branch-endpoints should be listed as end-of-merge.
263
        # Also, the order of the multiple merges should be 
264
        # left-right shown top to bottom.
265
        # * means end of merge
266
        # A 0    [H, B, E] 
267
        # B  1   [D, C]
268
        # C   2  [D]       *
269
        # D  1   [H]       *
270
        # E  1   [G, F]
271
        # F   2  [G]       *
272
        # G  1   [H]       *
273
        # H 0    []        *
274
        self.assertSortAndIterate(
275
            {'A': ['H', 'B', 'E'],
276
             'B': ['D', 'C'],
277
             'C': ['D'],
278
             'D': ['H'],
279
             'E': ['G', 'F'],
280
             'F': ['G'],
281
             'G': ['H'],
282
             'H': [],
283
             },
284
            'A',
285
            [(0, 'A', 0, False),
286
             (1, 'B', 1, False),
287
             (2, 'C', 2, True),
288
             (3, 'D', 1, True),
289
             (4, 'E', 1, False),
290
             (5, 'F', 2, True),
291
             (6, 'G', 1, True),
292
             (7, 'H', 0, True),
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
293
             ],
294
            False
295
            )
296
        self.assertSortAndIterate(
297
            {'A': ['H', 'B', 'E'],
298
             'B': ['D', 'C'],
299
             'C': ['D'],
300
             'D': ['H'],
301
             'E': ['G', 'F'],
302
             'F': ['G'],
303
             'G': ['H'],
304
             'H': [],
305
             },
306
            'A',
307
            [(0, 'A', 0, (2,), False),
308
             (1, 'B', 1, (1,2,2), False),
309
             (2, 'C', 2, (1,2,1,1,1), True),
310
             (3, 'D', 1, (1,2,1), True),
311
             (4, 'E', 1, (1,1,2), False),
312
             (5, 'F', 2, (1,1,1,1,1), True),
313
             (6, 'G', 1, (1,1,1), True),
314
             (7, 'H', 0, (1,), True),
315
             ],
316
            True
1624.1.2 by Robert Collins
Add MergeSort facility to bzrlib.tsort.
317
            )
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
318
319
    def test_mainline_revs_partial(self):
320
        # when a mainline_revisions list is passed this must
321
        # override the graphs idea of mainline, and must also
322
        # truncate the output to the specified range, if needed.
323
        # so we test both at once: a mainline_revisions list that
324
        # disagrees with the graph about which revs are 'mainline'
325
        # and also truncates the output.
326
        # graph:
327
        # A 0 [E, B]
328
        # B 1 [D, C]
329
        # C 2 [D]
330
        # D 1 [E]
331
        # E 0
332
        # with a mainline of NONE,E,A (the inferred one) this will show the merge
333
        # depths above.
334
        # with a overriden mainline of NONE,E,D,B,A it should show:
335
        # A 0
336
        # B 0
337
        # C 1
338
        # D 0
339
        # E 0
340
        # and thus when truncated to D,B,A it should show
341
        # A 0
342
        # B 0
343
        # C 1 
344
        # because C is brought in by B in this view and D
345
        # is the terminating revision id
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
346
        # 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.
347
        self.assertSortAndIterate(
348
            {'A': ['E', 'B'],
349
             'B': ['D', 'C'],
350
             'C': ['D'],
351
             'D': ['E'],
352
             'E': []
353
             },
354
            'A',
355
            [(0, 'A', 0, False),
356
             (1, 'B', 0, False),
357
             (2, 'C', 1, True),
358
             ],
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
359
            False,
360
            mainline_revisions=['D', 'B', 'A']
361
            )
362
        self.assertSortAndIterate(
363
            {'A': ['E', 'B'],
364
             'B': ['D', 'C'],
365
             'C': ['D'],
366
             'D': ['E'],
367
             'E': []
368
             },
369
            'A',
370
            [(0, 'A', 0, (4,), False),
371
             (1, 'B', 0, (3,), False),
372
             (2, 'C', 1, (2,1,1), True),
373
             ],
374
            True,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
375
            mainline_revisions=['D', 'B', 'A']
376
            )
377
378
    def test_mainline_revs_with_none(self):
379
        # a simple test to ensure that a mainline_revs
380
        # list which goes all the way to None works
381
        self.assertSortAndIterate(
382
            {'A': [],
383
             },
384
            'A',
385
            [(0, 'A', 0, True),
386
             ],
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
387
            False,
388
            mainline_revisions=[None, 'A']
389
            )
390
        self.assertSortAndIterate(
391
            {'A': [],
392
             },
393
            'A',
394
            [(0, 'A', 0, (1,), True),
395
             ],
396
            True,
1624.1.3 by Robert Collins
Convert log to use the new tsort.merge_sort routine.
397
            mainline_revisions=[None, 'A']
398
            )
399
1988.4.1 by Robert Collins
bzrlib.tsort.merge_sorted now accepts 'generate_revnos'. This parameter
400
    def test_parallel_root_sequence_numbers_increase_with_merges(self):
401
        """When there are parallel roots, check their revnos."""
402
        self.assertSortAndIterate(
403
            {'A': [],
404
             'B': [],
405
             'C': ['A', 'B']}.items(),
406
            'C',
407
            [(0, 'C', 0, (2,), False),
408
             (1, 'B', 1, (0,1,1), True),
409
             (2, 'A', 0, (1,), True),
410
             ],
411
            True
412
            )
413
        
414
    def test_revnos_are_globally_assigned(self):
415
        """revnos are assigned according to the revision they derive from."""
416
        # in this test we setup a number of branches that all derive from 
417
        # the first revision, and then merge them one at a time, which 
418
        # should give the revisions as they merge numbers still deriving from
419
        # the revision were based on.
420
        # merge 3: J: ['G', 'I']
421
        # branch 3:
422
        #  I: ['H']
423
        #  H: ['A']
424
        # merge 2: G: ['D', 'F']
425
        # branch 2:
426
        #  F: ['E']
427
        #  E: ['A']
428
        # merge 1: D: ['A', 'C']
429
        # branch 1:
430
        #  C: ['B']
431
        #  B: ['A']
432
        # root: A: []
433
        self.assertSortAndIterate(
434
            {'J': ['G', 'I'],
435
             'I': ['H',],
436
             'H': ['A'],
437
             'G': ['D', 'F'],
438
             'F': ['E'],
439
             'E': ['A'],
440
             'D': ['A', 'C'],
441
             'C': ['B'],
442
             'B': ['A'],
443
             'A': [],
444
             }.items(),
445
            'J',
446
            [(0, 'J', 0, (4,), False),
447
             (1, 'I', 1, (1,3,2), False),
448
             (2, 'H', 1, (1,3,1), True),
449
             (3, 'G', 0, (3,), False),
450
             (4, 'F', 1, (1,2,2), False),
451
             (5, 'E', 1, (1,2,1), True),
452
             (6, 'D', 0, (2,), False),
453
             (7, 'C', 1, (1,1,2), False),
454
             (8, 'B', 1, (1,1,1), True),
455
             (9, 'A', 0, (1,), True),
456
             ],
457
            True
458
            )