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