~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test__chk_map.py

  • Committer: Jelmer Vernooij
  • Date: 2009-04-10 15:58:09 UTC
  • mto: This revision was merged to the branch mainline in revision 4284.
  • Revision ID: jelmer@samba.org-20090410155809-kdibzcjvp7pdb83f
Fix missing import.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2009, 2010 Canonical Ltd
 
1
# Copyright (C) 2009 Canonical Ltd
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
20
20
    chk_map,
21
21
    tests,
22
22
    )
23
 
from bzrlib.static_tuple import StaticTuple
24
 
stuple = StaticTuple
25
23
 
26
24
 
27
25
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')
 
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}))
 
33
    else:
 
34
        # the compiled module isn't available, so we add a failing test
 
35
        class FailWithoutFeature(tests.TestCase):
 
36
            def test_fail(self):
 
37
                self.requireFeature(CompiledChkMapFeature)
 
38
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
 
39
    tests.multiply_tests(standard_tests, scenarios, suite)
30
40
    return suite
31
41
 
32
42
 
 
43
class _CompiledChkMapFeature(tests.Feature):
 
44
 
 
45
    def _probe(self):
 
46
        try:
 
47
            import bzrlib._chk_map_pyx
 
48
        except ImportError:
 
49
            return False
 
50
        return True
 
51
 
 
52
    def feature_name(self):
 
53
        return 'bzrlib._chk_map_pyx'
 
54
 
 
55
CompiledChkMapFeature = _CompiledChkMapFeature()
 
56
 
 
57
 
33
58
class TestSearchKeys(tests.TestCase):
34
59
 
35
60
    module = None # Filled in by test parameterization
42
67
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
43
68
 
44
69
    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',))
 
70
        self.assertSearchKey16('8C736521', ('foo',))
 
71
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
 
72
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
 
73
        self.assertSearchKey16('ED82CD11', ('abcd',))
49
74
 
50
75
    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'))
 
76
        self.assertSearchKey255('\x8cse!', ('foo',))
 
77
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
 
78
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
54
79
        # The standard mapping for these would include '\n', so it should be
55
80
        # mapped to '_'
56
 
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', stuple('<', 'V'))
 
81
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
57
82
 
58
83
    def test_255_does_not_include_newline(self):
59
84
        # When mapping via _search_key_255, we should never have the '\n'
60
85
        # character, but all other 255 values should be present
61
86
        chars_used = set()
62
87
        for char_in in range(256):
63
 
            search_key = self.module._search_key_255(stuple(chr(char_in),))
 
88
            search_key = self.module._search_key_255((chr(char_in),))
64
89
            chars_used.update(search_key)
65
90
        all_chars = set([chr(x) for x in range(256)])
66
91
        unused_chars = all_chars.symmetric_difference(chars_used)
88
113
 
89
114
    def test_deserialise_empty(self):
90
115
        node = self.module._deserialise_leaf_node(
91
 
            "chkleaf:\n10\n1\n0\n\n", stuple("sha1:1234",))
 
116
            "chkleaf:\n10\n1\n0\n\n", ("sha1:1234",))
92
117
        self.assertEqual(0, len(node))
93
118
        self.assertEqual(10, node.maximum_size)
94
119
        self.assertEqual(("sha1:1234",), node.key())
95
 
        self.assertIsInstance(node.key(), StaticTuple)
96
120
        self.assertIs(None, node._search_prefix)
97
121
        self.assertIs(None, node._common_serialised_prefix)
98
122
 
170
194
 
171
195
    def assertDeserialiseErrors(self, text):
172
196
        self.assertRaises((ValueError, IndexError),
173
 
            self.module._deserialise_internal_node, text,
174
 
                stuple('not-a-real-sha',))
 
197
            self.module._deserialise_internal_node, text, 'not-a-real-sha')
175
198
 
176
199
    def test_raises_on_non_internal(self):
177
200
        self.assertDeserialiseErrors('')
188
211
 
189
212
    def test_deserialise_one(self):
190
213
        node = self.module._deserialise_internal_node(
191
 
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
214
            "chknode:\n10\n1\n1\n\na\x00sha1:abcd\n", ('sha1:1234',))
192
215
        self.assertIsInstance(node, chk_map.InternalNode)
193
216
        self.assertEqual(1, len(node))
194
217
        self.assertEqual(10, node.maximum_size)
198
221
 
199
222
    def test_deserialise_with_prefix(self):
200
223
        node = self.module._deserialise_internal_node(
201
 
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", stuple('sha1:1234',))
 
224
            "chknode:\n10\n1\n1\npref\na\x00sha1:abcd\n", ('sha1:1234',))
202
225
        self.assertIsInstance(node, chk_map.InternalNode)
203
226
        self.assertEqual(1, len(node))
204
227
        self.assertEqual(10, node.maximum_size)
207
230
        self.assertEqual({'prefa': ('sha1:abcd',)}, node._items)
208
231
 
209
232
        node = self.module._deserialise_internal_node(
210
 
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", stuple('sha1:1234',))
 
233
            "chknode:\n10\n1\n1\npref\n\x00sha1:abcd\n", ('sha1:1234',))
211
234
        self.assertIsInstance(node, chk_map.InternalNode)
212
235
        self.assertEqual(1, len(node))
213
236
        self.assertEqual(10, node.maximum_size)
217
240
 
218
241
    def test_deserialise_pref_with_null(self):
219
242
        node = self.module._deserialise_internal_node(
220
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n",
221
 
            stuple('sha1:1234',))
 
243
            "chknode:\n10\n1\n1\npref\x00fo\n\x00sha1:abcd\n", ('sha1:1234',))
222
244
        self.assertIsInstance(node, chk_map.InternalNode)
223
245
        self.assertEqual(1, len(node))
224
246
        self.assertEqual(10, node.maximum_size)
228
250
 
229
251
    def test_deserialise_with_null_pref(self):
230
252
        node = self.module._deserialise_internal_node(
231
 
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n",
232
 
            stuple('sha1:1234',))
 
253
            "chknode:\n10\n1\n1\npref\x00fo\n\x00\x00sha1:abcd\n", ('sha1:1234',))
233
254
        self.assertIsInstance(node, chk_map.InternalNode)
234
255
        self.assertEqual(1, len(node))
235
256
        self.assertEqual(10, node.maximum_size)