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