update dev300-m58
[ooovba.git] / sal / qa / rtl / ostring / rtl_OString2.cxx
blob7c289376f45f071332bc51c6faed27e8f346114d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_OString2.cxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 // MARKER(update_precomp.py): autogen include statement, do not remove
33 #include "precompiled_sal.hxx"
34 // autogenerated file with codegen.pl
35 // There exist some more test code in sal/qa/rtl_strings/rtl_OString.cxx
37 #include <cppunit/simpleheader.hxx>
38 #include "valueequal.hxx"
40 namespace rtl_OString
43 class valueOf : public CppUnit::TestFixture
45 void valueOf_float_test_impl(float _nValue)
47 rtl::OString sValue;
48 sValue = rtl::OString::valueOf( _nValue );
49 t_print(T_VERBOSE, "nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr());
51 float nValueATOF = static_cast<float>(atof( sValue.getStr() ));
53 bool bEqualResult = is_float_equal(_nValue, nValueATOF);
54 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
57 void valueOf_float_test(float _nValue)
59 valueOf_float_test_impl(_nValue);
61 // test also the negative part.
62 float nNegativeValue = -_nValue;
63 valueOf_float_test_impl(nNegativeValue);
66 public:
67 // initialise your test code values here.
68 void setUp()
72 void tearDown()
76 // insert your test code here.
77 void valueOf_float_test_001()
79 // this is demonstration code
80 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
81 float nValue = 3.0f;
82 valueOf_float_test(nValue);
85 void valueOf_float_test_002()
87 float nValue = 3.5f;
88 valueOf_float_test(nValue);
91 void valueOf_float_test_003()
93 float nValue = 3.0625f;
94 valueOf_float_test(nValue);
97 void valueOf_float_test_004()
99 float nValue = 3.502525f;
100 valueOf_float_test(nValue);
103 void valueOf_float_test_005()
105 float nValue = 3.141592f;
106 valueOf_float_test(nValue);
109 void valueOf_float_test_006()
111 float nValue = 3.5025255f;
112 valueOf_float_test(nValue);
115 void valueOf_float_test_007()
117 float nValue = 3.0039062f;
118 valueOf_float_test(nValue);
121 private:
123 void valueOf_double_test_impl(double _nValue)
125 rtl::OString sValue;
126 sValue = rtl::OString::valueOf( _nValue );
127 t_print(T_VERBOSE, "nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr());
129 double nValueATOF = atof( sValue.getStr() );
131 bool bEqualResult = is_double_equal(_nValue, nValueATOF);
132 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
135 void valueOf_double_test(double _nValue)
137 valueOf_double_test_impl(_nValue);
139 // test also the negative part.
140 double nNegativeValue = -_nValue;
141 valueOf_double_test_impl(nNegativeValue);
143 public:
145 // valueOf double
146 void valueOf_double_test_001()
148 double nValue = 3.0;
149 valueOf_double_test(nValue);
151 void valueOf_double_test_002()
153 double nValue = 3.5;
154 valueOf_double_test(nValue);
156 void valueOf_double_test_003()
158 double nValue = 3.0625;
159 valueOf_double_test(nValue);
161 void valueOf_double_test_004()
163 double nValue = 3.1415926535;
164 valueOf_double_test(nValue);
166 void valueOf_double_test_005()
168 double nValue = 3.141592653589793;
169 valueOf_double_test(nValue);
171 void valueOf_double_test_006()
173 double nValue = 3.1415926535897932;
174 valueOf_double_test(nValue);
176 void valueOf_double_test_007()
178 double nValue = 3.14159265358979323;
179 valueOf_double_test(nValue);
181 void valueOf_double_test_008()
183 double nValue = 3.141592653589793238462643;
184 valueOf_double_test(nValue);
188 // Change the following lines only, if you add, remove or rename
189 // member functions of the current class,
190 // because these macros are need by auto register mechanism.
192 CPPUNIT_TEST_SUITE(valueOf);
193 CPPUNIT_TEST(valueOf_float_test_001);
194 CPPUNIT_TEST(valueOf_float_test_002);
195 CPPUNIT_TEST(valueOf_float_test_003);
196 CPPUNIT_TEST(valueOf_float_test_004);
197 CPPUNIT_TEST(valueOf_float_test_005);
198 CPPUNIT_TEST(valueOf_float_test_006);
199 CPPUNIT_TEST(valueOf_float_test_007);
201 CPPUNIT_TEST(valueOf_double_test_001);
202 CPPUNIT_TEST(valueOf_double_test_002);
203 CPPUNIT_TEST(valueOf_double_test_003);
204 CPPUNIT_TEST(valueOf_double_test_004);
205 CPPUNIT_TEST(valueOf_double_test_005);
206 CPPUNIT_TEST(valueOf_double_test_006);
207 CPPUNIT_TEST(valueOf_double_test_007);
208 CPPUNIT_TEST(valueOf_double_test_008);
209 CPPUNIT_TEST_SUITE_END();
210 }; // class valueOf
212 // -----------------------------------------------------------------------------
213 // - toDouble (tests)
214 // -----------------------------------------------------------------------------
215 class toDouble : public CppUnit::TestFixture
218 public:
220 toDouble()
222 // testPrecision a;
227 // initialise your test code values here.
228 void setUp()
232 void tearDown()
236 void toDouble_test_impl(rtl::OString const& _sValue)
238 double nValueATOF = atof( _sValue.getStr() );
240 // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
241 double nValueToDouble = _sValue.toDouble();
243 bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
244 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
247 void toDouble_test(rtl::OString const& _sValue)
249 toDouble_test_impl(_sValue);
251 // test also the negativ part.
252 rtl::OString sNegativValue("-");
253 sNegativValue += _sValue;
254 toDouble_test_impl(sNegativValue);
257 // insert your test code here.
258 void toDouble_selftest()
260 t_print("Start selftest:\n");
261 CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
262 CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
263 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
264 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
265 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
266 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
267 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
268 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
269 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
270 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
271 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
272 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
273 // we check til 14 values after comma
274 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
275 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
276 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
277 t_print("Selftest done.\n");
280 void toDouble_test_3()
282 rtl::OString sValue("3");
283 toDouble_test(sValue);
285 void toDouble_test_3_5()
287 rtl::OString sValue("3.5");
288 toDouble_test(sValue);
290 void toDouble_test_3_0625()
292 rtl::OString sValue("3.0625");
293 toDouble_test(sValue);
295 void toDouble_test_pi()
297 // value from http://www.angio.net/pi/digits/50.txt
298 rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
299 toDouble_test(sValue);
302 void toDouble_test_1()
304 rtl::OString sValue("1");
305 toDouble_test(sValue);
307 void toDouble_test_10()
309 rtl::OString sValue("10");
310 toDouble_test(sValue);
312 void toDouble_test_100()
314 rtl::OString sValue("100");
315 toDouble_test(sValue);
317 void toDouble_test_1000()
319 rtl::OString sValue("1000");
320 toDouble_test(sValue);
322 void toDouble_test_10000()
324 rtl::OString sValue("10000");
325 toDouble_test(sValue);
327 void toDouble_test_1e99()
329 rtl::OString sValue("1e99");
330 toDouble_test(sValue);
332 void toDouble_test_1e_n99()
334 rtl::OString sValue("1e-99");
335 toDouble_test(sValue);
337 void toDouble_test_1e308()
339 rtl::OString sValue("1e308");
340 toDouble_test(sValue);
343 // Change the following lines only, if you add, remove or rename
344 // member functions of the current class,
345 // because these macros are need by auto register mechanism.
347 CPPUNIT_TEST_SUITE(toDouble);
348 CPPUNIT_TEST(toDouble_selftest);
350 CPPUNIT_TEST(toDouble_test_3);
351 CPPUNIT_TEST(toDouble_test_3_5);
352 CPPUNIT_TEST(toDouble_test_3_0625);
353 CPPUNIT_TEST(toDouble_test_pi);
354 CPPUNIT_TEST(toDouble_test_1);
355 CPPUNIT_TEST(toDouble_test_10);
356 CPPUNIT_TEST(toDouble_test_100);
357 CPPUNIT_TEST(toDouble_test_1000);
358 CPPUNIT_TEST(toDouble_test_10000);
359 CPPUNIT_TEST(toDouble_test_1e99);
360 CPPUNIT_TEST(toDouble_test_1e_n99);
361 CPPUNIT_TEST(toDouble_test_1e308);
362 CPPUNIT_TEST_SUITE_END();
363 }; // class toDouble
365 // -----------------------------------------------------------------------------
366 // - getToken (tests)
367 // -----------------------------------------------------------------------------
368 class getToken : public CppUnit::TestFixture
371 public:
373 // initialise your test code values here.
374 void setUp()
378 void tearDown()
382 // -----------------------------------------------------------------------------
384 void getToken_000()
386 rtl::OString sTokenStr;
388 sal_Int32 nIndex = 0;
391 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
393 while ( nIndex >= 0 );
394 // t_print("Index %d\n", nIndex);
395 // should not GPF
398 void getToken_001()
400 rtl::OString sTokenStr = "a;b";
402 sal_Int32 nIndex = 0;
404 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
405 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
407 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
408 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
409 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
412 void getToken_002()
414 rtl::OString sTokenStr = "a;b.c";
416 sal_Int32 nIndex = 0;
418 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
419 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
421 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
422 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
424 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
425 CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken.equals("c") == sal_True);
426 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
429 void getToken_003()
431 rtl::OString sTokenStr = "a;;b";
433 sal_Int32 nIndex = 0;
435 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
436 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
438 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
439 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.getLength() == 0);
441 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
442 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
443 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
446 void getToken_004()
448 rtl::OString sTokenStr = "longer.then.ever.";
450 sal_Int32 nIndex = 0;
452 rtl::OString sToken = sTokenStr.getToken( 0, '.', nIndex );
453 CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken.equals("longer") == sal_True);
455 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
456 CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken.equals("then") == sal_True);
458 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
459 CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken.equals("ever") == sal_True);
461 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
462 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.getLength() == 0);
464 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
468 CPPUNIT_TEST_SUITE(getToken);
469 CPPUNIT_TEST(getToken_000);
470 CPPUNIT_TEST(getToken_001);
471 CPPUNIT_TEST(getToken_002);
472 CPPUNIT_TEST(getToken_003);
473 CPPUNIT_TEST(getToken_004);
474 CPPUNIT_TEST_SUITE_END();
475 }; // class getToken
477 // -----------------------------------------------------------------------------
478 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
479 // const OString& newStr )
480 // -----------------------------------------------------------------------------
482 // Developer note: Mindy Liu, 2004-04-23
483 // stollen from sal/qa/rtl_strings/rtl_OString.cxx
485 class replaceAt : public CppUnit::TestFixture
488 public:
489 // initialise your test code values here.
490 void setUp()
494 void tearDown()
497 sal_Bool check_replaceAt( const rtl::OString* expVal, const rtl::OString* input,
498 const rtl::OString* newStr, sal_Int32 index, sal_Int32 count)
500 ::rtl::OString aStr1;
501 aStr1= input->replaceAt( index, count, *newStr );
503 t_print("the result OString is %s#\n", aStr1.getStr() );
505 sal_Bool bRes = ( expVal->compareTo(aStr1) == 0 );
506 return bRes;
508 // -----------------------------------------------------------------------------
510 void replaceAt_001()
512 sal_Bool bRes = check_replaceAt(new rtl::OString("Java@Sun"),
513 new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
514 CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes == sal_True);
517 void replaceAt_002()
519 sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Java desktop system"),
520 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
521 CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes == sal_True);
524 void replaceAt_003()
526 sal_Bool bRes = check_replaceAt(new rtl::OString("SuJava desktop system"),
527 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
528 CPPUNIT_ASSERT_MESSAGE("larger count", bRes == sal_True);
531 void replaceAt_004()
534 sal_Bool bRes = check_replaceAt(new rtl::OString("Java desktop system"),
535 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
536 CPPUNIT_ASSERT_MESSAGE("navigate index", bRes == sal_True);
538 void replaceAt_005()
541 sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Jesktop System"),
542 new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
543 CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes == sal_True);
547 CPPUNIT_TEST_SUITE(replaceAt);
548 CPPUNIT_TEST(replaceAt_001);
549 CPPUNIT_TEST(replaceAt_002);
550 CPPUNIT_TEST(replaceAt_003);
551 CPPUNIT_TEST(replaceAt_004);
552 CPPUNIT_TEST(replaceAt_005);
553 CPPUNIT_TEST_SUITE_END();
554 }; // class replaceAt
557 // -----------------------------------------------------------------------------
558 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::valueOf, "rtl_OString");
559 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::toDouble, "rtl_OString");
560 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::getToken, "rtl_OString");
561 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::replaceAt, "rtl_OString");
563 } // namespace rtl_OString
566 // -----------------------------------------------------------------------------
568 // this macro creates an empty function, which will called by the RegisterAllFunctions()
569 // to let the user the possibility to also register some functions by hand.
570 NOADDITIONAL;