1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/string.hxx>
21 #include <cppuhelper/implbase1.hxx>
22 #include <com/sun/star/i18n/CharType.hpp>
24 #include "cppunit/TestAssert.h"
25 #include "cppunit/TestFixture.h"
26 #include "cppunit/extensions/HelperMacros.h"
27 #include "cppunit/plugin/TestPlugIn.h"
28 #include <rtl/string.hxx>
29 #include <rtl/ustring.hxx>
33 class TestString
: public CppUnit::TestFixture
38 void testStripStart();
42 void testTokenCount();
43 void testDecimalStringToNumber();
44 void testIsdigitAsciiString();
45 void testReverseString();
46 void testEqualsString();
47 void testCompareVersionStrings();
49 CPPUNIT_TEST_SUITE(TestString
);
50 CPPUNIT_TEST(testNatural
);
51 CPPUNIT_TEST(testRemove
);
52 CPPUNIT_TEST(testStripStart
);
53 CPPUNIT_TEST(testStripEnd
);
54 CPPUNIT_TEST(testStrip
);
55 CPPUNIT_TEST(testToken
);
56 CPPUNIT_TEST(testTokenCount
);
57 CPPUNIT_TEST(testDecimalStringToNumber
);
58 CPPUNIT_TEST(testIsdigitAsciiString
);
59 CPPUNIT_TEST(testReverseString
);
60 CPPUNIT_TEST(testEqualsString
);
61 CPPUNIT_TEST(testCompareVersionStrings
);
62 CPPUNIT_TEST_SUITE_END();
65 void TestString::testDecimalStringToNumber()
68 CPPUNIT_ASSERT_EQUAL((sal_uInt32
)1234, comphelper::string::decimalStringToNumber(s1
));
69 s1
+= OUString(static_cast<sal_Unicode
>(0x07C6));
70 CPPUNIT_ASSERT_EQUAL((sal_uInt32
)12346, comphelper::string::decimalStringToNumber(s1
));
71 // Codepoints on 2 16bits words
72 sal_uInt32 utf16String
[] = { 0x1D7FE /* 8 */, 0x1D7F7 /* 1 */};
73 s1
= OUString(utf16String
, 2);
74 CPPUNIT_ASSERT_EQUAL((sal_uInt32
)81, comphelper::string::decimalStringToNumber(s1
));
77 void TestString::testIsdigitAsciiString()
80 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s1
), true);
83 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s2
), false);
86 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3
), true);
89 using namespace ::com::sun::star
;
91 class testCollator
: public cppu::WeakImplHelper1
< i18n::XCollator
>
94 virtual sal_Int32 SAL_CALL
compareSubstring(
95 const OUString
& str1
, sal_Int32 off1
, sal_Int32 len1
,
96 const OUString
& str2
, sal_Int32 off2
, sal_Int32 len2
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
98 return str1
.copy(off1
, len1
).compareTo(str2
.copy(off2
, len2
));
100 virtual sal_Int32 SAL_CALL
compareString(
101 const OUString
& str1
,
102 const OUString
& str2
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
104 return str1
.compareTo(str2
);
106 virtual sal_Int32 SAL_CALL
loadDefaultCollator(const lang::Locale
&, sal_Int32
)
107 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{return 0;}
108 virtual sal_Int32 SAL_CALL
loadCollatorAlgorithm(const OUString
&,
109 const lang::Locale
&, sal_Int32
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{return 0;}
110 virtual void SAL_CALL
loadCollatorAlgorithmWithEndUserOption(const OUString
&,
111 const lang::Locale
&, const uno::Sequence
< sal_Int32
>&) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{}
112 virtual uno::Sequence
< OUString
> SAL_CALL
listCollatorAlgorithms(const lang::Locale
&)
113 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
115 return uno::Sequence
< OUString
>();
117 virtual uno::Sequence
< sal_Int32
> SAL_CALL
listCollatorOptions(const OUString
&)
118 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
120 return uno::Sequence
< sal_Int32
>();
124 #define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
126 class testBreakIterator
: public cppu::WeakImplHelper1
< i18n::XBreakIterator
>
129 virtual sal_Int32 SAL_CALL
nextCharacters( const OUString
&, sal_Int32
,
130 const lang::Locale
&, sal_Int16
, sal_Int32
, sal_Int32
& )
131 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{return -1;}
132 virtual sal_Int32 SAL_CALL
previousCharacters( const OUString
&, sal_Int32
,
133 const lang::Locale
&, sal_Int16
, sal_Int32
, sal_Int32
& )
134 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{return -1;}
136 virtual i18n::Boundary SAL_CALL
previousWord( const OUString
&, sal_Int32
,
137 const lang::Locale
&, sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
138 { return i18n::Boundary(); }
139 virtual i18n::Boundary SAL_CALL
nextWord( const OUString
&, sal_Int32
,
140 const lang::Locale
&, sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
141 { return i18n::Boundary(); }
142 virtual i18n::Boundary SAL_CALL
getWordBoundary( const OUString
&, sal_Int32
,
143 const lang::Locale
&, sal_Int16
, sal_Bool
)
144 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
145 { return i18n::Boundary(); }
147 virtual sal_Bool SAL_CALL
isBeginWord( const OUString
&, sal_Int32
,
148 const lang::Locale
&, sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
150 virtual sal_Bool SAL_CALL
isEndWord( const OUString
&, sal_Int32
,
151 const lang::Locale
& , sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
153 virtual sal_Int16 SAL_CALL
getWordType( const OUString
&, sal_Int32
,
154 const lang::Locale
& ) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
157 virtual sal_Int32 SAL_CALL
beginOfSentence( const OUString
&, sal_Int32
,
158 const lang::Locale
& ) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
160 virtual sal_Int32 SAL_CALL
endOfSentence( const OUString
& rText
, sal_Int32
,
161 const lang::Locale
& ) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
162 { return rText
.getLength(); }
164 virtual i18n::LineBreakResults SAL_CALL
getLineBreak( const OUString
&, sal_Int32
,
165 const lang::Locale
&, sal_Int32
,
166 const i18n::LineBreakHyphenationOptions
&,
167 const i18n::LineBreakUserOptions
&)
168 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
170 return i18n::LineBreakResults();
173 virtual sal_Int16 SAL_CALL
getScriptType( const OUString
&, sal_Int32
)
174 throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
175 virtual sal_Int32 SAL_CALL
beginOfScript( const OUString
&, sal_Int32
,
176 sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
177 virtual sal_Int32 SAL_CALL
endOfScript( const OUString
&, sal_Int32
,
178 sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
179 virtual sal_Int32 SAL_CALL
previousScript( const OUString
&, sal_Int32
,
180 sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
181 virtual sal_Int32 SAL_CALL
nextScript( const OUString
&, sal_Int32
,
182 sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
184 virtual sal_Int32 SAL_CALL
beginOfCharBlock( const OUString
&, sal_Int32
,
185 const lang::Locale
&, sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
186 virtual sal_Int32 SAL_CALL
endOfCharBlock( const OUString
& rText
, sal_Int32 nStartPos
,
187 const lang::Locale
&, sal_Int16 CharType
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
189 const sal_Unicode
*pStr
= rText
.getStr()+nStartPos
;
190 for (sal_Int32 nI
= nStartPos
; nI
< rText
.getLength(); ++nI
)
192 if (CharType
== i18n::CharType::DECIMAL_DIGIT_NUMBER
&& !IS_DIGIT(*pStr
))
194 else if (CharType
!= i18n::CharType::DECIMAL_DIGIT_NUMBER
&& IS_DIGIT(*pStr
))
200 virtual sal_Int32 SAL_CALL
previousCharBlock( const OUString
&, sal_Int32
,
201 const lang::Locale
&, sal_Int16
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
{ return -1; }
202 virtual sal_Int32 SAL_CALL
nextCharBlock( const OUString
& rText
, sal_Int32 nStartPos
,
203 const lang::Locale
&, sal_Int16 CharType
) throw(uno::RuntimeException
, std::exception
) SAL_OVERRIDE
205 const sal_Unicode
*pStr
= rText
.getStr()+nStartPos
;
206 for (sal_Int32 nI
= nStartPos
; nI
< rText
.getLength(); ++nI
)
208 if (CharType
== i18n::CharType::DECIMAL_DIGIT_NUMBER
&& IS_DIGIT(*pStr
))
210 else if (CharType
!= i18n::CharType::DECIMAL_DIGIT_NUMBER
&& !IS_DIGIT(*pStr
))
218 void TestString::testNatural()
220 using namespace comphelper::string
;
222 uno::Reference
< i18n::XCollator
> xCollator(new testCollator
);
223 uno::Reference
< i18n::XBreakIterator
> xBI(new testBreakIterator
);
225 // --- Some generic tests to ensure we do not alter original behavior
226 // outside what we want
228 compareNatural("ABC", "ABC", xCollator
, xBI
, lang::Locale()) == 0
232 compareNatural("ABC", "abc", xCollator
, xBI
, lang::Locale()) < 0
236 compareNatural("abc", "ABC", xCollator
, xBI
, lang::Locale()) > 0
240 compareNatural("alongstring", "alongerstring", xCollator
, xBI
, lang::Locale()) > 0
244 compareNatural("alongerstring", "alongstring", xCollator
, xBI
, lang::Locale()) < 0
246 // -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
247 // That's why we originally made the patch
249 compareNatural("Heading 9", "Heading 10", xCollator
, xBI
, lang::Locale()) < 0
253 OUString("Heading 9").compareTo("Heading 10") > 0
256 compareNatural("Heading 10", "Heading 9", xCollator
, xBI
, lang::Locale()) > 0
260 compareNatural("July, the 4th", "July, the 10th", xCollator
, xBI
, lang::Locale()) < 0
263 OUString("July, the 4th").compareTo("July, the 10th") > 0
266 compareNatural("July, the 10th", "July, the 4th", xCollator
, xBI
, lang::Locale()) > 0
270 compareNatural("abc08", "abc010", xCollator
, xBI
, lang::Locale()) < 0
273 OUString("abc08").compareTo("abc010") > 0
276 compareNatural("abc010", "abc08", xCollator
, xBI
, lang::Locale()) > 0
279 compareNatural("apple10apple", "apple10apple", xCollator
, xBI
, lang::Locale()) == 0
283 void TestString::testRemove()
288 aOut
= ::comphelper::string::remove(aIn
, 'b');
289 CPPUNIT_ASSERT(aOut
== "ac");
293 aOut
= ::comphelper::string::remove(aIn
, 'a');
294 CPPUNIT_ASSERT(aOut
.isEmpty());
297 void TestString::testStripStart()
302 aOut
= ::comphelper::string::stripStart(aIn
, 'b');
303 CPPUNIT_ASSERT(aOut
== "abc");
305 aOut
= ::comphelper::string::stripStart(aIn
, 'a');
306 CPPUNIT_ASSERT(aOut
== "bc");
309 aOut
= ::comphelper::string::stripStart(aIn
, 'a');
310 CPPUNIT_ASSERT(aOut
.isEmpty());
313 aOut
= ::comphelper::string::stripStart(aIn
, 'a');
314 CPPUNIT_ASSERT(aOut
== "ba");
317 void TestString::testStripEnd()
322 aOut
= ::comphelper::string::stripEnd(aIn
, 'b');
323 CPPUNIT_ASSERT(aOut
== "abc");
325 aOut
= ::comphelper::string::stripEnd(aIn
, 'c');
326 CPPUNIT_ASSERT(aOut
== "ab");
329 aOut
= ::comphelper::string::stripEnd(aIn
, 'a');
330 CPPUNIT_ASSERT(aOut
.isEmpty());
333 aOut
= ::comphelper::string::stripEnd(aIn
, 'a');
334 CPPUNIT_ASSERT(aOut
== "ab");
337 void TestString::testStrip()
342 aOut
= ::comphelper::string::strip(aIn
, 'b');
343 CPPUNIT_ASSERT(aOut
== "abc");
345 aOut
= ::comphelper::string::strip(aIn
, 'c');
346 CPPUNIT_ASSERT(aOut
== "ab");
349 aOut
= ::comphelper::string::strip(aIn
, 'a');
350 CPPUNIT_ASSERT(aOut
.isEmpty());
353 aOut
= ::comphelper::string::strip(aIn
, 'a');
354 CPPUNIT_ASSERT(aOut
== "b");
357 void TestString::testToken()
359 OString
aIn("10.11.12");
362 aOut
= aIn
.getToken(-1, '.');
363 CPPUNIT_ASSERT(aOut
.isEmpty());
365 aOut
= aIn
.getToken(0, '.');
366 CPPUNIT_ASSERT(aOut
== "10");
368 aOut
= aIn
.getToken(1, '.');
369 CPPUNIT_ASSERT(aOut
== "11");
371 aOut
= aIn
.getToken(2, '.');
372 CPPUNIT_ASSERT(aOut
== "12");
374 aOut
= aIn
.getToken(3, '.');
375 CPPUNIT_ASSERT(aOut
.isEmpty());
378 void TestString::testTokenCount()
380 OString
aIn("10.11.12");
383 nOut
= ::comphelper::string::getTokenCount(aIn
, '.');
384 CPPUNIT_ASSERT(nOut
== 3);
386 nOut
= ::comphelper::string::getTokenCount(aIn
, 'X');
387 CPPUNIT_ASSERT(nOut
== 1);
389 nOut
= ::comphelper::string::getTokenCount(OString(), 'X');
390 CPPUNIT_ASSERT(nOut
== 0);
393 void TestString::testReverseString()
396 OString aOut
= ::comphelper::string::reverseString(aIn
);
398 CPPUNIT_ASSERT(aOut
== "CBA");
401 void TestString::testEqualsString()
404 CPPUNIT_ASSERT(::comphelper::string::equals(aIn
, 'A'));
405 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn
, 'B'));
407 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn
, 'A'));
409 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn
, 'A'));
422 void TestString::testCompareVersionStrings()
425 #error TEST already defined
427 #define TEST(a,b,result) \
428 CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(a, b)) == result); \
430 CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(b, a)) == -(result))
439 TEST("01", "001", 1);
440 TEST("1.00", "1", 1);
442 TEST("1.01", "1.1", -1);
443 TEST("1.001", "1.1", -1);
444 TEST("1.001", "1.010", -1);
445 TEST("1.2.a", "1.2.b", -1);
446 TEST("1.2.3 (foo,bar)", "1.2.9", -1);
447 TEST("1.2.3 (foo,bar)", "1.2.4 (foo,bar)", -1);
448 TEST("1.2.3 (foo,bar)", "1.2.3 (foo)", 1); // Neither ordering makes any more sense than the other here, as long as they compare unequal
449 TEST("1.2.3 (foo,bar)", "1.2.2 (foo,bar)", 1);
454 CPPUNIT_TEST_SUITE_REGISTRATION(TestString
);
458 CPPUNIT_PLUGIN_IMPLEMENT();
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */