bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / qa / string / test_string.cxx
blob8971a4312dfd7d79b3525680ed84ede3e4699efc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
31 namespace {
33 class TestString: public CppUnit::TestFixture
35 public:
36 void testNatural();
37 void testRemove();
38 void testStripStart();
39 void testStripEnd();
40 void testStrip();
41 void testToken();
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()
67 OUString s1("1234");
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()
79 OString s1("1234");
80 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s1), true);
82 OString s2("1A34");
83 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s2), false);
85 OString s3;
86 CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3), true);
89 using namespace ::com::sun::star;
91 class testCollator : public cppu::WeakImplHelper1< i18n::XCollator >
93 public:
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 >
128 public:
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
149 { return false; }
150 virtual sal_Bool SAL_CALL isEndWord( const OUString&, sal_Int32,
151 const lang::Locale& , sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
152 { return false; }
153 virtual sal_Int16 SAL_CALL getWordType( const OUString&, sal_Int32,
154 const lang::Locale& ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
155 { return 0; }
157 virtual sal_Int32 SAL_CALL beginOfSentence( const OUString&, sal_Int32,
158 const lang::Locale& ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
159 { return 0; }
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))
193 return nI;
194 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
195 return nI;
196 ++pStr;
198 return -1;
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))
209 return nI;
210 else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
211 return nI;
212 ++pStr;
214 return -1;
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
227 CPPUNIT_ASSERT(
228 compareNatural("ABC", "ABC", xCollator, xBI, lang::Locale()) == 0
230 // Case sensitivity
231 CPPUNIT_ASSERT(
232 compareNatural("ABC", "abc", xCollator, xBI, lang::Locale()) < 0
234 // Reverse
235 CPPUNIT_ASSERT(
236 compareNatural("abc", "ABC", xCollator, xBI, lang::Locale()) > 0
238 // First shorter
239 CPPUNIT_ASSERT(
240 compareNatural("alongstring", "alongerstring", xCollator, xBI, lang::Locale()) > 0
242 // Second shorter
243 CPPUNIT_ASSERT(
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
248 CPPUNIT_ASSERT(
249 compareNatural("Heading 9", "Heading 10", xCollator, xBI, lang::Locale()) < 0
251 // Original behavior
252 CPPUNIT_ASSERT(
253 OUString("Heading 9").compareTo("Heading 10") > 0
255 CPPUNIT_ASSERT(
256 compareNatural("Heading 10", "Heading 9", xCollator, xBI, lang::Locale()) > 0
258 // Harder
259 CPPUNIT_ASSERT(
260 compareNatural("July, the 4th", "July, the 10th", xCollator, xBI, lang::Locale()) < 0
262 CPPUNIT_ASSERT(
263 OUString("July, the 4th").compareTo("July, the 10th") > 0
265 CPPUNIT_ASSERT(
266 compareNatural("July, the 10th", "July, the 4th", xCollator, xBI, lang::Locale()) > 0
268 // Hardest
269 CPPUNIT_ASSERT(
270 compareNatural("abc08", "abc010", xCollator, xBI, lang::Locale()) < 0
272 CPPUNIT_ASSERT(
273 OUString("abc08").compareTo("abc010") > 0
275 CPPUNIT_ASSERT(
276 compareNatural("abc010", "abc08", xCollator, xBI, lang::Locale()) > 0
278 CPPUNIT_ASSERT(
279 compareNatural("apple10apple", "apple10apple", xCollator, xBI, lang::Locale()) == 0
283 void TestString::testRemove()
285 OString aIn("abc");
286 OString aOut;
288 aOut = ::comphelper::string::remove(aIn, 'b');
289 CPPUNIT_ASSERT(aOut == "ac");
291 aIn = "aaa";
293 aOut = ::comphelper::string::remove(aIn, 'a');
294 CPPUNIT_ASSERT(aOut.isEmpty());
297 void TestString::testStripStart()
299 OString aIn("abc");
300 OString aOut;
302 aOut = ::comphelper::string::stripStart(aIn, 'b');
303 CPPUNIT_ASSERT(aOut == "abc");
305 aOut = ::comphelper::string::stripStart(aIn, 'a');
306 CPPUNIT_ASSERT(aOut == "bc");
308 aIn = "aaa";
309 aOut = ::comphelper::string::stripStart(aIn, 'a');
310 CPPUNIT_ASSERT(aOut.isEmpty());
312 aIn = "aba";
313 aOut = ::comphelper::string::stripStart(aIn, 'a');
314 CPPUNIT_ASSERT(aOut == "ba");
317 void TestString::testStripEnd()
319 OString aIn("abc");
320 OString aOut;
322 aOut = ::comphelper::string::stripEnd(aIn, 'b');
323 CPPUNIT_ASSERT(aOut == "abc");
325 aOut = ::comphelper::string::stripEnd(aIn, 'c');
326 CPPUNIT_ASSERT(aOut == "ab");
328 aIn = "aaa";
329 aOut = ::comphelper::string::stripEnd(aIn, 'a');
330 CPPUNIT_ASSERT(aOut.isEmpty());
332 aIn = "aba";
333 aOut = ::comphelper::string::stripEnd(aIn, 'a');
334 CPPUNIT_ASSERT(aOut == "ab");
337 void TestString::testStrip()
339 OString aIn("abc");
340 OString aOut;
342 aOut = ::comphelper::string::strip(aIn, 'b');
343 CPPUNIT_ASSERT(aOut == "abc");
345 aOut = ::comphelper::string::strip(aIn, 'c');
346 CPPUNIT_ASSERT(aOut == "ab");
348 aIn = "aaa";
349 aOut = ::comphelper::string::strip(aIn, 'a');
350 CPPUNIT_ASSERT(aOut.isEmpty());
352 aIn = "aba";
353 aOut = ::comphelper::string::strip(aIn, 'a');
354 CPPUNIT_ASSERT(aOut == "b");
357 void TestString::testToken()
359 OString aIn("10.11.12");
360 OString aOut;
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");
381 sal_Int32 nOut;
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()
395 OString aIn("ABC");
396 OString aOut = ::comphelper::string::reverseString(aIn);
398 CPPUNIT_ASSERT(aOut == "CBA");
401 void TestString::testEqualsString()
403 OString aIn("A");
404 CPPUNIT_ASSERT(::comphelper::string::equals(aIn, 'A'));
405 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'B'));
406 aIn = OString("AA");
407 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'A'));
408 aIn.clear();
409 CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'A'));
412 int sign(int n)
414 if (n == 0)
415 return 0;
416 if (n < 0)
417 return -1;
418 else
419 return 1;
422 void TestString::testCompareVersionStrings()
424 #ifdef TEST
425 #error TEST already defined
426 #endif
427 #define TEST(a,b,result) \
428 CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(a, b)) == result); \
429 if ( result != 0 ) \
430 CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(b, a)) == -(result))
432 TEST("", "", 0);
433 TEST("", "0", -1);
434 TEST("", "a", -1);
435 TEST("0", "1", -1);
436 TEST("1", "2", -1);
437 TEST("2", "10", -1);
438 TEST("01", "1", -1);
439 TEST("01", "001", 1);
440 TEST("1.00", "1", 1);
441 TEST("1.2", "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);
451 #undef TEST
454 CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
458 CPPUNIT_PLUGIN_IMPLEMENT();
460 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */