1*87d2adbcSAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*87d2adbcSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*87d2adbcSAndrew Rist * or more contributor license agreements. See the NOTICE file 5*87d2adbcSAndrew Rist * distributed with this work for additional information 6*87d2adbcSAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*87d2adbcSAndrew Rist * to you under the Apache License, Version 2.0 (the 8*87d2adbcSAndrew Rist * "License"); you may not use this file except in compliance 9*87d2adbcSAndrew Rist * with the License. You may obtain a copy of the License at 10*87d2adbcSAndrew Rist * 11*87d2adbcSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*87d2adbcSAndrew Rist * 13*87d2adbcSAndrew Rist * Unless required by applicable law or agreed to in writing, 14*87d2adbcSAndrew Rist * software distributed under the License is distributed on an 15*87d2adbcSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*87d2adbcSAndrew Rist * KIND, either express or implied. See the License for the 17*87d2adbcSAndrew Rist * specific language governing permissions and limitations 18*87d2adbcSAndrew Rist * under the License. 19*87d2adbcSAndrew Rist * 20*87d2adbcSAndrew Rist *************************************************************/ 21*87d2adbcSAndrew Rist 22*87d2adbcSAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir 25cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 26cdf0e10cSrcweir #include "precompiled_sal.hxx" 27cdf0e10cSrcweir // autogenerated file with codegen.pl 28cdf0e10cSrcweir 29cdf0e10cSrcweir #include <math.h> 30cdf0e10cSrcweir #include <stdio.h> 31cdf0e10cSrcweir 32cdf0e10cSrcweir #include <algorithm> // STL 33cdf0e10cSrcweir 34cdf0e10cSrcweir #include <testshl/simpleheader.hxx> 35cdf0e10cSrcweir #include "stringhelper.hxx" 36cdf0e10cSrcweir #include "valueequal.hxx" 37cdf0e10cSrcweir 38cdf0e10cSrcweir inline void printOUString( ::rtl::OUString const & _suStr ) 39cdf0e10cSrcweir { 40cdf0e10cSrcweir rtl::OString aString; 41cdf0e10cSrcweir 42cdf0e10cSrcweir t_print( "OUString: " ); 43cdf0e10cSrcweir aString = ::rtl::OUStringToOString( _suStr, RTL_TEXTENCODING_ASCII_US ); 44cdf0e10cSrcweir t_print( "'%s'\n", aString.getStr( ) ); 45cdf0e10cSrcweir } 46cdf0e10cSrcweir 47cdf0e10cSrcweir namespace rtl_OUString 48cdf0e10cSrcweir { 49cdf0e10cSrcweir 50cdf0e10cSrcweir class ctors_rtl_uString : public CppUnit::TestFixture 51cdf0e10cSrcweir { 52cdf0e10cSrcweir 53cdf0e10cSrcweir public: 54cdf0e10cSrcweir /// test of OUString(rtl_uString*) 55cdf0e10cSrcweir void ctors_001() 56cdf0e10cSrcweir { 57cdf0e10cSrcweir rtl::OUString *pStr = new rtl::OUString( rtl::OUString::createFromAscii("a String") ); 58cdf0e10cSrcweir 59cdf0e10cSrcweir rtl::OUString aStrToTest(pStr->pData); 60cdf0e10cSrcweir delete pStr; 61cdf0e10cSrcweir 62cdf0e10cSrcweir // maybe here should we do something with current memory 63cdf0e10cSrcweir char* pBuffer = (char*) malloc(2 * 8); 64cdf0e10cSrcweir memset(pBuffer, 0, 2 * 8); 65cdf0e10cSrcweir free(pBuffer); 66cdf0e10cSrcweir 67cdf0e10cSrcweir sal_Bool bResult = aStrToTest.equals(rtl::OUString::createFromAscii("a String")); 68cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("String must not be empty", bResult == sal_True); 69cdf0e10cSrcweir } 70cdf0e10cSrcweir 71cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 72cdf0e10cSrcweir // member functions of the current class, 73cdf0e10cSrcweir // because these macros are need by auto register mechanism. 74cdf0e10cSrcweir 75cdf0e10cSrcweir CPPUNIT_TEST_SUITE(ctors_rtl_uString); 76cdf0e10cSrcweir CPPUNIT_TEST(ctors_001); 77cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 78cdf0e10cSrcweir }; 79cdf0e10cSrcweir 80cdf0e10cSrcweir // ----------------------------------------------------------------------------- 81cdf0e10cSrcweir class valueOf : public CppUnit::TestFixture 82cdf0e10cSrcweir { 83cdf0e10cSrcweir void valueOf_float_test_impl(float _nValue) 84cdf0e10cSrcweir { 85cdf0e10cSrcweir rtl::OUString suValue; 86cdf0e10cSrcweir suValue = rtl::OUString::valueOf( _nValue ); 87cdf0e10cSrcweir rtl::OString sValue; 88cdf0e10cSrcweir sValue <<= suValue; 89cdf0e10cSrcweir t_print(T_VERBOSE, "nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr()); 90cdf0e10cSrcweir 91cdf0e10cSrcweir float nValueATOF = static_cast<float>(atof( sValue.getStr() )); 92cdf0e10cSrcweir 93cdf0e10cSrcweir bool bEqualResult = is_float_equal(_nValue, nValueATOF); 94cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true); 95cdf0e10cSrcweir } 96cdf0e10cSrcweir 97cdf0e10cSrcweir void valueOf_float_test(float _nValue) 98cdf0e10cSrcweir { 99cdf0e10cSrcweir valueOf_float_test_impl(_nValue); 100cdf0e10cSrcweir 101cdf0e10cSrcweir // test also the negative part. 102cdf0e10cSrcweir float nNegativeValue = -_nValue; 103cdf0e10cSrcweir valueOf_float_test_impl(nNegativeValue); 104cdf0e10cSrcweir } 105cdf0e10cSrcweir 106cdf0e10cSrcweir public: 107cdf0e10cSrcweir // insert your test code here. 108cdf0e10cSrcweir void valueOf_float_test_001() 109cdf0e10cSrcweir { 110cdf0e10cSrcweir // this is demonstration code 111cdf0e10cSrcweir // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1); 112cdf0e10cSrcweir float nValue = 3.0f; 113cdf0e10cSrcweir valueOf_float_test(nValue); 114cdf0e10cSrcweir } 115cdf0e10cSrcweir 116cdf0e10cSrcweir void valueOf_float_test_002() 117cdf0e10cSrcweir { 118cdf0e10cSrcweir float nValue = 3.5f; 119cdf0e10cSrcweir valueOf_float_test(nValue); 120cdf0e10cSrcweir } 121cdf0e10cSrcweir 122cdf0e10cSrcweir void valueOf_float_test_003() 123cdf0e10cSrcweir { 124cdf0e10cSrcweir float nValue = 3.0625f; 125cdf0e10cSrcweir valueOf_float_test(nValue); 126cdf0e10cSrcweir } 127cdf0e10cSrcweir 128cdf0e10cSrcweir void valueOf_float_test_004() 129cdf0e10cSrcweir { 130cdf0e10cSrcweir float nValue = 3.502525f; 131cdf0e10cSrcweir valueOf_float_test(nValue); 132cdf0e10cSrcweir } 133cdf0e10cSrcweir 134cdf0e10cSrcweir void valueOf_float_test_005() 135cdf0e10cSrcweir { 136cdf0e10cSrcweir float nValue = 3.141592f; 137cdf0e10cSrcweir valueOf_float_test(nValue); 138cdf0e10cSrcweir } 139cdf0e10cSrcweir 140cdf0e10cSrcweir void valueOf_float_test_006() 141cdf0e10cSrcweir { 142cdf0e10cSrcweir float nValue = 3.5025255f; 143cdf0e10cSrcweir valueOf_float_test(nValue); 144cdf0e10cSrcweir } 145cdf0e10cSrcweir 146cdf0e10cSrcweir void valueOf_float_test_007() 147cdf0e10cSrcweir { 148cdf0e10cSrcweir float nValue = 3.0039062f; 149cdf0e10cSrcweir valueOf_float_test(nValue); 150cdf0e10cSrcweir } 151cdf0e10cSrcweir 152cdf0e10cSrcweir private: 153cdf0e10cSrcweir 154cdf0e10cSrcweir void valueOf_double_test_impl(double _nValue) 155cdf0e10cSrcweir { 156cdf0e10cSrcweir rtl::OUString suValue; 157cdf0e10cSrcweir suValue = rtl::OUString::valueOf( _nValue ); 158cdf0e10cSrcweir rtl::OString sValue; 159cdf0e10cSrcweir sValue <<= suValue; 160cdf0e10cSrcweir t_print(T_VERBOSE, "nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr()); 161cdf0e10cSrcweir 162cdf0e10cSrcweir double nValueATOF = atof( sValue.getStr() ); 163cdf0e10cSrcweir 164cdf0e10cSrcweir bool bEqualResult = is_double_equal(_nValue, nValueATOF); 165cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true); 166cdf0e10cSrcweir } 167cdf0e10cSrcweir 168cdf0e10cSrcweir void valueOf_double_test(double _nValue) 169cdf0e10cSrcweir { 170cdf0e10cSrcweir valueOf_double_test_impl(_nValue); 171cdf0e10cSrcweir 172cdf0e10cSrcweir // test also the negative part. 173cdf0e10cSrcweir double nNegativeValue = -_nValue; 174cdf0e10cSrcweir valueOf_double_test_impl(nNegativeValue); 175cdf0e10cSrcweir } 176cdf0e10cSrcweir public: 177cdf0e10cSrcweir 178cdf0e10cSrcweir // valueOf double 179cdf0e10cSrcweir void valueOf_double_test_001() 180cdf0e10cSrcweir { 181cdf0e10cSrcweir double nValue = 3.0; 182cdf0e10cSrcweir valueOf_double_test(nValue); 183cdf0e10cSrcweir } 184cdf0e10cSrcweir void valueOf_double_test_002() 185cdf0e10cSrcweir { 186cdf0e10cSrcweir double nValue = 3.5; 187cdf0e10cSrcweir valueOf_double_test(nValue); 188cdf0e10cSrcweir } 189cdf0e10cSrcweir void valueOf_double_test_003() 190cdf0e10cSrcweir { 191cdf0e10cSrcweir double nValue = 3.0625; 192cdf0e10cSrcweir valueOf_double_test(nValue); 193cdf0e10cSrcweir } 194cdf0e10cSrcweir void valueOf_double_test_004() 195cdf0e10cSrcweir { 196cdf0e10cSrcweir double nValue = 3.1415926535; 197cdf0e10cSrcweir valueOf_double_test(nValue); 198cdf0e10cSrcweir } 199cdf0e10cSrcweir void valueOf_double_test_005() 200cdf0e10cSrcweir { 201cdf0e10cSrcweir double nValue = 3.141592653589793; 202cdf0e10cSrcweir valueOf_double_test(nValue); 203cdf0e10cSrcweir } 204cdf0e10cSrcweir void valueOf_double_test_006() 205cdf0e10cSrcweir { 206cdf0e10cSrcweir double nValue = 3.1415926535897932; 207cdf0e10cSrcweir valueOf_double_test(nValue); 208cdf0e10cSrcweir } 209cdf0e10cSrcweir void valueOf_double_test_007() 210cdf0e10cSrcweir { 211cdf0e10cSrcweir double nValue = 3.14159265358979323; 212cdf0e10cSrcweir valueOf_double_test(nValue); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir void valueOf_double_test_008() 215cdf0e10cSrcweir { 216cdf0e10cSrcweir double nValue = 3.141592653589793238462643; 217cdf0e10cSrcweir valueOf_double_test(nValue); 218cdf0e10cSrcweir } 219cdf0e10cSrcweir 220cdf0e10cSrcweir 221cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 222cdf0e10cSrcweir // member functions of the current class, 223cdf0e10cSrcweir // because these macros are need by auto register mechanism. 224cdf0e10cSrcweir 225cdf0e10cSrcweir CPPUNIT_TEST_SUITE(valueOf); 226cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_001); 227cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_002); 228cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_003); 229cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_004); 230cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_005); 231cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_006); 232cdf0e10cSrcweir CPPUNIT_TEST(valueOf_float_test_007); 233cdf0e10cSrcweir 234cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_001); 235cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_002); 236cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_003); 237cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_004); 238cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_005); 239cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_006); 240cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_007); 241cdf0e10cSrcweir CPPUNIT_TEST(valueOf_double_test_008); 242cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 243cdf0e10cSrcweir }; // class valueOf 244cdf0e10cSrcweir 245cdf0e10cSrcweir //------------------------------------------------------------------------ 246cdf0e10cSrcweir // testing the method toDouble() 247cdf0e10cSrcweir //------------------------------------------------------------------------ 248cdf0e10cSrcweir template<class T> 249cdf0e10cSrcweir sal_Int16 SAL_CALL checkPrecisionSize() 250cdf0e10cSrcweir { 251cdf0e10cSrcweir // sal_Int16 nSize = sizeof(T); 252cdf0e10cSrcweir volatile T nCalcValue = 1.0; 253cdf0e10cSrcweir 254cdf0e10cSrcweir 255cdf0e10cSrcweir // (i + 1) is the current precision 256cdf0e10cSrcweir // numerical series 257cdf0e10cSrcweir // 1.1 258cdf0e10cSrcweir // 10.1 259cdf0e10cSrcweir // 100.1 260cdf0e10cSrcweir // ... 261cdf0e10cSrcweir // 1000...0.1 262cdf0e10cSrcweir 263cdf0e10cSrcweir sal_Int16 i = 0; 264cdf0e10cSrcweir for (i=0;i<50;i++) 265cdf0e10cSrcweir { 266cdf0e10cSrcweir nCalcValue *= 10; 267cdf0e10cSrcweir volatile T nValue = nCalcValue + static_cast<T>(0.1); 268cdf0e10cSrcweir volatile T dSub = nValue - nCalcValue; 269cdf0e10cSrcweir // ----- 0.11 ---- 0.1 ---- 0.09 ----- 270cdf0e10cSrcweir if (0.11 > dSub && dSub < 0.09) 271cdf0e10cSrcweir { 272cdf0e10cSrcweir // due to the fact, that the value is break down we sub 1 from the precision value 273cdf0e10cSrcweir // but to suppress this, we start at zero, precision is i+1 till here --i; 274cdf0e10cSrcweir break; 275cdf0e10cSrcweir } 276cdf0e10cSrcweir } 277cdf0e10cSrcweir 278cdf0e10cSrcweir sal_Int16 j= 0; 279cdf0e10cSrcweir nCalcValue = 1.0; 280cdf0e10cSrcweir 281cdf0e10cSrcweir // numerical series 282cdf0e10cSrcweir // 1.1 283cdf0e10cSrcweir // 1.01 284cdf0e10cSrcweir // 1.001 285cdf0e10cSrcweir // ... 286cdf0e10cSrcweir // 1.000...001 287cdf0e10cSrcweir 288cdf0e10cSrcweir for (j=0;j<50;j++) 289cdf0e10cSrcweir { 290cdf0e10cSrcweir nCalcValue /= 10; 291cdf0e10cSrcweir volatile T nValue = nCalcValue + static_cast<T>(1.0); 292cdf0e10cSrcweir volatile T dSub = nValue - static_cast<T>(1.0); 293cdf0e10cSrcweir // ---- 0.02 ----- 0.01 ---- 0 --- -0.99 ---- -0.98 ---- 294cdf0e10cSrcweir // volatile T dSubAbsolut = fabs(dSub); 295cdf0e10cSrcweir // ---- 0.02 ----- 0.01 ---- 0 (cut) 296cdf0e10cSrcweir if ( dSub == 0) 297cdf0e10cSrcweir break; 298cdf0e10cSrcweir } 299cdf0e10cSrcweir if (i != j) 300cdf0e10cSrcweir { 301cdf0e10cSrcweir // hmmm.... 302cdf0e10cSrcweir // imho i +- 1 == j is a good value 303cdf0e10cSrcweir int n = i - j; 304cdf0e10cSrcweir if (n < 0) n = -n; 305cdf0e10cSrcweir if (n <= 1) 306cdf0e10cSrcweir { 307cdf0e10cSrcweir return std::min(i,j); 308cdf0e10cSrcweir } 309cdf0e10cSrcweir else 310cdf0e10cSrcweir { 311cdf0e10cSrcweir t_print("warning: presision differs more than 1!\n"); 312cdf0e10cSrcweir } 313cdf0e10cSrcweir } 314cdf0e10cSrcweir 315cdf0e10cSrcweir return i; 316cdf0e10cSrcweir } 317cdf0e10cSrcweir 318cdf0e10cSrcweir // ----------------------------------------------------------------------------- 319cdf0e10cSrcweir 320cdf0e10cSrcweir class testPrecision 321cdf0e10cSrcweir { 322cdf0e10cSrcweir public: 323cdf0e10cSrcweir testPrecision() 324cdf0e10cSrcweir { 325cdf0e10cSrcweir sal_Int16 nPrecision; 326cdf0e10cSrcweir nPrecision = checkPrecisionSize<float>(); 327cdf0e10cSrcweir t_print("precision of float: %d sizeof()=%d \n", nPrecision, sizeof(float)); 328cdf0e10cSrcweir 329cdf0e10cSrcweir nPrecision = checkPrecisionSize<double>(); 330cdf0e10cSrcweir t_print("precision of double: %d sizeof()=%d \n", nPrecision, sizeof(double)); 331cdf0e10cSrcweir 332cdf0e10cSrcweir nPrecision = checkPrecisionSize<long double>(); 333cdf0e10cSrcweir t_print("precision of long double: %d sizeof()=%d \n", nPrecision, sizeof(long double)); 334cdf0e10cSrcweir 335cdf0e10cSrcweir } 336cdf0e10cSrcweir 337cdf0e10cSrcweir }; 338cdf0e10cSrcweir 339cdf0e10cSrcweir class toInt: public CppUnit::TestFixture { 340cdf0e10cSrcweir public: 341cdf0e10cSrcweir void test() { 342cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 343cdf0e10cSrcweir static_cast< sal_Int32 >(-0x76543210), 344cdf0e10cSrcweir (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("-76543210")). 345cdf0e10cSrcweir toInt32(16))); 346cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 347cdf0e10cSrcweir static_cast< sal_Int32 >(0xFEDCBA98), 348cdf0e10cSrcweir (rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("+FEDCBA98")). 349cdf0e10cSrcweir toInt32(16))); 350cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 351cdf0e10cSrcweir static_cast< sal_Int64 >(-SAL_CONST_INT64(0x76543210FEDCBA98)), 352cdf0e10cSrcweir (rtl::OUString( 353cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("-76543210FEDCBA98")). 354cdf0e10cSrcweir toInt64(16))); 355cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 356cdf0e10cSrcweir static_cast< sal_Int64 >(SAL_CONST_INT64(0xFEDCBA9876543210)), 357cdf0e10cSrcweir (rtl::OUString( 358cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM("+FEDCBA9876543210")). 359cdf0e10cSrcweir toInt64(16))); 360cdf0e10cSrcweir } 361cdf0e10cSrcweir 362cdf0e10cSrcweir CPPUNIT_TEST_SUITE(toInt); 363cdf0e10cSrcweir CPPUNIT_TEST(test); 364cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 365cdf0e10cSrcweir }; 366cdf0e10cSrcweir 367cdf0e10cSrcweir // ----------------------------------------------------------------------------- 368cdf0e10cSrcweir // - toDouble (tests) 369cdf0e10cSrcweir // ----------------------------------------------------------------------------- 370cdf0e10cSrcweir class toDouble : public CppUnit::TestFixture 371cdf0e10cSrcweir { 372cdf0e10cSrcweir public: 373cdf0e10cSrcweir void toDouble_test_impl(rtl::OString const& _sValue) 374cdf0e10cSrcweir { 375cdf0e10cSrcweir //t_print("the original str is %s\n", _sValue.getStr()); 376cdf0e10cSrcweir double nValueATOF = atof( _sValue.getStr() ); 377cdf0e10cSrcweir //t_print("original data is %e\n", nValueATOF); 378cdf0e10cSrcweir rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() ); 379cdf0e10cSrcweir double nValueToDouble = suValue.toDouble(); 380cdf0e10cSrcweir //t_print("result data is %e\n", nValueToDouble); 381cdf0e10cSrcweir 382cdf0e10cSrcweir bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF); 383cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true); 384cdf0e10cSrcweir } 385cdf0e10cSrcweir 386cdf0e10cSrcweir void toDouble_test(rtl::OString const& _sValue) 387cdf0e10cSrcweir { 388cdf0e10cSrcweir toDouble_test_impl(_sValue); 389cdf0e10cSrcweir 390cdf0e10cSrcweir // test also the negativ part. 391cdf0e10cSrcweir rtl::OString sNegativValue("-"); 392cdf0e10cSrcweir sNegativValue += _sValue; 393cdf0e10cSrcweir toDouble_test_impl(sNegativValue); 394cdf0e10cSrcweir } 395cdf0e10cSrcweir 396cdf0e10cSrcweir // insert your test code here. 397cdf0e10cSrcweir void toDouble_selftest() 398cdf0e10cSrcweir { 399cdf0e10cSrcweir t_print("Start selftest:\n"); 400cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false); 401cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false); 402cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false); 403cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false); 404cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false); 405cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false); 406cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false); 407cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false); 408cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false); 409cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false); 410cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false); 411cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false); 412cdf0e10cSrcweir // we check til 15 values after comma 413cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true); 414cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true); 415cdf0e10cSrcweir CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true); 416cdf0e10cSrcweir t_print("Selftest done.\n"); 417cdf0e10cSrcweir } 418cdf0e10cSrcweir 419cdf0e10cSrcweir void toDouble_test_3() 420cdf0e10cSrcweir { 421cdf0e10cSrcweir rtl::OString sValue("3"); 422cdf0e10cSrcweir toDouble_test(sValue); 423cdf0e10cSrcweir } 424cdf0e10cSrcweir void toDouble_test_3_5() 425cdf0e10cSrcweir { 426cdf0e10cSrcweir rtl::OString sValue("3.5"); 427cdf0e10cSrcweir toDouble_test(sValue); 428cdf0e10cSrcweir } 429cdf0e10cSrcweir void toDouble_test_3_0625() 430cdf0e10cSrcweir { 431cdf0e10cSrcweir rtl::OString sValue("3.0625"); 432cdf0e10cSrcweir toDouble_test(sValue); 433cdf0e10cSrcweir } 434cdf0e10cSrcweir void toDouble_test_pi() 435cdf0e10cSrcweir { 436cdf0e10cSrcweir // value from http://www.angio.net/pi/digits/50.txt 437cdf0e10cSrcweir rtl::OString sValue("3.141592653589793238462643383279502884197169399375"); 438cdf0e10cSrcweir toDouble_test(sValue); 439cdf0e10cSrcweir } 440cdf0e10cSrcweir 441cdf0e10cSrcweir void toDouble_test_1() 442cdf0e10cSrcweir { 443cdf0e10cSrcweir rtl::OString sValue("1"); 444cdf0e10cSrcweir toDouble_test(sValue); 445cdf0e10cSrcweir } 446cdf0e10cSrcweir void toDouble_test_10() 447cdf0e10cSrcweir { 448cdf0e10cSrcweir rtl::OString sValue("10"); 449cdf0e10cSrcweir toDouble_test(sValue); 450cdf0e10cSrcweir } 451cdf0e10cSrcweir void toDouble_test_100() 452cdf0e10cSrcweir { 453cdf0e10cSrcweir rtl::OString sValue("100"); 454cdf0e10cSrcweir toDouble_test(sValue); 455cdf0e10cSrcweir } 456cdf0e10cSrcweir void toDouble_test_1000() 457cdf0e10cSrcweir { 458cdf0e10cSrcweir rtl::OString sValue("1000"); 459cdf0e10cSrcweir toDouble_test(sValue); 460cdf0e10cSrcweir } 461cdf0e10cSrcweir void toDouble_test_10000() 462cdf0e10cSrcweir { 463cdf0e10cSrcweir rtl::OString sValue("10000"); 464cdf0e10cSrcweir toDouble_test(sValue); 465cdf0e10cSrcweir } 466cdf0e10cSrcweir void toDouble_test_1e99() 467cdf0e10cSrcweir { 468cdf0e10cSrcweir rtl::OString sValue("1e99"); 469cdf0e10cSrcweir toDouble_test(sValue); 470cdf0e10cSrcweir } 471cdf0e10cSrcweir void toDouble_test_1e_n99() 472cdf0e10cSrcweir { 473cdf0e10cSrcweir rtl::OString sValue("1e-99"); 474cdf0e10cSrcweir toDouble_test(sValue); 475cdf0e10cSrcweir } 476cdf0e10cSrcweir void toDouble_test_1e308() 477cdf0e10cSrcweir { 478cdf0e10cSrcweir rtl::OString sValue("1e308"); 479cdf0e10cSrcweir toDouble_test(sValue); 480cdf0e10cSrcweir } 481cdf0e10cSrcweir 482cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 483cdf0e10cSrcweir // member functions of the current class, 484cdf0e10cSrcweir // because these macros are need by auto register mechanism. 485cdf0e10cSrcweir 486cdf0e10cSrcweir CPPUNIT_TEST_SUITE(toDouble); 487cdf0e10cSrcweir CPPUNIT_TEST(toDouble_selftest); 488cdf0e10cSrcweir 489cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_3); 490cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_3_5); 491cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_3_0625); 492cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_pi); 493cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_1); 494cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_10); 495cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_100); 496cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_1000); 497cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_10000); 498cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_1e99); 499cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_1e_n99); 500cdf0e10cSrcweir CPPUNIT_TEST(toDouble_test_1e308); 501cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 502cdf0e10cSrcweir }; // class toDouble 503cdf0e10cSrcweir 504cdf0e10cSrcweir // ----------------------------------------------------------------------------- 505cdf0e10cSrcweir // - toFloat (tests) 506cdf0e10cSrcweir // ----------------------------------------------------------------------------- 507cdf0e10cSrcweir class toFloat : public CppUnit::TestFixture 508cdf0e10cSrcweir { 509cdf0e10cSrcweir public: 510cdf0e10cSrcweir void toFloat_test_impl(rtl::OString const& _sValue) 511cdf0e10cSrcweir { 512cdf0e10cSrcweir //t_print("the original str is %s\n", _sValue.getStr()); 513cdf0e10cSrcweir float nValueATOF = static_cast<float>(atof( _sValue.getStr() )); 514cdf0e10cSrcweir //t_print("the original str is %.10f\n", nValueATOF); 515cdf0e10cSrcweir rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() ); 516cdf0e10cSrcweir float nValueToFloat = suValue.toFloat(); 517cdf0e10cSrcweir //t_print("the result str is %.10f\n", nValueToFloat); 518cdf0e10cSrcweir 519cdf0e10cSrcweir bool bEqualResult = is_float_equal(nValueToFloat, nValueATOF); 520cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true); 521cdf0e10cSrcweir } 522cdf0e10cSrcweir 523cdf0e10cSrcweir void toFloat_test(rtl::OString const& _sValue) 524cdf0e10cSrcweir { 525cdf0e10cSrcweir toFloat_test_impl(_sValue); 526cdf0e10cSrcweir 527cdf0e10cSrcweir // test also the negativ part. 528cdf0e10cSrcweir rtl::OString sNegativValue("-"); 529cdf0e10cSrcweir sNegativValue += _sValue; 530cdf0e10cSrcweir toFloat_test_impl(sNegativValue); 531cdf0e10cSrcweir } 532cdf0e10cSrcweir 533cdf0e10cSrcweir // insert your test code here. 534cdf0e10cSrcweir void toFloat_selftest() 535cdf0e10cSrcweir { 536cdf0e10cSrcweir t_print("Start selftest:\n"); 537cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.01f) == false); 538cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.001f) == false); 539cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0001f) == false); 540cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00001f) == false); 541cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000002f) == false); 542cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0000001f) == true); 543cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00000001f) == true); 544cdf0e10cSrcweir CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000000001f) == true); 545cdf0e10cSrcweir 546cdf0e10cSrcweir t_print("Selftest done.\n"); 547cdf0e10cSrcweir } 548cdf0e10cSrcweir 549cdf0e10cSrcweir void toFloat_test_3() 550cdf0e10cSrcweir { 551cdf0e10cSrcweir rtl::OString sValue("3"); 552cdf0e10cSrcweir toFloat_test(sValue); 553cdf0e10cSrcweir } 554cdf0e10cSrcweir void toFloat_test_3_5() 555cdf0e10cSrcweir { 556cdf0e10cSrcweir rtl::OString sValue("3.5"); 557cdf0e10cSrcweir toFloat_test(sValue); 558cdf0e10cSrcweir } 559cdf0e10cSrcweir void toFloat_test_3_0625() 560cdf0e10cSrcweir { 561cdf0e10cSrcweir rtl::OString sValue("3.0625"); 562cdf0e10cSrcweir toFloat_test(sValue); 563cdf0e10cSrcweir } 564cdf0e10cSrcweir void toFloat_test_3_0625_e() 565cdf0e10cSrcweir { 566cdf0e10cSrcweir rtl::OString sValue("3.0625e-4"); 567cdf0e10cSrcweir toFloat_test(sValue); 568cdf0e10cSrcweir } 569cdf0e10cSrcweir void toFloat_test_pi() 570cdf0e10cSrcweir { 571cdf0e10cSrcweir // value from http://www.angio.net/pi/digits/50.txt 572cdf0e10cSrcweir rtl::OString sValue("3.141592653589793238462643383279502884197169399375"); 573cdf0e10cSrcweir toFloat_test(sValue); 574cdf0e10cSrcweir } 575cdf0e10cSrcweir 576cdf0e10cSrcweir void toFloat_test_1() 577cdf0e10cSrcweir { 578cdf0e10cSrcweir rtl::OString sValue("1"); 579cdf0e10cSrcweir toFloat_test(sValue); 580cdf0e10cSrcweir } 581cdf0e10cSrcweir void toFloat_test_10() 582cdf0e10cSrcweir { 583cdf0e10cSrcweir rtl::OString sValue("10"); 584cdf0e10cSrcweir toFloat_test(sValue); 585cdf0e10cSrcweir } 586cdf0e10cSrcweir void toFloat_test_100() 587cdf0e10cSrcweir { 588cdf0e10cSrcweir rtl::OString sValue("100"); 589cdf0e10cSrcweir toFloat_test(sValue); 590cdf0e10cSrcweir } 591cdf0e10cSrcweir void toFloat_test_1000() 592cdf0e10cSrcweir { 593cdf0e10cSrcweir rtl::OString sValue("1000"); 594cdf0e10cSrcweir toFloat_test(sValue); 595cdf0e10cSrcweir } 596cdf0e10cSrcweir void toFloat_test_10000() 597cdf0e10cSrcweir { 598cdf0e10cSrcweir rtl::OString sValue("10000"); 599cdf0e10cSrcweir toFloat_test(sValue); 600cdf0e10cSrcweir } 601cdf0e10cSrcweir void toFloat_test_mix() 602cdf0e10cSrcweir { 603cdf0e10cSrcweir rtl::OString sValue("456789321455.123456789012"); 604cdf0e10cSrcweir toFloat_test(sValue); 605cdf0e10cSrcweir } 606cdf0e10cSrcweir void toFloat_test_1e99() 607cdf0e10cSrcweir { 608cdf0e10cSrcweir rtl::OString sValue("1e99"); 609cdf0e10cSrcweir toFloat_test(sValue); 610cdf0e10cSrcweir } 611cdf0e10cSrcweir void toFloat_test_1e_n99() 612cdf0e10cSrcweir { 613cdf0e10cSrcweir rtl::OString sValue("1e-9"); 614cdf0e10cSrcweir toFloat_test(sValue); 615cdf0e10cSrcweir } 616cdf0e10cSrcweir void toFloat_test_1e308() 617cdf0e10cSrcweir { 618cdf0e10cSrcweir rtl::OString sValue("1e308"); 619cdf0e10cSrcweir toFloat_test(sValue); 620cdf0e10cSrcweir } 621cdf0e10cSrcweir 622cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 623cdf0e10cSrcweir // member functions of the current class, 624cdf0e10cSrcweir // because these macros are need by auto register mechanism. 625cdf0e10cSrcweir 626cdf0e10cSrcweir CPPUNIT_TEST_SUITE(toFloat); 627cdf0e10cSrcweir CPPUNIT_TEST(toFloat_selftest); 628cdf0e10cSrcweir 629cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_3); 630cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_3_5); 631cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_3_0625); 632cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_3_0625_e); 633cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_pi); 634cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_1); 635cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_10); 636cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_100); 637cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_1000); 638cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_10000); 639cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_mix); 640cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_1e99); 641cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_1e_n99); 642cdf0e10cSrcweir CPPUNIT_TEST(toFloat_test_1e308); 643cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 644cdf0e10cSrcweir }; // class toFloat 645cdf0e10cSrcweir 646cdf0e10cSrcweir // ----------------------------------------------------------------------------- 647cdf0e10cSrcweir // - lastIndexOf (tests) 648cdf0e10cSrcweir // ----------------------------------------------------------------------------- 649cdf0e10cSrcweir class lastIndexOf : public CppUnit::TestFixture 650cdf0e10cSrcweir { 651cdf0e10cSrcweir 652cdf0e10cSrcweir public: 653cdf0e10cSrcweir void lastIndexOf_oustring(rtl::OUString const& _suStr, rtl::OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos) 654cdf0e10cSrcweir { 655cdf0e10cSrcweir // Algorithm 656cdf0e10cSrcweir // search the string _suSearchStr (rtl::OUString) in the string _suStr. 657cdf0e10cSrcweir // check if the _nExpectedResultPos occurs. 658cdf0e10cSrcweir 659cdf0e10cSrcweir sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr); 660cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos); 661cdf0e10cSrcweir } 662cdf0e10cSrcweir 663cdf0e10cSrcweir void lastIndexOf_salunicode(rtl::OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos) 664cdf0e10cSrcweir { 665cdf0e10cSrcweir // Algorithm 666cdf0e10cSrcweir // search the unicode char _suSearchChar (sal_Unicode) in the string _suStr. 667cdf0e10cSrcweir // check if the _nExpectedResultPos occurs. 668cdf0e10cSrcweir 669cdf0e10cSrcweir sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar); 670cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos); 671cdf0e10cSrcweir } 672cdf0e10cSrcweir 673cdf0e10cSrcweir void lastIndexOf_oustring_offset(rtl::OUString const& _suStr, rtl::OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset) 674cdf0e10cSrcweir { 675cdf0e10cSrcweir sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr, _nStartOffset); 676cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos); 677cdf0e10cSrcweir } 678cdf0e10cSrcweir 679cdf0e10cSrcweir void lastIndexOf_salunicode_offset(rtl::OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset) 680cdf0e10cSrcweir { 681cdf0e10cSrcweir sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar, _nStartOffset); 682cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos); 683cdf0e10cSrcweir } 684cdf0e10cSrcweir 685cdf0e10cSrcweir // ----------------------------------------------------------------------------- 686cdf0e10cSrcweir 687cdf0e10cSrcweir void lastIndexOf_test_oustring_offset_001() 688cdf0e10cSrcweir { 689cdf0e10cSrcweir // search for sun, start at the end, found (pos==0) 690cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("sun java system"); 691cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 692cdf0e10cSrcweir lastIndexOf_oustring_offset(aStr, aSearchStr, 0, aStr.getLength()); 693cdf0e10cSrcweir } 694cdf0e10cSrcweir 695cdf0e10cSrcweir void lastIndexOf_test_oustring_offset_002() 696cdf0e10cSrcweir { 697cdf0e10cSrcweir // search for sun, start at pos = 3, found (pos==0) 698cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("sun java system"); 699cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 700cdf0e10cSrcweir lastIndexOf_oustring_offset(aStr, aSearchStr, 0, 3); 701cdf0e10cSrcweir } 702cdf0e10cSrcweir 703cdf0e10cSrcweir void lastIndexOf_test_oustring_offset_003() 704cdf0e10cSrcweir { 705cdf0e10cSrcweir // search for sun, start at pos = 2, found (pos==-1) 706cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("sun java system"); 707cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 708cdf0e10cSrcweir lastIndexOf_oustring_offset(aStr, aSearchStr, -1, 2); 709cdf0e10cSrcweir } 710cdf0e10cSrcweir 711cdf0e10cSrcweir void lastIndexOf_test_oustring_offset_004() 712cdf0e10cSrcweir { 713cdf0e10cSrcweir // search for sun, start at the end, found (pos==0) 714cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("sun java system"); 715cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 716cdf0e10cSrcweir lastIndexOf_oustring_offset(aStr, aSearchStr, -1, -1); 717cdf0e10cSrcweir } 718cdf0e10cSrcweir 719cdf0e10cSrcweir void lastIndexOf_test_oustring_001() 720cdf0e10cSrcweir { 721cdf0e10cSrcweir // search for sun, found (pos==0) 722cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("sun java system"); 723cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 724cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, 0); 725cdf0e10cSrcweir } 726cdf0e10cSrcweir 727cdf0e10cSrcweir void lastIndexOf_test_oustring_002() 728cdf0e10cSrcweir { 729cdf0e10cSrcweir // search for sun, found (pos==4) 730cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun java system"); 731cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 732cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, 4); 733cdf0e10cSrcweir } 734cdf0e10cSrcweir 735cdf0e10cSrcweir void lastIndexOf_test_oustring_003() 736cdf0e10cSrcweir { 737cdf0e10cSrcweir // search for sun, found (pos==8) 738cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 739cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 740cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, 8); 741cdf0e10cSrcweir } 742cdf0e10cSrcweir 743cdf0e10cSrcweir void lastIndexOf_test_oustring_004() 744cdf0e10cSrcweir { 745cdf0e10cSrcweir // search for sun, found (pos==8) 746cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun"); 747cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 748cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, 8); 749cdf0e10cSrcweir } 750cdf0e10cSrcweir 751cdf0e10cSrcweir void lastIndexOf_test_oustring_005() 752cdf0e10cSrcweir { 753cdf0e10cSrcweir // search for sun, found (pos==4) 754cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun su"); 755cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 756cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, 4); 757cdf0e10cSrcweir } 758cdf0e10cSrcweir 759cdf0e10cSrcweir void lastIndexOf_test_oustring_006() 760cdf0e10cSrcweir { 761cdf0e10cSrcweir // search for sun, found (pos==-1) 762cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the su su"); 763cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("sun"); 764cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, -1); 765cdf0e10cSrcweir } 766cdf0e10cSrcweir 767cdf0e10cSrcweir void lastIndexOf_test_oustring_007() 768cdf0e10cSrcweir { 769cdf0e10cSrcweir // search for earth, not found (-1) 770cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the su su"); 771cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("earth"); 772cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, -1); 773cdf0e10cSrcweir } 774cdf0e10cSrcweir 775cdf0e10cSrcweir void lastIndexOf_test_oustring_008() 776cdf0e10cSrcweir { 777cdf0e10cSrcweir // search for earth, not found (-1) 778cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString(); 779cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString::createFromAscii("earth"); 780cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, -1); 781cdf0e10cSrcweir } 782cdf0e10cSrcweir 783cdf0e10cSrcweir void lastIndexOf_test_oustring_009() 784cdf0e10cSrcweir { 785cdf0e10cSrcweir // search for earth, not found (-1) 786cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString(); 787cdf0e10cSrcweir rtl::OUString aSearchStr = rtl::OUString(); 788cdf0e10cSrcweir lastIndexOf_oustring(aStr, aSearchStr, -1); 789cdf0e10cSrcweir 790cdf0e10cSrcweir } 791cdf0e10cSrcweir 792cdf0e10cSrcweir void lastIndexOf_test_salunicode_001() 793cdf0e10cSrcweir { 794cdf0e10cSrcweir // search for 's', found (19) 795cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 796cdf0e10cSrcweir sal_Unicode suChar = L's'; 797cdf0e10cSrcweir lastIndexOf_salunicode(aStr, suChar, 19); 798cdf0e10cSrcweir } 799cdf0e10cSrcweir 800cdf0e10cSrcweir void lastIndexOf_test_salunicode_002() 801cdf0e10cSrcweir { 802cdf0e10cSrcweir // search for 'x', not found (-1) 803cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 804cdf0e10cSrcweir sal_Unicode suChar = L'x'; 805cdf0e10cSrcweir lastIndexOf_salunicode(aStr, suChar, -1); 806cdf0e10cSrcweir } 807cdf0e10cSrcweir 808cdf0e10cSrcweir void lastIndexOf_test_salunicode_offset_001() 809cdf0e10cSrcweir { 810cdf0e10cSrcweir // search for 's', start from pos last char, found (19) 811cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 812cdf0e10cSrcweir sal_Unicode cuChar = L's'; 813cdf0e10cSrcweir lastIndexOf_salunicode_offset(aStr, cuChar, 19, aStr.getLength()); 814cdf0e10cSrcweir } 815cdf0e10cSrcweir void lastIndexOf_test_salunicode_offset_002() 816cdf0e10cSrcweir { 817cdf0e10cSrcweir // search for 's', start pos is last occur from search behind, found (17) 818cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 819cdf0e10cSrcweir sal_Unicode cuChar = L's'; 820cdf0e10cSrcweir lastIndexOf_salunicode_offset(aStr, cuChar, 17, 19); 821cdf0e10cSrcweir } 822cdf0e10cSrcweir void lastIndexOf_test_salunicode_offset_003() 823cdf0e10cSrcweir { 824cdf0e10cSrcweir // search for 't', start pos is 1, found (0) 825cdf0e10cSrcweir rtl::OUString aStr = rtl::OUString::createFromAscii("the sun sun java system"); 826cdf0e10cSrcweir sal_Unicode cuChar = L't'; 827cdf0e10cSrcweir lastIndexOf_salunicode_offset(aStr, cuChar, 0, 1); 828cdf0e10cSrcweir } 829cdf0e10cSrcweir 830cdf0e10cSrcweir // Change the following lines only, if you add, remove or rename 831cdf0e10cSrcweir // member functions of the current class, 832cdf0e10cSrcweir // because these macros are need by auto register mechanism. 833cdf0e10cSrcweir 834cdf0e10cSrcweir CPPUNIT_TEST_SUITE(lastIndexOf); 835cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_001); 836cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_002); 837cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_003); 838cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_004); 839cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_005); 840cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_006); 841cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_007); 842cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_008); 843cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_009); 844cdf0e10cSrcweir 845cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_offset_001); 846cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_offset_002); 847cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_offset_003); 848cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_oustring_offset_004); 849cdf0e10cSrcweir 850cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_salunicode_001); 851cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_salunicode_002); 852cdf0e10cSrcweir 853cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_001); 854cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_002); 855cdf0e10cSrcweir CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_003); 856cdf0e10cSrcweir 857cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 858cdf0e10cSrcweir }; // class lastIndexOf 859cdf0e10cSrcweir 860cdf0e10cSrcweir 861cdf0e10cSrcweir // ----------------------------------------------------------------------------- 862cdf0e10cSrcweir // - getToken (tests) 863cdf0e10cSrcweir // ----------------------------------------------------------------------------- 864cdf0e10cSrcweir class getToken : public CppUnit::TestFixture 865cdf0e10cSrcweir { 866cdf0e10cSrcweir 867cdf0e10cSrcweir public: 868cdf0e10cSrcweir void getToken_000() 869cdf0e10cSrcweir { 870cdf0e10cSrcweir rtl::OUString suTokenStr; 871cdf0e10cSrcweir 872cdf0e10cSrcweir sal_Int32 nIndex = 0; 873cdf0e10cSrcweir do 874cdf0e10cSrcweir { 875cdf0e10cSrcweir rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); 876cdf0e10cSrcweir } 877cdf0e10cSrcweir while ( nIndex >= 0 ); 878cdf0e10cSrcweir t_print("Index %d\n", nIndex); 879cdf0e10cSrcweir // should not GPF 880cdf0e10cSrcweir } 881cdf0e10cSrcweir 882cdf0e10cSrcweir void getToken_001() 883cdf0e10cSrcweir { 884cdf0e10cSrcweir rtl::OUString suTokenStr = rtl::OUString::createFromAscii("a;b"); 885cdf0e10cSrcweir 886cdf0e10cSrcweir sal_Int32 nIndex = 0; 887cdf0e10cSrcweir 888cdf0e10cSrcweir rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); 889cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", suToken.equals(rtl::OUString::createFromAscii("a")) == sal_True); 890cdf0e10cSrcweir 891cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); 892cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", suToken.equals(rtl::OUString::createFromAscii("b")) == sal_True); 893cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1); 894cdf0e10cSrcweir } 895cdf0e10cSrcweir 896cdf0e10cSrcweir void getToken_002() 897cdf0e10cSrcweir { 898cdf0e10cSrcweir rtl::OUString suTokenStr = rtl::OUString::createFromAscii("a;b.c"); 899cdf0e10cSrcweir 900cdf0e10cSrcweir sal_Int32 nIndex = 0; 901cdf0e10cSrcweir 902cdf0e10cSrcweir rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); 903cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", suToken.equals(rtl::OUString::createFromAscii("a")) == sal_True); 904cdf0e10cSrcweir 905cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); 906cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", suToken.equals(rtl::OUString::createFromAscii("b")) == sal_True); 907cdf0e10cSrcweir 908cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); 909cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", suToken.equals(rtl::OUString::createFromAscii("c")) == sal_True); 910cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1); 911cdf0e10cSrcweir } 912cdf0e10cSrcweir 913cdf0e10cSrcweir void getToken_003() 914cdf0e10cSrcweir { 915cdf0e10cSrcweir rtl::OUString suTokenStr = rtl::OUString::createFromAscii("a;;b"); 916cdf0e10cSrcweir 917cdf0e10cSrcweir sal_Int32 nIndex = 0; 918cdf0e10cSrcweir 919cdf0e10cSrcweir rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex ); 920cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", suToken.equals(rtl::OUString::createFromAscii("a")) == sal_True); 921cdf0e10cSrcweir 922cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); 923cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.getLength() == 0); 924cdf0e10cSrcweir 925cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex ); 926cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", suToken.equals(rtl::OUString::createFromAscii("b")) == sal_True); 927cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1); 928cdf0e10cSrcweir } 929cdf0e10cSrcweir 930cdf0e10cSrcweir void getToken_004() 931cdf0e10cSrcweir { 932cdf0e10cSrcweir rtl::OUString suTokenStr = rtl::OUString::createFromAscii("longer.then.ever."); 933cdf0e10cSrcweir 934cdf0e10cSrcweir sal_Int32 nIndex = 0; 935cdf0e10cSrcweir 936cdf0e10cSrcweir rtl::OUString suToken = suTokenStr.getToken( 0, '.', nIndex ); 937cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", suToken.equals(rtl::OUString::createFromAscii("longer")) == sal_True); 938cdf0e10cSrcweir 939cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); 940cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", suToken.equals(rtl::OUString::createFromAscii("then")) == sal_True); 941cdf0e10cSrcweir 942cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); 943cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", suToken.equals(rtl::OUString::createFromAscii("ever")) == sal_True); 944cdf0e10cSrcweir 945cdf0e10cSrcweir /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex ); 946cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.getLength() == 0); 947cdf0e10cSrcweir 948cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1); 949cdf0e10cSrcweir } 950cdf0e10cSrcweir 951cdf0e10cSrcweir void getToken_005() { 952cdf0e10cSrcweir rtl::OUString ab(RTL_CONSTASCII_USTRINGPARAM("ab")); 953cdf0e10cSrcweir sal_Int32 n = 0; 954cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( 955cdf0e10cSrcweir "token should be 'ab'", ab.getToken(0, '-', n) == ab); 956cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("n should be -1", n == -1); 957cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE( 958cdf0e10cSrcweir "token should be empty", ab.getToken(0, '-', n).getLength() == 0); 959cdf0e10cSrcweir } 960cdf0e10cSrcweir 961cdf0e10cSrcweir CPPUNIT_TEST_SUITE(getToken); 962cdf0e10cSrcweir CPPUNIT_TEST(getToken_000); 963cdf0e10cSrcweir CPPUNIT_TEST(getToken_001); 964cdf0e10cSrcweir CPPUNIT_TEST(getToken_002); 965cdf0e10cSrcweir CPPUNIT_TEST(getToken_003); 966cdf0e10cSrcweir CPPUNIT_TEST(getToken_004); 967cdf0e10cSrcweir CPPUNIT_TEST(getToken_005); 968cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 969cdf0e10cSrcweir }; // class getToken 970cdf0e10cSrcweir 971cdf0e10cSrcweir class convertToString: public CppUnit::TestFixture { 972cdf0e10cSrcweir public: 973cdf0e10cSrcweir void test(); 974cdf0e10cSrcweir 975cdf0e10cSrcweir CPPUNIT_TEST_SUITE(convertToString); 976cdf0e10cSrcweir CPPUNIT_TEST(test); 977cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 978cdf0e10cSrcweir }; 979cdf0e10cSrcweir 980cdf0e10cSrcweir void convertToString::test() { 981cdf0e10cSrcweir static sal_Unicode const utf16[] = { 0x0041, 0x00E4, 0x0061 }; 982cdf0e10cSrcweir rtl::OString s; 983cdf0e10cSrcweir CPPUNIT_ASSERT( 984cdf0e10cSrcweir rtl::OUString(utf16, sizeof utf16 / sizeof utf16[0]).convertToString( 985cdf0e10cSrcweir &s, RTL_TEXTENCODING_UTF7, 986cdf0e10cSrcweir (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR | 987cdf0e10cSrcweir RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR))); 988cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 989cdf0e10cSrcweir rtl::OString(RTL_CONSTASCII_STRINGPARAM("A+AOQ-a")), s); 990cdf0e10cSrcweir } 991cdf0e10cSrcweir 992cdf0e10cSrcweir // ----------------------------------------------------------------------------- 993cdf0e10cSrcweir // - string construction & interning (tests) 994cdf0e10cSrcweir // ----------------------------------------------------------------------------- 995cdf0e10cSrcweir class construction : public CppUnit::TestFixture 996cdf0e10cSrcweir { 997cdf0e10cSrcweir public: 998cdf0e10cSrcweir void construct() 999cdf0e10cSrcweir { 1000cdf0e10cSrcweir #ifdef RTL_INLINE_STRINGS 1001cdf0e10cSrcweir ::rtl::OUString aFoo( RTL_CONSTASCII_USTRINGPARAM("foo") ); 1002cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[0] == 'f'); 1003cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[1] == 'o'); 1004cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[2] == 'o'); 1005cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string length", aFoo.getLength() == 3); 1006cdf0e10cSrcweir 1007cdf0e10cSrcweir ::rtl::OUString aBaa( RTL_CONSTASCII_USTRINGPARAM("this is a very long string with a lot of long things inside it and it goes on and on and on forever etc.") ); 1008cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string length", aBaa.getLength() == 104); 1009cdf0e10cSrcweir // Dig at the internals ... FIXME: should we have the bit-flag defines public ? 1010cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string static flags", (aBaa.pData->refCount & 1<<30) != 0); 1011cdf0e10cSrcweir #endif 1012cdf0e10cSrcweir } 1013cdf0e10cSrcweir 1014cdf0e10cSrcweir void intern() 1015cdf0e10cSrcweir { 1016cdf0e10cSrcweir // The empty string is 'static' a special case ... 1017cdf0e10cSrcweir rtl::OUString aEmpty = rtl::OUString().intern(); 1018cdf0e10cSrcweir rtl::OUString aEmpty2 = rtl::OUString::intern( RTL_CONSTASCII_USTRINGPARAM( "" ) ); 1019cdf0e10cSrcweir 1020cdf0e10cSrcweir ::rtl::OUString aFoo( RTL_CONSTASCII_USTRINGPARAM("foo") ); 1021cdf0e10cSrcweir ::rtl::OUString aFooIntern = aFoo.intern(); 1022cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string contents", aFooIntern.equalsAscii("foo")); 1023cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("string length", aFooIntern.getLength() == 3); 1024cdf0e10cSrcweir // We have to dup due to no atomic 'intern' bit-set operation 1025cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("intern dups", aFoo.pData != aFooIntern.pData); 1026cdf0e10cSrcweir 1027cdf0e10cSrcweir // Test interning lots of things 1028cdf0e10cSrcweir int i; 1029cdf0e10cSrcweir static const int nSequence = 4096; 1030cdf0e10cSrcweir rtl::OUString *pStrs; 1031cdf0e10cSrcweir sal_uIntPtr *pValues; 1032cdf0e10cSrcweir 1033cdf0e10cSrcweir pStrs = new rtl::OUString[nSequence]; 1034cdf0e10cSrcweir pValues = new sal_uIntPtr[nSequence]; 1035cdf0e10cSrcweir for (i = 0; i < nSequence; i++) 1036cdf0e10cSrcweir { 1037cdf0e10cSrcweir pStrs[i] = rtl::OUString::valueOf( sqrt( static_cast<double>(i) ) ).intern(); 1038cdf0e10cSrcweir pValues[i] = reinterpret_cast<sal_uIntPtr>( pStrs[i].pData ); 1039cdf0e10cSrcweir } 1040cdf0e10cSrcweir for (i = 0; i < nSequence; i++) 1041cdf0e10cSrcweir { 1042cdf0e10cSrcweir rtl::OUString aNew = rtl::OUString::valueOf( sqrt( static_cast<double>(i) ) ).intern(); 1043cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("double intern failed", 1044cdf0e10cSrcweir aNew.pData == pStrs[i].pData); 1045cdf0e10cSrcweir } 1046cdf0e10cSrcweir 1047cdf0e10cSrcweir // Free strings to check for leaks 1048cdf0e10cSrcweir for (i = 0; i < nSequence; i++) 1049cdf0e10cSrcweir { 1050cdf0e10cSrcweir // Overwrite - hopefully this re-uses the memory 1051cdf0e10cSrcweir pStrs[i] = rtl::OUString(); 1052cdf0e10cSrcweir pStrs[i] = rtl::OUString::valueOf( sqrt( static_cast<double>(i) ) ); 1053cdf0e10cSrcweir } 1054cdf0e10cSrcweir 1055cdf0e10cSrcweir for (i = 0; i < nSequence; i++) 1056cdf0e10cSrcweir { 1057cdf0e10cSrcweir rtl::OUString aIntern; 1058cdf0e10cSrcweir sal_uIntPtr nValue; 1059cdf0e10cSrcweir aIntern = rtl::OUString::valueOf( sqrt( static_cast<double>(i) ) ).intern(); 1060cdf0e10cSrcweir 1061cdf0e10cSrcweir nValue = reinterpret_cast<sal_uIntPtr>( aIntern.pData ); 1062cdf0e10cSrcweir // This may not be 100% reliable: memory may 1063cdf0e10cSrcweir // have been re-used, but it's worth checking. 1064cdf0e10cSrcweir CPPUNIT_ASSERT_MESSAGE("intern leaking", nValue != pValues[i]); 1065cdf0e10cSrcweir } 1066cdf0e10cSrcweir delete [] pValues; 1067cdf0e10cSrcweir delete [] pStrs; 1068cdf0e10cSrcweir } 1069cdf0e10cSrcweir 1070cdf0e10cSrcweir CPPUNIT_TEST_SUITE(construction); 1071cdf0e10cSrcweir CPPUNIT_TEST(construct); 1072cdf0e10cSrcweir CPPUNIT_TEST(intern); 1073cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1074cdf0e10cSrcweir }; 1075cdf0e10cSrcweir 1076cdf0e10cSrcweir class indexOfAscii: public CppUnit::TestFixture { 1077cdf0e10cSrcweir public: 1078cdf0e10cSrcweir void test(); 1079cdf0e10cSrcweir 1080cdf0e10cSrcweir CPPUNIT_TEST_SUITE(indexOfAscii); 1081cdf0e10cSrcweir CPPUNIT_TEST(test); 1082cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1083cdf0e10cSrcweir }; 1084cdf0e10cSrcweir 1085cdf0e10cSrcweir void indexOfAscii::test() { 1086cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1087cdf0e10cSrcweir sal_Int32(-1), 1088cdf0e10cSrcweir rtl::OUString().indexOfAsciiL(RTL_CONSTASCII_STRINGPARAM(""))); 1089cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1090cdf0e10cSrcweir sal_Int32(-1), 1091cdf0e10cSrcweir rtl::OUString().lastIndexOfAsciiL(RTL_CONSTASCII_STRINGPARAM(""))); 1092cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1093cdf0e10cSrcweir sal_Int32(0), 1094cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")).indexOfAsciiL( 1095cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("foo"))); 1096cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1097cdf0e10cSrcweir sal_Int32(0), 1098cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foo")).lastIndexOfAsciiL( 1099cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("foo"))); 1100cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1101cdf0e10cSrcweir sal_Int32(2), 1102cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("fofoobar")).indexOfAsciiL( 1103cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("foo"))); 1104cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1105cdf0e10cSrcweir sal_Int32(3), 1106cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foofoofob")). 1107cdf0e10cSrcweir lastIndexOfAsciiL(RTL_CONSTASCII_STRINGPARAM("foo"))); 1108cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1109cdf0e10cSrcweir sal_Int32(3), 1110cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foofoobar")).indexOfAsciiL( 1111cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("foo"), 1)); 1112cdf0e10cSrcweir } 1113cdf0e10cSrcweir 1114cdf0e10cSrcweir class endsWith: public CppUnit::TestFixture { 1115cdf0e10cSrcweir public: 1116cdf0e10cSrcweir void test(); 1117cdf0e10cSrcweir 1118cdf0e10cSrcweir CPPUNIT_TEST_SUITE(endsWith); 1119cdf0e10cSrcweir CPPUNIT_TEST(test); 1120cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1121cdf0e10cSrcweir }; 1122cdf0e10cSrcweir 1123cdf0e10cSrcweir void endsWith::test() { 1124cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1125cdf0e10cSrcweir true, 1126cdf0e10cSrcweir rtl::OUString().endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM(""))); 1127cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1128cdf0e10cSrcweir false, 1129cdf0e10cSrcweir rtl::OUString().endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM("foo"))); 1130cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1131cdf0e10cSrcweir true, 1132cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bar")).endsWithAsciiL( 1133cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("bar"))); 1134cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1135cdf0e10cSrcweir true, 1136cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobar")).endsWithAsciiL( 1137cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("bar"))); 1138cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1139cdf0e10cSrcweir false, 1140cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("FOOBAR")).endsWithAsciiL( 1141cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("bar"))); 1142cdf0e10cSrcweir } 1143cdf0e10cSrcweir 1144cdf0e10cSrcweir class createFromCodePoints: public CppUnit::TestFixture { 1145cdf0e10cSrcweir public: 1146cdf0e10cSrcweir void test(); 1147cdf0e10cSrcweir 1148cdf0e10cSrcweir CPPUNIT_TEST_SUITE(createFromCodePoints); 1149cdf0e10cSrcweir CPPUNIT_TEST(test); 1150cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1151cdf0e10cSrcweir }; 1152cdf0e10cSrcweir 1153cdf0e10cSrcweir void createFromCodePoints::test() { 1154cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL( 1155cdf0e10cSrcweir sal_Int32(0), 1156cdf0e10cSrcweir rtl::OUString(static_cast< sal_uInt32 const * >(NULL), 0).getLength()); 1157cdf0e10cSrcweir static sal_uInt32 const cp[] = { 0, 0xD800, 0xFFFF, 0x10000, 0x10FFFF }; 1158cdf0e10cSrcweir rtl::OUString s(cp, sizeof cp / sizeof (sal_uInt32)); 1159cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength()); 1160cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0), s[0]); 1161cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xD800), s[1]); 1162cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xFFFF), s[2]); 1163cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xD800), s[3]); 1164cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDC00), s[4]); 1165cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDBFF), s[5]); 1166cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDFFF), s[6]); 1167cdf0e10cSrcweir } 1168cdf0e10cSrcweir 1169cdf0e10cSrcweir class iterateCodePoints: public CppUnit::TestFixture { 1170cdf0e10cSrcweir public: 1171cdf0e10cSrcweir void testNotWellFormed(); 1172cdf0e10cSrcweir 1173cdf0e10cSrcweir CPPUNIT_TEST_SUITE(iterateCodePoints); 1174cdf0e10cSrcweir CPPUNIT_TEST(testNotWellFormed); 1175cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1176cdf0e10cSrcweir }; 1177cdf0e10cSrcweir 1178cdf0e10cSrcweir void iterateCodePoints::testNotWellFormed() { 1179cdf0e10cSrcweir static sal_Unicode const utf16[] = 1180cdf0e10cSrcweir { 0xD800, 0xDC00, 0x0041, 0xDBFF, 0xDFFF, 0xDDEF, 0xD9AB }; 1181cdf0e10cSrcweir rtl::OUString s(utf16, sizeof utf16 / sizeof (sal_Unicode)); 1182cdf0e10cSrcweir sal_Int32 i = 0; 1183cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i)); 1184cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i); 1185cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i)); 1186cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); 1187cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i)); 1188cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i); 1189cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i)); 1190cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i); 1191cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i)); 1192cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(7), i); 1193cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i, -1)); 1194cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i); 1195cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i, -1)); 1196cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i); 1197cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i, -1)); 1198cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); 1199cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i, -1)); 1200cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i); 1201cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -1)); 1202cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); 1203cdf0e10cSrcweir i = 1; 1204cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), s.iterateCodePoints(&i, 2)); 1205cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i); 1206cdf0e10cSrcweir i = 4; 1207cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -3)); 1208cdf0e10cSrcweir CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); 1209cdf0e10cSrcweir } 1210cdf0e10cSrcweir 1211cdf0e10cSrcweir class convertFromString: public CppUnit::TestFixture { 1212cdf0e10cSrcweir public: 1213cdf0e10cSrcweir void test(); 1214cdf0e10cSrcweir 1215cdf0e10cSrcweir CPPUNIT_TEST_SUITE(createFromCodePoints); 1216cdf0e10cSrcweir CPPUNIT_TEST(test); 1217cdf0e10cSrcweir CPPUNIT_TEST_SUITE_END(); 1218cdf0e10cSrcweir }; 1219cdf0e10cSrcweir 1220cdf0e10cSrcweir void convertFromString::test() { 1221cdf0e10cSrcweir rtl::OUString t; 1222cdf0e10cSrcweir CPPUNIT_ASSERT( 1223cdf0e10cSrcweir !rtl_convertStringToUString( 1224cdf0e10cSrcweir &t.pData, RTL_CONSTASCII_STRINGPARAM("\x80"), RTL_TEXTENCODING_UTF8, 1225cdf0e10cSrcweir (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | 1226cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | 1227cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); 1228cdf0e10cSrcweir CPPUNIT_ASSERT( 1229cdf0e10cSrcweir !rtl_convertStringToUString( 1230cdf0e10cSrcweir &t.pData, RTL_CONSTASCII_STRINGPARAM("\xC0"), RTL_TEXTENCODING_UTF8, 1231cdf0e10cSrcweir (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | 1232cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | 1233cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); 1234cdf0e10cSrcweir CPPUNIT_ASSERT( 1235cdf0e10cSrcweir !rtl_convertStringToUString( 1236cdf0e10cSrcweir &t.pData, RTL_CONSTASCII_STRINGPARAM("\xFF"), RTL_TEXTENCODING_UTF8, 1237cdf0e10cSrcweir (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | 1238cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | 1239cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); 1240cdf0e10cSrcweir CPPUNIT_ASSERT( 1241cdf0e10cSrcweir rtl_convertStringToUString( 1242cdf0e10cSrcweir &t.pData, RTL_CONSTASCII_STRINGPARAM("abc"), RTL_TEXTENCODING_UTF8, 1243cdf0e10cSrcweir (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR | 1244cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR | 1245cdf0e10cSrcweir RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR))); 1246cdf0e10cSrcweir CPPUNIT_ASSERT(t.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("abc"))); 1247cdf0e10cSrcweir } 1248cdf0e10cSrcweir 1249cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1250cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::valueOf, "rtl_OUString"); 1251cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::toInt, "rtl_OUString"); 1252cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::toDouble, "rtl_OUString"); 1253cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::toFloat, "rtl_OUString"); 1254cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::lastIndexOf, "rtl_OUString"); 1255cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::getToken, "rtl_OUString"); 1256cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( 1257cdf0e10cSrcweir rtl_OUString::convertToString, "rtl_OUString"); 1258cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::construction, "rtl_OUString"); 1259cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( 1260cdf0e10cSrcweir rtl_OUString::indexOfAscii, "rtl_OUString"); 1261cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OUString::endsWith, "rtl_OUString"); 1262cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( 1263cdf0e10cSrcweir rtl_OUString::createFromCodePoints, "rtl_OUString"); 1264cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( 1265cdf0e10cSrcweir rtl_OUString::iterateCodePoints, "rtl_OUString"); 1266cdf0e10cSrcweir CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( 1267cdf0e10cSrcweir rtl_OUString::convertFromString, "rtl_OUString"); 1268cdf0e10cSrcweir 1269cdf0e10cSrcweir } // namespace rtl_OUString 1270cdf0e10cSrcweir 1271cdf0e10cSrcweir 1272cdf0e10cSrcweir // ----------------------------------------------------------------------------- 1273cdf0e10cSrcweir 1274cdf0e10cSrcweir // this macro creates an empty function, which will called by the RegisterAllFunctions() 1275cdf0e10cSrcweir // to let the user the possibility to also register some functions by hand. 1276cdf0e10cSrcweir NOADDITIONAL; 1277