bump product version to 4.1.6.2
[LibreOffice.git] / sal / qa / rtl / ostring / rtl_OString2.cxx
blob0ea73cdcbd43882e06af588005532d1b309c01be
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 #include <sal/types.h>
21 #include <cppunit/TestFixture.h>
22 #include <cppunit/extensions/HelperMacros.h>
23 #include <cppunit/plugin/TestPlugIn.h>
25 #include <rtl/string.hxx>
27 #include "valueequal.hxx"
29 namespace rtl_OString
32 class valueOf : public CppUnit::TestFixture
34 void valueOf_float_test_impl(float _nValue)
36 rtl::OString sValue;
37 sValue = rtl::OString::valueOf( _nValue );
38 printf("nFloat := %.9f sValue := %s\n", _nValue, sValue.getStr());
40 float nValueATOF = static_cast<float>(atof( sValue.getStr() ));
42 bool bEqualResult = is_float_equal(_nValue, nValueATOF);
43 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
46 void valueOf_float_test(float _nValue)
48 valueOf_float_test_impl(_nValue);
50 // test also the negative part.
51 float nNegativeValue = -_nValue;
52 valueOf_float_test_impl(nNegativeValue);
55 public:
56 // initialise your test code values here.
57 void setUp()
61 void tearDown()
65 // insert your test code here.
66 void valueOf_float_test_001()
68 // this is demonstration code
69 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
70 float nValue = 3.0f;
71 valueOf_float_test(nValue);
74 void valueOf_float_test_002()
76 float nValue = 3.5f;
77 valueOf_float_test(nValue);
80 void valueOf_float_test_003()
82 float nValue = 3.0625f;
83 valueOf_float_test(nValue);
86 void valueOf_float_test_004()
88 float nValue = 3.502525f;
89 valueOf_float_test(nValue);
92 void valueOf_float_test_005()
94 float nValue = 3.141592f;
95 valueOf_float_test(nValue);
98 void valueOf_float_test_006()
100 float nValue = 3.5025255f;
101 valueOf_float_test(nValue);
104 void valueOf_float_test_007()
106 float nValue = 3.0039062f;
107 valueOf_float_test(nValue);
110 private:
112 void valueOf_double_test_impl(double _nValue)
114 rtl::OString sValue;
115 sValue = rtl::OString::valueOf( _nValue );
116 printf("nDouble := %.20f sValue := %s\n", _nValue, sValue.getStr());
118 double nValueATOF = atof( sValue.getStr() );
120 bool bEqualResult = is_double_equal(_nValue, nValueATOF);
121 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
124 void valueOf_double_test(double _nValue)
126 valueOf_double_test_impl(_nValue);
128 // test also the negative part.
129 double nNegativeValue = -_nValue;
130 valueOf_double_test_impl(nNegativeValue);
132 public:
134 // valueOf double
135 void valueOf_double_test_001()
137 double nValue = 3.0;
138 valueOf_double_test(nValue);
140 void valueOf_double_test_002()
142 double nValue = 3.5;
143 valueOf_double_test(nValue);
145 void valueOf_double_test_003()
147 double nValue = 3.0625;
148 valueOf_double_test(nValue);
150 void valueOf_double_test_004()
152 double nValue = 3.1415926535;
153 valueOf_double_test(nValue);
155 void valueOf_double_test_005()
157 double nValue = 3.141592653589793;
158 valueOf_double_test(nValue);
160 void valueOf_double_test_006()
162 double nValue = 3.1415926535897932;
163 valueOf_double_test(nValue);
165 void valueOf_double_test_007()
167 double nValue = 3.14159265358979323;
168 valueOf_double_test(nValue);
170 void valueOf_double_test_008()
172 double nValue = 3.141592653589793238462643;
173 valueOf_double_test(nValue);
177 // Change the following lines only, if you add, remove or rename
178 // member functions of the current class,
179 // because these macros are need by auto register mechanism.
181 CPPUNIT_TEST_SUITE(valueOf);
182 CPPUNIT_TEST(valueOf_float_test_001);
183 CPPUNIT_TEST(valueOf_float_test_002);
184 CPPUNIT_TEST(valueOf_float_test_003);
185 CPPUNIT_TEST(valueOf_float_test_004);
186 CPPUNIT_TEST(valueOf_float_test_005);
187 CPPUNIT_TEST(valueOf_float_test_006);
188 CPPUNIT_TEST(valueOf_float_test_007);
190 CPPUNIT_TEST(valueOf_double_test_001);
191 CPPUNIT_TEST(valueOf_double_test_002);
192 CPPUNIT_TEST(valueOf_double_test_003);
193 CPPUNIT_TEST(valueOf_double_test_004);
194 CPPUNIT_TEST(valueOf_double_test_005);
195 CPPUNIT_TEST(valueOf_double_test_006);
196 CPPUNIT_TEST(valueOf_double_test_007);
197 CPPUNIT_TEST(valueOf_double_test_008);
198 CPPUNIT_TEST_SUITE_END();
199 }; // class valueOf
201 // -----------------------------------------------------------------------------
202 // - toDouble (tests)
203 // -----------------------------------------------------------------------------
204 class toDouble : public CppUnit::TestFixture
207 public:
209 toDouble()
211 // testPrecision a;
216 // initialise your test code values here.
217 void setUp()
221 void tearDown()
225 void toDouble_test_impl(rtl::OString const& _sValue)
227 double nValueATOF = atof( _sValue.getStr() );
229 // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
230 double nValueToDouble = _sValue.toDouble();
232 bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
233 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
236 void toDouble_test(rtl::OString const& _sValue)
238 toDouble_test_impl(_sValue);
240 // test also the negativ part.
241 rtl::OString sNegativValue("-");
242 sNegativValue += _sValue;
243 toDouble_test_impl(sNegativValue);
246 // insert your test code here.
247 void toDouble_selftest()
249 printf("Start selftest:\n");
250 CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
251 CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
252 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
253 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
254 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
255 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
256 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
257 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
258 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
259 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
260 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
261 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
262 // we check til 14 values after comma
263 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
264 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
265 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
266 printf("Selftest done.\n");
269 void toDouble_test_3()
271 rtl::OString sValue("3");
272 toDouble_test(sValue);
274 void toDouble_test_3_5()
276 rtl::OString sValue("3.5");
277 toDouble_test(sValue);
279 void toDouble_test_3_0625()
281 rtl::OString sValue("3.0625");
282 toDouble_test(sValue);
284 void toDouble_test_pi()
286 // value from http://www.angio.net/pi/digits/50.txt
287 rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
288 toDouble_test(sValue);
291 void toDouble_test_1()
293 rtl::OString sValue("1");
294 toDouble_test(sValue);
296 void toDouble_test_10()
298 rtl::OString sValue("10");
299 toDouble_test(sValue);
301 void toDouble_test_100()
303 rtl::OString sValue("100");
304 toDouble_test(sValue);
306 void toDouble_test_1000()
308 rtl::OString sValue("1000");
309 toDouble_test(sValue);
311 void toDouble_test_10000()
313 rtl::OString sValue("10000");
314 toDouble_test(sValue);
316 void toDouble_test_1e99()
318 rtl::OString sValue("1e99");
319 toDouble_test(sValue);
321 void toDouble_test_1e_n99()
323 rtl::OString sValue("1e-99");
324 toDouble_test(sValue);
326 void toDouble_test_1e308()
328 rtl::OString sValue("1e308");
329 toDouble_test(sValue);
332 // Change the following lines only, if you add, remove or rename
333 // member functions of the current class,
334 // because these macros are need by auto register mechanism.
336 CPPUNIT_TEST_SUITE(toDouble);
337 CPPUNIT_TEST(toDouble_selftest);
339 CPPUNIT_TEST(toDouble_test_3);
340 CPPUNIT_TEST(toDouble_test_3_5);
341 CPPUNIT_TEST(toDouble_test_3_0625);
342 CPPUNIT_TEST(toDouble_test_pi);
343 CPPUNIT_TEST(toDouble_test_1);
344 CPPUNIT_TEST(toDouble_test_10);
345 CPPUNIT_TEST(toDouble_test_100);
346 CPPUNIT_TEST(toDouble_test_1000);
347 CPPUNIT_TEST(toDouble_test_10000);
348 CPPUNIT_TEST(toDouble_test_1e99);
349 CPPUNIT_TEST(toDouble_test_1e_n99);
350 CPPUNIT_TEST(toDouble_test_1e308);
351 CPPUNIT_TEST_SUITE_END();
352 }; // class toDouble
354 // -----------------------------------------------------------------------------
355 // - getToken (tests)
356 // -----------------------------------------------------------------------------
357 class getToken : public CppUnit::TestFixture
360 public:
362 // initialise your test code values here.
363 void setUp()
367 void tearDown()
371 // -----------------------------------------------------------------------------
373 void getToken_000()
375 rtl::OString sTokenStr;
377 sal_Int32 nIndex = 0;
380 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
382 while ( nIndex >= 0 );
383 // printf("Index %d\n", nIndex);
384 // should not GPF
387 void getToken_001()
389 rtl::OString sTokenStr = "a;b";
391 sal_Int32 nIndex = 0;
393 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
394 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
396 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
397 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
398 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
401 void getToken_002()
403 rtl::OString sTokenStr = "a;b.c";
405 sal_Int32 nIndex = 0;
407 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
408 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
410 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
411 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
413 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
414 CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken.equals("c") == sal_True);
415 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
418 void getToken_003()
420 rtl::OString sTokenStr = "a;;b";
422 sal_Int32 nIndex = 0;
424 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
425 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
427 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
428 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.isEmpty());
430 /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
431 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
432 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
435 void getToken_004()
437 rtl::OString sTokenStr = "longer.then.ever.";
439 sal_Int32 nIndex = 0;
441 rtl::OString sToken = sTokenStr.getToken( 0, '.', nIndex );
442 CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken.equals("longer") == sal_True);
444 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
445 CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken.equals("then") == sal_True);
447 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
448 CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken.equals("ever") == sal_True);
450 /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
451 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.isEmpty());
453 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
457 CPPUNIT_TEST_SUITE(getToken);
458 CPPUNIT_TEST(getToken_000);
459 CPPUNIT_TEST(getToken_001);
460 CPPUNIT_TEST(getToken_002);
461 CPPUNIT_TEST(getToken_003);
462 CPPUNIT_TEST(getToken_004);
463 CPPUNIT_TEST_SUITE_END();
464 }; // class getToken
466 // -----------------------------------------------------------------------------
467 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
468 // const OString& newStr )
469 // -----------------------------------------------------------------------------
471 // Developer note: Mindy Liu, 2004-04-23
472 // stollen from sal/qa/rtl_strings/rtl_OString.cxx
474 class replaceAt : public CppUnit::TestFixture
477 public:
478 // initialise your test code values here.
479 void setUp()
483 void tearDown()
486 sal_Bool check_replaceAt( const rtl::OString* expVal, const rtl::OString* input,
487 const rtl::OString* newStr, sal_Int32 index, sal_Int32 count)
489 ::rtl::OString aStr1;
490 aStr1= input->replaceAt( index, count, *newStr );
492 printf("the result OString is %s#\n", aStr1.getStr() );
494 sal_Bool bRes = ( expVal->compareTo(aStr1) == 0 );
495 return bRes;
497 // -----------------------------------------------------------------------------
499 void replaceAt_001()
501 sal_Bool bRes = check_replaceAt(new rtl::OString("Java@Sun"),
502 new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
503 CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes == sal_True);
506 void replaceAt_002()
508 sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Java desktop system"),
509 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
510 CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes == sal_True);
513 void replaceAt_003()
515 sal_Bool bRes = check_replaceAt(new rtl::OString("SuJava desktop system"),
516 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
517 CPPUNIT_ASSERT_MESSAGE("larger count", bRes == sal_True);
520 void replaceAt_004()
523 sal_Bool bRes = check_replaceAt(new rtl::OString("Java desktop system"),
524 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
525 CPPUNIT_ASSERT_MESSAGE("navigate index", bRes == sal_True);
527 void replaceAt_005()
530 sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Jesktop System"),
531 new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
532 CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes == sal_True);
536 CPPUNIT_TEST_SUITE(replaceAt);
537 CPPUNIT_TEST(replaceAt_001);
538 CPPUNIT_TEST(replaceAt_002);
539 CPPUNIT_TEST(replaceAt_003);
540 CPPUNIT_TEST(replaceAt_004);
541 CPPUNIT_TEST(replaceAt_005);
542 CPPUNIT_TEST_SUITE_END();
543 }; // class replaceAt
546 // -----------------------------------------------------------------------------
547 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::valueOf);
548 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::toDouble);
549 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::getToken);
550 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::replaceAt);
552 } // namespace rtl_OString
555 // -----------------------------------------------------------------------------
557 // this macro creates an empty function, which will called by the RegisterAllFunctions()
558 // to let the user the possibility to also register some functions by hand.
559 CPPUNIT_PLUGIN_IMPLEMENT();
561 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */