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