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
= 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 // insert your test code here.
57 void valueOf_float_test_001()
59 // this is demonstration code
60 // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
62 valueOf_float_test(nValue
);
65 void valueOf_float_test_002()
68 valueOf_float_test(nValue
);
71 void valueOf_float_test_003()
73 float nValue
= 3.0625f
;
74 valueOf_float_test(nValue
);
77 void valueOf_float_test_004()
79 float nValue
= 3.502525f
;
80 valueOf_float_test(nValue
);
83 void valueOf_float_test_005()
85 float nValue
= 3.141592f
;
86 valueOf_float_test(nValue
);
89 void valueOf_float_test_006()
91 float nValue
= 3.5025255f
;
92 valueOf_float_test(nValue
);
95 void valueOf_float_test_007()
97 float nValue
= 3.0039062f
;
98 valueOf_float_test(nValue
);
103 void valueOf_double_test_impl(double _nValue
)
106 sValue
= OString::valueOf( _nValue
);
107 printf("nDouble := %.20f sValue := %s\n", _nValue
, sValue
.getStr());
109 double nValueATOF
= atof( sValue
.getStr() );
111 bool bEqualResult
= is_double_equal(_nValue
, nValueATOF
);
112 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult
== true);
115 void valueOf_double_test(double _nValue
)
117 valueOf_double_test_impl(_nValue
);
119 // test also the negative part.
120 double nNegativeValue
= -_nValue
;
121 valueOf_double_test_impl(nNegativeValue
);
126 void valueOf_double_test_001()
129 valueOf_double_test(nValue
);
131 void valueOf_double_test_002()
134 valueOf_double_test(nValue
);
136 void valueOf_double_test_003()
138 double nValue
= 3.0625;
139 valueOf_double_test(nValue
);
141 void valueOf_double_test_004()
143 double nValue
= 3.1415926535;
144 valueOf_double_test(nValue
);
146 void valueOf_double_test_005()
148 double nValue
= 3.141592653589793;
149 valueOf_double_test(nValue
);
151 void valueOf_double_test_006()
153 double nValue
= 3.1415926535897932;
154 valueOf_double_test(nValue
);
156 void valueOf_double_test_007()
158 double nValue
= 3.14159265358979323;
159 valueOf_double_test(nValue
);
161 void valueOf_double_test_008()
163 double nValue
= 3.141592653589793238462643;
164 valueOf_double_test(nValue
);
167 // Change the following lines only, if you add, remove or rename
168 // member functions of the current class,
169 // because these macros are need by auto register mechanism.
171 CPPUNIT_TEST_SUITE(valueOf
);
172 CPPUNIT_TEST(valueOf_float_test_001
);
173 CPPUNIT_TEST(valueOf_float_test_002
);
174 CPPUNIT_TEST(valueOf_float_test_003
);
175 CPPUNIT_TEST(valueOf_float_test_004
);
176 CPPUNIT_TEST(valueOf_float_test_005
);
177 CPPUNIT_TEST(valueOf_float_test_006
);
178 CPPUNIT_TEST(valueOf_float_test_007
);
180 CPPUNIT_TEST(valueOf_double_test_001
);
181 CPPUNIT_TEST(valueOf_double_test_002
);
182 CPPUNIT_TEST(valueOf_double_test_003
);
183 CPPUNIT_TEST(valueOf_double_test_004
);
184 CPPUNIT_TEST(valueOf_double_test_005
);
185 CPPUNIT_TEST(valueOf_double_test_006
);
186 CPPUNIT_TEST(valueOf_double_test_007
);
187 CPPUNIT_TEST(valueOf_double_test_008
);
188 CPPUNIT_TEST_SUITE_END();
191 // - toDouble (tests)
193 class toDouble
: public CppUnit::TestFixture
203 void toDouble_test_impl(OString
const& _sValue
)
205 double nValueATOF
= atof( _sValue
.getStr() );
207 // OUString suValue = OUString::createFromAscii( _sValue.getStr() );
208 double nValueToDouble
= _sValue
.toDouble();
210 bool bEqualResult
= is_double_equal(nValueToDouble
, nValueATOF
);
211 CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult
== true);
214 void toDouble_test(OString
const& _sValue
)
216 toDouble_test_impl(_sValue
);
218 // test also the negative part.
219 OString
sNegativValue("-");
220 sNegativValue
+= _sValue
;
221 toDouble_test_impl(sNegativValue
);
224 // insert your test code here.
225 void toDouble_selftest()
227 printf("Start selftest:\n");
228 CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
229 CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
230 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
231 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
232 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
233 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
234 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
235 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
236 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
237 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
238 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
239 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
240 // we check til 14 values after comma
241 CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
242 CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
243 CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
244 printf("Selftest done.\n");
247 void toDouble_test_3()
250 toDouble_test(sValue
);
252 void toDouble_test_3_5()
254 OString
sValue("3.5");
255 toDouble_test(sValue
);
257 void toDouble_test_3_0625()
259 OString
sValue("3.0625");
260 toDouble_test(sValue
);
262 void toDouble_test_pi()
264 // value from http://www.angio.net/pi/digits/50.txt
265 OString
sValue("3.141592653589793238462643383279502884197169399375");
266 toDouble_test(sValue
);
269 void toDouble_test_1()
272 toDouble_test(sValue
);
274 void toDouble_test_10()
276 OString
sValue("10");
277 toDouble_test(sValue
);
279 void toDouble_test_100()
281 OString
sValue("100");
282 toDouble_test(sValue
);
284 void toDouble_test_1000()
286 OString
sValue("1000");
287 toDouble_test(sValue
);
289 void toDouble_test_10000()
291 OString
sValue("10000");
292 toDouble_test(sValue
);
294 void toDouble_test_1e99()
296 OString
sValue("1e99");
297 toDouble_test(sValue
);
299 void toDouble_test_1e_n99()
301 OString
sValue("1e-99");
302 toDouble_test(sValue
);
304 void toDouble_test_1e308()
306 OString
sValue("1e308");
307 toDouble_test(sValue
);
310 // Change the following lines only, if you add, remove or rename
311 // member functions of the current class,
312 // because these macros are need by auto register mechanism.
314 CPPUNIT_TEST_SUITE(toDouble
);
315 CPPUNIT_TEST(toDouble_selftest
);
317 CPPUNIT_TEST(toDouble_test_3
);
318 CPPUNIT_TEST(toDouble_test_3_5
);
319 CPPUNIT_TEST(toDouble_test_3_0625
);
320 CPPUNIT_TEST(toDouble_test_pi
);
321 CPPUNIT_TEST(toDouble_test_1
);
322 CPPUNIT_TEST(toDouble_test_10
);
323 CPPUNIT_TEST(toDouble_test_100
);
324 CPPUNIT_TEST(toDouble_test_1000
);
325 CPPUNIT_TEST(toDouble_test_10000
);
326 CPPUNIT_TEST(toDouble_test_1e99
);
327 CPPUNIT_TEST(toDouble_test_1e_n99
);
328 CPPUNIT_TEST(toDouble_test_1e308
);
329 CPPUNIT_TEST_SUITE_END();
332 // - getToken (tests)
334 class getToken
: public CppUnit::TestFixture
342 sal_Int32 nIndex
= 0;
345 OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
347 while ( nIndex
>= 0 );
348 // printf("Index %d\n", nIndex);
354 OString sTokenStr
= "a;b";
356 sal_Int32 nIndex
= 0;
358 OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
359 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
361 /* OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
362 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
363 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
368 OString sTokenStr
= "a;b.c";
370 sal_Int32 nIndex
= 0;
372 OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
373 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
375 /* OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
376 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
378 /* OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
379 CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken
.equals("c") == sal_True
);
380 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
385 OString sTokenStr
= "a;;b";
387 sal_Int32 nIndex
= 0;
389 OString sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
390 CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken
.equals("a") == sal_True
);
392 /* OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
393 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken
.isEmpty());
395 /* OString */ sToken
= sTokenStr
.getToken( 0, ';', nIndex
);
396 CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken
.equals("b") == sal_True
);
397 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
402 OString sTokenStr
= "longer.then.ever.";
404 sal_Int32 nIndex
= 0;
406 OString sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
407 CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken
.equals("longer") == sal_True
);
409 /* OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
410 CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken
.equals("then") == sal_True
);
412 /* OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
413 CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken
.equals("ever") == sal_True
);
415 /* OString */ sToken
= sTokenStr
.getToken( 0, '.', nIndex
);
416 CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken
.isEmpty());
418 CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex
== -1);
421 CPPUNIT_TEST_SUITE(getToken
);
422 CPPUNIT_TEST(getToken_000
);
423 CPPUNIT_TEST(getToken_001
);
424 CPPUNIT_TEST(getToken_002
);
425 CPPUNIT_TEST(getToken_003
);
426 CPPUNIT_TEST(getToken_004
);
427 CPPUNIT_TEST_SUITE_END();
430 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
431 // const OString& newStr )
433 // Developer note: Mindy Liu, 2004-04-23
434 // stolen from sal/qa/rtl_strings/rtl_OString.cxx
436 class replaceAt
: public CppUnit::TestFixture
440 sal_Bool
check_replaceAt( const OString
* expVal
, const OString
* input
,
441 const OString
* newStr
, sal_Int32 index
, sal_Int32 count
)
444 aStr1
= input
->replaceAt( index
, count
, *newStr
);
446 printf("the result OString is %s#\n", aStr1
.getStr() );
448 sal_Bool bRes
= ( expVal
->compareTo(aStr1
) == 0 );
454 sal_Bool bRes
= check_replaceAt(new OString("Java@Sun"),
455 new OString("Sun java"), new OString("Java@Sun"), 0, 8 );
456 CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes
== sal_True
);
461 sal_Bool bRes
= check_replaceAt(new OString("Sun Java desktop system"),
462 new OString("Sun "), new OString("Java desktop system"), 10, 8 );
463 CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes
== sal_True
);
468 sal_Bool bRes
= check_replaceAt(new OString("SuJava desktop system"),
469 new OString("Sun "), new OString("Java desktop system"), 2, 64 );
470 CPPUNIT_ASSERT_MESSAGE("larger count", bRes
== sal_True
);
476 sal_Bool bRes
= check_replaceAt(new OString("Java desktop system"),
477 new OString("Sun "), new OString("Java desktop system"), -4, 8 );
478 CPPUNIT_ASSERT_MESSAGE("navigate index", bRes
== sal_True
);
483 sal_Bool bRes
= check_replaceAt(new OString("Sun Jesktop System"),
484 new OString("Sun Java Desktop System"), new OString(""), 5, 5 );
485 CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes
== sal_True
);
488 CPPUNIT_TEST_SUITE(replaceAt
);
489 CPPUNIT_TEST(replaceAt_001
);
490 CPPUNIT_TEST(replaceAt_002
);
491 CPPUNIT_TEST(replaceAt_003
);
492 CPPUNIT_TEST(replaceAt_004
);
493 CPPUNIT_TEST(replaceAt_005
);
494 CPPUNIT_TEST_SUITE_END();
495 }; // class replaceAt
497 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::valueOf
);
498 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::toDouble
);
499 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::getToken
);
500 CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::replaceAt
);
502 } // namespace rtl_OString
504 // this macro creates an empty function, which will called by the RegisterAllFunctions()
505 // to let the user the possibility to also register some functions by hand.
506 CPPUNIT_PLUGIN_IMPLEMENT();
508 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */