1
from bzrlib.selftest import TestCase
2
from bzrlib.graph import shortest_path
3
from bzrlib.graph import farthest_nodes_ab
4
from bzrlib.graph import farthest_node
6
class TestBase(TestCase):
7
def edge_add(self, *args):
8
for start, end in zip(args[:-1], args[1:]):
9
if start not in self.graph:
10
self.graph[start] = {}
11
if end not in self.graph:
13
self.graph[start][end] = 1
18
self.edge_add('A', 'B', 'C')
19
self.edge_add('A', 'D', 'E', 'B', 'G')
20
self.edge_add('E', 'F')
22
def test_shortest(self):
23
"""Ensure we find the longest path to A"""
24
assert 'B' in self.graph['A']
25
self.assertEqual(shortest_path(self.graph, 'A', 'F'),
27
self.assertEqual(shortest_path(self.graph, 'A', 'G'),
29
def test_farthest(self):
30
self.assertEqual(farthest_nodes_ab(self.graph, 'A')[0], 'G')
31
self.assertEqual(farthest_nodes_ab(self.graph, 'F')[0], 'F')
33
self.edge_add('A', 'B', 'C', 'D')
34
self.edge_add('A', 'E', 'F', 'C')
35
self.edge_add('A', 'G', 'H', 'I', 'B')
36
self.edge_add('A', 'J', 'K', 'L', 'M', 'N')
37
self.edge_add('O', 'N')
38
descendants = {'A':set()}
39
for node in self.graph:
40
for ancestor in self.graph[node]:
41
if ancestor not in descendants:
42
descendants[ancestor] = set()
43
descendants[ancestor].add(node)
44
nodes = farthest_node(self.graph, descendants, 'A')
45
self.assertEqual(nodes[0], 'D')
46
assert nodes[1] in ('N', 'C')
47
assert nodes[2] in ('N', 'C')
48
assert nodes[3] in ('B', 'M')
49
assert nodes[4] in ('B', 'M')