Bump version to 6.4-15
[LibreOffice.git] / sal / qa / rtl / oustring / rtl_OUString2.cxx
blob4a3a405d8fc56f28fa6fca3f206989e7205ace67
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 // autogenerated file with codegen.pl
22 #include <memory>
23 #include <math.h>
24 #include <stdio.h>
26 #include <algorithm>
27 #include <limits>
29 #include <cppunit/TestFixture.h>
30 #include <cppunit/extensions/HelperMacros.h>
31 #include <cppunit/plugin/TestPlugIn.h>
33 #include <o3tl/cppunittraitshelper.hxx>
35 #include <stringhelper.hxx>
36 #include <valueequal.hxx>
38 namespace rtl_OUString
41 namespace {
43 // Avoid -fsanitize=undefined warning e.g. "runtime error: value 1e+99 is
44 // outside the range of representable values of type 'float'":
45 float doubleToFloat(double x) {
46 return
47 x < -std::numeric_limits<float>::max()
48 ? -std::numeric_limits<float>::infinity()
49 : x > std::numeric_limits<float>::max()
50 ? std::numeric_limits<float>::infinity()
51 : static_cast<float>(x);
56 class number : public CppUnit::TestFixture
58 void number_float_test_impl(float _nValue)
60 OUString suValue(OUString::number(_nValue));
61 OString sValue;
62 sValue <<= suValue;
63 printf("nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr());
65 double nValueATOF = doubleToFloat(atof( sValue.getStr() ));
67 bool bEqualResult = is_float_equal(_nValue, nValueATOF);
68 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
71 void number_float_test(float _nValue)
73 number_float_test_impl(_nValue);
75 // test also the negative part.
76 float nNegativeValue = -_nValue;
77 number_float_test_impl(nNegativeValue);
80 public:
81 // insert your test code here.
82 void number_float_test_001()
84 // this is demonstration code
85 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
86 float nValue = 3.0f;
87 number_float_test(nValue);
90 void number_float_test_002()
92 float nValue = 3.5f;
93 number_float_test(nValue);
96 void number_float_test_003()
98 float nValue = 3.0625f;
99 number_float_test(nValue);
102 void number_float_test_004()
104 float nValue = 3.502525f;
105 number_float_test(nValue);
108 void number_float_test_005()
110 float nValue = 3.141592f;
111 number_float_test(nValue);
114 void number_float_test_006()
116 float nValue = 3.5025255f;
117 number_float_test(nValue);
120 void number_float_test_007()
122 float nValue = 3.0039062f;
123 number_float_test(nValue);
126 private:
128 void number_double_test_impl(double _nValue)
130 OUString suValue = OUString::number( _nValue );
131 OString sValue;
132 sValue <<= suValue;
133 printf("nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr());
135 double nValueATOF = atof( sValue.getStr() );
137 bool bEqualResult = is_double_equal(_nValue, nValueATOF);
138 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
141 void number_double_test(double _nValue)
143 number_double_test_impl(_nValue);
145 // test also the negative part.
146 double nNegativeValue = -_nValue;
147 number_double_test_impl(nNegativeValue);
149 public:
151 // number double
152 void number_double_test_001()
154 double nValue = 3.0;
155 number_double_test(nValue);
157 void number_double_test_002()
159 double nValue = 3.5;
160 number_double_test(nValue);
162 void number_double_test_003()
164 double nValue = 3.0625;
165 number_double_test(nValue);
167 void number_double_test_004()
169 double nValue = 3.1415926535;
170 number_double_test(nValue);
172 void number_double_test_005()
174 double nValue = 3.141592653589793;
175 number_double_test(nValue);
177 void number_double_test_006()
179 double nValue = 3.1415926535897932;
180 number_double_test(nValue);
182 void number_double_test_007()
184 double nValue = 3.14159265358979323;
185 number_double_test(nValue);
187 void number_double_test_008()
189 double nValue = 3.141592653589793238462643;
190 number_double_test(nValue);
193 // Change the following lines only, if you add, remove or rename
194 // member functions of the current class,
195 // because these macros are need by auto register mechanism.
197 CPPUNIT_TEST_SUITE(number);
198 CPPUNIT_TEST(number_float_test_001);
199 CPPUNIT_TEST(number_float_test_002);
200 CPPUNIT_TEST(number_float_test_003);
201 CPPUNIT_TEST(number_float_test_004);
202 CPPUNIT_TEST(number_float_test_005);
203 CPPUNIT_TEST(number_float_test_006);
204 CPPUNIT_TEST(number_float_test_007);
206 CPPUNIT_TEST(number_double_test_001);
207 CPPUNIT_TEST(number_double_test_002);
208 CPPUNIT_TEST(number_double_test_003);
209 CPPUNIT_TEST(number_double_test_004);
210 CPPUNIT_TEST(number_double_test_005);
211 CPPUNIT_TEST(number_double_test_006);
212 CPPUNIT_TEST(number_double_test_007);
213 CPPUNIT_TEST(number_double_test_008);
214 CPPUNIT_TEST_SUITE_END();
215 }; // class number
217 class toInt: public CppUnit::TestFixture {
218 public:
219 void test() {
220 CPPUNIT_ASSERT_EQUAL(
221 static_cast< sal_Int32 >(-0x76543210),
222 (OUString("-76543210").
223 toInt32(16)));
224 // @return 0 if this string represents no number or one of too large magnitude
225 CPPUNIT_ASSERT_EQUAL(
226 static_cast< sal_Int32 >(0),
227 (OUString("+FEDCBA98").
228 toInt32(16)));
229 CPPUNIT_ASSERT_EQUAL(
230 static_cast< sal_Int64 >(-SAL_CONST_INT64(0x76543210FEDCBA98)),
231 (OUString(
232 "-76543210FEDCBA98").
233 toInt64(16)));
234 // @return 0 if this string represents no number or one of too large magnitude
235 CPPUNIT_ASSERT_EQUAL(
236 static_cast< sal_Int64 >(SAL_CONST_INT64(0)),
237 (OUString(
238 "+FEDCBA9876543210").
239 toInt64(16)));
242 CPPUNIT_TEST_SUITE(toInt);
243 CPPUNIT_TEST(test);
244 CPPUNIT_TEST_SUITE_END();
247 // - toDouble (tests)
248 class toDouble : public CppUnit::TestFixture
250 public:
251 void toDouble_test_impl(OString const& _sValue)
253 //printf("the original str is %s\n", _sValue.getStr());
254 double nValueATOF = atof( _sValue.getStr() );
255 //printf("original data is %e\n", nValueATOF);
256 OUString suValue = OUString::createFromAscii( _sValue.getStr() );
257 double nValueToDouble = suValue.toDouble();
258 //printf("result data is %e\n", nValueToDouble);
260 bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
261 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
264 void toDouble_test(OString const& _sValue)
266 toDouble_test_impl(_sValue);
268 // test also the negative part.
269 OString sNegativValue("-");
270 sNegativValue += _sValue;
271 toDouble_test_impl(sNegativValue);
274 // insert your test code here.
275 void toDouble_selftest()
277 printf("Start selftest:\n");
278 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.01));
279 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.001));
280 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0001));
281 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00001));
282 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000001));
283 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000001));
284 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000001));
285 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000001));
286 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000001));
287 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000000001));
288 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000000001));
289 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000000001));
290 // we check til 15 values after comma
291 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001));
292 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001));
293 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001));
294 printf("Selftest done.\n");
297 void toDouble_test_3()
299 OString sValue("3");
300 toDouble_test(sValue);
302 void toDouble_test_3_5()
304 OString sValue("3.5");
305 toDouble_test(sValue);
307 void toDouble_test_3_0625()
309 OString sValue("3.0625");
310 toDouble_test(sValue);
312 void toDouble_test_pi()
314 // value from http://www.angio.net/pi/digits/50.txt
315 OString sValue("3.141592653589793238462643383279502884197169399375");
316 toDouble_test(sValue);
319 void toDouble_test_1()
321 OString sValue("1");
322 toDouble_test(sValue);
324 void toDouble_test_10()
326 OString sValue("10");
327 toDouble_test(sValue);
329 void toDouble_test_100()
331 OString sValue("100");
332 toDouble_test(sValue);
334 void toDouble_test_1000()
336 OString sValue("1000");
337 toDouble_test(sValue);
339 void toDouble_test_10000()
341 OString sValue("10000");
342 toDouble_test(sValue);
344 void toDouble_test_1e99()
346 OString sValue("1e99");
347 toDouble_test(sValue);
349 void toDouble_test_1e_n99()
351 OString sValue("1e-99");
352 toDouble_test(sValue);
354 void toDouble_test_1e308()
356 OString sValue("1e308");
357 toDouble_test(sValue);
360 // Change the following lines only, if you add, remove or rename
361 // member functions of the current class,
362 // because these macros are need by auto register mechanism.
364 CPPUNIT_TEST_SUITE(toDouble);
365 CPPUNIT_TEST(toDouble_selftest);
367 CPPUNIT_TEST(toDouble_test_3);
368 CPPUNIT_TEST(toDouble_test_3_5);
369 CPPUNIT_TEST(toDouble_test_3_0625);
370 CPPUNIT_TEST(toDouble_test_pi);
371 CPPUNIT_TEST(toDouble_test_1);
372 CPPUNIT_TEST(toDouble_test_10);
373 CPPUNIT_TEST(toDouble_test_100);
374 CPPUNIT_TEST(toDouble_test_1000);
375 CPPUNIT_TEST(toDouble_test_10000);
376 CPPUNIT_TEST(toDouble_test_1e99);
377 CPPUNIT_TEST(toDouble_test_1e_n99);
378 CPPUNIT_TEST(toDouble_test_1e308);
379 CPPUNIT_TEST_SUITE_END();
380 }; // class toDouble
382 // - toFloat (tests)
383 class toFloat : public CppUnit::TestFixture
385 public:
386 void toFloat_test_impl(OString const& _sValue)
388 //printf("the original str is %s\n", _sValue.getStr());
389 float nValueATOF = doubleToFloat(atof( _sValue.getStr() ));
390 //printf("the original str is %.10f\n", nValueATOF);
391 OUString suValue = OUString::createFromAscii( _sValue.getStr() );
392 float nValueToFloat = suValue.toFloat();
393 //printf("the result str is %.10f\n", nValueToFloat);
395 bool bEqualResult = is_float_equal(nValueToFloat, nValueATOF);
396 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
399 void toFloat_test(OString const& _sValue)
401 toFloat_test_impl(_sValue);
403 // test also the negative part.
404 OString sNegativValue("-");
405 sNegativValue += _sValue;
406 toFloat_test_impl(sNegativValue);
409 // insert your test code here.
410 void toFloat_selftest()
412 printf("Start selftest:\n");
413 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.01f));
414 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.001f));
415 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.0001f));
416 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.00001f));
417 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.000002f));
418 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0000001f));
419 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00000001f));
420 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000000001f));
422 printf("Selftest done.\n");
425 void toFloat_test_3()
427 OString sValue("3");
428 toFloat_test(sValue);
430 void toFloat_test_3_5()
432 OString sValue("3.5");
433 toFloat_test(sValue);
435 void toFloat_test_3_0625()
437 OString sValue("3.0625");
438 toFloat_test(sValue);
440 void toFloat_test_3_0625_e()
442 OString sValue("3.0625e-4");
443 toFloat_test(sValue);
445 void toFloat_test_pi()
447 // value from http://www.angio.net/pi/digits/50.txt
448 OString sValue("3.141592653589793238462643383279502884197169399375");
449 toFloat_test(sValue);
452 void toFloat_test_1()
454 OString sValue("1");
455 toFloat_test(sValue);
457 void toFloat_test_10()
459 OString sValue("10");
460 toFloat_test(sValue);
462 void toFloat_test_100()
464 OString sValue("100");
465 toFloat_test(sValue);
467 void toFloat_test_1000()
469 OString sValue("1000");
470 toFloat_test(sValue);
472 void toFloat_test_10000()
474 OString sValue("10000");
475 toFloat_test(sValue);
477 void toFloat_test_mix()
479 OString sValue("456789321455.123456789012");
480 toFloat_test(sValue);
482 void toFloat_test_1e99()
484 OString sValue("1e99");
485 toFloat_test(sValue);
487 void toFloat_test_1e_n99()
489 OString sValue("1e-9");
490 toFloat_test(sValue);
492 void toFloat_test_1e308()
494 OString sValue("1e308");
495 toFloat_test(sValue);
498 // Change the following lines only, if you add, remove or rename
499 // member functions of the current class,
500 // because these macros are need by auto register mechanism.
502 CPPUNIT_TEST_SUITE(toFloat);
503 CPPUNIT_TEST(toFloat_selftest);
505 CPPUNIT_TEST(toFloat_test_3);
506 CPPUNIT_TEST(toFloat_test_3_5);
507 CPPUNIT_TEST(toFloat_test_3_0625);
508 CPPUNIT_TEST(toFloat_test_3_0625_e);
509 CPPUNIT_TEST(toFloat_test_pi);
510 CPPUNIT_TEST(toFloat_test_1);
511 CPPUNIT_TEST(toFloat_test_10);
512 CPPUNIT_TEST(toFloat_test_100);
513 CPPUNIT_TEST(toFloat_test_1000);
514 CPPUNIT_TEST(toFloat_test_10000);
515 CPPUNIT_TEST(toFloat_test_mix);
516 CPPUNIT_TEST(toFloat_test_1e99);
517 CPPUNIT_TEST(toFloat_test_1e_n99);
518 CPPUNIT_TEST(toFloat_test_1e308);
519 CPPUNIT_TEST_SUITE_END();
520 }; // class toFloat
522 // - lastIndexOf (tests)
523 class lastIndexOf : public CppUnit::TestFixture
526 public:
527 void lastIndexOf_oustring(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos)
529 // Algorithm
530 // search the string _suSearchStr (OUString) in the string _suStr.
531 // check if the _nExpectedResultPos occurs.
533 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr);
534 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
537 void lastIndexOf_salunicode(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos)
539 // Algorithm
540 // search the unicode char _suSearchChar (sal_Unicode) in the string _suStr.
541 // check if the _nExpectedResultPos occurs.
543 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar);
544 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
547 void lastIndexOf_oustring_offset(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
549 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr, _nStartOffset);
550 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
553 void lastIndexOf_salunicode_offset(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
555 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar, _nStartOffset);
556 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
559 void lastIndexOf_test_oustring_offset_001()
561 // search for sun, start at the end, found (pos==0)
562 OUString aStr("sun java system");
563 OUString aSearchStr("sun");
564 lastIndexOf_oustring_offset(aStr, aSearchStr, 0, aStr.getLength());
567 void lastIndexOf_test_oustring_offset_002()
569 // search for sun, start at pos = 3, found (pos==0)
570 OUString aStr("sun java system");
571 OUString aSearchStr("sun");
572 lastIndexOf_oustring_offset(aStr, aSearchStr, 0, 3);
575 void lastIndexOf_test_oustring_offset_003()
577 // search for sun, start at pos = 2, found (pos==-1)
578 OUString aStr("sun java system");
579 OUString aSearchStr("sun");
580 lastIndexOf_oustring_offset(aStr, aSearchStr, -1, 2);
583 void lastIndexOf_test_oustring_offset_004()
585 // search for sun, start at the end, found (pos==0)
586 OUString aStr("sun java system");
587 OUString aSearchStr("sun");
588 lastIndexOf_oustring_offset(aStr, aSearchStr, -1, 1);
591 void lastIndexOf_test_oustring_001()
593 // search for sun, found (pos==0)
594 OUString aStr("sun java system");
595 OUString aSearchStr("sun");
596 lastIndexOf_oustring(aStr, aSearchStr, 0);
599 void lastIndexOf_test_oustring_002()
601 // search for sun, found (pos==4)
602 OUString aStr("the sun java system");
603 OUString aSearchStr("sun");
604 lastIndexOf_oustring(aStr, aSearchStr, 4);
607 void lastIndexOf_test_oustring_003()
609 // search for sun, found (pos==8)
610 OUString aStr("the sun sun java system");
611 OUString aSearchStr("sun");
612 lastIndexOf_oustring(aStr, aSearchStr, 8);
615 void lastIndexOf_test_oustring_004()
617 // search for sun, found (pos==8)
618 OUString aStr("the sun sun");
619 OUString aSearchStr("sun");
620 lastIndexOf_oustring(aStr, aSearchStr, 8);
623 void lastIndexOf_test_oustring_005()
625 // search for sun, found (pos==4)
626 OUString aStr("the sun su");
627 OUString aSearchStr("sun");
628 lastIndexOf_oustring(aStr, aSearchStr, 4);
631 void lastIndexOf_test_oustring_006()
633 // search for sun, found (pos==-1)
634 OUString aStr("the su su");
635 OUString aSearchStr("sun");
636 lastIndexOf_oustring(aStr, aSearchStr, -1);
639 void lastIndexOf_test_oustring_007()
641 // search for earth, not found (-1)
642 OUString aStr("the su su");
643 OUString aSearchStr("earth");
644 lastIndexOf_oustring(aStr, aSearchStr, -1);
647 void lastIndexOf_test_oustring_008()
649 // search for earth, not found (-1)
650 OUString aStr;
651 OUString aSearchStr("earth");
652 lastIndexOf_oustring(aStr, aSearchStr, -1);
655 void lastIndexOf_test_oustring_009()
657 // search for earth, not found (-1)
658 OUString aStr;
659 OUString aSearchStr;
660 lastIndexOf_oustring(aStr, aSearchStr, -1);
664 void lastIndexOf_test_salunicode_001()
666 // search for 's', found (19)
667 OUString aStr("the sun sun java system");
668 sal_Unicode suChar = L's';
669 lastIndexOf_salunicode(aStr, suChar, 19);
672 void lastIndexOf_test_salunicode_002()
674 // search for 'x', not found (-1)
675 OUString aStr("the sun sun java system");
676 sal_Unicode suChar = L'x';
677 lastIndexOf_salunicode(aStr, suChar, -1);
680 void lastIndexOf_test_salunicode_offset_001()
682 // search for 's', start from pos last char, found (19)
683 OUString aStr("the sun sun java system");
684 sal_Unicode cuChar = L's';
685 lastIndexOf_salunicode_offset(aStr, cuChar, 19, aStr.getLength());
687 void lastIndexOf_test_salunicode_offset_002()
689 // search for 's', start pos is last occur from search behind, found (17)
690 OUString aStr("the sun sun java system");
691 sal_Unicode cuChar = L's';
692 lastIndexOf_salunicode_offset(aStr, cuChar, 17, 19);
694 void lastIndexOf_test_salunicode_offset_003()
696 // search for 't', start pos is 1, found (0)
697 OUString aStr("the sun sun java system");
698 sal_Unicode cuChar = L't';
699 lastIndexOf_salunicode_offset(aStr, cuChar, 0, 1);
702 // Change the following lines only, if you add, remove or rename
703 // member functions of the current class,
704 // because these macros are need by auto register mechanism.
706 CPPUNIT_TEST_SUITE(lastIndexOf);
707 CPPUNIT_TEST(lastIndexOf_test_oustring_001);
708 CPPUNIT_TEST(lastIndexOf_test_oustring_002);
709 CPPUNIT_TEST(lastIndexOf_test_oustring_003);
710 CPPUNIT_TEST(lastIndexOf_test_oustring_004);
711 CPPUNIT_TEST(lastIndexOf_test_oustring_005);
712 CPPUNIT_TEST(lastIndexOf_test_oustring_006);
713 CPPUNIT_TEST(lastIndexOf_test_oustring_007);
714 CPPUNIT_TEST(lastIndexOf_test_oustring_008);
715 CPPUNIT_TEST(lastIndexOf_test_oustring_009);
717 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_001);
718 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_002);
719 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_003);
720 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_004);
722 CPPUNIT_TEST(lastIndexOf_test_salunicode_001);
723 CPPUNIT_TEST(lastIndexOf_test_salunicode_002);
725 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_001);
726 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_002);
727 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_003);
729 CPPUNIT_TEST_SUITE_END();
730 }; // class lastIndexOf
732 // - getToken (tests)
733 class getToken : public CppUnit::TestFixture
736 public:
737 void getToken_000()
739 OUString suTokenStr;
741 sal_Int32 nIndex = 0;
744 suTokenStr.getToken( 0, ';', nIndex );
746 while ( nIndex >= 0 );
747 printf("Index %" SAL_PRIdINT32 "\n", nIndex);
748 // should not GPF
751 void getToken_001()
753 OUString suTokenStr("a;b");
755 sal_Int32 nIndex = 0;
757 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
758 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", OUString("a"), suToken);
760 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
761 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", OUString("b"), suToken);
762 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
765 void getToken_002()
767 OUString suTokenStr("a;b.c");
769 sal_Int32 nIndex = 0;
771 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
772 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", OUString("a"), suToken );
774 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
775 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", OUString("b"), suToken );
777 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
778 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'c'", OUString("c"), suToken );
779 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
782 void getToken_003()
784 OUString suTokenStr("a;;b");
786 sal_Int32 nIndex = 0;
788 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
789 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", OUString("a"), suToken );
791 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
792 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
794 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
795 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", OUString("b"), suToken );
796 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
799 void getToken_004()
801 OUString suTokenStr("longer.then.ever.");
803 sal_Int32 nIndex = 0;
805 OUString suToken = suTokenStr.getToken( 0, '.', nIndex );
806 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'longer'", OUString("longer"), suToken );
808 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
809 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'then'", OUString("then"), suToken );
811 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
812 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'ever'", OUString("ever"), suToken );
814 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
815 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
817 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
820 void getToken_005() {
821 OUString ab("ab");
822 sal_Int32 n = 0;
823 CPPUNIT_ASSERT_EQUAL_MESSAGE(
824 "token should be 'ab'", ab, ab.getToken(0, '-', n));
825 CPPUNIT_ASSERT_EQUAL_MESSAGE("n should be -1", static_cast<sal_Int32>(-1), n);
826 CPPUNIT_ASSERT_MESSAGE(
827 "token should be empty", ab.getToken(0, '-', n).isEmpty());
830 CPPUNIT_TEST_SUITE(getToken);
831 CPPUNIT_TEST(getToken_000);
832 CPPUNIT_TEST(getToken_001);
833 CPPUNIT_TEST(getToken_002);
834 CPPUNIT_TEST(getToken_003);
835 CPPUNIT_TEST(getToken_004);
836 CPPUNIT_TEST(getToken_005);
837 CPPUNIT_TEST_SUITE_END();
838 }; // class getToken
840 class convertToString: public CppUnit::TestFixture {
841 public:
842 void test();
844 CPPUNIT_TEST_SUITE(convertToString);
845 CPPUNIT_TEST(test);
846 CPPUNIT_TEST_SUITE_END();
849 void convertToString::test() {
850 static sal_Unicode const utf16[] = { 0x0041, 0x00E4, 0x0061 };
851 OString s;
852 CPPUNIT_ASSERT(
853 OUString(utf16, SAL_N_ELEMENTS(utf16)).convertToString(
854 &s, RTL_TEXTENCODING_UTF7,
855 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
856 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)));
857 CPPUNIT_ASSERT_EQUAL(
858 OString(RTL_CONSTASCII_STRINGPARAM("A+AOQ-a")), s);
861 // - string construction & interning (tests)
863 class construction : public CppUnit::TestFixture
865 public:
866 void construct()
868 #ifdef RTL_INLINE_STRINGS
869 OUString aFoo( "foo" );
870 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[0] == 'f');
871 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[1] == 'o');
872 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[2] == 'o');
873 CPPUNIT_ASSERT_MESSAGE("string length", aFoo.getLength() == 3);
875 OUString aBaa( "this is a very long string with a lot of long things inside it and it goes on and on and on forever etc." );
876 CPPUNIT_ASSERT_MESSAGE("string length", aBaa.getLength() == 104);
877 // Dig at the internals ... FIXME: should we have the bit-flag defines public ?
878 CPPUNIT_ASSERT_MESSAGE("string static flags", (aBaa.pData->refCount & 1<<30) != 0);
879 #endif
882 void intern()
884 // The empty string is 'static' a special case ...
885 OUString().intern();
886 OUString::intern( "",strlen(""),RTL_TEXTENCODING_ASCII_US );
888 OUString aFoo( "foo" );
889 OUString aFooIntern = aFoo.intern();
890 CPPUNIT_ASSERT_EQUAL_MESSAGE( "string contents", OUString("foo"), aFooIntern);
891 CPPUNIT_ASSERT_EQUAL_MESSAGE("string length", static_cast<sal_Int32>(3), aFooIntern.getLength());
892 // We have to dup due to no atomic 'intern' bit-set operation
893 CPPUNIT_ASSERT_MESSAGE("intern dups", aFoo.pData != aFooIntern.pData);
895 // Test interning lots of things
896 int i;
897 static const int nSequence = 4096;
898 std::unique_ptr<OUString[]> pStrs(new OUString[nSequence]);
899 for (i = 0; i < nSequence; i++)
901 pStrs[i] = OUString( OUString::number( sqrt( static_cast<double>(i) ) ) ).intern();
903 for (i = 0; i < nSequence; i++)
905 OUString aNew = OUString( OUString::number( sqrt( static_cast<double>(i) ) ) ).intern();
906 CPPUNIT_ASSERT_EQUAL_MESSAGE("double intern failed",
907 pStrs[i].pData, aNew.pData);
911 CPPUNIT_TEST_SUITE(construction);
912 CPPUNIT_TEST(construct);
913 CPPUNIT_TEST(intern);
914 CPPUNIT_TEST_SUITE_END();
917 class indexOfAscii: public CppUnit::TestFixture {
918 public:
919 void test();
921 CPPUNIT_TEST_SUITE(indexOfAscii);
922 CPPUNIT_TEST(test);
923 CPPUNIT_TEST_SUITE_END();
926 void indexOfAscii::test() {
927 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().indexOf(""));
928 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().lastIndexOf(""));
929 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), OUString("foo").indexOf("foo"));
930 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), OUString("foo").lastIndexOf("foo"));
931 CPPUNIT_ASSERT_EQUAL(
932 sal_Int32(2), OUString("fofoobar").indexOf("foo"));
933 CPPUNIT_ASSERT_EQUAL(
934 sal_Int32(3), OUString("foofoofob").lastIndexOf("foo"));
935 CPPUNIT_ASSERT_EQUAL(
936 sal_Int32(3), OUString("foofoobar").indexOf("foo", 1));
939 class endsWith: public CppUnit::TestFixture {
940 public:
941 void test();
943 CPPUNIT_TEST_SUITE(endsWith);
944 CPPUNIT_TEST(test);
945 CPPUNIT_TEST_SUITE_END();
948 void endsWith::test() {
949 CPPUNIT_ASSERT_EQUAL(true, OUString().endsWith(""));
950 CPPUNIT_ASSERT_EQUAL(false, OUString().endsWith("foo"));
951 CPPUNIT_ASSERT_EQUAL(true, OUString("bar").endsWith("bar"));
952 CPPUNIT_ASSERT_EQUAL(true, OUString("foobar").endsWith("bar"));
953 CPPUNIT_ASSERT_EQUAL(false, OUString("FOOBAR").endsWith("bar"));
956 class isEmpty: public CppUnit::TestFixture {
957 public:
958 void test();
960 CPPUNIT_TEST_SUITE(isEmpty);
961 CPPUNIT_TEST(test);
962 CPPUNIT_TEST_SUITE_END();
965 void isEmpty::test() {
966 OUString aString;
967 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Newly constructed string should be empty", true, aString.isEmpty() );
969 aString = "Not empty any more";
970 CPPUNIT_ASSERT_EQUAL_MESSAGE( "String should not be empty", false, aString.isEmpty() );
972 aString.clear();
973 CPPUNIT_ASSERT_EQUAL_MESSAGE( "aString.clear(), so should now be empty", true, aString.isEmpty() );
977 class createFromCodePoints: public CppUnit::TestFixture {
978 public:
979 void test();
981 CPPUNIT_TEST_SUITE(createFromCodePoints);
982 CPPUNIT_TEST(test);
983 CPPUNIT_TEST_SUITE_END();
986 void createFromCodePoints::test() {
987 CPPUNIT_ASSERT_EQUAL(
988 sal_Int32(0),
989 OUString(static_cast< sal_uInt32 const * >(nullptr), 0).getLength());
990 static sal_uInt32 const cp[] = { 0, 0xD800, 0xFFFF, 0x10000, 0x10FFFF };
991 OUString s(cp, SAL_N_ELEMENTS(cp));
992 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength());
993 CPPUNIT_ASSERT_EQUAL(u'\0', s[0]);
994 CPPUNIT_ASSERT_EQUAL(u'\xD800', s[1]);
995 CPPUNIT_ASSERT_EQUAL(u'\xFFFF', s[2]);
996 CPPUNIT_ASSERT_EQUAL(u'\xD800', s[3]);
997 CPPUNIT_ASSERT_EQUAL(u'\xDC00', s[4]);
998 CPPUNIT_ASSERT_EQUAL(u'\xDBFF', s[5]);
999 CPPUNIT_ASSERT_EQUAL(u'\xDFFF', s[6]);
1002 class iterateCodePoints: public CppUnit::TestFixture {
1003 public:
1004 void testNotWellFormed();
1006 CPPUNIT_TEST_SUITE(iterateCodePoints);
1007 CPPUNIT_TEST(testNotWellFormed);
1008 CPPUNIT_TEST_SUITE_END();
1011 void iterateCodePoints::testNotWellFormed() {
1012 static sal_Unicode const utf16[] =
1013 { 0xD800, 0xDC00, 0x0041, 0xDBFF, 0xDFFF, 0xDDEF, 0xD9AB };
1014 OUString s(utf16, SAL_N_ELEMENTS(utf16));
1015 sal_Int32 i = 0;
1016 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i));
1017 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
1018 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i));
1019 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1020 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i));
1021 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
1022 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i));
1023 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
1024 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i));
1025 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), i);
1026 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i, -1));
1027 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
1028 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i, -1));
1029 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
1030 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i, -1));
1031 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1032 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i, -1));
1033 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
1034 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -1));
1035 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
1036 i = 1;
1037 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), s.iterateCodePoints(&i, 2));
1038 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1039 i = 4;
1040 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -3));
1041 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
1044 class convertFromString: public CppUnit::TestFixture {
1045 public:
1046 void test();
1048 CPPUNIT_TEST_SUITE(convertFromString);
1049 CPPUNIT_TEST(test);
1050 CPPUNIT_TEST_SUITE_END();
1053 void convertFromString::test() {
1054 OUString t;
1055 CPPUNIT_ASSERT(
1056 !rtl_convertStringToUString(
1057 &t.pData, RTL_CONSTASCII_STRINGPARAM("\x80"), RTL_TEXTENCODING_UTF8,
1058 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1059 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1060 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1061 CPPUNIT_ASSERT(
1062 !rtl_convertStringToUString(
1063 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xC0"), RTL_TEXTENCODING_UTF8,
1064 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1065 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1066 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1067 CPPUNIT_ASSERT(
1068 !rtl_convertStringToUString(
1069 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xFF"), RTL_TEXTENCODING_UTF8,
1070 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1071 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1072 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1073 CPPUNIT_ASSERT(
1074 rtl_convertStringToUString(
1075 &t.pData, RTL_CONSTASCII_STRINGPARAM("abc"), RTL_TEXTENCODING_UTF8,
1076 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1077 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1078 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1079 CPPUNIT_ASSERT_EQUAL( OUString("abc"), t );
1082 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::number);
1083 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toInt);
1084 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toDouble);
1085 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toFloat);
1086 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::lastIndexOf);
1087 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::getToken);
1088 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertToString);
1089 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::construction);
1090 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::indexOfAscii);
1091 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::endsWith);
1092 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::isEmpty);
1093 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::createFromCodePoints);
1094 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::iterateCodePoints);
1095 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertFromString);
1097 } // namespace rtl_OUString
1099 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */