1
# Copyright (C) 2007 Canonical Ltd
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.
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.
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
17
"""Tests for the compiled dirstate helpers."""
24
from bzrlib.compiled import dirstate_helpers
26
have_dirstate_helpers = False
28
have_dirstate_helpers = True
31
class _CompiledDirstateHelpersFeature(tests.Feature):
33
return have_dirstate_helpers
35
def feature_name(self):
36
return 'bzrlib.compiled.dirstate_helpers'
38
CompiledDirstateHelpersFeature = _CompiledDirstateHelpersFeature()
41
class TestCMPDirblockStrings(tests.TestCase):
43
_test_needs_features = [CompiledDirstateHelpersFeature]
45
def assertPositive(self, val):
46
"""Assert that val is greater than 0."""
47
self.assertTrue(val > 0, 'expected a positive value, but got %s' % val)
49
def assertNegative(self, val):
50
"""Assert that val is less than 0."""
51
self.assertTrue(val < 0, 'expected a negative value, but got %s' % val)
53
def assertStrCmp(self, expected, str1, str2):
54
"""Compare the two strings, in both directions.
56
:param expected: The expected comparison value. -1 means str1 comes
57
first, 0 means they are equal, 1 means str2 comes first
58
:param str1: string to compare
59
:param str2: string to compare
61
cmp_dirblock_strings = dirstate_helpers.cmp_dirblock_strings
63
self.assertEqual(0, cmp(str1.split('/'), str2.split('/')))
64
self.assertEqual(0, cmp_dirblock_strings(str1, str2))
65
self.assertEqual(0, cmp_dirblock_strings(str2, str1))
67
self.assertPositive(cmp(str1.split('/'), str2.split('/')))
68
self.assertPositive(cmp_dirblock_strings(str1, str2))
69
self.assertNegative(cmp_dirblock_strings(str2, str1))
71
self.assertNegative(cmp(str1.split('/'), str2.split('/')))
72
self.assertNegative(cmp_dirblock_strings(str1, str2))
73
self.assertPositive(cmp_dirblock_strings(str2, str1))
75
def test_cmp_empty(self):
76
"""Compare against the empty string."""
77
self.assertStrCmp(0, '', '')
78
self.assertStrCmp(1, 'a', '')
79
self.assertStrCmp(1, 'b', '')
80
self.assertStrCmp(1, 'testing', '')
81
self.assertStrCmp(1, 'test/ing/a/path/', '')
83
def test_cmp_same_str(self):
84
"""Compare the same string"""
85
self.assertStrCmp(0, 'a', 'a')
86
self.assertStrCmp(0, 'b', 'b')
87
self.assertStrCmp(0, 'testing a long string', 'testing a long string')
88
self.assertStrCmp(0, 'x'*10000, 'x'*10000)
89
self.assertStrCmp(0, 'x y', 'x' + ' ' + 'y')
90
self.assertStrCmp(0, 'a/b/c/d', 'a/b/c/d')
92
def test_simple_paths(self):
93
"""Compare strings that act like normal string comparison"""
94
self.assertStrCmp(-1, 'a', 'b')
95
self.assertStrCmp(-1, 'b', 'c')
96
self.assertStrCmp(1, 'd', 'c')
97
self.assertStrCmp(-1, 'testing a long string', 'testing b long string')
98
self.assertStrCmp(-1, 'a/b/c/d', 'a/c/c/d')
100
def test_tricky_paths(self):
101
self.assertStrCmp(1, 'ab/cd/ef', 'ab/cc/ef')
102
self.assertStrCmp(1, 'ab/cd/ef', 'ab/c/ef')
103
self.assertStrCmp(-1, 'ab/cd/ef', 'ab/cd-ef')
104
self.assertStrCmp(-1, 'ab/cd', 'ab/cd-')
105
self.assertStrCmp(-1, 'ab/cd', 'ab-cd')