1
# Copyright (C) 2009 Canonical Ltd
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.
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.
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
17
"""Tests for the python and pyrex extensions of groupcompress"""
25
from bzrlib.tests import test_graph
26
from bzrlib.revision import NULL_REVISION
29
def load_tests(standard_tests, module, loader):
30
"""Parameterize tests for all versions of groupcompress."""
32
('python', {'module': _known_graph_py, 'do_cache': True}),
33
('python-nocache', {'module': _known_graph_py, 'do_cache': False}),
35
suite = loader.suiteClass()
36
if CompiledKnownGraphFeature.available():
37
from bzrlib import _known_graph_pyx
38
scenarios.append(('C', {'module': _known_graph_pyx, 'do_cache': True}))
39
scenarios.append(('C-nocache',
40
{'module': _known_graph_pyx, 'do_cache': False}))
42
# the compiled module isn't available, so we add a failing test
43
class FailWithoutFeature(tests.TestCase):
45
self.requireFeature(CompiledKnownGraphFeature)
46
suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
47
result = tests.multiply_tests(standard_tests, scenarios, suite)
51
class _CompiledKnownGraphFeature(tests.Feature):
55
import bzrlib._known_graph_pyx
60
def feature_name(self):
61
return 'bzrlib._known_graph_pyx'
63
CompiledKnownGraphFeature = _CompiledKnownGraphFeature()
66
class TestKnownGraph(tests.TestCase):
68
module = None # Set by load_tests
69
do_cache = None # Set by load_tests
71
def make_known_graph(self, ancestry):
72
return self.module.KnownGraph(ancestry, do_cache=self.do_cache)
74
def assertGDFO(self, graph, rev, gdfo):
75
node = graph._nodes[rev]
76
self.assertEqual(gdfo, node.gdfo)
78
def test_children_ancestry1(self):
79
graph = self.make_known_graph(test_graph.ancestry_1)
80
self.assertEqual(['rev1'], graph._nodes[NULL_REVISION].child_keys)
81
self.assertEqual(['rev2a', 'rev2b'],
82
sorted(graph._nodes['rev1'].child_keys))
83
self.assertEqual(['rev3'], sorted(graph._nodes['rev2a'].child_keys))
84
self.assertEqual(['rev4'], sorted(graph._nodes['rev3'].child_keys))
85
self.assertEqual(['rev4'], sorted(graph._nodes['rev2b'].child_keys))
87
def test_gdfo_ancestry_1(self):
88
graph = self.make_known_graph(test_graph.ancestry_1)
89
self.assertGDFO(graph, 'rev1', 2)
90
self.assertGDFO(graph, 'rev2b', 3)
91
self.assertGDFO(graph, 'rev2a', 3)
92
self.assertGDFO(graph, 'rev3', 4)
93
self.assertGDFO(graph, 'rev4', 5)
95
def test_gdfo_feature_branch(self):
96
graph = self.make_known_graph(test_graph.feature_branch)
97
self.assertGDFO(graph, 'rev1', 2)
98
self.assertGDFO(graph, 'rev2b', 3)
99
self.assertGDFO(graph, 'rev3b', 4)
101
def test_gdfo_extended_history_shortcut(self):
102
graph = self.make_known_graph(test_graph.extended_history_shortcut)
103
self.assertGDFO(graph, 'a', 2)
104
self.assertGDFO(graph, 'b', 3)
105
self.assertGDFO(graph, 'c', 4)
106
self.assertGDFO(graph, 'd', 5)
107
self.assertGDFO(graph, 'e', 6)
108
self.assertGDFO(graph, 'f', 6)
110
def test_gdfo_with_ghost(self):
111
graph = self.make_known_graph(test_graph.with_ghost)
112
self.assertGDFO(graph, 'f', 2)
113
self.assertGDFO(graph, 'e', 3)
114
self.assertGDFO(graph, 'g', 1)
115
self.assertGDFO(graph, 'b', 4)
116
self.assertGDFO(graph, 'd', 4)
117
self.assertGDFO(graph, 'a', 5)
118
self.assertGDFO(graph, 'c', 5)
120
def test_heads_null(self):
121
graph = self.make_known_graph(test_graph.ancestry_1)
122
self.assertEqual(set(['null:']), graph.heads(['null:']))
123
self.assertEqual(set(['rev1']), graph.heads(['null:', 'rev1']))
124
self.assertEqual(set(['rev1']), graph.heads(['rev1', 'null:']))
125
self.assertEqual(set(['rev1']), graph.heads(set(['rev1', 'null:'])))
126
self.assertEqual(set(['rev1']), graph.heads(('rev1', 'null:')))
128
def test_heads_one(self):
129
# A single node will always be a head
130
graph = self.make_known_graph(test_graph.ancestry_1)
131
self.assertEqual(set(['null:']), graph.heads(['null:']))
132
self.assertEqual(set(['rev1']), graph.heads(['rev1']))
133
self.assertEqual(set(['rev2a']), graph.heads(['rev2a']))
134
self.assertEqual(set(['rev2b']), graph.heads(['rev2b']))
135
self.assertEqual(set(['rev3']), graph.heads(['rev3']))
136
self.assertEqual(set(['rev4']), graph.heads(['rev4']))
138
def test_heads_single(self):
139
graph = self.make_known_graph(test_graph.ancestry_1)
140
self.assertEqual(set(['rev4']), graph.heads(['null:', 'rev4']))
141
self.assertEqual(set(['rev2a']), graph.heads(['rev1', 'rev2a']))
142
self.assertEqual(set(['rev2b']), graph.heads(['rev1', 'rev2b']))
143
self.assertEqual(set(['rev3']), graph.heads(['rev1', 'rev3']))
144
self.assertEqual(set(['rev3']), graph.heads(['rev3', 'rev2a']))
145
self.assertEqual(set(['rev4']), graph.heads(['rev1', 'rev4']))
146
self.assertEqual(set(['rev4']), graph.heads(['rev2a', 'rev4']))
147
self.assertEqual(set(['rev4']), graph.heads(['rev2b', 'rev4']))
148
self.assertEqual(set(['rev4']), graph.heads(['rev3', 'rev4']))
150
def test_heads_two_heads(self):
151
graph = self.make_known_graph(test_graph.ancestry_1)
152
self.assertEqual(set(['rev2a', 'rev2b']),
153
graph.heads(['rev2a', 'rev2b']))
154
self.assertEqual(set(['rev3', 'rev2b']),
155
graph.heads(['rev3', 'rev2b']))
157
def test_heads_criss_cross(self):
158
graph = self.make_known_graph(test_graph.criss_cross)
159
self.assertEqual(set(['rev2a']),
160
graph.heads(['rev2a', 'rev1']))
161
self.assertEqual(set(['rev2b']),
162
graph.heads(['rev2b', 'rev1']))
163
self.assertEqual(set(['rev3a']),
164
graph.heads(['rev3a', 'rev1']))
165
self.assertEqual(set(['rev3b']),
166
graph.heads(['rev3b', 'rev1']))
167
self.assertEqual(set(['rev2a', 'rev2b']),
168
graph.heads(['rev2a', 'rev2b']))
169
self.assertEqual(set(['rev3a']),
170
graph.heads(['rev3a', 'rev2a']))
171
self.assertEqual(set(['rev3a']),
172
graph.heads(['rev3a', 'rev2b']))
173
self.assertEqual(set(['rev3a']),
174
graph.heads(['rev3a', 'rev2a', 'rev2b']))
175
self.assertEqual(set(['rev3b']),
176
graph.heads(['rev3b', 'rev2a']))
177
self.assertEqual(set(['rev3b']),
178
graph.heads(['rev3b', 'rev2b']))
179
self.assertEqual(set(['rev3b']),
180
graph.heads(['rev3b', 'rev2a', 'rev2b']))
181
self.assertEqual(set(['rev3a', 'rev3b']),
182
graph.heads(['rev3a', 'rev3b']))
183
self.assertEqual(set(['rev3a', 'rev3b']),
184
graph.heads(['rev3a', 'rev3b', 'rev2a', 'rev2b']))
186
def test_heads_shortcut(self):
187
graph = self.make_known_graph(test_graph.history_shortcut)
188
self.assertEqual(set(['rev2a', 'rev2b', 'rev2c']),
189
graph.heads(['rev2a', 'rev2b', 'rev2c']))
190
self.assertEqual(set(['rev3a', 'rev3b']),
191
graph.heads(['rev3a', 'rev3b']))
192
self.assertEqual(set(['rev3a', 'rev3b']),
193
graph.heads(['rev2a', 'rev3a', 'rev3b']))
194
self.assertEqual(set(['rev2a', 'rev3b']),
195
graph.heads(['rev2a', 'rev3b']))
196
self.assertEqual(set(['rev2c', 'rev3a']),
197
graph.heads(['rev2c', 'rev3a']))
199
def test_heads_linear(self):
200
graph = self.make_known_graph(test_graph.racing_shortcuts)
201
self.assertEqual(set(['w']), graph.heads(['w', 's']))
202
self.assertEqual(set(['z']), graph.heads(['w', 's', 'z']))
203
self.assertEqual(set(['w', 'q']), graph.heads(['w', 's', 'q']))
204
self.assertEqual(set(['z']), graph.heads(['s', 'z']))
206
def test_heads_with_ghost(self):
207
graph = self.make_known_graph(test_graph.with_ghost)
208
self.assertEqual(set(['e', 'g']), graph.heads(['e', 'g']))
209
self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c']))
210
self.assertEqual(set(['a', 'g']), graph.heads(['a', 'g']))
211
self.assertEqual(set(['f', 'g']), graph.heads(['f', 'g']))
212
self.assertEqual(set(['c']), graph.heads(['c', 'g']))
213
self.assertEqual(set(['c']), graph.heads(['c', 'b', 'd', 'g']))
214
self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'e', 'g']))
215
self.assertEqual(set(['a', 'c']), graph.heads(['a', 'c', 'f']))