/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
3735.26.1 by John Arbash Meinel
Write a pyrex extension for computing search keys.
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
17
"""Tests for _chk_map_*."""
18
19
from bzrlib import tests
20
21
22
def load_tests(standard_tests, module, loader):
23
    # parameterize all tests in this module
24
    suite = loader.suiteClass()
25
    applier = tests.TestScenarioApplier()
26
    import bzrlib._chk_map_py as py_module
27
    applier.scenarios = [('python', {'module': py_module})]
28
    if CompiledChkMapFeature.available():
29
        import bzrlib._chk_map_pyx as c_module
30
        applier.scenarios.append(('C', {'module': c_module}))
31
    else:
32
        # the compiled module isn't available, so we add a failing test
33
        class FailWithoutFeature(tests.TestCase):
34
            def test_fail(self):
35
                self.requireFeature(CompiledChkMapFeature)
36
        suite.addTest(loader.loadTestsFromTestCase(FailWithoutFeature))
37
    tests.adapt_tests(standard_tests, applier, suite)
38
    return suite
39
40
41
class _CompiledChkMapFeature(tests.Feature):
42
43
    def _probe(self):
44
        try:
45
            import bzrlib._chk_map_pyx
46
        except ImportError:
47
            return False
48
        return True
49
50
    def feature_name(self):
51
        return 'bzrlib._chk_map_pyx'
52
53
CompiledChkMapFeature = _CompiledChkMapFeature()
54
55
56
class TestSearchKeys(tests.TestCase):
57
58
    module = None # Filled in by test parameterization
59
60
    def assertSearchKey16(self, expected, key):
61
        self.assertEqual(expected, self.module._search_key_16(key))
62
63
    def assertSearchKey255(self, expected, key):
64
        actual = self.module._search_key_255(key)
65
        self.assertEqual(expected, actual, 'actual: %r' % (actual,))
66
67
    def test_simple_16(self):
68
        self.assertSearchKey16('8C736521', ('foo',))
69
        self.assertSearchKey16('8C736521\x008C736521', ('foo', 'foo'))
70
        self.assertSearchKey16('8C736521\x0076FF8CAA', ('foo', 'bar'))
71
        self.assertSearchKey16('ED82CD11', ('abcd',))
72
73
    def test_simple_255(self):
74
        self.assertSearchKey255('\x8cse!', ('foo',))
75
        self.assertSearchKey255('\x8cse!\x00\x8cse!', ('foo', 'foo'))
76
        self.assertSearchKey255('\x8cse!\x00v\xff\x8c\xaa', ('foo', 'bar'))
77
        # The standard mapping for these would include '\n', so it should be
78
        # mapped to '_'
79
        self.assertSearchKey255('\xfdm\x93_\x00P_\x1bL', ('<', 'V'))
80
81
    def test_255_does_not_include_newline(self):
82
        # When mapping via _search_key_255, we should never have the '\n'
83
        # character, but all other 255 values should be present
84
        chars_used = set()
85
        for char_in in range(256):
86
            search_key = self.module._search_key_255((chr(char_in),))
87
            chars_used.update(search_key)
88
        all_chars = set([chr(x) for x in range(256)])
89
        unused_chars = all_chars.symmetric_difference(chars_used)
90
        self.assertEqual(set('\n'), unused_chars)