~bzr-pqm/bzr/bzr.dev

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright (C) 2007 Canonical Ltd
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

"""Tests for the compiled dirstate helpers."""

from bzrlib import (
    tests,
    )
try:
    from bzrlib.compiled import dirstate_helpers
except ImportError:
    have_dirstate_helpers = False
else:
    have_dirstate_helpers = True
from bzrlib.tests import test_dirstate


class _CompiledDirstateHelpersFeature(tests.Feature):
    def _probe(self):
        return have_dirstate_helpers

    def feature_name(self):
        return 'bzrlib.compiled.dirstate_helpers'

CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()


class TestCMPDirblockStrings(tests.TestCase):

    _test_needs_features = [CompiledDirstateHelpersFeature]

    def assertPositive(self, val):
        """Assert that val is greater than 0."""
        self.assertTrue(val > 0, 'expected a positive value, but got %s' % val)

    def assertNegative(self, val):
        """Assert that val is less than 0."""
        self.assertTrue(val < 0, 'expected a negative value, but got %s' % val)

    def assertStrCmp(self, expected, str1, str2):
        """Compare the two strings, in both directions.

        :param expected: The expected comparison value. -1 means str1 comes
            first, 0 means they are equal, 1 means str2 comes first
        :param str1: string to compare
        :param str2: string to compare
        """
        cmp_dirblock_strings = dirstate_helpers.cmp_dirblock_strings
        if expected == 0:
            self.assertEqual(0, cmp(str1.split('/'), str2.split('/')))
            self.assertEqual(0, cmp_dirblock_strings(str1, str2))
            self.assertEqual(0, cmp_dirblock_strings(str2, str1))
        elif expected > 0:
            self.assertPositive(cmp(str1.split('/'), str2.split('/')))
            self.assertPositive(cmp_dirblock_strings(str1, str2))
            self.assertNegative(cmp_dirblock_strings(str2, str1))
        else:
            self.assertNegative(cmp(str1.split('/'), str2.split('/')))
            self.assertNegative(cmp_dirblock_strings(str1, str2))
            self.assertPositive(cmp_dirblock_strings(str2, str1))

    def test_cmp_empty(self):
        """Compare against the empty string."""
        self.assertStrCmp(0, '', '')
        self.assertStrCmp(1, 'a', '')
        self.assertStrCmp(1, 'ab', '')
        self.assertStrCmp(1, 'abc', '')
        self.assertStrCmp(1, 'abcd', '')
        self.assertStrCmp(1, 'abcde', '')
        self.assertStrCmp(1, 'abcdef', '')
        self.assertStrCmp(1, 'abcdefg', '')
        self.assertStrCmp(1, 'abcdefgh', '')
        self.assertStrCmp(1, 'abcdefghi', '')
        self.assertStrCmp(1, 'test/ing/a/path/', '')

    def test_cmp_same_str(self):
        """Compare the same string"""
        self.assertStrCmp(0, 'a', 'a')
        self.assertStrCmp(0, 'ab', 'ab')
        self.assertStrCmp(0, 'abc', 'abc')
        self.assertStrCmp(0, 'abcd', 'abcd')
        self.assertStrCmp(0, 'abcde', 'abcde')
        self.assertStrCmp(0, 'abcdef', 'abcdef')
        self.assertStrCmp(0, 'abcdefg', 'abcdefg')
        self.assertStrCmp(0, 'abcdefgh', 'abcdefgh')
        self.assertStrCmp(0, 'abcdefghi', 'abcdefghi')
        self.assertStrCmp(0, 'testing a long string', 'testing a long string')
        self.assertStrCmp(0, 'x'*10000, 'x'*10000)
        self.assertStrCmp(0, 'a/b', 'a/b')
        self.assertStrCmp(0, 'a/b/c', 'a/b/c')
        self.assertStrCmp(0, 'a/b/c/d', 'a/b/c/d')
        self.assertStrCmp(0, 'a/b/c/d/e', 'a/b/c/d/e')

    def test_simple_paths(self):
        """Compare strings that act like normal string comparison"""
        self.assertStrCmp(-1, 'a', 'b')
        self.assertStrCmp(-1, 'aa', 'ab')
        self.assertStrCmp(-1, 'ab', 'bb')
        self.assertStrCmp(-1, 'aaa', 'aab')
        self.assertStrCmp(-1, 'aab', 'abb')
        self.assertStrCmp(-1, 'abb', 'bbb')
        self.assertStrCmp(-1, 'aaaa', 'aaab')
        self.assertStrCmp(-1, 'aaab', 'aabb')
        self.assertStrCmp(-1, 'aabb', 'abbb')
        self.assertStrCmp(-1, 'abbb', 'bbbb')
        self.assertStrCmp(-1, 'aaaaa', 'aaaab')
        self.assertStrCmp(-1, 'a/a', 'a/b')
        self.assertStrCmp(-1, 'a/b', 'b/b')
        self.assertStrCmp(-1, 'a/a/a', 'a/a/b')
        self.assertStrCmp(-1, 'a/a/b', 'a/b/b')
        self.assertStrCmp(-1, 'a/b/b', 'b/b/b')
        self.assertStrCmp(-1, 'a/a/a/a', 'a/a/a/b')
        self.assertStrCmp(-1, 'a/a/a/b', 'a/a/b/b')
        self.assertStrCmp(-1, 'a/a/b/b', 'a/b/b/b')
        self.assertStrCmp(-1, 'a/b/b/b', 'b/b/b/b')
        self.assertStrCmp(-1, 'a/a/a/a/a', 'a/a/a/a/b')

    def test_tricky_paths(self):
        self.assertStrCmp(1, 'ab/cd/ef', 'ab/cc/ef')
        self.assertStrCmp(1, 'ab/cd/ef', 'ab/c/ef')
        self.assertStrCmp(-1, 'ab/cd/ef', 'ab/cd-ef')
        self.assertStrCmp(-1, 'ab/cd', 'ab/cd-')
        self.assertStrCmp(-1, 'ab/cd', 'ab-cd')


class TestCompiledBisectDirblock(test_dirstate.TestBisectDirblock):
    """Test that bisect_dirblock() returns the expected values.

    bisect_dirblock is intended to work like bisect.bisect_left() except it
    knows it is working on dirblocks and that dirblocks are sorted by ('path',
    'to', 'foo') chunks rather than by raw 'path/to/foo'.

    This runs all the normal tests that TestBisectDirblock did, but uses the
    compiled version.
    """

    _test_needs_features = [CompiledDirstateHelpersFeature]

    def setUp(self):
        super(TestCompiledBisectDirblock, self).setUp()
        if have_dirstate_helpers:
            self.bisect_dirblock_func = dirstate_helpers.c_bisect_dirblock
        else:
            # We shouldn't be running these tests because of the
            # _test_needs_features, so make sure they would fail otherwise
            self.bisect_dirblock_func = None