~abentley/bzrtools/bzrtools.dev

756.1.1 by Jelmer Vernooij
Merge in bzrlib.deprecated_graph functions still used by bzrtools.
1
# Copyright (C) 2005, 2006 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
from bzrlib.tests import TestCase
18
from bzrlib.plugins.bzrtools.graph import (
19
    node_distances,
20
    nodes_by_distance,
21
    )
22
23
24
class TestBase(TestCase):
25
26
    def edge_add(self, *args):
27
        for start, end in zip(args[:-1], args[1:]):
28
            if start not in self.graph:
29
                self.graph[start] = {}
30
            if end not in self.graph:
31
                self.graph[end] = {}
32
            self.graph[start][end] = 1
33
34
    def setUp(self):
35
        TestCase.setUp(self)
36
        self.graph = {}
37
        self.edge_add('A', 'B', 'C', 'D')
38
        self.edge_add('A', 'E', 'F', 'C')
39
        self.edge_add('A', 'G', 'H', 'I', 'B')
40
        self.edge_add('A', 'J', 'K', 'L', 'M', 'N')
41
        self.edge_add('O', 'N')
42
43
    def node_descendants(self):
44
        descendants = {'A':set()}
45
        for node in self.graph:
46
            for ancestor in self.graph[node]:
47
                if ancestor not in descendants:
48
                    descendants[ancestor] = set()
49
                descendants[ancestor].add(node)
50
        return descendants
51
52
    def test_distances(self):
53
        descendants = self.node_descendants()
54
        distances = node_distances(self.graph, descendants, 'A')
55
        nodes = nodes_by_distance(distances)
56
        self.assertEqual(nodes[0], 'D')
57
        self.assert_(nodes[1] in ('N', 'C'))
58
        self.assert_(nodes[2] in ('N', 'C'))
59
        self.assert_(nodes[3] in ('B', 'M'))
60
        self.assert_(nodes[4] in ('B', 'M'))
61
62
        #Ensure we don't shortcut through B when there's only a difference of
63
        # 1 in distance
64
        self.graph = {}
65
        self.edge_add('A', 'B', 'C')
66
        self.edge_add('A', 'D', 'E', 'C')
67
        descendants = self.node_descendants()
68
        distances = node_distances(self.graph, descendants, 'A')
69
        self.assertEqual(distances['C'], 3)