~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__known_graph.py

  • Committer: Robert Collins
  • Date: 2009-07-07 04:32:13 UTC
  • mto: This revision was merged to the branch mainline in revision 4524.
  • Revision ID: robertc@robertcollins.net-20090707043213-4hjjhgr40iq7gk2d
More informative assertions in xml serialisation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2009 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
"""Tests for the python and pyrex extensions of groupcompress"""
 
18
 
 
19
from bzrlib import (
 
20
    errors,
 
21
    graph as _mod_graph,
 
22
    _known_graph_py,
 
23
    tests,
 
24
    )
 
25
from bzrlib.tests import test_graph
 
26
from bzrlib.revision import NULL_REVISION
 
27
 
 
28
 
 
29
def load_tests(standard_tests, module, loader):
 
30
    """Parameterize tests for all versions of groupcompress."""
 
31
    scenarios = [
 
32
        ('python', {'module': _known_graph_py, 'do_cache': True}),
 
33
        ('python-nocache', {'module': _known_graph_py, 'do_cache': False}),
 
34
    ]
 
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}))
 
41
    else:
 
42
        # the compiled module isn't available, so we add a failing test
 
43
        class FailWithoutFeature(tests.TestCase):
 
44
            def test_fail(self):
 
45
                self.requireFeature(CompiledKnownGraphFeature)
 
46
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
47
    result = tests.multiply_tests(standard_tests, scenarios, suite)
 
48
    return result
 
49
 
 
50
 
 
51
class _CompiledKnownGraphFeature(tests.Feature):
 
52
 
 
53
    def _probe(self):
 
54
        try:
 
55
            import bzrlib._known_graph_pyx
 
56
        except ImportError:
 
57
            return False
 
58
        return True
 
59
 
 
60
    def feature_name(self):
 
61
        return 'bzrlib._known_graph_pyx'
 
62
 
 
63
CompiledKnownGraphFeature = _CompiledKnownGraphFeature()
 
64
 
 
65
 
 
66
class TestKnownGraph(tests.TestCase):
 
67
 
 
68
    module = None # Set by load_tests
 
69
    do_cache = None # Set by load_tests
 
70
 
 
71
    def make_known_graph(self, ancestry):
 
72
        return self.module.KnownGraph(ancestry, do_cache=self.do_cache)
 
73
 
 
74
    def assertGDFO(self, graph, rev, gdfo):
 
75
        node = graph._nodes[rev]
 
76
        self.assertEqual(gdfo, node.gdfo)
 
77
 
 
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))
 
86
 
 
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)
 
94
 
 
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)
 
100
 
 
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)
 
109
 
 
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)
 
119
 
 
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:')))
 
127
 
 
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']))
 
137
 
 
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']))
 
149
 
 
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']))
 
156
 
 
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']))
 
185
 
 
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']))
 
198
 
 
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']))
 
205
 
 
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']))