1
# Copyright (C) 2009, 2010 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_*."""
24
from bzrlib.static_tuple import StaticTuple
28
def load_tests(standard_tests, module, loader):
29
suite, _ = tests.permute_tests_for_extension(standard_tests, loader,
30
'bzrlib._chk_map_py', 'bzrlib._chk_map_pyx')
34
class TestSearchKeys(tests.TestCase):
36
module = None # Filled in by test parameterization
38
def assertSearchKey16(self, expected, key):
39
self.assertEqual(expected, self.module._search_key_16(key))
41
def assertSearchKey255(self, expected, key):
42
actual = self.module._search_key_255(key)
43
self.assertEqual(expected, actual, 'actual: %r' % (actual,))
45
def test_simple_16(self):
46
self.assertSearchKey16('8C736521', stuple('foo',))
47
self.assertSearchKey16('8C736521\x008C736521', stuple('foo', 'foo'))
48
self.assertSearchKey16('8C736521\x0076FF8CAA', stuple('foo', 'bar'))
49
self.assertSearchKey16('ED82CD11', stuple('abcd',))
51
def test_simple_255(self):
52
self.assertSearchKey255('\x8cse!', stuple('foo',))
53
self.assertSearchKey255('\x8cse!\x00\x8cse!', stuple('foo', 'foo'))
54
self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', stuple('foo', 'bar'))
55
# The standard mapping for these would include '\n', so it should be
57
self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
59
def test_255_does_not_include_newline(self):
60
# When mapping via _search_key_255, we should never have the '\n'
61
# character, but all other 255 values should be present
63
for char_in in range(256):
64
search_key = self.module._search_key_255(stuple(chr(char_in),))
65
chars_used.update(search_key)
66
all_chars = set([chr(x) for x in range(256)])
67
unused_chars = all_chars.symmetric_difference(chars_used)
68
self.assertEqual(set('\n'), unused_chars)
71
class TestDeserialiseLeafNode(tests.TestCase):
75
def assertDeserialiseErrors(self, text):
76
self.assertRaises((ValueError, IndexError),
77
self.module._deserialise_leaf_node, text, 'not-a-real-sha')
79
def test_raises_on_non_leaf(self):
80
self.assertDeserialiseErrors('')
81
self.assertDeserialiseErrors('short\n')
82
self.assertDeserialiseErrors('chknotleaf:\n')
83
self.assertDeserialiseErrors('chkleaf:x\n')
84
self.assertDeserialiseErrors('chkleaf:\n')
85
self.assertDeserialiseErrors('chkleaf:\nnotint\n')
86
self.assertDeserialiseErrors('chkleaf:\n10\n')
87
self.assertDeserialiseErrors('chkleaf:\n10\n256\n')
88
self.assertDeserialiseErrors('chkleaf:\n10\n256\n10\n')
90
def test_deserialise_empty(self):
91
node = self.module._deserialise_leaf_node(
92
"chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
93
self.assertEqual(0, len(node))
94
self.assertEqual(10, node.maximum_size)
95
self.assertEqual(("sha1:1234",), node.key())
96
self.assertIsInstance(node.key(), StaticTuple)
97
self.assertIs(None, node._search_prefix)
98
self.assertIs(None, node._common_serialised_prefix)
100
def test_deserialise_items(self):
101
node = self.module._deserialise_leaf_node(
102
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
104
self.assertEqual(2, len(node))
105
self.assertEqual([(("foo bar",), "baz"), (("quux",), "blarh")],
106
sorted(node.iteritems(None)))
108
def test_deserialise_item_with_null_width_1(self):
109
node = self.module._deserialise_leaf_node(
110
"chkleaf:\n0\n1\n2\n\nfoo\x001\nbar\x00baz\nquux\x001\nblarh\n",
112
self.assertEqual(2, len(node))
113
self.assertEqual([(("foo",), "bar\x00baz"), (("quux",), "blarh")],
114
sorted(node.iteritems(None)))
116
def test_deserialise_item_with_null_width_2(self):
117
node = self.module._deserialise_leaf_node(
118
"chkleaf:\n0\n2\n2\n\nfoo\x001\x001\nbar\x00baz\n"
119
"quux\x00\x001\nblarh\n",
121
self.assertEqual(2, len(node))
122
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("quux", ""), "blarh")],
123
sorted(node.iteritems(None)))
125
def test_iteritems_selected_one_of_two_items(self):
126
node = self.module._deserialise_leaf_node(
127
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
129
self.assertEqual(2, len(node))
130
self.assertEqual([(("quux",), "blarh")],
131
sorted(node.iteritems(None, [("quux",), ("qaz",)])))
133
def test_deserialise_item_with_common_prefix(self):
134
node = self.module._deserialise_leaf_node(
135
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x001\nbar\x00baz\n2\x001\nblarh\n",
137
self.assertEqual(2, len(node))
138
self.assertEqual([(("foo", "1"), "bar\x00baz"), (("foo", "2"), "blarh")],
139
sorted(node.iteritems(None)))
140
self.assertIs(chk_map._unknown, node._search_prefix)
141
self.assertEqual('foo\x00', node._common_serialised_prefix)
143
def test_deserialise_multi_line(self):
144
node = self.module._deserialise_leaf_node(
145
"chkleaf:\n0\n2\n2\nfoo\x00\n1\x002\nbar\nbaz\n2\x002\nblarh\n\n",
147
self.assertEqual(2, len(node))
148
self.assertEqual([(("foo", "1"), "bar\nbaz"),
149
(("foo", "2"), "blarh\n"),
150
], sorted(node.iteritems(None)))
151
self.assertIs(chk_map._unknown, node._search_prefix)
152
self.assertEqual('foo\x00', node._common_serialised_prefix)
154
def test_key_after_map(self):
155
node = self.module._deserialise_leaf_node(
156
"chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
157
node.map(None, ("foo bar",), "baz quux")
158
self.assertEqual(None, node.key())
160
def test_key_after_unmap(self):
161
node = self.module._deserialise_leaf_node(
162
"chkleaf:\n0\n1\n2\n\nfoo bar\x001\nbaz\nquux\x001\nblarh\n",
164
node.unmap(None, ("foo bar",))
165
self.assertEqual(None, node.key())
168
class TestDeserialiseInternalNode(tests.TestCase):
172
def assertDeserialiseErrors(self, text):
173
self.assertRaises((ValueError, IndexError),
174
self.module._deserialise_internal_node, text,
175
stuple('not-a-real-sha',))
177
def test_raises_on_non_internal(self):
178
self.assertDeserialiseErrors('')
179
self.assertDeserialiseErrors('short\n')
180
self.assertDeserialiseErrors('chknotnode:\n')
181
self.assertDeserialiseErrors('chknode:x\n')
182
self.assertDeserialiseErrors('chknode:\n')
183
self.assertDeserialiseErrors('chknode:\nnotint\n')
184
self.assertDeserialiseErrors('chknode:\n10\n')
185
self.assertDeserialiseErrors('chknode:\n10\n256\n')
186
self.assertDeserialiseErrors('chknode:\n10\n256\n10\n')
187
# no trailing newline
188
self.assertDeserialiseErrors('chknode:\n10\n256\n0\n1\nfo')
190
def test_deserialise_one(self):
191
node = self.module._deserialise_internal_node(
192
"chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
193
self.assertIsInstance(node, chk_map.InternalNode)
194
self.assertEqual(1, len(node))
195
self.assertEqual(10, node.maximum_size)
196
self.assertEqual(("sha1:1234",), node.key())
197
self.assertEqual('', node._search_prefix)
198
self.assertEqual({'a': ('sha1:abcd',)}, node._items)
200
def test_deserialise_with_prefix(self):
201
node = self.module._deserialise_internal_node(
202
"chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
203
self.assertIsInstance(node, chk_map.InternalNode)
204
self.assertEqual(1, len(node))
205
self.assertEqual(10, node.maximum_size)
206
self.assertEqual(("sha1:1234",), node.key())
207
self.assertEqual('pref', node._search_prefix)
208
self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
210
node = self.module._deserialise_internal_node(
211
"chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
212
self.assertIsInstance(node, chk_map.InternalNode)
213
self.assertEqual(1, len(node))
214
self.assertEqual(10, node.maximum_size)
215
self.assertEqual(("sha1:1234",), node.key())
216
self.assertEqual('pref', node._search_prefix)
217
self.assertEqual({'pref': ('sha1:abcd',)}, node._items)
219
def test_deserialise_pref_with_null(self):
220
node = self.module._deserialise_internal_node(
221
"chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
222
stuple('sha1:1234',))
223
self.assertIsInstance(node, chk_map.InternalNode)
224
self.assertEqual(1, len(node))
225
self.assertEqual(10, node.maximum_size)
226
self.assertEqual(("sha1:1234",), node.key())
227
self.assertEqual('pref\x00fo', node._search_prefix)
228
self.assertEqual({'pref\x00fo': ('sha1:abcd',)}, node._items)
230
def test_deserialise_with_null_pref(self):
231
node = self.module._deserialise_internal_node(
232
"chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
233
stuple('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\x00fo', node._search_prefix)
239
self.assertEqual({'pref\x00fo\x00': ('sha1:abcd',)}, node._items)
242
class Test_BytesToTextKey(tests.TestCase):
244
def assertBytesToTextKey(self, key, bytes):
245
self.assertEqual(key,
246
self.module._bytes_to_text_key(bytes))
248
def assertBytesToTextKeyRaises(self, bytes):
249
# These are invalid bytes, and we want to make sure the code under test
250
# raises an exception rather than segfaults, etc. We don't particularly
251
# care what exception.
252
self.assertRaises(Exception, self.module._bytes_to_text_key, bytes)
255
self.assertBytesToTextKey(('file-id', 'revision-id'),
256
'file: file-id\nparent-id\nname\nrevision-id\n'
257
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
259
def test_invalid_no_kind(self):
260
self.assertBytesToTextKeyRaises(
261
'file file-id\nparent-id\nname\nrevision-id\n'
262
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
264
def test_invalid_no_space(self):
265
self.assertBytesToTextKeyRaises(
266
'file:file-id\nparent-id\nname\nrevision-id\n'
267
'da39a3ee5e6b4b0d3255bfef95601890afd80709\n100\nN')
269
def test_invalid_too_short_file_id(self):
270
self.assertBytesToTextKeyRaises('file:file-id')
272
def test_invalid_too_short_parent_id(self):
273
self.assertBytesToTextKeyRaises('file:file-id\nparent-id')
275
def test_invalid_too_short_name(self):
276
self.assertBytesToTextKeyRaises('file:file-id\nparent-id\nname')
279
self.assertBytesToTextKey(('dir-id', 'revision-id'),
280
'dir: dir-id\nparent-id\nname\nrevision-id')