~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_graph_walker.py

  • Committer: Aaron Bentley
  • Date: 2007-05-25 03:17:26 UTC
  • mto: This revision was merged to the branch mainline in revision 2534.
  • Revision ID: aaron.bentley@utoronto.ca-20070525031726-tey81wph8x3hg4po
Start work on GraphWalker

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from bzrlib.revision import NULL_REVISION
 
2
from bzrlib.tests import TestCaseWithMemoryTransport
 
3
 
 
4
class TestGraphWalker(TestCaseWithMemoryTransport):
 
5
 
 
6
    def test_distance_from_origin(self):
 
7
        tree = self.make_branch_and_memory_tree('.')
 
8
        tree.lock_write()
 
9
        tree.add('.')
 
10
        self.build_ancestry(tree, {'rev1': [NULL_REVISION], 'rev2a': ['rev1'],
 
11
                                   'rev2b': ['rev1'], 'rev3':['rev2a'],
 
12
                                   'rev4': ['rev3', 'rev2b']})
 
13
        tree.unlock()
 
14
        graph_walker = tree.branch.repository.get_graph_walker()
 
15
        self.assertEqual([1, 0, 2, 4],
 
16
                         graph_walker.distance_from_origin(['rev1', 'null:',
 
17
                         'rev2b', 'rev4']))
 
18
 
 
19
    def build_ancestry(self, tree, ancestors):
 
20
        pending = [NULL_REVISION]
 
21
        descendants = {}
 
22
        for descendant, parents in ancestors.iteritems():
 
23
            for parent in parents:
 
24
                descendants.setdefault(parent, []).append(descendant)
 
25
        while len(pending) > 0:
 
26
            cur_node = pending.pop()
 
27
            for descendant in descendants.get(cur_node, []):
 
28
                parents = [p for p in ancestors[descendant] if p is not
 
29
                           NULL_REVISION]
 
30
                if len([p for p in parents if not
 
31
                    tree.branch.repository.has_revision(p)]) > 0:
 
32
                    continue
 
33
                tree.set_parent_ids(parents)
 
34
                tree.branch.set_last_revision_info(
 
35
                    len(tree.branch._lefthand_history(cur_node)),
 
36
                    cur_node)
 
37
                tree.commit(descendant, rev_id=descendant)
 
38
                pending.append(descendant)