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