xref: /aoo41x/main/sal/qa/rtl/ostring/rtl_OString2.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_sal.hxx"
31 // autogenerated file with codegen.pl
32 // There exist some more test code in sal/qa/rtl_strings/rtl_OString.cxx
33 
34 #include <testshl/simpleheader.hxx>
35 #include "valueequal.hxx"
36 
37 namespace rtl_OString
38 {
39 
40 class valueOf : public CppUnit::TestFixture
41 {
42     void valueOf_float_test_impl(float _nValue)
43         {
44             rtl::OString sValue;
45             sValue = rtl::OString::valueOf( _nValue );
46             t_print(T_VERBOSE, "nFloat := %.9f  sValue := %s\n", _nValue, sValue.getStr());
47 
48             float nValueATOF = static_cast<float>(atof( sValue.getStr() ));
49 
50             bool bEqualResult = is_float_equal(_nValue, nValueATOF);
51             CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
52         }
53 
54     void valueOf_float_test(float _nValue)
55         {
56             valueOf_float_test_impl(_nValue);
57 
58             // test also the negative part.
59             float nNegativeValue = -_nValue;
60             valueOf_float_test_impl(nNegativeValue);
61         }
62 
63 public:
64     // initialise your test code values here.
65     void setUp()
66     {
67     }
68 
69     void tearDown()
70     {
71     }
72 
73     // insert your test code here.
74     void valueOf_float_test_001()
75     {
76         // this is demonstration code
77         // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
78         float nValue = 3.0f;
79         valueOf_float_test(nValue);
80     }
81 
82     void valueOf_float_test_002()
83     {
84         float nValue = 3.5f;
85         valueOf_float_test(nValue);
86     }
87 
88     void valueOf_float_test_003()
89     {
90         float nValue = 3.0625f;
91         valueOf_float_test(nValue);
92     }
93 
94     void valueOf_float_test_004()
95     {
96         float nValue = 3.502525f;
97         valueOf_float_test(nValue);
98     }
99 
100     void valueOf_float_test_005()
101     {
102         float nValue = 3.141592f;
103         valueOf_float_test(nValue);
104     }
105 
106     void valueOf_float_test_006()
107     {
108         float nValue = 3.5025255f;
109         valueOf_float_test(nValue);
110     }
111 
112     void valueOf_float_test_007()
113     {
114         float nValue = 3.0039062f;
115         valueOf_float_test(nValue);
116     }
117 
118 private:
119 
120     void valueOf_double_test_impl(double _nValue)
121         {
122             rtl::OString sValue;
123             sValue = rtl::OString::valueOf( _nValue );
124             t_print(T_VERBOSE, "nDouble := %.20f  sValue := %s\n", _nValue, sValue.getStr());
125 
126             double nValueATOF = atof( sValue.getStr() );
127 
128             bool bEqualResult = is_double_equal(_nValue, nValueATOF);
129             CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
130         }
131 
132     void valueOf_double_test(double _nValue)
133         {
134             valueOf_double_test_impl(_nValue);
135 
136             // test also the negative part.
137             double nNegativeValue = -_nValue;
138             valueOf_double_test_impl(nNegativeValue);
139         }
140 public:
141 
142     // valueOf double
143     void valueOf_double_test_001()
144         {
145             double nValue = 3.0;
146             valueOf_double_test(nValue);
147         }
148     void valueOf_double_test_002()
149         {
150             double nValue = 3.5;
151             valueOf_double_test(nValue);
152         }
153     void valueOf_double_test_003()
154         {
155             double nValue = 3.0625;
156             valueOf_double_test(nValue);
157         }
158     void valueOf_double_test_004()
159         {
160             double nValue = 3.1415926535;
161             valueOf_double_test(nValue);
162         }
163     void valueOf_double_test_005()
164         {
165             double nValue = 3.141592653589793;
166             valueOf_double_test(nValue);
167         }
168     void valueOf_double_test_006()
169         {
170             double nValue = 3.1415926535897932;
171             valueOf_double_test(nValue);
172         }
173     void valueOf_double_test_007()
174         {
175             double nValue = 3.14159265358979323;
176             valueOf_double_test(nValue);
177         }
178     void valueOf_double_test_008()
179         {
180             double nValue = 3.141592653589793238462643;
181             valueOf_double_test(nValue);
182         }
183 
184 
185     // Change the following lines only, if you add, remove or rename
186     // member functions of the current class,
187     // because these macros are need by auto register mechanism.
188 
189     CPPUNIT_TEST_SUITE(valueOf);
190     CPPUNIT_TEST(valueOf_float_test_001);
191     CPPUNIT_TEST(valueOf_float_test_002);
192     CPPUNIT_TEST(valueOf_float_test_003);
193     CPPUNIT_TEST(valueOf_float_test_004);
194     CPPUNIT_TEST(valueOf_float_test_005);
195     CPPUNIT_TEST(valueOf_float_test_006);
196     CPPUNIT_TEST(valueOf_float_test_007);
197 
198     CPPUNIT_TEST(valueOf_double_test_001);
199     CPPUNIT_TEST(valueOf_double_test_002);
200     CPPUNIT_TEST(valueOf_double_test_003);
201     CPPUNIT_TEST(valueOf_double_test_004);
202     CPPUNIT_TEST(valueOf_double_test_005);
203     CPPUNIT_TEST(valueOf_double_test_006);
204     CPPUNIT_TEST(valueOf_double_test_007);
205     CPPUNIT_TEST(valueOf_double_test_008);
206     CPPUNIT_TEST_SUITE_END();
207 }; // class valueOf
208 
209 // -----------------------------------------------------------------------------
210 // - toDouble (tests)
211 // -----------------------------------------------------------------------------
212 class toDouble : public CppUnit::TestFixture
213 {
214 
215 public:
216 
217     toDouble()
218         {
219             // testPrecision a;
220         }
221 
222 
223 
224     // initialise your test code values here.
225     void setUp()
226         {
227         }
228 
229     void tearDown()
230         {
231         }
232 
233     void toDouble_test_impl(rtl::OString const& _sValue)
234         {
235             double nValueATOF = atof( _sValue.getStr() );
236 
237             // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
238             double nValueToDouble = _sValue.toDouble();
239 
240             bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
241             CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
242         }
243 
244     void toDouble_test(rtl::OString const& _sValue)
245         {
246             toDouble_test_impl(_sValue);
247 
248             // test also the negativ part.
249             rtl::OString sNegativValue("-");
250             sNegativValue += _sValue;
251             toDouble_test_impl(sNegativValue);
252         }
253 
254     // insert your test code here.
255     void toDouble_selftest()
256         {
257             t_print("Start selftest:\n");
258             CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
259             CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
260             CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
261             CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
262             CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
263             CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
264             CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
265             CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
266             CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
267             CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
268             CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
269             CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
270             // we check til 14 values after comma
271             CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
272             CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
273             CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
274             t_print("Selftest done.\n");
275         }
276 
277     void toDouble_test_3()
278         {
279             rtl::OString sValue("3");
280             toDouble_test(sValue);
281         }
282     void toDouble_test_3_5()
283         {
284             rtl::OString sValue("3.5");
285             toDouble_test(sValue);
286         }
287     void toDouble_test_3_0625()
288         {
289             rtl::OString sValue("3.0625");
290             toDouble_test(sValue);
291         }
292     void toDouble_test_pi()
293         {
294             // value from http://www.angio.net/pi/digits/50.txt
295             rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
296             toDouble_test(sValue);
297         }
298 
299     void toDouble_test_1()
300         {
301             rtl::OString sValue("1");
302             toDouble_test(sValue);
303         }
304     void toDouble_test_10()
305         {
306             rtl::OString sValue("10");
307             toDouble_test(sValue);
308         }
309     void toDouble_test_100()
310         {
311             rtl::OString sValue("100");
312             toDouble_test(sValue);
313         }
314     void toDouble_test_1000()
315         {
316             rtl::OString sValue("1000");
317             toDouble_test(sValue);
318         }
319     void toDouble_test_10000()
320         {
321             rtl::OString sValue("10000");
322             toDouble_test(sValue);
323         }
324     void toDouble_test_1e99()
325         {
326             rtl::OString sValue("1e99");
327             toDouble_test(sValue);
328         }
329     void toDouble_test_1e_n99()
330         {
331             rtl::OString sValue("1e-99");
332             toDouble_test(sValue);
333         }
334     void toDouble_test_1e308()
335         {
336             rtl::OString sValue("1e308");
337             toDouble_test(sValue);
338         }
339 
340     // Change the following lines only, if you add, remove or rename
341     // member functions of the current class,
342     // because these macros are need by auto register mechanism.
343 
344     CPPUNIT_TEST_SUITE(toDouble);
345     CPPUNIT_TEST(toDouble_selftest);
346 
347     CPPUNIT_TEST(toDouble_test_3);
348     CPPUNIT_TEST(toDouble_test_3_5);
349     CPPUNIT_TEST(toDouble_test_3_0625);
350     CPPUNIT_TEST(toDouble_test_pi);
351     CPPUNIT_TEST(toDouble_test_1);
352     CPPUNIT_TEST(toDouble_test_10);
353     CPPUNIT_TEST(toDouble_test_100);
354     CPPUNIT_TEST(toDouble_test_1000);
355     CPPUNIT_TEST(toDouble_test_10000);
356     CPPUNIT_TEST(toDouble_test_1e99);
357     CPPUNIT_TEST(toDouble_test_1e_n99);
358     CPPUNIT_TEST(toDouble_test_1e308);
359     CPPUNIT_TEST_SUITE_END();
360 }; // class toDouble
361 
362 // -----------------------------------------------------------------------------
363 // - getToken (tests)
364 // -----------------------------------------------------------------------------
365 class getToken : public CppUnit::TestFixture
366 {
367 
368 public:
369 
370     // initialise your test code values here.
371     void setUp()
372         {
373         }
374 
375     void tearDown()
376         {
377         }
378 
379     // -----------------------------------------------------------------------------
380 
381     void getToken_000()
382         {
383             rtl::OString sTokenStr;
384 
385             sal_Int32 nIndex = 0;
386             do
387             {
388                 rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
389             }
390             while ( nIndex >= 0 );
391             // t_print("Index %d\n", nIndex);
392             // should not GPF
393         }
394 
395     void getToken_001()
396         {
397             rtl::OString sTokenStr = "a;b";
398 
399             sal_Int32 nIndex = 0;
400 
401             rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
402             CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
403 
404             /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
405             CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
406             CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
407         }
408 
409     void getToken_002()
410         {
411             rtl::OString sTokenStr = "a;b.c";
412 
413             sal_Int32 nIndex = 0;
414 
415             rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
416             CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
417 
418             /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
419             CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
420 
421             /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
422             CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken.equals("c") == sal_True);
423             CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
424         }
425 
426     void getToken_003()
427         {
428             rtl::OString sTokenStr = "a;;b";
429 
430             sal_Int32 nIndex = 0;
431 
432             rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
433             CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);
434 
435             /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
436             CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.getLength() == 0);
437 
438             /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
439             CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
440             CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
441         }
442 
443     void getToken_004()
444         {
445             rtl::OString sTokenStr = "longer.then.ever.";
446 
447             sal_Int32 nIndex = 0;
448 
449             rtl::OString sToken = sTokenStr.getToken( 0, '.', nIndex );
450             CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken.equals("longer") == sal_True);
451 
452             /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
453             CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken.equals("then") == sal_True);
454 
455             /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
456             CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken.equals("ever") == sal_True);
457 
458             /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
459             CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.getLength() == 0);
460 
461             CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
462         }
463 
464 
465     CPPUNIT_TEST_SUITE(getToken);
466     CPPUNIT_TEST(getToken_000);
467     CPPUNIT_TEST(getToken_001);
468     CPPUNIT_TEST(getToken_002);
469     CPPUNIT_TEST(getToken_003);
470     CPPUNIT_TEST(getToken_004);
471     CPPUNIT_TEST_SUITE_END();
472 }; // class getToken
473 
474 // -----------------------------------------------------------------------------
475 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
476 // const OString& newStr )
477 // -----------------------------------------------------------------------------
478 
479 // Developer note: Mindy Liu, 2004-04-23
480 // stollen from sal/qa/rtl_strings/rtl_OString.cxx
481 
482 class replaceAt : public CppUnit::TestFixture
483 {
484 
485 public:
486     // initialise your test code values here.
487     void setUp()
488         {
489         }
490 
491     void tearDown()
492         {
493         }
494     sal_Bool check_replaceAt( const rtl::OString* expVal, const rtl::OString* input,
495         const rtl::OString* newStr, sal_Int32  index, sal_Int32 count)
496     {
497     	 ::rtl::OString aStr1;
498         aStr1= input->replaceAt( index, count, *newStr );
499 
500         t_print("the result OString is %s#\n", aStr1.getStr() );
501 
502         sal_Bool bRes = ( expVal->compareTo(aStr1) == 0 );
503         return bRes;
504     }
505     // -----------------------------------------------------------------------------
506 
507    void replaceAt_001()
508         {
509 	    sal_Bool bRes = check_replaceAt(new rtl::OString("Java@Sun"),
510 			new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
511             CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes == sal_True);
512         }
513 
514     void replaceAt_002()
515         {
516             sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Java desktop system"),
517 			new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
518             CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes == sal_True);
519         }
520 
521     void replaceAt_003()
522         {
523             sal_Bool bRes = check_replaceAt(new rtl::OString("SuJava desktop system"),
524 			new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
525             CPPUNIT_ASSERT_MESSAGE("larger count", bRes == sal_True);
526         }
527 
528     void replaceAt_004()
529         {
530 
531             sal_Bool bRes = check_replaceAt(new rtl::OString("Java desktop system"),
532 			new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
533             CPPUNIT_ASSERT_MESSAGE("navigate index", bRes == sal_True);
534         }
535     void replaceAt_005()
536         {
537 
538             sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Jesktop System"),
539 			new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
540             CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes == sal_True);
541         }
542 
543 
544     CPPUNIT_TEST_SUITE(replaceAt);
545     CPPUNIT_TEST(replaceAt_001);
546     CPPUNIT_TEST(replaceAt_002);
547     CPPUNIT_TEST(replaceAt_003);
548     CPPUNIT_TEST(replaceAt_004);
549     CPPUNIT_TEST(replaceAt_005);
550     CPPUNIT_TEST_SUITE_END();
551 }; // class replaceAt
552 
553 
554 // -----------------------------------------------------------------------------
555 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::valueOf, "rtl_OString");
556 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::toDouble, "rtl_OString");
557 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::getToken, "rtl_OString");
558 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(rtl_OString::replaceAt, "rtl_OString");
559 
560 } // namespace rtl_OString
561 
562 
563 // -----------------------------------------------------------------------------
564 
565 // this macro creates an empty function, which will called by the RegisterAllFunctions()
566 // to let the user the possibility to also register some functions by hand.
567 NOADDITIONAL;
568 
569