1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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"
32 class valueOf
: public CppUnit::TestFixture
34 void valueOf_float_test_impl(float _nValue
)
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
);
56 // initialise your test code values here.
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);
71 valueOf_float_test(nValue
);
74 void valueOf_float_test_002()
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
);
112 void valueOf_double_test_impl(double _nValue
)
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
);
135 void valueOf_double_test_001()
138 valueOf_double_test(nValue
);
140 void valueOf_double_test_002()
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
);
176 // Change the following lines only, if you add, remove or rename
177 // member functions of the current class,
178 // because these macros are need by auto register mechanism.
180 CPPUNIT_TEST_SUITE(valueOf
);
181 CPPUNIT_TEST(valueOf_float_test_001
);
182 CPPUNIT_TEST(valueOf_float_test_002
);
183 CPPUNIT_TEST(valueOf_float_test_003
);
184 CPPUNIT_TEST(valueOf_float_test_004
);
185 CPPUNIT_TEST(valueOf_float_test_005
);
186 CPPUNIT_TEST(valueOf_float_test_006
);
187 CPPUNIT_TEST(valueOf_float_test_007
);
189 CPPUNIT_TEST(valueOf_double_test_001
);
190 CPPUNIT_TEST(valueOf_double_test_002
);
191 CPPUNIT_TEST(valueOf_double_test_003
);
192 CPPUNIT_TEST(valueOf_double_test_004
);
193 CPPUNIT_TEST(valueOf_double_test_005
);
194 CPPUNIT_TEST(valueOf_double_test_006
);
195 CPPUNIT_TEST(valueOf_double_test_007
);
196 CPPUNIT_TEST(valueOf_double_test_008
);
197 CPPUNIT_TEST_SUITE_END();
200 // - toDouble (tests)
202 class toDouble
: public CppUnit::TestFixture
212 // initialise your test code values here.
221 void toDouble_test_impl(rtl::OString
const& _sValue
)
223 double nValueATOF
= atof( _sValue
.getStr() );
225 // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
226 double nValueToDouble
= _sValue
.toDouble();
228 bool bEqualResult
= is_double_equal(nValueToDouble
, nValueATOF
);
229 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult
== true);
232 void toDouble_test(rtl::OString
const& _sValue
)
234 toDouble_test_impl(_sValue
);
236 // test also the negativ part.
237 rtl::OString
sNegativValue("-");
238 sNegativValue
+= _sValue
;
239 toDouble_test_impl(sNegativValue
);
242 // insert your test code here.
243 void toDouble_selftest()
245 printf("Start selftest:\n");
246 CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
247 CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
248 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
249 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
250 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
251 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
252 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
253 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
254 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
255 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
256 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
257 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
258 // we check til 14 values after comma
259 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
260 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
261 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
262 printf("Selftest done.\n");
265 void toDouble_test_3()
267 rtl::OString
sValue("3");
268 toDouble_test(sValue
);
270 void toDouble_test_3_5()
272 rtl::OString
sValue("3.5");
273 toDouble_test(sValue
);
275 void toDouble_test_3_0625()
277 rtl::OString
sValue("3.0625");
278 toDouble_test(sValue
);
280 void toDouble_test_pi()
282 // value from http://www.angio.net/pi/digits/50.txt
283 rtl::OString
sValue("3.141592653589793238462643383279502884197169399375");
284 toDouble_test(sValue
);
287 void toDouble_test_1()
289 rtl::OString
sValue("1");
290 toDouble_test(sValue
);
292 void toDouble_test_10()
294 rtl::OString
sValue("10");
295 toDouble_test(sValue
);
297 void toDouble_test_100()
299 rtl::OString
sValue("100");
300 toDouble_test(sValue
);
302 void toDouble_test_1000()
304 rtl::OString
sValue("1000");
305 toDouble_test(sValue
);
307 void toDouble_test_10000()
309 rtl::OString
sValue("10000");
310 toDouble_test(sValue
);
312 void toDouble_test_1e99()
314 rtl::OString
sValue("1e99");
315 toDouble_test(sValue
);
317 void toDouble_test_1e_n99()
319 rtl::OString
sValue("1e-99");
320 toDouble_test(sValue
);
322 void toDouble_test_1e308()
324 rtl::OString
sValue("1e308");
325 toDouble_test(sValue
);
328 // Change the following lines only, if you add, remove or rename
329 // member functions of the current class,
330 // because these macros are need by auto register mechanism.
332 CPPUNIT_TEST_SUITE(toDouble
);
333 CPPUNIT_TEST(toDouble_selftest
);
335 CPPUNIT_TEST(toDouble_test_3
);
336 CPPUNIT_TEST(toDouble_test_3_5
);
337 CPPUNIT_TEST(toDouble_test_3_0625
);
338 CPPUNIT_TEST(toDouble_test_pi
);
339 CPPUNIT_TEST(toDouble_test_1
);
340 CPPUNIT_TEST(toDouble_test_10
);
341 CPPUNIT_TEST(toDouble_test_100
);
342 CPPUNIT_TEST(toDouble_test_1000
);
343 CPPUNIT_TEST(toDouble_test_10000
);
344 CPPUNIT_TEST(toDouble_test_1e99
);
345 CPPUNIT_TEST(toDouble_test_1e_n99
);
346 CPPUNIT_TEST(toDouble_test_1e308
);
347 CPPUNIT_TEST_SUITE_END();
350 // - getToken (tests)
352 class getToken
: public CppUnit::TestFixture
357 // initialise your test code values here.
368 rtl::OString sTokenStr
;
370 sal_Int32 nIndex
= 0;
373 rtl::OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
375 while ( nIndex
>= 0 );
376 // printf("Index %d\n", nIndex);
382 rtl::OString sTokenStr
= "a;b";
384 sal_Int32 nIndex
= 0;
386 rtl::OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
387 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
389 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
390 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
391 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
396 rtl::OString sTokenStr
= "a;b.c";
398 sal_Int32 nIndex
= 0;
400 rtl::OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
401 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
403 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
404 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
406 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
407 CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken
.equals("c") == sal_True
);
408 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
413 rtl::OString sTokenStr
= "a;;b";
415 sal_Int32 nIndex
= 0;
417 rtl::OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
418 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
420 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
421 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken
.isEmpty());
423 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
424 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
425 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
430 rtl::OString sTokenStr
= "longer.then.ever.";
432 sal_Int32 nIndex
= 0;
434 rtl::OString sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
435 CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken
.equals("longer") == sal_True
);
437 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
438 CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken
.equals("then") == sal_True
);
440 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
441 CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken
.equals("ever") == sal_True
);
443 /* rtl::OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
444 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken
.isEmpty());
446 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
449 CPPUNIT_TEST_SUITE(getToken
);
450 CPPUNIT_TEST(getToken_000
);
451 CPPUNIT_TEST(getToken_001
);
452 CPPUNIT_TEST(getToken_002
);
453 CPPUNIT_TEST(getToken_003
);
454 CPPUNIT_TEST(getToken_004
);
455 CPPUNIT_TEST_SUITE_END();
458 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
459 // const OString& newStr )
461 // Developer note: Mindy Liu, 2004-04-23
462 // stollen from sal/qa/rtl_strings/rtl_OString.cxx
464 class replaceAt
: public CppUnit::TestFixture
468 // initialise your test code values here.
476 sal_Bool
check_replaceAt( const rtl::OString
* expVal
, const rtl::OString
* input
,
477 const rtl::OString
* newStr
, sal_Int32 index
, sal_Int32 count
)
479 ::rtl::OString aStr1
;
480 aStr1
= input
->replaceAt( index
, count
, *newStr
);
482 printf("the result OString is %s#\n", aStr1
.getStr() );
484 sal_Bool bRes
= ( expVal
->compareTo(aStr1
) == 0 );
490 sal_Bool bRes
= check_replaceAt(new rtl::OString("Java@Sun"),
491 new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
492 CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes
== sal_True
);
497 sal_Bool bRes
= check_replaceAt(new rtl::OString("Sun Java desktop system"),
498 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
499 CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes
== sal_True
);
504 sal_Bool bRes
= check_replaceAt(new rtl::OString("SuJava desktop system"),
505 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
506 CPPUNIT_ASSERT_MESSAGE("larger count", bRes
== sal_True
);
512 sal_Bool bRes
= check_replaceAt(new rtl::OString("Java desktop system"),
513 new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
514 CPPUNIT_ASSERT_MESSAGE("navigate index", bRes
== sal_True
);
519 sal_Bool bRes
= check_replaceAt(new rtl::OString("Sun Jesktop System"),
520 new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
521 CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes
== sal_True
);
524 CPPUNIT_TEST_SUITE(replaceAt
);
525 CPPUNIT_TEST(replaceAt_001
);
526 CPPUNIT_TEST(replaceAt_002
);
527 CPPUNIT_TEST(replaceAt_003
);
528 CPPUNIT_TEST(replaceAt_004
);
529 CPPUNIT_TEST(replaceAt_005
);
530 CPPUNIT_TEST_SUITE_END();
531 }; // class replaceAt
533 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::valueOf
);
534 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::toDouble
);
535 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::getToken
);
536 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::replaceAt
);
538 } // namespace rtl_OString
540 // this macro creates an empty function, which will called by the RegisterAllFunctions()
541 // to let the user the possibility to also register some functions by hand.
542 CPPUNIT_PLUGIN_IMPLEMENT();
544 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */