tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sal / qa / rtl / oustring / rtl_OUString2.cxx
blob505d4d7c701abca9d95eb9492b0b6a96b73b118a
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 <cppunit/TestFixture.h>
27 #include <cppunit/extensions/HelperMacros.h>
29 #include <config_options.h>
30 #include <o3tl/cppunittraitshelper.hxx>
31 #include <o3tl/safeint.hxx>
33 #include <stringhelper.hxx>
34 #include <valueequal.hxx>
36 namespace rtl_OUString
39 namespace {
41 float doubleToFloat(double x) {
42 return static_cast<float>(x);
47 class number : public CppUnit::TestFixture
49 void number_float_test_impl(float _nValue)
51 OUString suValue(OUString::number(_nValue));
52 OString sValue;
53 sValue <<= suValue;
54 printf("nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr());
56 double nValueATOF = doubleToFloat(atof( sValue.getStr() ));
58 bool bEqualResult = is_float_equal(_nValue, nValueATOF);
59 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
62 void number_float_test(float _nValue)
64 number_float_test_impl(_nValue);
66 // test also the negative part.
67 float nNegativeValue = -_nValue;
68 number_float_test_impl(nNegativeValue);
71 public:
72 // insert your test code here.
73 void number_float_test_001()
75 // this is demonstration code
76 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
77 float nValue = 3.0f;
78 number_float_test(nValue);
81 void number_float_test_002()
83 float nValue = 3.5f;
84 number_float_test(nValue);
87 void number_float_test_003()
89 float nValue = 3.0625f;
90 number_float_test(nValue);
93 void number_float_test_004()
95 float nValue = 3.502525f;
96 number_float_test(nValue);
99 void number_float_test_005()
101 float nValue = 3.141592f;
102 number_float_test(nValue);
105 void number_float_test_006()
107 float nValue = 3.5025255f;
108 number_float_test(nValue);
111 void number_float_test_007()
113 float nValue = 3.0039062f;
114 number_float_test(nValue);
117 private:
119 void number_double_test_impl(double _nValue)
121 OUString suValue = OUString::number( _nValue );
122 OString sValue;
123 sValue <<= suValue;
124 printf("nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr());
126 double nValueATOF = atof( sValue.getStr() );
128 bool bEqualResult = is_double_equal(_nValue, nValueATOF);
129 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
132 void number_double_test(double _nValue)
134 number_double_test_impl(_nValue);
136 // test also the negative part.
137 double nNegativeValue = -_nValue;
138 number_double_test_impl(nNegativeValue);
140 public:
142 // number double
143 void number_double_test_001()
145 double nValue = 3.0;
146 number_double_test(nValue);
148 void number_double_test_002()
150 double nValue = 3.5;
151 number_double_test(nValue);
153 void number_double_test_003()
155 double nValue = 3.0625;
156 number_double_test(nValue);
158 void number_double_test_004()
160 double nValue = 3.1415926535;
161 number_double_test(nValue);
163 void number_double_test_005()
165 double nValue = 3.141592653589793;
166 number_double_test(nValue);
168 void number_double_test_006()
170 double nValue = 3.1415926535897932;
171 number_double_test(nValue);
173 void number_double_test_007()
175 double nValue = 3.14159265358979323;
176 number_double_test(nValue);
178 void number_double_test_008()
180 double nValue = 3.141592653589793238462643;
181 number_double_test(nValue);
184 // Change the following lines only, if you add, remove or rename
185 // member functions of the current class,
186 // because these macros are need by auto register mechanism.
188 CPPUNIT_TEST_SUITE(number);
189 CPPUNIT_TEST(number_float_test_001);
190 CPPUNIT_TEST(number_float_test_002);
191 CPPUNIT_TEST(number_float_test_003);
192 CPPUNIT_TEST(number_float_test_004);
193 CPPUNIT_TEST(number_float_test_005);
194 CPPUNIT_TEST(number_float_test_006);
195 CPPUNIT_TEST(number_float_test_007);
197 CPPUNIT_TEST(number_double_test_001);
198 CPPUNIT_TEST(number_double_test_002);
199 CPPUNIT_TEST(number_double_test_003);
200 CPPUNIT_TEST(number_double_test_004);
201 CPPUNIT_TEST(number_double_test_005);
202 CPPUNIT_TEST(number_double_test_006);
203 CPPUNIT_TEST(number_double_test_007);
204 CPPUNIT_TEST(number_double_test_008);
205 CPPUNIT_TEST_SUITE_END();
206 }; // class number
208 class toInt: public CppUnit::TestFixture {
209 public:
210 void test() {
211 CPPUNIT_ASSERT_EQUAL(
212 static_cast< sal_Int32 >(-0x76543210),
213 (u"-76543210"_ustr.
214 toInt32(16)));
215 // @return 0 if this string represents no number or one of too large magnitude
216 CPPUNIT_ASSERT_EQUAL(
217 static_cast< sal_Int32 >(0),
218 (u"+FEDCBA98"_ustr.
219 toInt32(16)));
220 CPPUNIT_ASSERT_EQUAL(
221 static_cast< sal_Int64 >(-SAL_CONST_INT64(0x76543210FEDCBA98)),
222 (u"-76543210FEDCBA98"_ustr.
223 toInt64(16)));
224 // @return 0 if this string represents no number or one of too large magnitude
225 CPPUNIT_ASSERT_EQUAL(
226 static_cast< sal_Int64 >(SAL_CONST_INT64(0)),
227 (u"+FEDCBA9876543210"_ustr.
228 toInt64(16)));
231 CPPUNIT_TEST_SUITE(toInt);
232 CPPUNIT_TEST(test);
233 CPPUNIT_TEST_SUITE_END();
236 // - toDouble (tests)
237 class toDouble : public CppUnit::TestFixture
239 public:
240 void toDouble_test_impl(OString const& _sValue)
242 //printf("the original str is %s\n", _sValue.getStr());
243 double nValueATOF = atof( _sValue.getStr() );
244 //printf("original data is %e\n", nValueATOF);
245 OUString suValue = OUString::createFromAscii( _sValue.getStr() );
246 double nValueToDouble = suValue.toDouble();
247 //printf("result data is %e\n", nValueToDouble);
249 bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
250 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
253 void toDouble_test(OString const& _sValue)
255 toDouble_test_impl(_sValue);
257 // test also the negative part.
258 OString sNegativValue("-"_ostr);
259 sNegativValue += _sValue;
260 toDouble_test_impl(sNegativValue);
263 // insert your test code here.
264 void toDouble_selftest()
266 printf("Start selftest:\n");
267 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.01));
268 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.001));
269 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0001));
270 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00001));
271 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000001));
272 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000001));
273 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000001));
274 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000001));
275 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000001));
276 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000000001));
277 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000000001));
278 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000000001));
279 // we check til 15 values after comma
280 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001));
281 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001));
282 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001));
283 printf("Selftest done.\n");
286 void toDouble_test_3()
288 toDouble_test("3"_ostr);
290 void toDouble_test_3_5()
292 toDouble_test("3.5"_ostr);
294 void toDouble_test_3_0625()
296 toDouble_test("3.0625"_ostr);
298 void toDouble_test_pi()
300 // value from http://www.angio.net/pi/digits/50.txt
301 toDouble_test("3.141592653589793238462643383279502884197169399375"_ostr);
304 void toDouble_test_1()
306 toDouble_test("1"_ostr);
308 void toDouble_test_10()
310 toDouble_test("10"_ostr);
312 void toDouble_test_100()
314 toDouble_test("100"_ostr);
316 void toDouble_test_1000()
318 toDouble_test("1000"_ostr);
320 void toDouble_test_10000()
322 toDouble_test("10000"_ostr);
324 void toDouble_test_1e99()
326 toDouble_test("1e99"_ostr);
328 void toDouble_test_1e_n99()
330 toDouble_test("1e-99"_ostr);
332 void toDouble_test_1e308()
334 toDouble_test("1e308"_ostr);
337 // Change the following lines only, if you add, remove or rename
338 // member functions of the current class,
339 // because these macros are need by auto register mechanism.
341 CPPUNIT_TEST_SUITE(toDouble);
342 CPPUNIT_TEST(toDouble_selftest);
344 CPPUNIT_TEST(toDouble_test_3);
345 CPPUNIT_TEST(toDouble_test_3_5);
346 CPPUNIT_TEST(toDouble_test_3_0625);
347 CPPUNIT_TEST(toDouble_test_pi);
348 CPPUNIT_TEST(toDouble_test_1);
349 CPPUNIT_TEST(toDouble_test_10);
350 CPPUNIT_TEST(toDouble_test_100);
351 CPPUNIT_TEST(toDouble_test_1000);
352 CPPUNIT_TEST(toDouble_test_10000);
353 CPPUNIT_TEST(toDouble_test_1e99);
354 CPPUNIT_TEST(toDouble_test_1e_n99);
355 CPPUNIT_TEST(toDouble_test_1e308);
356 CPPUNIT_TEST_SUITE_END();
357 }; // class toDouble
359 // - toFloat (tests)
360 class toFloat : public CppUnit::TestFixture
362 public:
363 void toFloat_test_impl(OString const& _sValue)
365 //printf("the original str is %s\n", _sValue.getStr());
366 float nValueATOF = doubleToFloat(atof( _sValue.getStr() ));
367 //printf("the original str is %.10f\n", nValueATOF);
368 OUString suValue = OUString::createFromAscii( _sValue.getStr() );
369 float nValueToFloat = suValue.toFloat();
370 //printf("the result str is %.10f\n", nValueToFloat);
372 bool bEqualResult = is_float_equal(nValueToFloat, nValueATOF);
373 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
376 void toFloat_test(OString const& _sValue)
378 toFloat_test_impl(_sValue);
380 // test also the negative part.
381 OString sNegativValue("-"_ostr);
382 sNegativValue += _sValue;
383 toFloat_test_impl(sNegativValue);
386 // insert your test code here.
387 void toFloat_selftest()
389 printf("Start selftest:\n");
390 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.01f));
391 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.001f));
392 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.0001f));
393 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.00001f));
394 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.000002f));
395 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0000001f));
396 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00000001f));
397 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000000001f));
399 printf("Selftest done.\n");
402 void toFloat_test_3()
404 toFloat_test("3"_ostr);
406 void toFloat_test_3_5()
408 toFloat_test("3.5"_ostr);
410 void toFloat_test_3_0625()
412 toFloat_test("3.0625"_ostr);
414 void toFloat_test_3_0625_e()
416 toFloat_test("3.0625e-4"_ostr);
418 void toFloat_test_pi()
420 // value from http://www.angio.net/pi/digits/50.txt
421 toFloat_test("3.141592653589793238462643383279502884197169399375"_ostr);
424 void toFloat_test_1()
426 toFloat_test("1"_ostr);
428 void toFloat_test_10()
430 toFloat_test("10"_ostr);
432 void toFloat_test_100()
434 toFloat_test("100"_ostr);
436 void toFloat_test_1000()
438 toFloat_test("1000"_ostr);
440 void toFloat_test_10000()
442 toFloat_test("10000"_ostr);
444 void toFloat_test_mix()
446 toFloat_test("456789321455.123456789012"_ostr);
448 void toFloat_test_1e99()
450 toFloat_test("1e99"_ostr);
452 void toFloat_test_1e_n99()
454 toFloat_test("1e-9"_ostr);
456 void toFloat_test_1e308()
458 toFloat_test("1e308"_ostr);
461 // Change the following lines only, if you add, remove or rename
462 // member functions of the current class,
463 // because these macros are need by auto register mechanism.
465 CPPUNIT_TEST_SUITE(toFloat);
466 CPPUNIT_TEST(toFloat_selftest);
468 CPPUNIT_TEST(toFloat_test_3);
469 CPPUNIT_TEST(toFloat_test_3_5);
470 CPPUNIT_TEST(toFloat_test_3_0625);
471 CPPUNIT_TEST(toFloat_test_3_0625_e);
472 CPPUNIT_TEST(toFloat_test_pi);
473 CPPUNIT_TEST(toFloat_test_1);
474 CPPUNIT_TEST(toFloat_test_10);
475 CPPUNIT_TEST(toFloat_test_100);
476 CPPUNIT_TEST(toFloat_test_1000);
477 CPPUNIT_TEST(toFloat_test_10000);
478 CPPUNIT_TEST(toFloat_test_mix);
479 CPPUNIT_TEST(toFloat_test_1e99);
480 CPPUNIT_TEST(toFloat_test_1e_n99);
481 CPPUNIT_TEST(toFloat_test_1e308);
482 CPPUNIT_TEST_SUITE_END();
483 }; // class toFloat
485 // - lastIndexOf (tests)
486 class lastIndexOf : public CppUnit::TestFixture
489 public:
490 void lastIndexOf_oustring(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos)
492 // Algorithm
493 // search the string _suSearchStr (OUString) in the string _suStr.
494 // check if the _nExpectedResultPos occurs.
496 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr);
497 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
500 void lastIndexOf_salunicode(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos)
502 // Algorithm
503 // search the unicode char _suSearchChar (sal_Unicode) in the string _suStr.
504 // check if the _nExpectedResultPos occurs.
506 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar);
507 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
510 void lastIndexOf_oustring_offset(OUString const& _suStr, OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
512 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr, _nStartOffset);
513 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
516 void lastIndexOf_salunicode_offset(OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
518 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar, _nStartOffset);
519 CPPUNIT_ASSERT_EQUAL_MESSAGE("expected position is wrong", _nExpectedResultPos, nPos);
522 void lastIndexOf_test_oustring_offset_001()
524 // search for sun, start at the end, found (pos==0)
525 OUString aStr(u"sun java system"_ustr);
526 lastIndexOf_oustring_offset(aStr, u"sun"_ustr, 0, aStr.getLength());
529 void lastIndexOf_test_oustring_offset_002()
531 // search for sun, start at pos = 3, found (pos==0)
532 lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, 0, 3);
535 void lastIndexOf_test_oustring_offset_003()
537 // search for sun, start at pos = 2, found (pos==-1)
538 lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, -1, 2);
541 void lastIndexOf_test_oustring_offset_004()
543 // search for sun, start at the end, found (pos==0)
544 lastIndexOf_oustring_offset(u"sun java system"_ustr, u"sun"_ustr, -1, 1);
547 void lastIndexOf_test_oustring_001()
549 // search for sun, found (pos==0)
550 lastIndexOf_oustring(u"sun java system"_ustr, u"sun"_ustr, 0);
553 void lastIndexOf_test_oustring_002()
555 // search for sun, found (pos==4)
556 lastIndexOf_oustring(u"the sun java system"_ustr, u"sun"_ustr, 4);
559 void lastIndexOf_test_oustring_003()
561 // search for sun, found (pos==8)
562 lastIndexOf_oustring(u"the sun sun java system"_ustr, u"sun"_ustr, 8);
565 void lastIndexOf_test_oustring_004()
567 // search for sun, found (pos==8)
568 lastIndexOf_oustring(u"the sun sun"_ustr, u"sun"_ustr, 8);
571 void lastIndexOf_test_oustring_005()
573 // search for sun, found (pos==4)
574 lastIndexOf_oustring(u"the sun su"_ustr, u"sun"_ustr, 4);
577 void lastIndexOf_test_oustring_006()
579 // search for sun, found (pos==-1)
580 lastIndexOf_oustring(u"the su su"_ustr, u"sun"_ustr, -1);
583 void lastIndexOf_test_oustring_007()
585 // search for earth, not found (-1)
586 lastIndexOf_oustring(u"the su su"_ustr, u"earth"_ustr, -1);
589 void lastIndexOf_test_oustring_008()
591 // search for earth, not found (-1)
592 lastIndexOf_oustring(u""_ustr, u"earth"_ustr, -1);
595 void lastIndexOf_test_oustring_009()
597 // search for earth, not found (-1)
598 lastIndexOf_oustring(u""_ustr, u""_ustr, -1);
602 void lastIndexOf_test_salunicode_001()
604 // search for 's', found (19)
605 sal_Unicode suChar = L's';
606 lastIndexOf_salunicode(u"the sun sun java system"_ustr, suChar, 19);
609 void lastIndexOf_test_salunicode_002()
611 // search for 'x', not found (-1)
612 sal_Unicode suChar = L'x';
613 lastIndexOf_salunicode(u"the sun sun java system"_ustr, suChar, -1);
616 void lastIndexOf_test_salunicode_offset_001()
618 // search for 's', start from pos last char, found (19)
619 OUString aStr(u"the sun sun java system"_ustr);
620 sal_Unicode cuChar = L's';
621 lastIndexOf_salunicode_offset(aStr, cuChar, 19, aStr.getLength());
623 void lastIndexOf_test_salunicode_offset_002()
625 // search for 's', start pos is last occur from search behind, found (17)
626 sal_Unicode cuChar = L's';
627 lastIndexOf_salunicode_offset(u"the sun sun java system"_ustr, cuChar, 17, 19);
629 void lastIndexOf_test_salunicode_offset_003()
631 // search for 't', start pos is 1, found (0)
632 sal_Unicode cuChar = L't';
633 lastIndexOf_salunicode_offset(u"the sun sun java system"_ustr, cuChar, 0, 1);
636 // Change the following lines only, if you add, remove or rename
637 // member functions of the current class,
638 // because these macros are need by auto register mechanism.
640 CPPUNIT_TEST_SUITE(lastIndexOf);
641 CPPUNIT_TEST(lastIndexOf_test_oustring_001);
642 CPPUNIT_TEST(lastIndexOf_test_oustring_002);
643 CPPUNIT_TEST(lastIndexOf_test_oustring_003);
644 CPPUNIT_TEST(lastIndexOf_test_oustring_004);
645 CPPUNIT_TEST(lastIndexOf_test_oustring_005);
646 CPPUNIT_TEST(lastIndexOf_test_oustring_006);
647 CPPUNIT_TEST(lastIndexOf_test_oustring_007);
648 CPPUNIT_TEST(lastIndexOf_test_oustring_008);
649 CPPUNIT_TEST(lastIndexOf_test_oustring_009);
651 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_001);
652 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_002);
653 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_003);
654 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_004);
656 CPPUNIT_TEST(lastIndexOf_test_salunicode_001);
657 CPPUNIT_TEST(lastIndexOf_test_salunicode_002);
659 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_001);
660 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_002);
661 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_003);
663 CPPUNIT_TEST_SUITE_END();
664 }; // class lastIndexOf
666 // - getToken (tests)
667 class getToken : public CppUnit::TestFixture
670 public:
671 void getToken_000()
673 OUString suTokenStr;
675 sal_Int32 nIndex = 0;
678 suTokenStr.getToken( 0, ';', nIndex );
680 while ( nIndex >= 0 );
681 printf("Index %" SAL_PRIdINT32 "\n", nIndex);
682 // should not GPF
685 void getToken_001()
687 OUString suTokenStr(u"a;b"_ustr);
689 sal_Int32 nIndex = 0;
691 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
692 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken);
694 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
695 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken);
696 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
699 void getToken_002()
701 OUString suTokenStr(u"a;b.c"_ustr);
703 sal_Int32 nIndex = 0;
705 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
706 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken );
708 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
709 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken );
711 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
712 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'c'", u"c"_ustr, suToken );
713 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
716 void getToken_003()
718 OUString suTokenStr(u"a;;b"_ustr);
720 sal_Int32 nIndex = 0;
722 OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
723 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'a'", u"a"_ustr, suToken );
725 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
726 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
728 /* OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
729 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be a 'b'", u"b"_ustr, suToken );
730 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
733 void getToken_004()
735 OUString suTokenStr(u"longer.then.ever."_ustr);
737 sal_Int32 nIndex = 0;
739 OUString suToken = suTokenStr.getToken( 0, '.', nIndex );
740 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'longer'", u"longer"_ustr, suToken );
742 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
743 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'then'", u"then"_ustr, suToken );
745 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
746 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Token should be 'ever'", u"ever"_ustr, suToken );
748 /* OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
749 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
751 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1), nIndex);
754 void getToken_005() {
755 OUString ab(u"ab"_ustr);
756 sal_Int32 n = 0;
757 CPPUNIT_ASSERT_EQUAL_MESSAGE(
758 "token should be 'ab'", ab, ab.getToken(0, '-', n));
759 CPPUNIT_ASSERT_EQUAL_MESSAGE("n should be -1", static_cast<sal_Int32>(-1), n);
760 CPPUNIT_ASSERT_MESSAGE(
761 "token should be empty", ab.getToken(0, '-', n).isEmpty());
764 void getToken_006()
766 OUString suTokenStr;
767 auto pTokenStr = suTokenStr.getStr();
768 sal_uInt64 n64 = reinterpret_cast<sal_uInt64>(pTokenStr) / sizeof(sal_Unicode);
769 // Point either to 0x0, or to some random address -4GiB away from this string
770 sal_Int32 n = n64 > o3tl::make_unsigned(SAL_MAX_INT32) ? -SAL_MAX_INT32
771 : -static_cast<sal_Int32>(n64);
772 suTokenStr.getToken(0, ';', n);
773 // should not GPF with negative index
776 CPPUNIT_TEST_SUITE(getToken);
777 CPPUNIT_TEST(getToken_000);
778 CPPUNIT_TEST(getToken_001);
779 CPPUNIT_TEST(getToken_002);
780 CPPUNIT_TEST(getToken_003);
781 CPPUNIT_TEST(getToken_004);
782 CPPUNIT_TEST(getToken_005);
783 CPPUNIT_TEST(getToken_006);
784 CPPUNIT_TEST_SUITE_END();
785 }; // class getToken
787 class convertToString: public CppUnit::TestFixture {
788 public:
789 void test();
791 CPPUNIT_TEST_SUITE(convertToString);
792 CPPUNIT_TEST(test);
793 CPPUNIT_TEST_SUITE_END();
796 void convertToString::test() {
797 static constexpr OUStringLiteral utf16 = u"A\u00E4a";
798 OString s;
799 CPPUNIT_ASSERT(
800 OUString(utf16).convertToString(
801 &s, RTL_TEXTENCODING_UTF7,
802 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
803 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)));
804 CPPUNIT_ASSERT_EQUAL(
805 OString(RTL_CONSTASCII_STRINGPARAM("A+AOQ-a")), s);
808 // - string construction & interning (tests)
810 class construction : public CppUnit::TestFixture
812 public:
813 void construct()
815 #ifdef RTL_INLINE_STRINGS
816 OUString aFoo( "foo" );
817 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[0] == 'f');
818 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[1] == 'o');
819 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[2] == 'o');
820 CPPUNIT_ASSERT_MESSAGE("string length", aFoo.getLength() == 3);
822 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." );
823 CPPUNIT_ASSERT_MESSAGE("string length", aBaa.getLength() == 104);
824 // Dig at the internals ... FIXME: should we have the bit-flag defines public ?
825 CPPUNIT_ASSERT_MESSAGE("string static flags", (aBaa.pData->refCount & 1<<30) != 0);
826 #endif
829 void intern()
831 // The empty string is 'static' a special case ...
832 OUString().intern();
833 OUString::intern( "",strlen(""),RTL_TEXTENCODING_ASCII_US );
835 OUString aFoo( u"foo"_ustr );
836 OUString aFooIntern = aFoo.intern();
837 CPPUNIT_ASSERT_EQUAL_MESSAGE( "string contents", u"foo"_ustr, aFooIntern);
838 CPPUNIT_ASSERT_EQUAL_MESSAGE("string length", static_cast<sal_Int32>(3), aFooIntern.getLength());
839 // We have to dup due to no atomic 'intern' bit-set operation
840 CPPUNIT_ASSERT_MESSAGE("intern dups", aFoo.pData != aFooIntern.pData);
842 // Test interning lots of things
843 int i;
844 static const int nSequence = 4096;
845 std::unique_ptr<OUString[]> pStrs(new OUString[nSequence]);
846 for (i = 0; i < nSequence; i++)
848 pStrs[i] = OUString::number( sqrt( static_cast<double>(i) ) ).intern();
850 for (i = 0; i < nSequence; i++)
852 OUString aNew = OUString::number( sqrt( static_cast<double>(i) ) ).intern();
853 CPPUNIT_ASSERT_EQUAL_MESSAGE("double intern failed",
854 pStrs[i].pData, aNew.pData);
858 CPPUNIT_TEST_SUITE(construction);
859 CPPUNIT_TEST(construct);
860 CPPUNIT_TEST(intern);
861 CPPUNIT_TEST_SUITE_END();
864 class indexOfAscii: public CppUnit::TestFixture {
865 public:
866 void test();
868 CPPUNIT_TEST_SUITE(indexOfAscii);
869 CPPUNIT_TEST(test);
870 CPPUNIT_TEST_SUITE_END();
873 void indexOfAscii::test() {
874 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().indexOf(""));
875 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), OUString().lastIndexOf(""));
876 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u"foo"_ustr.indexOf("foo"));
877 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u"foo"_ustr.lastIndexOf("foo"));
878 CPPUNIT_ASSERT_EQUAL(
879 sal_Int32(2), u"fofoobar"_ustr.indexOf("foo"));
880 CPPUNIT_ASSERT_EQUAL(
881 sal_Int32(3), u"foofoofob"_ustr.lastIndexOf("foo"));
882 CPPUNIT_ASSERT_EQUAL(
883 sal_Int32(3), u"foofoobar"_ustr.indexOf("foo", 1));
886 class endsWith: public CppUnit::TestFixture {
887 public:
888 void test();
890 CPPUNIT_TEST_SUITE(endsWith);
891 CPPUNIT_TEST(test);
892 CPPUNIT_TEST_SUITE_END();
895 void endsWith::test() {
896 CPPUNIT_ASSERT_EQUAL(true, OUString().endsWith(""));
897 CPPUNIT_ASSERT_EQUAL(false, OUString().endsWith("foo"));
898 CPPUNIT_ASSERT_EQUAL(true, u"bar"_ustr.endsWith("bar"));
899 CPPUNIT_ASSERT_EQUAL(true, u"foobar"_ustr.endsWith("bar"));
900 CPPUNIT_ASSERT_EQUAL(false, u"FOOBAR"_ustr.endsWith("bar"));
903 class isEmpty: public CppUnit::TestFixture {
904 public:
905 void test();
907 CPPUNIT_TEST_SUITE(isEmpty);
908 CPPUNIT_TEST(test);
909 CPPUNIT_TEST_SUITE_END();
912 void isEmpty::test() {
913 OUString aString;
914 CPPUNIT_ASSERT_EQUAL_MESSAGE( "Newly constructed string should be empty", true, aString.isEmpty() );
916 aString = "Not empty any more";
917 CPPUNIT_ASSERT_EQUAL_MESSAGE( "String should not be empty", false, aString.isEmpty() );
919 aString.clear();
920 CPPUNIT_ASSERT_EQUAL_MESSAGE( "aString.clear(), so should now be empty", true, aString.isEmpty() );
924 class createFromCodePoints: public CppUnit::TestFixture {
925 public:
926 void test();
928 CPPUNIT_TEST_SUITE(createFromCodePoints);
929 CPPUNIT_TEST(test);
930 CPPUNIT_TEST_SUITE_END();
933 void createFromCodePoints::test() {
934 CPPUNIT_ASSERT_EQUAL(
935 sal_Int32(0),
936 OUString(static_cast< sal_uInt32 const * >(nullptr), 0).getLength());
937 sal_uInt32 cp[] = { 0, 0xD800, 0xFFFF, 0x10000, 0x10FFFF };
938 // non-const, to avoid loplugin:stringliteralvar
939 OUString s(cp, SAL_N_ELEMENTS(cp));
940 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength());
941 CPPUNIT_ASSERT_EQUAL(u'\0', s[0]);
942 CPPUNIT_ASSERT_EQUAL(u'\xD800', s[1]);
943 CPPUNIT_ASSERT_EQUAL(u'\xFFFF', s[2]);
944 CPPUNIT_ASSERT_EQUAL(u'\xD800', s[3]);
945 CPPUNIT_ASSERT_EQUAL(u'\xDC00', s[4]);
946 CPPUNIT_ASSERT_EQUAL(u'\xDBFF', s[5]);
947 CPPUNIT_ASSERT_EQUAL(u'\xDFFF', s[6]);
950 class iterateCodePoints: public CppUnit::TestFixture {
951 public:
952 void testNotWellFormed();
954 CPPUNIT_TEST_SUITE(iterateCodePoints);
955 CPPUNIT_TEST(testNotWellFormed);
956 CPPUNIT_TEST_SUITE_END();
959 void iterateCodePoints::testNotWellFormed() {
960 static constexpr OUStringLiteral utf16 =
961 u"\U00010000A\U0010FFFF\xDDEF\xD9AB";
962 OUString s(utf16);
963 sal_Int32 i = 0;
964 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i));
965 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
966 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i));
967 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
968 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i));
969 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
970 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i));
971 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
972 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i));
973 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), i);
974 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i, -1));
975 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
976 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i, -1));
977 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
978 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i, -1));
979 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
980 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i, -1));
981 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
982 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -1));
983 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
984 i = 1;
985 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), s.iterateCodePoints(&i, 2));
986 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
987 i = 4;
988 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -3));
989 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
992 class convertFromString: public CppUnit::TestFixture {
993 public:
994 void test();
996 CPPUNIT_TEST_SUITE(convertFromString);
997 CPPUNIT_TEST(test);
998 CPPUNIT_TEST_SUITE_END();
1001 void convertFromString::test() {
1002 OUString t;
1003 CPPUNIT_ASSERT(
1004 !rtl_convertStringToUString(
1005 &t.pData, RTL_CONSTASCII_STRINGPARAM("\x80"), RTL_TEXTENCODING_UTF8,
1006 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1007 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1008 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1009 CPPUNIT_ASSERT(
1010 !rtl_convertStringToUString(
1011 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xC0"), RTL_TEXTENCODING_UTF8,
1012 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1013 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1014 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1015 CPPUNIT_ASSERT(
1016 !rtl_convertStringToUString(
1017 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xFF"), RTL_TEXTENCODING_UTF8,
1018 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1019 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1020 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1021 CPPUNIT_ASSERT(
1022 rtl_convertStringToUString(
1023 &t.pData, RTL_CONSTASCII_STRINGPARAM("abc"), RTL_TEXTENCODING_UTF8,
1024 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1025 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1026 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1027 CPPUNIT_ASSERT_EQUAL( u"abc"_ustr, t );
1030 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::number);
1031 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toInt);
1032 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toDouble);
1033 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toFloat);
1034 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::lastIndexOf);
1035 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::getToken);
1036 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertToString);
1037 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::construction);
1038 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::indexOfAscii);
1039 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::endsWith);
1040 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::isEmpty);
1041 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::createFromCodePoints);
1042 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::iterateCodePoints);
1043 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertFromString);
1045 } // namespace rtl_OUString
1047 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */