~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__chk_map.py

  • Committer: John Arbash Meinel
  • Date: 2005-09-15 21:35:53 UTC
  • mfrom: (907.1.57)
  • mto: (1393.2.1)
  • mto: This revision was merged to the branch mainline in revision 1396.
  • Revision ID: john@arbash-meinel.com-20050915213552-a6c83a5ef1e20897
(broken) Transport work is merged in. Tests do not pass yet.

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 _chk_map_*."""
18
 
 
19
 
from bzrlib import (
20
 
    chk_map,
21
 
    tests,
22
 
    )
23
 
from bzrlib.static_tuple import StaticTuple
24
 
stuple = StaticTuple
25
 
 
26
 
 
27
 
def load_tests(standard_tests, module, loader):
28
 
    # parameterize all tests in this module
29
 
    suite = loader.suiteClass()
30
 
    import bzrlib._chk_map_py as py_module
31
 
    scenarios = [('python', {'module': py_module})]
32
 
    if compiled_chkmap_feature.available():
33
 
        scenarios.append(('C', {'module': compiled_chkmap_feature.module}))
34
 
    else:
35
 
        # the compiled module isn't available, so we add a failing test
36
 
        class FailWithoutFeature(tests.TestCase):
37
 
            def test_fail(self):
38
 
                self.requireFeature(compiled_chkmap_feature)
39
 
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
40
 
    tests.multiply_tests(standard_tests, scenarios, suite)
41
 
    return suite
42
 
 
43
 
 
44
 
compiled_chkmap_feature = tests.ModuleAvailableFeature('bzrlib._chk_map_pyx')
45
 
 
46
 
 
47
 
class TestSearchKeys(tests.TestCase):
48
 
 
49
 
    module = None # Filled in by test parameterization
50
 
 
51
 
    def assertSearchKey16(self, expected, key):
52
 
        self.assertEqual(expected, self.module._search_key_16(key))
53
 
 
54
 
    def assertSearchKey255(self, expected, key):
55
 
        actual = self.module._search_key_255(key)
56
 
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
57
 
 
58
 
    def test_simple_16(self):
59
 
        self.assertSearchKey16('8C736521', stuple('foo',))
60
 
        self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
61
 
        self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
62
 
        self.assertSearchKey16('ED82CD11', stuple('abcd',))
63
 
 
64
 
    def test_simple_255(self):
65
 
        self.assertSearchKey255('\x8cse!', stuple('foo',))
66
 
        self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
67
 
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
68
 
        # The standard mapping for these would include '\n', so it should be
69
 
        # mapped to '_'
70
 
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
71
 
 
72
 
    def test_255_does_not_include_newline(self):
73
 
        # When mapping via _search_key_255, we should never have the '\n'
74
 
        # character, but all other 255 values should be present
75
 
        chars_used = set()
76
 
        for char_in in range(256):
77
 
            search_key = self.module._search_key_255(stuple(chr(char_in),))
78
 
            chars_used.update(search_key)
79
 
        all_chars = set([chr(x) for x in range(256)])
80
 
        unused_chars = all_chars.symmetric_difference(chars_used)
81
 
        self.assertEqual(set('\n'), unused_chars)
82
 
 
83
 
 
84
 
class TestDeserialiseLeafNode(tests.TestCase):
85
 
 
86
 
    module = None
87
 
 
88
 
    def assertDeserialiseErrors(self, text):
89
 
        self.assertRaises((ValueError, IndexError),
90
 
            self.module._deserialise_leaf_node, text, 'not-a-real-sha')
91
 
 
92
 
    def test_raises_on_non_leaf(self):
93
 
        self.assertDeserialiseErrors('')
94
 
        self.assertDeserialiseErrors('short\n')
95
 
        self.assertDeserialiseErrors('chknotleaf:\n')
96
 
        self.assertDeserialiseErrors('chkleaf:x\n')
97
 
        self.assertDeserialiseErrors('chkleaf:\n')
98
 
        self.assertDeserialiseErrors('chkleaf:\nnotint\n')
99
 
        self.assertDeserialiseErrors('chkleaf:\n10\n')
100
 
        self.assertDeserialiseErrors('chkleaf:\n10\n256\n')
101
 
        self.assertDeserialiseErrors('chkleaf:\n10\n256\n10\n')
102
 
 
103
 
    def test_deserialise_empty(self):
104
 
        node = self.module._deserialise_leaf_node(
105
 
            "chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
106
 
        self.assertEqual(0, len(node))
107
 
        self.assertEqual(10, node.maximum_size)
108
 
        self.assertEqual(("sha1:1234",), node.key())
109
 
        self.assertIsInstance(node.key(), StaticTuple)
110
 
        self.assertIs(None, node._search_prefix)
111
 
        self.assertIs(None, node._common_serialised_prefix)
112
 
 
113
 
    def test_deserialise_items(self):
114
 
        node = self.module._deserialise_leaf_node(
115
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
116
 
            ("sha1:1234",))
117
 
        self.assertEqual(2, len(node))
118
 
        self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
119
 
            sorted(node.iteritems(None)))
120
 
 
121
 
    def test_deserialise_item_with_null_width_1(self):
122
 
        node = self.module._deserialise_leaf_node(
123
 
            "chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
124
 
            ("sha1:1234",))
125
 
        self.assertEqual(2, len(node))
126
 
        self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
127
 
            sorted(node.iteritems(None)))
128
 
 
129
 
    def test_deserialise_item_with_null_width_2(self):
130
 
        node = self.module._deserialise_leaf_node(
131
 
            "chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
132
 
            "quux\x00\x001\nblarh\n",
133
 
            ("sha1:1234",))
134
 
        self.assertEqual(2, len(node))
135
 
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
136
 
            sorted(node.iteritems(None)))
137
 
 
138
 
    def test_iteritems_selected_one_of_two_items(self):
139
 
        node = self.module._deserialise_leaf_node(
140
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
141
 
            ("sha1:1234",))
142
 
        self.assertEqual(2, len(node))
143
 
        self.assertEqual([(("quux",), "blarh")],
144
 
            sorted(node.iteritems(None, [("quux",), ("qaz",)])))
145
 
 
146
 
    def test_deserialise_item_with_common_prefix(self):
147
 
        node = self.module._deserialise_leaf_node(
148
 
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
149
 
            ("sha1:1234",))
150
 
        self.assertEqual(2, len(node))
151
 
        self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
152
 
            sorted(node.iteritems(None)))
153
 
        self.assertIs(chk_map._unknown, node._search_prefix)
154
 
        self.assertEqual('foo\x00', node._common_serialised_prefix)
155
 
 
156
 
    def test_deserialise_multi_line(self):
157
 
        node = self.module._deserialise_leaf_node(
158
 
            "chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
159
 
            ("sha1:1234",))
160
 
        self.assertEqual(2, len(node))
161
 
        self.assertEqual([(("foo", "1"), "bar\nbaz"),
162
 
                          (("foo", "2"), "blarh\n"),
163
 
                         ], sorted(node.iteritems(None)))
164
 
        self.assertIs(chk_map._unknown, node._search_prefix)
165
 
        self.assertEqual('foo\x00', node._common_serialised_prefix)
166
 
 
167
 
    def test_key_after_map(self):
168
 
        node = self.module._deserialise_leaf_node(
169
 
            "chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
170
 
        node.map(None, ("foo bar",), "baz quux")
171
 
        self.assertEqual(None, node.key())
172
 
 
173
 
    def test_key_after_unmap(self):
174
 
        node = self.module._deserialise_leaf_node(
175
 
            "chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
176
 
            ("sha1:1234",))
177
 
        node.unmap(None, ("foo bar",))
178
 
        self.assertEqual(None, node.key())
179
 
 
180
 
 
181
 
class TestDeserialiseInternalNode(tests.TestCase):
182
 
 
183
 
    module = None
184
 
 
185
 
    def assertDeserialiseErrors(self, text):
186
 
        self.assertRaises((ValueError, IndexError),
187
 
            self.module._deserialise_internal_node, text,
188
 
                stuple('not-a-real-sha',))
189
 
 
190
 
    def test_raises_on_non_internal(self):
191
 
        self.assertDeserialiseErrors('')
192
 
        self.assertDeserialiseErrors('short\n')
193
 
        self.assertDeserialiseErrors('chknotnode:\n')
194
 
        self.assertDeserialiseErrors('chknode:x\n')
195
 
        self.assertDeserialiseErrors('chknode:\n')
196
 
        self.assertDeserialiseErrors('chknode:\nnotint\n')
197
 
        self.assertDeserialiseErrors('chknode:\n10\n')
198
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n')
199
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n10\n')
200
 
        # no trailing newline
201
 
        self.assertDeserialiseErrors('chknode:\n10\n256\n0\n1\nfo')
202
 
 
203
 
    def test_deserialise_one(self):
204
 
        node = self.module._deserialise_internal_node(
205
 
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
206
 
        self.assertIsInstance(node, chk_map.InternalNode)
207
 
        self.assertEqual(1, len(node))
208
 
        self.assertEqual(10, node.maximum_size)
209
 
        self.assertEqual(("sha1:1234",), node.key())
210
 
        self.assertEqual('', node._search_prefix)
211
 
        self.assertEqual({'a': ('sha1:abcd',)}, node._items)
212
 
 
213
 
    def test_deserialise_with_prefix(self):
214
 
        node = self.module._deserialise_internal_node(
215
 
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
216
 
        self.assertIsInstance(node, chk_map.InternalNode)
217
 
        self.assertEqual(1, len(node))
218
 
        self.assertEqual(10, node.maximum_size)
219
 
        self.assertEqual(("sha1:1234",), node.key())
220
 
        self.assertEqual('pref', node._search_prefix)
221
 
        self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
222
 
 
223
 
        node = self.module._deserialise_internal_node(
224
 
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
225
 
        self.assertIsInstance(node, chk_map.InternalNode)
226
 
        self.assertEqual(1, len(node))
227
 
        self.assertEqual(10, node.maximum_size)
228
 
        self.assertEqual(("sha1:1234",), node.key())
229
 
        self.assertEqual('pref', node._search_prefix)
230
 
        self.assertEqual({'pref': ('sha1:abcd',)}, node._items)
231
 
 
232
 
    def test_deserialise_pref_with_null(self):
233
 
        node = self.module._deserialise_internal_node(
234
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
235
 
            stuple('sha1:1234',))
236
 
        self.assertIsInstance(node, chk_map.InternalNode)
237
 
        self.assertEqual(1, len(node))
238
 
        self.assertEqual(10, node.maximum_size)
239
 
        self.assertEqual(("sha1:1234",), node.key())
240
 
        self.assertEqual('pref\x00fo', node._search_prefix)
241
 
        self.assertEqual({'pref\x00fo': ('sha1:abcd',)}, node._items)
242
 
 
243
 
    def test_deserialise_with_null_pref(self):
244
 
        node = self.module._deserialise_internal_node(
245
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
246
 
            stuple('sha1:1234',))
247
 
        self.assertIsInstance(node, chk_map.InternalNode)
248
 
        self.assertEqual(1, len(node))
249
 
        self.assertEqual(10, node.maximum_size)
250
 
        self.assertEqual(("sha1:1234",), node.key())
251
 
        self.assertEqual('pref\x00fo', node._search_prefix)
252
 
        self.assertEqual({'pref\x00fo\x00': ('sha1:abcd',)}, node._items)