~bzr-pqm/bzr/bzr.dev

974.1.57 by aaron.bentley at utoronto
Started work on djkstra longest-path algorithm
1
from bzrlib.selftest import TestCase
974.1.87 by Aaron Bentley
Refactored and documented graph stuff
2
from bzrlib.graph import node_distances, nodes_by_distance
974.1.57 by aaron.bentley at utoronto
Started work on djkstra longest-path algorithm
3
4
class TestBase(TestCase):
5
    def edge_add(self, *args):
6
        for start, end in zip(args[:-1], args[1:]):
7
            if start not in self.graph:
8
                self.graph[start] = {}
9
            if end not in self.graph:
10
                self.graph[end] = {}
11
            self.graph[start][end] = 1
12
13
    def setUp(self):
14
        TestCase.setUp(self)
15
        self.graph = {}
974.1.58 by aaron.bentley at utoronto
Implemented yet another farthest_node algorithm
16
        self.edge_add('A', 'B', 'C', 'D')
17
        self.edge_add('A', 'E', 'F', 'C')
18
        self.edge_add('A', 'G', 'H', 'I', 'B')
19
        self.edge_add('A', 'J', 'K', 'L', 'M', 'N')
974.1.64 by Aaron Bentley
Handled ancestors that are missing when finding a base
20
        self.edge_add('O', 'N')
974.1.87 by Aaron Bentley
Refactored and documented graph stuff
21
22
    def node_descendants(self):
974.1.64 by Aaron Bentley
Handled ancestors that are missing when finding a base
23
        descendants = {'A':set()}
974.1.59 by aaron.bentley at utoronto
Created yet another longest-patch-picker
24
        for node in self.graph:
25
            for ancestor in self.graph[node]:
26
                if ancestor not in descendants:
27
                    descendants[ancestor] = set()
28
                descendants[ancestor].add(node)
974.1.87 by Aaron Bentley
Refactored and documented graph stuff
29
        return descendants
30
    
31
    def test_distances(self):
32
        descendants = self.node_descendants()
33
        distances = node_distances(self.graph, descendants, 'A')
34
        nodes = nodes_by_distance(distances)
974.1.59 by aaron.bentley at utoronto
Created yet another longest-patch-picker
35
        self.assertEqual(nodes[0], 'D')
36
        assert nodes[1] in ('N', 'C')
37
        assert nodes[2] in ('N', 'C')
38
        assert nodes[3] in ('B', 'M')
39
        assert nodes[4] in ('B', 'M')
40
974.1.58 by aaron.bentley at utoronto
Implemented yet another farthest_node algorithm
41