LCOV - code coverage report
Current view: top level - comphelper/qa/string - test_string.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 175 225 77.8 %
Date: 2014-11-03 Functions: 29 54 53.7 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include <comphelper/string.hxx>
      21             : #include <cppuhelper/implbase1.hxx>
      22             : #include <com/sun/star/i18n/CharType.hpp>
      23             : 
      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>
      30             : 
      31             : namespace {
      32             : 
      33          66 : class TestString: public CppUnit::TestFixture
      34             : {
      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             : 
      48           4 :     CPPUNIT_TEST_SUITE(TestString);
      49           2 :     CPPUNIT_TEST(testNatural);
      50           2 :     CPPUNIT_TEST(testRemove);
      51           2 :     CPPUNIT_TEST(testStripStart);
      52           2 :     CPPUNIT_TEST(testStripEnd);
      53           2 :     CPPUNIT_TEST(testStrip);
      54           2 :     CPPUNIT_TEST(testToken);
      55           2 :     CPPUNIT_TEST(testTokenCount);
      56           2 :     CPPUNIT_TEST(testDecimalStringToNumber);
      57           2 :     CPPUNIT_TEST(testIsdigitAsciiString);
      58           2 :     CPPUNIT_TEST(testReverseString);
      59           2 :     CPPUNIT_TEST(testEqualsString);
      60           4 :     CPPUNIT_TEST_SUITE_END();
      61             : };
      62             : 
      63           2 : void TestString::testDecimalStringToNumber()
      64             : {
      65           2 :     OUString s1("1234");
      66           2 :     CPPUNIT_ASSERT_EQUAL((sal_uInt32)1234, comphelper::string::decimalStringToNumber(s1));
      67           2 :     s1 += OUString(static_cast<sal_Unicode>(0x07C6));
      68           2 :     CPPUNIT_ASSERT_EQUAL((sal_uInt32)12346, comphelper::string::decimalStringToNumber(s1));
      69             :     // Codepoints on 2 16bits words
      70           2 :     sal_uInt32 utf16String[] = { 0x1D7FE /* 8 */, 0x1D7F7 /* 1 */};
      71           2 :     s1 = OUString(utf16String, 2);
      72           2 :     CPPUNIT_ASSERT_EQUAL((sal_uInt32)81, comphelper::string::decimalStringToNumber(s1));
      73           2 : }
      74             : 
      75           2 : void TestString::testIsdigitAsciiString()
      76             : {
      77           2 :     OString s1("1234");
      78           2 :     CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s1), true);
      79             : 
      80           4 :     OString s2("1A34");
      81           2 :     CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s2), false);
      82             : 
      83           4 :     OString s3;
      84           4 :     CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3), true);
      85           2 : }
      86             : 
      87             : using namespace ::com::sun::star;
      88             : 
      89           6 : class testCollator : public cppu::WeakImplHelper1< i18n::XCollator >
      90             : {
      91             : public:
      92          26 :     virtual sal_Int32 SAL_CALL compareSubstring(
      93             :         const OUString& str1, sal_Int32 off1, sal_Int32 len1,
      94             :         const OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
      95             :     {
      96          26 :         return str1.copy(off1, len1).compareTo(str2.copy(off2, len2));
      97             :     }
      98           0 :     virtual sal_Int32 SAL_CALL compareString(
      99             :         const OUString& str1,
     100             :         const OUString& str2) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     101             :     {
     102           0 :         return str1.compareTo(str2);
     103             :     }
     104           0 :     virtual sal_Int32 SAL_CALL loadDefaultCollator(const lang::Locale&, sal_Int32)
     105           0 :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE {return 0;}
     106           0 :     virtual sal_Int32 SAL_CALL loadCollatorAlgorithm(const OUString&,
     107           0 :         const lang::Locale&, sal_Int32) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE {return 0;}
     108           0 :     virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption(const OUString&,
     109           0 :         const lang::Locale&, const uno::Sequence< sal_Int32 >&) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE {}
     110           0 :     virtual uno::Sequence< OUString > SAL_CALL listCollatorAlgorithms(const lang::Locale&)
     111             :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     112             :     {
     113           0 :         return uno::Sequence< OUString >();
     114             :     }
     115           0 :     virtual uno::Sequence< sal_Int32 > SAL_CALL listCollatorOptions(const OUString&)
     116             :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     117             :     {
     118           0 :         return uno::Sequence< sal_Int32 >();
     119             :     }
     120             : };
     121             : 
     122             : #define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
     123             : 
     124           6 : class testBreakIterator : public cppu::WeakImplHelper1< i18n::XBreakIterator >
     125             : {
     126             : public:
     127           0 :     virtual sal_Int32 SAL_CALL nextCharacters( const OUString&, sal_Int32,
     128             :         const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& )
     129           0 :             throw(uno::RuntimeException, std::exception) SAL_OVERRIDE {return -1;}
     130           0 :     virtual sal_Int32 SAL_CALL previousCharacters( const OUString&, sal_Int32,
     131             :         const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& )
     132           0 :             throw(uno::RuntimeException, std::exception) SAL_OVERRIDE {return -1;}
     133             : 
     134           0 :     virtual i18n::Boundary SAL_CALL previousWord( const OUString&, sal_Int32,
     135             :         const lang::Locale&, sal_Int16) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     136           0 :         { return i18n::Boundary(); }
     137           0 :     virtual i18n::Boundary SAL_CALL nextWord( const OUString&, sal_Int32,
     138             :         const lang::Locale&, sal_Int16) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     139           0 :         { return i18n::Boundary(); }
     140           0 :     virtual i18n::Boundary SAL_CALL getWordBoundary( const OUString&, sal_Int32,
     141             :         const lang::Locale&, sal_Int16, sal_Bool )
     142             :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     143           0 :         { return i18n::Boundary(); }
     144             : 
     145           0 :     virtual sal_Bool SAL_CALL isBeginWord( const OUString&, sal_Int32,
     146             :         const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     147           0 :         { return false; }
     148           0 :     virtual sal_Bool SAL_CALL isEndWord( const OUString&, sal_Int32,
     149             :         const lang::Locale& , sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     150           0 :         { return false; }
     151           0 :     virtual sal_Int16 SAL_CALL getWordType( const OUString&, sal_Int32,
     152             :         const lang::Locale& ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     153           0 :         { return 0; }
     154             : 
     155           0 :     virtual sal_Int32 SAL_CALL beginOfSentence( const OUString&, sal_Int32,
     156             :         const lang::Locale& ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     157           0 :         { return 0; }
     158           0 :     virtual sal_Int32 SAL_CALL endOfSentence( const OUString& rText, sal_Int32,
     159             :         const lang::Locale& ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     160           0 :         { return rText.getLength(); }
     161             : 
     162           0 :     virtual i18n::LineBreakResults SAL_CALL getLineBreak( const OUString&, sal_Int32,
     163             :         const lang::Locale&, sal_Int32,
     164             :         const i18n::LineBreakHyphenationOptions&,
     165             :         const i18n::LineBreakUserOptions&)
     166             :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     167             :     {
     168           0 :         return i18n::LineBreakResults();
     169             :     }
     170             : 
     171           0 :     virtual sal_Int16 SAL_CALL getScriptType( const OUString&, sal_Int32 )
     172           0 :         throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     173           0 :     virtual sal_Int32 SAL_CALL beginOfScript( const OUString&, sal_Int32,
     174           0 :         sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     175           0 :     virtual sal_Int32 SAL_CALL endOfScript( const OUString&, sal_Int32,
     176           0 :         sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     177           0 :     virtual sal_Int32 SAL_CALL previousScript( const OUString&, sal_Int32,
     178           0 :         sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     179           0 :     virtual sal_Int32 SAL_CALL nextScript( const OUString&, sal_Int32,
     180           0 :         sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     181             : 
     182           0 :     virtual sal_Int32 SAL_CALL beginOfCharBlock( const OUString&, sal_Int32,
     183           0 :         const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     184          36 :     virtual sal_Int32 SAL_CALL endOfCharBlock( const OUString& rText, sal_Int32 nStartPos,
     185             :         const lang::Locale&, sal_Int16 CharType ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     186             :     {
     187          36 :         const sal_Unicode *pStr = rText.getStr()+nStartPos;
     188          88 :         for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
     189             :         {
     190          64 :             if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
     191          12 :                 return nI;
     192          52 :             else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
     193           0 :                 return nI;
     194          52 :             ++pStr;
     195             :         }
     196          24 :         return -1;
     197             :     }
     198           0 :     virtual sal_Int32 SAL_CALL previousCharBlock( const OUString&, sal_Int32,
     199           0 :         const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE { return -1; }
     200          52 :     virtual sal_Int32 SAL_CALL nextCharBlock( const OUString& rText, sal_Int32 nStartPos,
     201             :         const lang::Locale&, sal_Int16 CharType ) throw(uno::RuntimeException, std::exception) SAL_OVERRIDE
     202             :     {
     203          52 :         const sal_Unicode *pStr = rText.getStr()+nStartPos;
     204         392 :         for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
     205             :         {
     206         368 :             if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
     207          28 :                 return nI;
     208         340 :             else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
     209           0 :                 return nI;
     210         340 :             ++pStr;
     211             :         }
     212          24 :         return -1;
     213             :     }
     214             : };
     215             : 
     216           2 : void TestString::testNatural()
     217             : {
     218             :     using namespace comphelper::string;
     219             : 
     220           2 :     uno::Reference< i18n::XCollator > xCollator(new testCollator);
     221           4 :     uno::Reference< i18n::XBreakIterator > xBI(new testBreakIterator);
     222             : 
     223             : // --- Some generic tests to ensure we do not alter original behavior
     224             : // outside what we want
     225           4 :     CPPUNIT_ASSERT(
     226             :         compareNatural("ABC", "ABC", xCollator, xBI, lang::Locale()) == 0
     227           2 :     );
     228             :     // Case sensitivity
     229           4 :     CPPUNIT_ASSERT(
     230             :         compareNatural("ABC", "abc", xCollator, xBI, lang::Locale()) < 0
     231           2 :     );
     232             :     // Reverse
     233           4 :     CPPUNIT_ASSERT(
     234             :         compareNatural("abc", "ABC", xCollator, xBI, lang::Locale()) > 0
     235           2 :     );
     236             :     // First shorter
     237           4 :     CPPUNIT_ASSERT(
     238             :         compareNatural("alongstring", "alongerstring", xCollator, xBI, lang::Locale()) > 0
     239           2 :     );
     240             :     // Second shorter
     241           4 :     CPPUNIT_ASSERT(
     242             :         compareNatural("alongerstring", "alongstring", xCollator, xBI, lang::Locale()) < 0
     243           2 :     );
     244             : // -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
     245             :     // That's why we originally made the patch
     246           4 :     CPPUNIT_ASSERT(
     247             :         compareNatural("Heading 9", "Heading 10", xCollator, xBI, lang::Locale()) < 0
     248           2 :     );
     249             :     // Original behavior
     250           4 :     CPPUNIT_ASSERT(
     251             :         OUString("Heading 9").compareTo("Heading 10") > 0
     252           2 :     );
     253           4 :     CPPUNIT_ASSERT(
     254             :         compareNatural("Heading 10", "Heading 9", xCollator, xBI, lang::Locale()) > 0
     255           2 :     );
     256             :     // Harder
     257           4 :     CPPUNIT_ASSERT(
     258             :         compareNatural("July, the 4th", "July, the 10th", xCollator, xBI, lang::Locale()) < 0
     259           2 :     );
     260           4 :     CPPUNIT_ASSERT(
     261             :         OUString("July, the 4th").compareTo("July, the 10th") > 0
     262           2 :     );
     263           4 :     CPPUNIT_ASSERT(
     264             :         compareNatural("July, the 10th", "July, the 4th", xCollator, xBI, lang::Locale()) > 0
     265           2 :     );
     266             :     // Hardest
     267           4 :     CPPUNIT_ASSERT(
     268             :         compareNatural("abc08", "abc010", xCollator, xBI, lang::Locale()) < 0
     269           2 :     );
     270           4 :     CPPUNIT_ASSERT(
     271             :         OUString("abc08").compareTo("abc010") > 0
     272           2 :     );
     273           4 :     CPPUNIT_ASSERT(
     274             :         compareNatural("abc010", "abc08", xCollator, xBI, lang::Locale()) > 0
     275           2 :     );
     276           4 :     CPPUNIT_ASSERT(
     277             :         compareNatural("apple10apple", "apple10apple", xCollator, xBI, lang::Locale()) == 0
     278           4 :     );
     279           2 : }
     280             : 
     281           2 : void TestString::testRemove()
     282             : {
     283           2 :     OString aIn("abc");
     284           4 :     OString aOut;
     285             : 
     286           2 :     aOut = ::comphelper::string::remove(aIn, 'b');
     287           2 :     CPPUNIT_ASSERT(aOut == "ac");
     288             : 
     289           2 :     aIn = "aaa";
     290             : 
     291           2 :     aOut = ::comphelper::string::remove(aIn, 'a');
     292           4 :     CPPUNIT_ASSERT(aOut.isEmpty());
     293           2 : }
     294             : 
     295           2 : void TestString::testStripStart()
     296             : {
     297           2 :     OString aIn("abc");
     298           4 :     OString aOut;
     299             : 
     300           2 :     aOut = ::comphelper::string::stripStart(aIn, 'b');
     301           2 :     CPPUNIT_ASSERT(aOut == "abc");
     302             : 
     303           2 :     aOut = ::comphelper::string::stripStart(aIn, 'a');
     304           2 :     CPPUNIT_ASSERT(aOut == "bc");
     305             : 
     306           2 :     aIn = "aaa";
     307           2 :     aOut = ::comphelper::string::stripStart(aIn, 'a');
     308           2 :     CPPUNIT_ASSERT(aOut.isEmpty());
     309             : 
     310           2 :     aIn = "aba";
     311           2 :     aOut = ::comphelper::string::stripStart(aIn, 'a');
     312           4 :     CPPUNIT_ASSERT(aOut == "ba");
     313           2 : }
     314             : 
     315           2 : void TestString::testStripEnd()
     316             : {
     317           2 :     OString aIn("abc");
     318           4 :     OString aOut;
     319             : 
     320           2 :     aOut = ::comphelper::string::stripEnd(aIn, 'b');
     321           2 :     CPPUNIT_ASSERT(aOut == "abc");
     322             : 
     323           2 :     aOut = ::comphelper::string::stripEnd(aIn, 'c');
     324           2 :     CPPUNIT_ASSERT(aOut == "ab");
     325             : 
     326           2 :     aIn = "aaa";
     327           2 :     aOut = ::comphelper::string::stripEnd(aIn, 'a');
     328           2 :     CPPUNIT_ASSERT(aOut.isEmpty());
     329             : 
     330           2 :     aIn = "aba";
     331           2 :     aOut = ::comphelper::string::stripEnd(aIn, 'a');
     332           4 :     CPPUNIT_ASSERT(aOut == "ab");
     333           2 : }
     334             : 
     335           2 : void TestString::testStrip()
     336             : {
     337           2 :     OString aIn("abc");
     338           4 :     OString aOut;
     339             : 
     340           2 :     aOut = ::comphelper::string::strip(aIn, 'b');
     341           2 :     CPPUNIT_ASSERT(aOut == "abc");
     342             : 
     343           2 :     aOut = ::comphelper::string::strip(aIn, 'c');
     344           2 :     CPPUNIT_ASSERT(aOut == "ab");
     345             : 
     346           2 :     aIn = "aaa";
     347           2 :     aOut = ::comphelper::string::strip(aIn, 'a');
     348           2 :     CPPUNIT_ASSERT(aOut.isEmpty());
     349             : 
     350           2 :     aIn = "aba";
     351           2 :     aOut = ::comphelper::string::strip(aIn, 'a');
     352           4 :     CPPUNIT_ASSERT(aOut == "b");
     353           2 : }
     354             : 
     355           2 : void TestString::testToken()
     356             : {
     357           2 :     OString aIn("10.11.12");
     358           4 :     OString aOut;
     359             : 
     360           2 :     aOut = ::comphelper::string::getToken(aIn, -1, '.');
     361           2 :     CPPUNIT_ASSERT(aOut.isEmpty());
     362             : 
     363           2 :     aOut = ::comphelper::string::getToken(aIn, 0, '.');
     364           2 :     CPPUNIT_ASSERT(aOut == "10");
     365             : 
     366           2 :     aOut = ::comphelper::string::getToken(aIn, 1, '.');
     367           2 :     CPPUNIT_ASSERT(aOut == "11");
     368             : 
     369           2 :     aOut = ::comphelper::string::getToken(aIn, 2, '.');
     370           2 :     CPPUNIT_ASSERT(aOut == "12");
     371             : 
     372           2 :     aOut = ::comphelper::string::getToken(aIn, 3, '.');
     373           4 :     CPPUNIT_ASSERT(aOut.isEmpty());
     374           2 : }
     375             : 
     376           2 : void TestString::testTokenCount()
     377             : {
     378           2 :     OString aIn("10.11.12");
     379             :     sal_Int32 nOut;
     380             : 
     381           2 :     nOut = ::comphelper::string::getTokenCount(aIn, '.');
     382           2 :     CPPUNIT_ASSERT(nOut == 3);
     383             : 
     384           2 :     nOut = ::comphelper::string::getTokenCount(aIn, 'X');
     385           2 :     CPPUNIT_ASSERT(nOut == 1);
     386             : 
     387           2 :     nOut = ::comphelper::string::getTokenCount(OString(), 'X');
     388           2 :     CPPUNIT_ASSERT(nOut == 0);
     389           2 : }
     390             : 
     391           2 : void TestString::testReverseString()
     392             : {
     393           2 :     OString aIn("ABC");
     394           4 :     OString aOut = ::comphelper::string::reverseString(aIn);
     395             : 
     396           4 :     CPPUNIT_ASSERT(aOut == "CBA");
     397           2 : }
     398             : 
     399           2 : void TestString::testEqualsString()
     400             : {
     401           2 :     OString aIn("A");
     402           2 :     CPPUNIT_ASSERT(::comphelper::string::equals(aIn, 'A'));
     403           2 :     CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'B'));
     404           2 :     aIn = OString("AA");
     405           2 :     CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'A'));
     406           2 :     aIn = OString();
     407           2 :     CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'A'));
     408           2 : }
     409             : 
     410           2 : CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
     411             : 
     412             : }
     413             : 
     414           8 : CPPUNIT_PLUGIN_IMPLEMENT();
     415             : 
     416             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10