bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / oustring / rtl_OUString2.cxx
blobeff33e9bc3ec47a105e57a55e1cc38bf599839de
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 <math.h>
23 #include <stdio.h>
25 #include <algorithm>
26 #include <limits>
28 #include <cppunit/TestFixture.h>
29 #include <cppunit/extensions/HelperMacros.h>
30 #include <cppunit/plugin/TestPlugIn.h>
32 #include "stringhelper.hxx"
33 #include "valueequal.hxx"
35 namespace rtl_OUString
38 namespace {
40 // Avoid -fsanitize=undefined warning e.g. "runtime error: value 1e+99 is
41 // outside the range of representable values of type 'float'":
42 float doubleToFloat(double x) {
43 return
44 x < -std::numeric_limits<float>::max()
45 ? -std::numeric_limits<float>::infinity()
46 : x > std::numeric_limits<float>::max()
47 ? std::numeric_limits<float>::infinity()
48 : static_cast<float>(x);
53 class number : public CppUnit::TestFixture
55 void number_float_test_impl(float _nValue)
57 rtl::OUString suValue(rtl::OUString::number(_nValue));
58 rtl::OString sValue;
59 sValue <<= suValue;
60 printf("nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr());
62 double nValueATOF = doubleToFloat(atof( sValue.getStr() ));
64 bool bEqualResult = is_float_equal(_nValue, nValueATOF);
65 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
68 void number_float_test(float _nValue)
70 number_float_test_impl(_nValue);
72 // test also the negative part.
73 float nNegativeValue = -_nValue;
74 number_float_test_impl(nNegativeValue);
77 public:
78 // insert your test code here.
79 void number_float_test_001()
81 // this is demonstration code
82 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
83 float nValue = 3.0f;
84 number_float_test(nValue);
87 void number_float_test_002()
89 float nValue = 3.5f;
90 number_float_test(nValue);
93 void number_float_test_003()
95 float nValue = 3.0625f;
96 number_float_test(nValue);
99 void number_float_test_004()
101 float nValue = 3.502525f;
102 number_float_test(nValue);
105 void number_float_test_005()
107 float nValue = 3.141592f;
108 number_float_test(nValue);
111 void number_float_test_006()
113 float nValue = 3.5025255f;
114 number_float_test(nValue);
117 void number_float_test_007()
119 float nValue = 3.0039062f;
120 number_float_test(nValue);
123 private:
125 void number_double_test_impl(double _nValue)
127 rtl::OUString suValue;
128 suValue = rtl::OUString::number( _nValue );
129 rtl::OString sValue;
130 sValue <<= suValue;
131 printf("nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr());
133 double nValueATOF = atof( sValue.getStr() );
135 bool bEqualResult = is_double_equal(_nValue, nValueATOF);
136 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
139 void number_double_test(double _nValue)
141 number_double_test_impl(_nValue);
143 // test also the negative part.
144 double nNegativeValue = -_nValue;
145 number_double_test_impl(nNegativeValue);
147 public:
149 // number double
150 void number_double_test_001()
152 double nValue = 3.0;
153 number_double_test(nValue);
155 void number_double_test_002()
157 double nValue = 3.5;
158 number_double_test(nValue);
160 void number_double_test_003()
162 double nValue = 3.0625;
163 number_double_test(nValue);
165 void number_double_test_004()
167 double nValue = 3.1415926535;
168 number_double_test(nValue);
170 void number_double_test_005()
172 double nValue = 3.141592653589793;
173 number_double_test(nValue);
175 void number_double_test_006()
177 double nValue = 3.1415926535897932;
178 number_double_test(nValue);
180 void number_double_test_007()
182 double nValue = 3.14159265358979323;
183 number_double_test(nValue);
185 void number_double_test_008()
187 double nValue = 3.141592653589793238462643;
188 number_double_test(nValue);
191 // Change the following lines only, if you add, remove or rename
192 // member functions of the current class,
193 // because these macros are need by auto register mechanism.
195 CPPUNIT_TEST_SUITE(number);
196 CPPUNIT_TEST(number_float_test_001);
197 CPPUNIT_TEST(number_float_test_002);
198 CPPUNIT_TEST(number_float_test_003);
199 CPPUNIT_TEST(number_float_test_004);
200 CPPUNIT_TEST(number_float_test_005);
201 CPPUNIT_TEST(number_float_test_006);
202 CPPUNIT_TEST(number_float_test_007);
204 CPPUNIT_TEST(number_double_test_001);
205 CPPUNIT_TEST(number_double_test_002);
206 CPPUNIT_TEST(number_double_test_003);
207 CPPUNIT_TEST(number_double_test_004);
208 CPPUNIT_TEST(number_double_test_005);
209 CPPUNIT_TEST(number_double_test_006);
210 CPPUNIT_TEST(number_double_test_007);
211 CPPUNIT_TEST(number_double_test_008);
212 CPPUNIT_TEST_SUITE_END();
213 }; // class number
215 class toInt: public CppUnit::TestFixture {
216 public:
217 void test() {
218 CPPUNIT_ASSERT_EQUAL(
219 static_cast< sal_Int32 >(-0x76543210),
220 (rtl::OUString("-76543210").
221 toInt32(16)));
222 // @return 0 if this string represents no number or one of too large magnitude
223 CPPUNIT_ASSERT_EQUAL(
224 static_cast< sal_Int32 >(0),
225 (rtl::OUString("+FEDCBA98").
226 toInt32(16)));
227 CPPUNIT_ASSERT_EQUAL(
228 static_cast< sal_Int64 >(-SAL_CONST_INT64(0x76543210FEDCBA98)),
229 (rtl::OUString(
230 "-76543210FEDCBA98").
231 toInt64(16)));
232 // @return 0 if this string represents no number or one of too large magnitude
233 CPPUNIT_ASSERT_EQUAL(
234 static_cast< sal_Int64 >(SAL_CONST_INT64(0)),
235 (rtl::OUString(
236 "+FEDCBA9876543210").
237 toInt64(16)));
240 CPPUNIT_TEST_SUITE(toInt);
241 CPPUNIT_TEST(test);
242 CPPUNIT_TEST_SUITE_END();
245 // - toDouble (tests)
246 class toDouble : public CppUnit::TestFixture
248 public:
249 void toDouble_test_impl(rtl::OString const& _sValue)
251 //printf("the original str is %s\n", _sValue.getStr());
252 double nValueATOF = atof( _sValue.getStr() );
253 //printf("original data is %e\n", nValueATOF);
254 rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
255 double nValueToDouble = suValue.toDouble();
256 //printf("result data is %e\n", nValueToDouble);
258 bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
259 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
262 void toDouble_test(rtl::OString const& _sValue)
264 toDouble_test_impl(_sValue);
266 // test also the negativ part.
267 rtl::OString sNegativValue("-");
268 sNegativValue += _sValue;
269 toDouble_test_impl(sNegativValue);
272 // insert your test code here.
273 void toDouble_selftest()
275 printf("Start selftest:\n");
276 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.01));
277 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.001));
278 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0001));
279 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00001));
280 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000001));
281 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000001));
282 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000001));
283 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000001));
284 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000001));
285 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.00000000001));
286 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.000000000001));
287 CPPUNIT_ASSERT (!is_double_equal(1.0, 1.0000000000001));
288 // we check til 15 values after comma
289 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001));
290 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001));
291 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001));
292 printf("Selftest done.\n");
295 void toDouble_test_3()
297 rtl::OString sValue("3");
298 toDouble_test(sValue);
300 void toDouble_test_3_5()
302 rtl::OString sValue("3.5");
303 toDouble_test(sValue);
305 void toDouble_test_3_0625()
307 rtl::OString sValue("3.0625");
308 toDouble_test(sValue);
310 void toDouble_test_pi()
312 // value from http://www.angio.net/pi/digits/50.txt
313 rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
314 toDouble_test(sValue);
317 void toDouble_test_1()
319 rtl::OString sValue("1");
320 toDouble_test(sValue);
322 void toDouble_test_10()
324 rtl::OString sValue("10");
325 toDouble_test(sValue);
327 void toDouble_test_100()
329 rtl::OString sValue("100");
330 toDouble_test(sValue);
332 void toDouble_test_1000()
334 rtl::OString sValue("1000");
335 toDouble_test(sValue);
337 void toDouble_test_10000()
339 rtl::OString sValue("10000");
340 toDouble_test(sValue);
342 void toDouble_test_1e99()
344 rtl::OString sValue("1e99");
345 toDouble_test(sValue);
347 void toDouble_test_1e_n99()
349 rtl::OString sValue("1e-99");
350 toDouble_test(sValue);
352 void toDouble_test_1e308()
354 rtl::OString sValue("1e308");
355 toDouble_test(sValue);
358 // Change the following lines only, if you add, remove or rename
359 // member functions of the current class,
360 // because these macros are need by auto register mechanism.
362 CPPUNIT_TEST_SUITE(toDouble);
363 CPPUNIT_TEST(toDouble_selftest);
365 CPPUNIT_TEST(toDouble_test_3);
366 CPPUNIT_TEST(toDouble_test_3_5);
367 CPPUNIT_TEST(toDouble_test_3_0625);
368 CPPUNIT_TEST(toDouble_test_pi);
369 CPPUNIT_TEST(toDouble_test_1);
370 CPPUNIT_TEST(toDouble_test_10);
371 CPPUNIT_TEST(toDouble_test_100);
372 CPPUNIT_TEST(toDouble_test_1000);
373 CPPUNIT_TEST(toDouble_test_10000);
374 CPPUNIT_TEST(toDouble_test_1e99);
375 CPPUNIT_TEST(toDouble_test_1e_n99);
376 CPPUNIT_TEST(toDouble_test_1e308);
377 CPPUNIT_TEST_SUITE_END();
378 }; // class toDouble
380 // - toFloat (tests)
381 class toFloat : public CppUnit::TestFixture
383 public:
384 void toFloat_test_impl(rtl::OString const& _sValue)
386 //printf("the original str is %s\n", _sValue.getStr());
387 float nValueATOF = doubleToFloat(atof( _sValue.getStr() ));
388 //printf("the original str is %.10f\n", nValueATOF);
389 rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
390 float nValueToFloat = suValue.toFloat();
391 //printf("the result str is %.10f\n", nValueToFloat);
393 bool bEqualResult = is_float_equal(nValueToFloat, nValueATOF);
394 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult);
397 void toFloat_test(rtl::OString const& _sValue)
399 toFloat_test_impl(_sValue);
401 // test also the negativ part.
402 rtl::OString sNegativValue("-");
403 sNegativValue += _sValue;
404 toFloat_test_impl(sNegativValue);
407 // insert your test code here.
408 void toFloat_selftest()
410 printf("Start selftest:\n");
411 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.01f));
412 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.001f));
413 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.0001f));
414 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.00001f));
415 CPPUNIT_ASSERT (!is_float_equal(1.0f, 1.000002f));
416 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.0000001f));
417 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.00000001f));
418 CPPUNIT_ASSERT (is_float_equal(1.0f, 1.000000001f));
420 printf("Selftest done.\n");
423 void toFloat_test_3()
425 rtl::OString sValue("3");
426 toFloat_test(sValue);
428 void toFloat_test_3_5()
430 rtl::OString sValue("3.5");
431 toFloat_test(sValue);
433 void toFloat_test_3_0625()
435 rtl::OString sValue("3.0625");
436 toFloat_test(sValue);
438 void toFloat_test_3_0625_e()
440 rtl::OString sValue("3.0625e-4");
441 toFloat_test(sValue);
443 void toFloat_test_pi()
445 // value from http://www.angio.net/pi/digits/50.txt
446 rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
447 toFloat_test(sValue);
450 void toFloat_test_1()
452 rtl::OString sValue("1");
453 toFloat_test(sValue);
455 void toFloat_test_10()
457 rtl::OString sValue("10");
458 toFloat_test(sValue);
460 void toFloat_test_100()
462 rtl::OString sValue("100");
463 toFloat_test(sValue);
465 void toFloat_test_1000()
467 rtl::OString sValue("1000");
468 toFloat_test(sValue);
470 void toFloat_test_10000()
472 rtl::OString sValue("10000");
473 toFloat_test(sValue);
475 void toFloat_test_mix()
477 rtl::OString sValue("456789321455.123456789012");
478 toFloat_test(sValue);
480 void toFloat_test_1e99()
482 rtl::OString sValue("1e99");
483 toFloat_test(sValue);
485 void toFloat_test_1e_n99()
487 rtl::OString sValue("1e-9");
488 toFloat_test(sValue);
490 void toFloat_test_1e308()
492 rtl::OString sValue("1e308");
493 toFloat_test(sValue);
496 // Change the following lines only, if you add, remove or rename
497 // member functions of the current class,
498 // because these macros are need by auto register mechanism.
500 CPPUNIT_TEST_SUITE(toFloat);
501 CPPUNIT_TEST(toFloat_selftest);
503 CPPUNIT_TEST(toFloat_test_3);
504 CPPUNIT_TEST(toFloat_test_3_5);
505 CPPUNIT_TEST(toFloat_test_3_0625);
506 CPPUNIT_TEST(toFloat_test_3_0625_e);
507 CPPUNIT_TEST(toFloat_test_pi);
508 CPPUNIT_TEST(toFloat_test_1);
509 CPPUNIT_TEST(toFloat_test_10);
510 CPPUNIT_TEST(toFloat_test_100);
511 CPPUNIT_TEST(toFloat_test_1000);
512 CPPUNIT_TEST(toFloat_test_10000);
513 CPPUNIT_TEST(toFloat_test_mix);
514 CPPUNIT_TEST(toFloat_test_1e99);
515 CPPUNIT_TEST(toFloat_test_1e_n99);
516 CPPUNIT_TEST(toFloat_test_1e308);
517 CPPUNIT_TEST_SUITE_END();
518 }; // class toFloat
520 // - lastIndexOf (tests)
521 class lastIndexOf : public CppUnit::TestFixture
524 public:
525 void lastIndexOf_oustring(rtl::OUString const& _suStr, rtl::OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos)
527 // Algorithm
528 // search the string _suSearchStr (rtl::OUString) in the string _suStr.
529 // check if the _nExpectedResultPos occurs.
531 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr);
532 CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos);
535 void lastIndexOf_salunicode(rtl::OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos)
537 // Algorithm
538 // search the unicode char _suSearchChar (sal_Unicode) in the string _suStr.
539 // check if the _nExpectedResultPos occurs.
541 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar);
542 CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos);
545 void lastIndexOf_oustring_offset(rtl::OUString const& _suStr, rtl::OUString const& _suSearchStr, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
547 sal_Int32 nPos = _suStr.lastIndexOf(_suSearchStr, _nStartOffset);
548 CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos);
551 void lastIndexOf_salunicode_offset(rtl::OUString const& _suStr, sal_Unicode _cuSearchChar, sal_Int32 _nExpectedResultPos, sal_Int32 _nStartOffset)
553 sal_Int32 nPos = _suStr.lastIndexOf(_cuSearchChar, _nStartOffset);
554 CPPUNIT_ASSERT_MESSAGE("expected position is wrong", nPos == _nExpectedResultPos);
557 void lastIndexOf_test_oustring_offset_001()
559 // search for sun, start at the end, found (pos==0)
560 rtl::OUString aStr("sun java system");
561 rtl::OUString aSearchStr("sun");
562 lastIndexOf_oustring_offset(aStr, aSearchStr, 0, aStr.getLength());
565 void lastIndexOf_test_oustring_offset_002()
567 // search for sun, start at pos = 3, found (pos==0)
568 rtl::OUString aStr("sun java system");
569 rtl::OUString aSearchStr("sun");
570 lastIndexOf_oustring_offset(aStr, aSearchStr, 0, 3);
573 void lastIndexOf_test_oustring_offset_003()
575 // search for sun, start at pos = 2, found (pos==-1)
576 rtl::OUString aStr("sun java system");
577 rtl::OUString aSearchStr("sun");
578 lastIndexOf_oustring_offset(aStr, aSearchStr, -1, 2);
581 void lastIndexOf_test_oustring_offset_004()
583 // search for sun, start at the end, found (pos==0)
584 rtl::OUString aStr("sun java system");
585 rtl::OUString aSearchStr("sun");
586 lastIndexOf_oustring_offset(aStr, aSearchStr, -1, -1);
589 void lastIndexOf_test_oustring_001()
591 // search for sun, found (pos==0)
592 rtl::OUString aStr("sun java system");
593 rtl::OUString aSearchStr("sun");
594 lastIndexOf_oustring(aStr, aSearchStr, 0);
597 void lastIndexOf_test_oustring_002()
599 // search for sun, found (pos==4)
600 rtl::OUString aStr("the sun java system");
601 rtl::OUString aSearchStr("sun");
602 lastIndexOf_oustring(aStr, aSearchStr, 4);
605 void lastIndexOf_test_oustring_003()
607 // search for sun, found (pos==8)
608 rtl::OUString aStr("the sun sun java system");
609 rtl::OUString aSearchStr("sun");
610 lastIndexOf_oustring(aStr, aSearchStr, 8);
613 void lastIndexOf_test_oustring_004()
615 // search for sun, found (pos==8)
616 rtl::OUString aStr("the sun sun");
617 rtl::OUString aSearchStr("sun");
618 lastIndexOf_oustring(aStr, aSearchStr, 8);
621 void lastIndexOf_test_oustring_005()
623 // search for sun, found (pos==4)
624 rtl::OUString aStr("the sun su");
625 rtl::OUString aSearchStr("sun");
626 lastIndexOf_oustring(aStr, aSearchStr, 4);
629 void lastIndexOf_test_oustring_006()
631 // search for sun, found (pos==-1)
632 rtl::OUString aStr("the su su");
633 rtl::OUString aSearchStr("sun");
634 lastIndexOf_oustring(aStr, aSearchStr, -1);
637 void lastIndexOf_test_oustring_007()
639 // search for earth, not found (-1)
640 rtl::OUString aStr("the su su");
641 rtl::OUString aSearchStr("earth");
642 lastIndexOf_oustring(aStr, aSearchStr, -1);
645 void lastIndexOf_test_oustring_008()
647 // search for earth, not found (-1)
648 rtl::OUString aStr = rtl::OUString();
649 rtl::OUString aSearchStr("earth");
650 lastIndexOf_oustring(aStr, aSearchStr, -1);
653 void lastIndexOf_test_oustring_009()
655 // search for earth, not found (-1)
656 rtl::OUString aStr = rtl::OUString();
657 rtl::OUString aSearchStr = rtl::OUString();
658 lastIndexOf_oustring(aStr, aSearchStr, -1);
662 void lastIndexOf_test_salunicode_001()
664 // search for 's', found (19)
665 rtl::OUString aStr("the sun sun java system");
666 sal_Unicode suChar = L's';
667 lastIndexOf_salunicode(aStr, suChar, 19);
670 void lastIndexOf_test_salunicode_002()
672 // search for 'x', not found (-1)
673 rtl::OUString aStr("the sun sun java system");
674 sal_Unicode suChar = L'x';
675 lastIndexOf_salunicode(aStr, suChar, -1);
678 void lastIndexOf_test_salunicode_offset_001()
680 // search for 's', start from pos last char, found (19)
681 rtl::OUString aStr("the sun sun java system");
682 sal_Unicode cuChar = L's';
683 lastIndexOf_salunicode_offset(aStr, cuChar, 19, aStr.getLength());
685 void lastIndexOf_test_salunicode_offset_002()
687 // search for 's', start pos is last occur from search behind, found (17)
688 rtl::OUString aStr("the sun sun java system");
689 sal_Unicode cuChar = L's';
690 lastIndexOf_salunicode_offset(aStr, cuChar, 17, 19);
692 void lastIndexOf_test_salunicode_offset_003()
694 // search for 't', start pos is 1, found (0)
695 rtl::OUString aStr("the sun sun java system");
696 sal_Unicode cuChar = L't';
697 lastIndexOf_salunicode_offset(aStr, cuChar, 0, 1);
700 // Change the following lines only, if you add, remove or rename
701 // member functions of the current class,
702 // because these macros are need by auto register mechanism.
704 CPPUNIT_TEST_SUITE(lastIndexOf);
705 CPPUNIT_TEST(lastIndexOf_test_oustring_001);
706 CPPUNIT_TEST(lastIndexOf_test_oustring_002);
707 CPPUNIT_TEST(lastIndexOf_test_oustring_003);
708 CPPUNIT_TEST(lastIndexOf_test_oustring_004);
709 CPPUNIT_TEST(lastIndexOf_test_oustring_005);
710 CPPUNIT_TEST(lastIndexOf_test_oustring_006);
711 CPPUNIT_TEST(lastIndexOf_test_oustring_007);
712 CPPUNIT_TEST(lastIndexOf_test_oustring_008);
713 CPPUNIT_TEST(lastIndexOf_test_oustring_009);
715 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_001);
716 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_002);
717 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_003);
718 CPPUNIT_TEST(lastIndexOf_test_oustring_offset_004);
720 CPPUNIT_TEST(lastIndexOf_test_salunicode_001);
721 CPPUNIT_TEST(lastIndexOf_test_salunicode_002);
723 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_001);
724 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_002);
725 CPPUNIT_TEST(lastIndexOf_test_salunicode_offset_003);
727 CPPUNIT_TEST_SUITE_END();
728 }; // class lastIndexOf
730 // - getToken (tests)
731 class getToken : public CppUnit::TestFixture
734 public:
735 void getToken_000()
737 rtl::OUString suTokenStr;
739 sal_Int32 nIndex = 0;
742 suTokenStr.getToken( 0, ';', nIndex );
744 while ( nIndex >= 0 );
745 printf("Index %" SAL_PRIdINT32 "\n", nIndex);
746 // should not GPF
749 void getToken_001()
751 rtl::OUString suTokenStr("a;b");
753 sal_Int32 nIndex = 0;
755 rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
756 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'a'", suToken == "a" );
758 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
759 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'b'", suToken == "b" );
760 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
763 void getToken_002()
765 rtl::OUString suTokenStr("a;b.c");
767 sal_Int32 nIndex = 0;
769 rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
770 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'a'", suToken == "a" );
772 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
773 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'b'", suToken == "b" );
775 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
776 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'c'", suToken == "c" );
777 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
780 void getToken_003()
782 rtl::OUString suTokenStr("a;;b");
784 sal_Int32 nIndex = 0;
786 rtl::OUString suToken = suTokenStr.getToken( 0, ';', nIndex );
787 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'a'", suToken == "a" );
789 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
790 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
792 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, ';', nIndex );
793 CPPUNIT_ASSERT_MESSAGE( "Token should be a 'b'", suToken == "b" );
794 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
797 void getToken_004()
799 rtl::OUString suTokenStr("longer.then.ever.");
801 sal_Int32 nIndex = 0;
803 rtl::OUString suToken = suTokenStr.getToken( 0, '.', nIndex );
804 CPPUNIT_ASSERT_MESSAGE( "Token should be 'longer'", suToken == "longer" );
806 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
807 CPPUNIT_ASSERT_MESSAGE( "Token should be 'then'", suToken == "then" );
809 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
810 CPPUNIT_ASSERT_MESSAGE( "Token should be 'ever'", suToken == "ever" );
812 /* rtl::OUString */ suToken = suTokenStr.getToken( 0, '.', nIndex );
813 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.isEmpty());
815 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
818 void getToken_005() {
819 rtl::OUString ab("ab");
820 sal_Int32 n = 0;
821 CPPUNIT_ASSERT_MESSAGE(
822 "token should be 'ab'", ab.getToken(0, '-', n) == ab);
823 CPPUNIT_ASSERT_MESSAGE("n should be -1", n == -1);
824 CPPUNIT_ASSERT_MESSAGE(
825 "token should be empty", ab.getToken(0, '-', n).isEmpty());
828 CPPUNIT_TEST_SUITE(getToken);
829 CPPUNIT_TEST(getToken_000);
830 CPPUNIT_TEST(getToken_001);
831 CPPUNIT_TEST(getToken_002);
832 CPPUNIT_TEST(getToken_003);
833 CPPUNIT_TEST(getToken_004);
834 CPPUNIT_TEST(getToken_005);
835 CPPUNIT_TEST_SUITE_END();
836 }; // class getToken
838 class convertToString: public CppUnit::TestFixture {
839 public:
840 void test();
842 CPPUNIT_TEST_SUITE(convertToString);
843 CPPUNIT_TEST(test);
844 CPPUNIT_TEST_SUITE_END();
847 void convertToString::test() {
848 static sal_Unicode const utf16[] = { 0x0041, 0x00E4, 0x0061 };
849 rtl::OString s;
850 CPPUNIT_ASSERT(
851 rtl::OUString(utf16, SAL_N_ELEMENTS(utf16)).convertToString(
852 &s, RTL_TEXTENCODING_UTF7,
853 (RTL_UNICODETOTEXT_FLAGS_UNDEFINED_ERROR |
854 RTL_UNICODETOTEXT_FLAGS_INVALID_ERROR)));
855 CPPUNIT_ASSERT_EQUAL(
856 rtl::OString(RTL_CONSTASCII_STRINGPARAM("A+AOQ-a")), s);
859 // - string construction & interning (tests)
861 class construction : public CppUnit::TestFixture
863 public:
864 void construct()
866 #ifdef RTL_INLINE_STRINGS
867 ::rtl::OUString aFoo( "foo" );
868 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[0] == 'f');
869 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[1] == 'o');
870 CPPUNIT_ASSERT_MESSAGE("string contents", aFoo[2] == 'o');
871 CPPUNIT_ASSERT_MESSAGE("string length", aFoo.getLength() == 3);
873 ::rtl::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." );
874 CPPUNIT_ASSERT_MESSAGE("string length", aBaa.getLength() == 104);
875 // Dig at the internals ... FIXME: should we have the bit-flag defines public ?
876 CPPUNIT_ASSERT_MESSAGE("string static flags", (aBaa.pData->refCount & 1<<30) != 0);
877 #endif
880 void intern()
882 // The empty string is 'static' a special case ...
883 rtl::OUString().intern();
884 rtl::OUString::intern( "",strlen(""),RTL_TEXTENCODING_ASCII_US );
886 ::rtl::OUString aFoo( "foo" );
887 ::rtl::OUString aFooIntern = aFoo.intern();
888 CPPUNIT_ASSERT_MESSAGE( "string contents", aFooIntern == "foo" );
889 CPPUNIT_ASSERT_MESSAGE("string length", aFooIntern.getLength() == 3);
890 // We have to dup due to no atomic 'intern' bit-set operation
891 CPPUNIT_ASSERT_MESSAGE("intern dups", aFoo.pData != aFooIntern.pData);
893 // Test interning lots of things
894 int i;
895 static const int nSequence = 4096;
896 rtl::OUString *pStrs;
897 sal_uIntPtr *pValues;
899 pStrs = new rtl::OUString[nSequence];
900 pValues = new sal_uIntPtr[nSequence];
901 for (i = 0; i < nSequence; i++)
903 pStrs[i] = rtl::OUString::number( sqrt( static_cast<double>(i) ) ).intern();
904 pValues[i] = reinterpret_cast<sal_uIntPtr>( pStrs[i].pData );
906 for (i = 0; i < nSequence; i++)
908 rtl::OUString aNew = rtl::OUString::number( sqrt( static_cast<double>(i) ) ).intern();
909 CPPUNIT_ASSERT_MESSAGE("double intern failed",
910 aNew.pData == pStrs[i].pData);
913 // Free strings to check for leaks
914 for (i = 0; i < nSequence; i++)
916 // Overwrite - hopefully this re-uses the memory
917 pStrs[i] = rtl::OUString();
918 pStrs[i] = rtl::OUString::number( sqrt( static_cast<double>(i) ) );
921 for (i = 0; i < nSequence; i++)
923 rtl::OUString aIntern;
924 sal_uIntPtr nValue;
925 aIntern = rtl::OUString::number( sqrt( static_cast<double>(i) ) ).intern();
927 nValue = reinterpret_cast<sal_uIntPtr>( aIntern.pData );
928 // This may not be 100% reliable: memory may
929 // have been re-used, but it's worth checking.
930 CPPUNIT_ASSERT_MESSAGE("intern leaking", nValue != pValues[i]);
932 delete [] pValues;
933 delete [] pStrs;
936 CPPUNIT_TEST_SUITE(construction);
937 CPPUNIT_TEST(construct);
938 CPPUNIT_TEST(intern);
939 CPPUNIT_TEST_SUITE_END();
942 class indexOfAscii: public CppUnit::TestFixture {
943 public:
944 void test();
946 CPPUNIT_TEST_SUITE(indexOfAscii);
947 CPPUNIT_TEST(test);
948 CPPUNIT_TEST_SUITE_END();
951 void indexOfAscii::test() {
952 CPPUNIT_ASSERT_EQUAL(
953 sal_Int32(-1),
954 rtl::OUString().indexOfAsciiL(RTL_CONSTASCII_STRINGPARAM("")));
955 CPPUNIT_ASSERT_EQUAL(
956 sal_Int32(-1),
957 rtl::OUString().lastIndexOfAsciiL(RTL_CONSTASCII_STRINGPARAM("")));
958 CPPUNIT_ASSERT_EQUAL(
959 sal_Int32(0),
960 rtl::OUString("foo").indexOfAsciiL(
961 RTL_CONSTASCII_STRINGPARAM("foo")));
962 CPPUNIT_ASSERT_EQUAL(
963 sal_Int32(0),
964 rtl::OUString("foo").lastIndexOfAsciiL(
965 RTL_CONSTASCII_STRINGPARAM("foo")));
966 CPPUNIT_ASSERT_EQUAL(
967 sal_Int32(2),
968 rtl::OUString("fofoobar").indexOfAsciiL(
969 RTL_CONSTASCII_STRINGPARAM("foo")));
970 CPPUNIT_ASSERT_EQUAL(
971 sal_Int32(3),
972 rtl::OUString("foofoofob").
973 lastIndexOfAsciiL(RTL_CONSTASCII_STRINGPARAM("foo")));
974 CPPUNIT_ASSERT_EQUAL(
975 sal_Int32(3),
976 rtl::OUString("foofoobar").indexOfAsciiL(
977 RTL_CONSTASCII_STRINGPARAM("foo"), 1));
980 class endsWith: public CppUnit::TestFixture {
981 public:
982 void test();
984 CPPUNIT_TEST_SUITE(endsWith);
985 CPPUNIT_TEST(test);
986 CPPUNIT_TEST_SUITE_END();
989 void endsWith::test() {
990 CPPUNIT_ASSERT_EQUAL(
991 true,
992 rtl::OUString().endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM("")));
993 CPPUNIT_ASSERT_EQUAL(
994 false,
995 rtl::OUString().endsWithAsciiL(RTL_CONSTASCII_STRINGPARAM("foo")));
996 CPPUNIT_ASSERT_EQUAL(
997 true,
998 rtl::OUString("bar").endsWithAsciiL(
999 RTL_CONSTASCII_STRINGPARAM("bar")));
1000 CPPUNIT_ASSERT_EQUAL(
1001 true,
1002 rtl::OUString("foobar").endsWithAsciiL(
1003 RTL_CONSTASCII_STRINGPARAM("bar")));
1004 CPPUNIT_ASSERT_EQUAL(
1005 false,
1006 rtl::OUString("FOOBAR").endsWithAsciiL(
1007 RTL_CONSTASCII_STRINGPARAM("bar")));
1010 class createFromCodePoints: public CppUnit::TestFixture {
1011 public:
1012 void test();
1014 CPPUNIT_TEST_SUITE(createFromCodePoints);
1015 CPPUNIT_TEST(test);
1016 CPPUNIT_TEST_SUITE_END();
1019 void createFromCodePoints::test() {
1020 CPPUNIT_ASSERT_EQUAL(
1021 sal_Int32(0),
1022 rtl::OUString(static_cast< sal_uInt32 const * >(NULL), 0).getLength());
1023 static sal_uInt32 const cp[] = { 0, 0xD800, 0xFFFF, 0x10000, 0x10FFFF };
1024 rtl::OUString s(cp, sizeof cp / sizeof (sal_uInt32));
1025 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength());
1026 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0), s[0]);
1027 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xD800), s[1]);
1028 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xFFFF), s[2]);
1029 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xD800), s[3]);
1030 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDC00), s[4]);
1031 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDBFF), s[5]);
1032 CPPUNIT_ASSERT_EQUAL(sal_Unicode(0xDFFF), s[6]);
1035 class iterateCodePoints: public CppUnit::TestFixture {
1036 public:
1037 void testNotWellFormed();
1039 CPPUNIT_TEST_SUITE(iterateCodePoints);
1040 CPPUNIT_TEST(testNotWellFormed);
1041 CPPUNIT_TEST_SUITE_END();
1044 void iterateCodePoints::testNotWellFormed() {
1045 static sal_Unicode const utf16[] =
1046 { 0xD800, 0xDC00, 0x0041, 0xDBFF, 0xDFFF, 0xDDEF, 0xD9AB };
1047 rtl::OUString s(utf16, sizeof utf16 / sizeof (sal_Unicode));
1048 sal_Int32 i = 0;
1049 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i));
1050 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
1051 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i));
1052 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1053 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i));
1054 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
1055 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i));
1056 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
1057 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i));
1058 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), i);
1059 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xD9AB), s.iterateCodePoints(&i, -1));
1060 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), i);
1061 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDDEF), s.iterateCodePoints(&i, -1));
1062 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), i);
1063 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10FFFF), s.iterateCodePoints(&i, -1));
1064 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1065 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x0041), s.iterateCodePoints(&i, -1));
1066 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
1067 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -1));
1068 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
1069 i = 1;
1070 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), s.iterateCodePoints(&i, 2));
1071 CPPUNIT_ASSERT_EQUAL(sal_Int32(3), i);
1072 i = 4;
1073 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), s.iterateCodePoints(&i, -3));
1074 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i);
1077 class convertFromString: public CppUnit::TestFixture {
1078 public:
1079 void test();
1081 CPPUNIT_TEST_SUITE(convertFromString);
1082 CPPUNIT_TEST(test);
1083 CPPUNIT_TEST_SUITE_END();
1086 void convertFromString::test() {
1087 rtl::OUString t;
1088 CPPUNIT_ASSERT(
1089 !rtl_convertStringToUString(
1090 &t.pData, RTL_CONSTASCII_STRINGPARAM("\x80"), RTL_TEXTENCODING_UTF8,
1091 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1092 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1093 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1094 CPPUNIT_ASSERT(
1095 !rtl_convertStringToUString(
1096 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xC0"), RTL_TEXTENCODING_UTF8,
1097 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1098 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1099 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1100 CPPUNIT_ASSERT(
1101 !rtl_convertStringToUString(
1102 &t.pData, RTL_CONSTASCII_STRINGPARAM("\xFF"), RTL_TEXTENCODING_UTF8,
1103 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1104 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1105 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1106 CPPUNIT_ASSERT(
1107 rtl_convertStringToUString(
1108 &t.pData, RTL_CONSTASCII_STRINGPARAM("abc"), RTL_TEXTENCODING_UTF8,
1109 (RTL_TEXTTOUNICODE_FLAGS_UNDEFINED_ERROR |
1110 RTL_TEXTTOUNICODE_FLAGS_MBUNDEFINED_ERROR |
1111 RTL_TEXTTOUNICODE_FLAGS_INVALID_ERROR)));
1112 CPPUNIT_ASSERT( t == "abc" );
1115 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::number);
1116 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toInt);
1117 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toDouble);
1118 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::toFloat);
1119 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::lastIndexOf);
1120 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::getToken);
1121 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertToString);
1122 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::construction);
1123 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::indexOfAscii);
1124 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::endsWith);
1125 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::createFromCodePoints);
1126 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::iterateCodePoints);
1127 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OUString::convertFromString);
1129 } // namespace rtl_OUString
1131 // this macro creates an empty function, which will called by the RegisterAllFunctions()
1132 // to let the user the possibility to also register some functions by hand.
1133 CPPUNIT_PLUGIN_IMPLEMENT();
1135 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */