~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__chk_map.py

  • Committer: Vincent Ladeuil
  • Date: 2010-02-11 09:27:55 UTC
  • mfrom: (5017.3.46 test-servers)
  • mto: This revision was merged to the branch mainline in revision 5030.
  • Revision ID: v.ladeuil+lp@free.fr-20100211092755-3vvu4vbwiwjjte3s
Move tests servers from bzrlib.transport to bzrlib.tests.test_server

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