2010-10-14 08:27:31 +02:00
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2007-11-23 12:15:02 +00:00
/*************************************************************************
*
2008-04-11 09:51:52 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
2007-11-23 12:15:02 +00:00
*
2010-02-12 15:01:35 +01:00
* Copyright 2000 , 2010 Oracle and / or its affiliates .
2007-11-23 12:15:02 +00:00
*
2008-04-11 09:51:52 +00:00
* OpenOffice . org - a multi - platform office productivity suite
2007-11-23 12:15:02 +00:00
*
2008-04-11 09:51:52 +00:00
* This file is part of OpenOffice . org .
2007-11-23 12:15:02 +00:00
*
2008-04-11 09:51:52 +00:00
* OpenOffice . org is free software : you can redistribute it and / or modify
* it under the terms of the GNU Lesser General Public License version 3
* only , as published by the Free Software Foundation .
2007-11-23 12:15:02 +00:00
*
2008-04-11 09:51:52 +00:00
* OpenOffice . org 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 Lesser General Public License version 3 for more details
* ( a copy is included in the LICENSE file that accompanied this code ) .
2007-11-23 12:15:02 +00:00
*
2008-04-11 09:51:52 +00:00
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice . org . If not , see
* < http : //www.openoffice.org/license.html>
* for a copy of the LGPLv3 License .
2007-11-23 12:15:02 +00:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "precompiled_comphelper.hxx"
2011-09-12 21:24:31 +02:00
# include "sal/precppunit.hxx"
2007-11-23 12:15:02 +00:00
2011-08-14 23:46:37 +01:00
# include <comphelper/string.hxx>
2011-02-14 15:37:58 +00:00
# include <cppuhelper/implbase1.hxx>
# include <com/sun/star/i18n/CharType.hpp>
2011-02-09 10:51:01 +01:00
2011-09-12 21:24:31 +02:00
# include "cppunit/TestAssert.h"
# include "cppunit/TestFixture.h"
# include "cppunit/extensions/HelperMacros.h"
# include "cppunit/plugin/TestPlugIn.h"
2011-08-14 23:46:37 +01:00
# include "rtl/string.hxx"
2007-11-23 12:15:02 +00:00
# include "rtl/ustring.hxx"
namespace {
2011-02-14 11:22:58 +00:00
class TestString : public CppUnit : : TestFixture
{
2007-11-23 12:15:02 +00:00
public :
2011-08-15 09:11:35 +01:00
void testSearchAndReplaceAsciiL ( ) ;
2011-02-14 11:22:58 +00:00
void testNatural ( ) ;
2011-08-15 00:10:42 +01:00
void testReplace ( ) ;
2011-11-16 22:19:58 +00:00
void testRemove ( ) ;
2011-08-15 00:22:23 +01:00
void testToken ( ) ;
2011-02-17 10:41:21 +01:00
void testDecimalStringToNumber ( ) ;
2011-08-28 00:17:42 +01:00
void testIsdigitAsciiString ( ) ;
void testIsalnumAsciiString ( ) ;
2011-08-28 23:08:39 +01:00
void testIsupperAsciiString ( ) ;
2011-11-09 22:07:24 +00:00
void testIndexOfL ( ) ;
2007-11-23 12:15:02 +00:00
2011-02-05 22:56:20 +01:00
CPPUNIT_TEST_SUITE ( TestString ) ;
2011-08-15 09:11:35 +01:00
CPPUNIT_TEST ( testSearchAndReplaceAsciiL ) ;
2011-02-14 11:22:58 +00:00
CPPUNIT_TEST ( testNatural ) ;
2011-08-15 00:10:42 +01:00
CPPUNIT_TEST ( testReplace ) ;
2011-11-16 22:19:58 +00:00
CPPUNIT_TEST ( testRemove ) ;
2011-08-15 00:22:23 +01:00
CPPUNIT_TEST ( testToken ) ;
2011-02-17 10:41:21 +01:00
CPPUNIT_TEST ( testDecimalStringToNumber ) ;
2011-08-28 00:17:42 +01:00
CPPUNIT_TEST ( testIsdigitAsciiString ) ;
CPPUNIT_TEST ( testIsalnumAsciiString ) ;
2011-08-28 23:08:39 +01:00
CPPUNIT_TEST ( testIsupperAsciiString ) ;
2011-11-09 22:07:24 +00:00
CPPUNIT_TEST ( testIndexOfL ) ;
2007-11-23 12:15:02 +00:00
CPPUNIT_TEST_SUITE_END ( ) ;
} ;
2011-08-15 09:11:35 +01:00
void TestString : : testSearchAndReplaceAsciiL ( )
2011-02-14 11:22:58 +00:00
{
2007-11-23 12:15:02 +00:00
rtl : : OUString s1 ( RTL_CONSTASCII_USTRINGPARAM ( " foobarbar " ) ) ;
sal_Int32 n1 ;
rtl : : OUString s2 (
2011-02-05 22:56:20 +01:00
comphelper : : string : : searchAndReplaceAsciiL (
2007-11-23 12:15:02 +00:00
s1 , RTL_CONSTASCII_STRINGPARAM ( " bar " ) ,
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " baaz " ) ) , 0 , & n1 ) ) ;
CPPUNIT_ASSERT (
s2 = = rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " foobaazbar " ) ) ) ;
CPPUNIT_ASSERT ( n1 = = 3 ) ;
sal_Int32 n2 ;
rtl : : OUString s3 (
2011-02-05 22:56:20 +01:00
comphelper : : string : : searchAndReplaceAsciiL (
2007-11-23 12:15:02 +00:00
s2 , RTL_CONSTASCII_STRINGPARAM ( " bar " ) ,
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " bz " ) ) ,
n1 + RTL_CONSTASCII_LENGTH ( " baaz " ) , & n2 ) ) ;
CPPUNIT_ASSERT (
s3 = = rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " foobaazbz " ) ) ) ;
CPPUNIT_ASSERT ( n2 = = 7 ) ;
sal_Int32 n3 ;
rtl : : OUString s4 (
2011-02-05 22:56:20 +01:00
comphelper : : string : : searchAndReplaceAsciiL (
2007-11-23 12:15:02 +00:00
s3 , RTL_CONSTASCII_STRINGPARAM ( " bar " ) ,
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( " baz " ) ) ,
n2 + RTL_CONSTASCII_LENGTH ( " bz " ) , & n3 ) ) ;
CPPUNIT_ASSERT ( s4 = = s3 ) ;
CPPUNIT_ASSERT ( n3 = = - 1 ) ;
}
2011-03-16 10:13:09 +00:00
void TestString : : testDecimalStringToNumber ( )
{
2011-02-17 10:41:21 +01:00
rtl : : OUString s1 ( RTL_CONSTASCII_USTRINGPARAM ( " 1234 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( ( sal_uInt32 ) 1234 , comphelper : : string : : decimalStringToNumber ( s1 ) ) ;
2011-03-16 10:13:09 +00:00
s1 + = rtl : : OUString ( static_cast < sal_Unicode > ( 0x07C6 ) ) ;
2011-02-17 10:41:21 +01:00
CPPUNIT_ASSERT_EQUAL ( ( sal_uInt32 ) 12346 , comphelper : : string : : decimalStringToNumber ( s1 ) ) ;
// Codepoints on 2 16bits words
sal_uInt32 utf16String [ ] = { 0x1D7FE /* 8 */ , 0x1D7F7 /* 1 */ } ;
s1 = rtl : : OUString ( utf16String , 2 ) ;
CPPUNIT_ASSERT_EQUAL ( ( sal_uInt32 ) 81 , comphelper : : string : : decimalStringToNumber ( s1 ) ) ;
}
2011-08-28 00:17:42 +01:00
void TestString : : testIsdigitAsciiString ( )
{
rtl : : OString s1 ( RTL_CONSTASCII_STRINGPARAM ( " 1234 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isdigitAsciiString ( s1 ) , true ) ;
rtl : : OString s2 ( RTL_CONSTASCII_STRINGPARAM ( " 1A34 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isdigitAsciiString ( s2 ) , false ) ;
rtl : : OString s3 ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isdigitAsciiString ( s3 ) , true ) ;
}
void TestString : : testIsalnumAsciiString ( )
{
rtl : : OString s1 ( RTL_CONSTASCII_STRINGPARAM ( " 1234 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isalnumAsciiString ( s1 ) , true ) ;
rtl : : OString s2 ( RTL_CONSTASCII_STRINGPARAM ( " 1A34 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isalnumAsciiString ( s2 ) , true ) ;
rtl : : OString s3 ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isalnumAsciiString ( s3 ) , true ) ;
rtl : : OString s4 ( RTL_CONSTASCII_STRINGPARAM ( " 1A[4 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isalnumAsciiString ( s4 ) , false ) ;
}
2011-08-28 23:08:39 +01:00
void TestString : : testIsupperAsciiString ( )
{
rtl : : OString s1 ( RTL_CONSTASCII_STRINGPARAM ( " 1234 " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isupperAsciiString ( s1 ) , false ) ;
rtl : : OString s2 ( RTL_CONSTASCII_STRINGPARAM ( " aAbB " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isupperAsciiString ( s2 ) , false ) ;
rtl : : OString s3 ( RTL_CONSTASCII_STRINGPARAM ( " AABB " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : isupperAsciiString ( s3 ) , true ) ;
}
2011-11-09 22:07:24 +00:00
void TestString : : testIndexOfL ( )
{
rtl : : OString s1 ( RTL_CONSTASCII_STRINGPARAM ( " one two three " ) ) ;
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : indexOfL ( s1 ,
2011-11-10 11:16:51 +00:00
RTL_CONSTASCII_STRINGPARAM ( " one " ) ) , static_cast < sal_Int32 > ( 0 ) ) ;
2011-11-09 22:07:24 +00:00
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : indexOfL ( s1 ,
2011-11-10 11:16:51 +00:00
RTL_CONSTASCII_STRINGPARAM ( " two " ) ) , static_cast < sal_Int32 > ( 4 ) ) ;
2011-11-09 22:07:24 +00:00
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : indexOfL ( s1 ,
2011-11-10 11:16:51 +00:00
RTL_CONSTASCII_STRINGPARAM ( " four " ) ) , static_cast < sal_Int32 > ( - 1 ) ) ;
2011-11-09 22:07:24 +00:00
CPPUNIT_ASSERT_EQUAL ( comphelper : : string : : indexOfL ( s1 ,
2011-11-10 11:16:51 +00:00
RTL_CONSTASCII_STRINGPARAM ( " two " ) , 5 ) , static_cast < sal_Int32 > ( - 1 ) ) ;
2011-11-09 22:07:24 +00:00
}
2011-02-14 15:37:58 +00:00
using namespace : : com : : sun : : star ;
class testCollator : public cppu : : WeakImplHelper1 < i18n : : XCollator >
{
public :
virtual sal_Int32 SAL_CALL compareSubstring (
const rtl : : OUString & str1 , sal_Int32 off1 , sal_Int32 len1 ,
const rtl : : OUString & str2 , sal_Int32 off2 , sal_Int32 len2 ) throw ( uno : : RuntimeException )
{
return str1 . copy ( off1 , len1 ) . compareTo ( str2 . copy ( off2 , len2 ) ) ;
}
virtual sal_Int32 SAL_CALL compareString (
const rtl : : OUString & str1 ,
const rtl : : OUString & str2 ) throw ( uno : : RuntimeException )
{
return str1 . compareTo ( str2 ) ;
}
virtual sal_Int32 SAL_CALL loadDefaultCollator ( const lang : : Locale & , sal_Int32 )
throw ( uno : : RuntimeException ) { return 0 ; }
virtual sal_Int32 SAL_CALL loadCollatorAlgorithm ( const rtl : : OUString & ,
const lang : : Locale & , sal_Int32 ) throw ( uno : : RuntimeException ) { return 0 ; }
virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption ( const rtl : : OUString & ,
const lang : : Locale & , const uno : : Sequence < sal_Int32 > & ) throw ( uno : : RuntimeException ) { }
virtual uno : : Sequence < rtl : : OUString > SAL_CALL listCollatorAlgorithms ( const lang : : Locale & )
throw ( uno : : RuntimeException )
{
return uno : : Sequence < rtl : : OUString > ( ) ;
}
virtual uno : : Sequence < sal_Int32 > SAL_CALL listCollatorOptions ( const rtl : : OUString & )
throw ( uno : : RuntimeException )
{
return uno : : Sequence < sal_Int32 > ( ) ;
}
} ;
# define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))
class testBreakIterator : public cppu : : WeakImplHelper1 < i18n : : XBreakIterator >
{
public :
virtual sal_Int32 SAL_CALL nextCharacters ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 , sal_Int32 , sal_Int32 & )
throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL previousCharacters ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 , sal_Int32 , sal_Int32 & )
throw ( uno : : RuntimeException ) { return - 1 ; }
virtual i18n : : Boundary SAL_CALL previousWord ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException )
{ return i18n : : Boundary ( ) ; }
virtual i18n : : Boundary SAL_CALL nextWord ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException )
{ return i18n : : Boundary ( ) ; }
virtual i18n : : Boundary SAL_CALL getWordBoundary ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 , sal_Bool )
throw ( uno : : RuntimeException )
{ return i18n : : Boundary ( ) ; }
virtual sal_Bool SAL_CALL isBeginWord ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException )
{ return false ; }
virtual sal_Bool SAL_CALL isEndWord ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException )
{ return false ; }
virtual sal_Int16 SAL_CALL getWordType ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & ) throw ( uno : : RuntimeException )
{ return 0 ; }
virtual sal_Int32 SAL_CALL beginOfSentence ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & ) throw ( uno : : RuntimeException )
{ return 0 ; }
virtual sal_Int32 SAL_CALL endOfSentence ( const rtl : : OUString & rText , sal_Int32 ,
const lang : : Locale & ) throw ( uno : : RuntimeException )
{ return rText . getLength ( ) ; }
virtual i18n : : LineBreakResults SAL_CALL getLineBreak ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int32 ,
const i18n : : LineBreakHyphenationOptions & ,
const i18n : : LineBreakUserOptions & )
throw ( uno : : RuntimeException )
{
return i18n : : LineBreakResults ( ) ;
}
virtual sal_Int16 SAL_CALL getScriptType ( const rtl : : OUString & , sal_Int32 )
throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL beginOfScript ( const rtl : : OUString & , sal_Int32 ,
sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL endOfScript ( const rtl : : OUString & , sal_Int32 ,
sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL previousScript ( const rtl : : OUString & , sal_Int32 ,
sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL nextScript ( const rtl : : OUString & , sal_Int32 ,
sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL beginOfCharBlock ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL endOfCharBlock ( const rtl : : OUString & rText , sal_Int32 nStartPos ,
const lang : : Locale & , sal_Int16 CharType ) throw ( uno : : RuntimeException )
{
const sal_Unicode * pStr = rText . getStr ( ) + nStartPos ;
2011-02-18 21:26:40 +00:00
for ( sal_Int32 nI = nStartPos ; nI < rText . getLength ( ) ; + + nI )
2011-02-14 15:37:58 +00:00
{
if ( CharType = = i18n : : CharType : : DECIMAL_DIGIT_NUMBER & & ! IS_DIGIT ( * pStr ) )
return nI ;
else if ( CharType ! = i18n : : CharType : : DECIMAL_DIGIT_NUMBER & & IS_DIGIT ( * pStr ) )
return nI ;
+ + pStr ;
}
return - 1 ;
}
virtual sal_Int32 SAL_CALL previousCharBlock ( const rtl : : OUString & , sal_Int32 ,
const lang : : Locale & , sal_Int16 ) throw ( uno : : RuntimeException ) { return - 1 ; }
virtual sal_Int32 SAL_CALL nextCharBlock ( const rtl : : OUString & rText , sal_Int32 nStartPos ,
const lang : : Locale & , sal_Int16 CharType ) throw ( uno : : RuntimeException )
{
const sal_Unicode * pStr = rText . getStr ( ) + nStartPos ;
2011-02-18 21:26:40 +00:00
for ( sal_Int32 nI = nStartPos ; nI < rText . getLength ( ) ; + + nI )
2011-02-14 15:37:58 +00:00
{
if ( CharType = = i18n : : CharType : : DECIMAL_DIGIT_NUMBER & & IS_DIGIT ( * pStr ) )
return nI ;
else if ( CharType ! = i18n : : CharType : : DECIMAL_DIGIT_NUMBER & & ! IS_DIGIT ( * pStr ) )
return nI ;
+ + pStr ;
}
return - 1 ;
}
} ;
2011-02-14 11:22:58 +00:00
void TestString : : testNatural ( )
{
using namespace comphelper : : string ;
2011-02-14 15:37:58 +00:00
uno : : Reference < i18n : : XCollator > xCollator ( new testCollator ) ;
uno : : Reference < i18n : : XBreakIterator > xBI ( new testBreakIterator ) ;
2011-02-14 11:22:58 +00:00
// --- Some generic tests to ensure we do not alter original behavior
// outside what we want
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " ABC " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " ABC " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) = = 0
2011-02-14 11:22:58 +00:00
) ;
// Case sensitivity
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " ABC " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) < 0
2011-02-14 11:22:58 +00:00
) ;
// Reverse
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " ABC " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) > 0
2011-02-14 11:22:58 +00:00
) ;
// First shorter
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " alongstring " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " alongerstring " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) > 0
2011-02-14 11:22:58 +00:00
) ;
// Second shorter
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " alongerstring " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " alongstring " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) < 0
2011-02-14 11:22:58 +00:00
) ;
// -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
// That's why we originally made the patch
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 9 " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 10 " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) < 0
2011-02-14 11:22:58 +00:00
) ;
// Original behavior
CPPUNIT_ASSERT (
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 9 " ) ) ) . compareTo ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 10 " ) ) ) ) > 0
) ;
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 10 " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " Heading 9 " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) > 0
2011-02-14 11:22:58 +00:00
) ;
// Harder
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 4th " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 10th " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) < 0
2011-02-14 11:22:58 +00:00
) ;
CPPUNIT_ASSERT (
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 4th " ) ) ) . compareTo ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 10th " ) ) ) ) > 0
) ;
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 10th " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " July, the 4th " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) > 0
2011-02-14 11:22:58 +00:00
) ;
// Hardest
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc08 " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc010 " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) < 0
2011-02-14 11:22:58 +00:00
) ;
CPPUNIT_ASSERT (
rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc08 " ) ) ) . compareTo ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc010 " ) ) ) ) > 0
) ;
CPPUNIT_ASSERT (
2011-02-14 15:37:58 +00:00
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc010 " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " abc08 " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) > 0
) ;
CPPUNIT_ASSERT (
compareNatural ( rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " apple10apple " ) ) ) , rtl : : OUString ( RTL_CONSTASCII_USTRINGPARAM ( ( " apple10apple " ) ) ) , xCollator , xBI , lang : : Locale ( ) ) = = 0
2011-02-14 11:22:58 +00:00
) ;
2007-11-23 12:15:02 +00:00
}
2011-08-15 00:10:42 +01:00
void TestString : : testReplace ( )
{
: : rtl : : OString aIn ( RTL_CONSTASCII_STRINGPARAM ( " aaa " ) ) ;
: : rtl : : OString aOut ;
aOut = : : comphelper : : string : : replace ( aIn ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " aa " ) ) ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " b " ) ) ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " ba " ) ) ) ;
aOut = : : comphelper : : string : : replace ( aIn ,
rtl : : OString ( ) ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " whatever " ) ) ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " aaa " ) ) ) ;
aOut = : : comphelper : : string : : replace ( aIn ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " aaa " ) ) ,
rtl : : OString ( ) ) ;
CPPUNIT_ASSERT ( aOut . isEmpty ( ) ) ;
aIn = rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " aaa foo aaa foo bbb " ) ) ;
2011-08-15 00:22:23 +01:00
2011-08-15 00:10:42 +01:00
aOut = : : comphelper : : string : : replace ( aIn ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " foo " ) ) ,
rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " bar " ) ) ) ;
CPPUNIT_ASSERT ( aOut . equalsL (
RTL_CONSTASCII_STRINGPARAM ( " aaa bar aaa bar bbb " ) ) ) ;
2011-08-15 00:22:23 +01:00
aOut = : : comphelper : : string : : replace ( aIn ,
rtl : : OString ( ' ' ) ,
rtl : : OString ( ) ) ;
CPPUNIT_ASSERT ( aOut . equalsL (
RTL_CONSTASCII_STRINGPARAM ( " aaafooaaafoobbb " ) ) ) ;
}
2011-11-16 22:19:58 +00:00
void TestString : : testRemove ( )
{
: : rtl : : OString aIn ( RTL_CONSTASCII_STRINGPARAM ( " abc " ) ) ;
: : rtl : : OString aOut ;
aOut = : : comphelper : : string : : remove ( aIn , ' b ' ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " ac " ) ) ) ;
aIn = rtl : : OString ( RTL_CONSTASCII_STRINGPARAM ( " aaa " ) ) ;
aOut = : : comphelper : : string : : remove ( aIn , ' a ' ) ;
CPPUNIT_ASSERT ( aOut . isEmpty ( ) ) ;
}
2011-08-15 00:22:23 +01:00
void TestString : : testToken ( )
{
: : rtl : : OString aIn ( RTL_CONSTASCII_STRINGPARAM ( " 10.11.12 " ) ) ;
: : rtl : : OString aOut ;
aOut = : : comphelper : : string : : getToken ( aIn , - 1 , ' . ' ) ;
CPPUNIT_ASSERT ( aOut . isEmpty ( ) ) ;
aOut = : : comphelper : : string : : getToken ( aIn , 0 , ' . ' ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " 10 " ) ) ) ;
aOut = : : comphelper : : string : : getToken ( aIn , 1 , ' . ' ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " 11 " ) ) ) ;
aOut = : : comphelper : : string : : getToken ( aIn , 2 , ' . ' ) ;
CPPUNIT_ASSERT ( aOut . equalsL ( RTL_CONSTASCII_STRINGPARAM ( " 12 " ) ) ) ;
aOut = : : comphelper : : string : : getToken ( aIn , 3 , ' . ' ) ;
CPPUNIT_ASSERT ( aOut . isEmpty ( ) ) ;
2011-08-15 00:10:42 +01:00
}
2011-02-14 11:22:58 +00:00
CPPUNIT_TEST_SUITE_REGISTRATION ( TestString ) ;
2011-02-05 23:02:36 +01:00
2011-02-14 11:22:58 +00:00
}
2010-10-14 08:27:31 +02:00
2011-08-14 23:46:37 +01:00
CPPUNIT_PLUGIN_IMPLEMENT ( ) ;
2010-10-14 08:27:31 +02:00
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */