xref: /aoo42x/main/sal/qa/rtl/ostring/rtl_OString2.cxx (revision b3f397bd)
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 #include <rtl/string.hxx>
30 
31 #include "gtest/gtest.h"
32 #include "valueequal.hxx"
33 
34 namespace rtl_OString
35 {
36 
37 class valueOf : public ::testing::Test
38 {
39 protected:
valueOf_float_test_impl(float _nValue)40     void valueOf_float_test_impl(float _nValue)
41         {
42             rtl::OString sValue;
43             sValue = rtl::OString::valueOf( _nValue );
44             printf("nFloat := %.9f  sValue := %s\n", _nValue, sValue.getStr());
45 
46             float nValueATOF = static_cast<float>(atof( sValue.getStr() ));
47 
48             bool bEqualResult = is_float_equal(_nValue, nValueATOF);
49             ASSERT_TRUE(bEqualResult == true) << "Values are not equal.";
50         }
51 
valueOf_float_test(float _nValue)52     void valueOf_float_test(float _nValue)
53         {
54             valueOf_float_test_impl(_nValue);
55 
56             // test also the negative part.
57             float nNegativeValue = -_nValue;
58             valueOf_float_test_impl(nNegativeValue);
59         }
60 
61 public:
62     // initialise your test code values here.
SetUp()63     void SetUp()
64     {
65     }
66 
TearDown()67     void TearDown()
68     {
69     }
70 
71 protected:
72 
valueOf_double_test_impl(double _nValue)73     void valueOf_double_test_impl(double _nValue)
74         {
75             rtl::OString sValue;
76             sValue = rtl::OString::valueOf( _nValue );
77             printf("nDouble := %.20f  sValue := %s\n", _nValue, sValue.getStr());
78 
79             double nValueATOF = atof( sValue.getStr() );
80 
81             bool bEqualResult = is_double_equal(_nValue, nValueATOF);
82             ASSERT_TRUE(bEqualResult == true) << "Values are not equal.";
83         }
84 
valueOf_double_test(double _nValue)85     void valueOf_double_test(double _nValue)
86         {
87             valueOf_double_test_impl(_nValue);
88 
89             // test also the negative part.
90             double nNegativeValue = -_nValue;
91             valueOf_double_test_impl(nNegativeValue);
92         }
93 }; // class valueOf
94 
TEST_F(valueOf,valueOf_float_test_001)95 TEST_F(valueOf, valueOf_float_test_001)
96 {
97     // this is demonstration code
98     // ASSERT_TRUE(1 == 1) << "a message";
99     float nValue = 3.0f;
100     valueOf_float_test(nValue);
101 }
102 
TEST_F(valueOf,valueOf_float_test_002)103 TEST_F(valueOf, valueOf_float_test_002)
104 {
105     float nValue = 3.5f;
106     valueOf_float_test(nValue);
107 }
108 
TEST_F(valueOf,valueOf_float_test_003)109 TEST_F(valueOf, valueOf_float_test_003)
110 {
111     float nValue = 3.0625f;
112     valueOf_float_test(nValue);
113 }
114 
TEST_F(valueOf,valueOf_float_test_004)115 TEST_F(valueOf, valueOf_float_test_004)
116 {
117     float nValue = 3.502525f;
118     valueOf_float_test(nValue);
119 }
120 
TEST_F(valueOf,valueOf_float_test_005)121 TEST_F(valueOf, valueOf_float_test_005)
122 {
123     float nValue = 3.141592f;
124     valueOf_float_test(nValue);
125 }
126 
TEST_F(valueOf,valueOf_float_test_006)127 TEST_F(valueOf, valueOf_float_test_006)
128 {
129     float nValue = 3.5025255f;
130     valueOf_float_test(nValue);
131 }
132 
TEST_F(valueOf,valueOf_float_test_007)133 TEST_F(valueOf, valueOf_float_test_007)
134 {
135     float nValue = 3.0039062f;
136     valueOf_float_test(nValue);
137 }
138 
TEST_F(valueOf,valueOf_double_test_001)139 TEST_F(valueOf, valueOf_double_test_001)
140 {
141     double nValue = 3.0;
142     valueOf_double_test(nValue);
143 }
TEST_F(valueOf,valueOf_double_test_002)144 TEST_F(valueOf, valueOf_double_test_002)
145 {
146     double nValue = 3.5;
147     valueOf_double_test(nValue);
148 }
TEST_F(valueOf,valueOf_double_test_003)149 TEST_F(valueOf, valueOf_double_test_003)
150 {
151     double nValue = 3.0625;
152     valueOf_double_test(nValue);
153 }
TEST_F(valueOf,valueOf_double_test_004)154 TEST_F(valueOf, valueOf_double_test_004)
155 {
156     double nValue = 3.1415926535;
157     valueOf_double_test(nValue);
158 }
TEST_F(valueOf,valueOf_double_test_005)159 TEST_F(valueOf, valueOf_double_test_005)
160 {
161     double nValue = 3.141592653589793;
162     valueOf_double_test(nValue);
163 }
TEST_F(valueOf,valueOf_double_test_006)164 TEST_F(valueOf, valueOf_double_test_006)
165 {
166     double nValue = 3.1415926535897932;
167     valueOf_double_test(nValue);
168 }
TEST_F(valueOf,valueOf_double_test_007)169 TEST_F(valueOf, valueOf_double_test_007)
170 {
171     double nValue = 3.14159265358979323;
172     valueOf_double_test(nValue);
173 }
TEST_F(valueOf,valueOf_double_test_008)174 TEST_F(valueOf, valueOf_double_test_008)
175 {
176     double nValue = 3.141592653589793238462643;
177     valueOf_double_test(nValue);
178 }
179 
180 
181 // -----------------------------------------------------------------------------
182 // - toDouble (tests)
183 // -----------------------------------------------------------------------------
184 class toDouble : public ::testing::Test
185 {
186 
187 public:
188 
toDouble()189     toDouble()
190         {
191             // testPrecision a;
192         }
193 
194 
195 
196     // initialise your test code values here.
SetUp()197     void SetUp()
198         {
199         }
200 
TearDown()201     void TearDown()
202 	{
203 	}
204 
toDouble_test_impl(rtl::OString const & _sValue)205     void toDouble_test_impl(rtl::OString const& _sValue)
206         {
207             double nValueATOF = atof( _sValue.getStr() );
208 
209             // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
210             double nValueToDouble = _sValue.toDouble();
211 
212             bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
213             ASSERT_TRUE(bEqualResult == true) << "Values are not equal.";
214         }
215 
toDouble_test(rtl::OString const & _sValue)216     void toDouble_test(rtl::OString const& _sValue)
217         {
218             toDouble_test_impl(_sValue);
219 
220             // test also the negativ part.
221             rtl::OString sNegativValue("-");
222             sNegativValue += _sValue;
223             toDouble_test_impl(sNegativValue);
224         }
225 }; // class toDouble
226 
TEST_F(toDouble,toDouble_selftest)227 TEST_F(toDouble, toDouble_selftest)
228 {
229     printf("Start selftest:\n");
230     ASSERT_TRUE (is_double_equal(1.0, 1.01) == false);
231     ASSERT_TRUE (is_double_equal(1.0, 1.001) == false);
232     ASSERT_TRUE (is_double_equal(1.0, 1.0001) == false);
233     ASSERT_TRUE (is_double_equal(1.0, 1.00001) == false);
234     ASSERT_TRUE (is_double_equal(1.0, 1.000001) == false);
235     ASSERT_TRUE (is_double_equal(1.0, 1.0000001) == false);
236     ASSERT_TRUE (is_double_equal(1.0, 1.00000001) == false);
237     ASSERT_TRUE (is_double_equal(1.0, 1.000000001) == false);
238     ASSERT_TRUE (is_double_equal(1.0, 1.0000000001) == false);
239     ASSERT_TRUE (is_double_equal(1.0, 1.00000000001) == false);
240     ASSERT_TRUE (is_double_equal(1.0, 1.000000000001) == false);
241     ASSERT_TRUE (is_double_equal(1.0, 1.0000000000001) == false);
242     // we check til 14 values after comma
243     ASSERT_TRUE (is_double_equal(1.0, 1.00000000000001) == true);
244     ASSERT_TRUE (is_double_equal(1.0, 1.000000000000001) == true);
245     ASSERT_TRUE (is_double_equal(1.0, 1.0000000000000001) == true);
246     printf("Selftest done.\n");
247 }
248 
TEST_F(toDouble,toDouble_test_3)249 TEST_F(toDouble, toDouble_test_3)
250 {
251     rtl::OString sValue("3");
252     toDouble_test(sValue);
253 }
TEST_F(toDouble,toDouble_test_3_5)254 TEST_F(toDouble, toDouble_test_3_5)
255 {
256     rtl::OString sValue("3.5");
257     toDouble_test(sValue);
258 }
TEST_F(toDouble,toDouble_test_3_0625)259 TEST_F(toDouble, toDouble_test_3_0625)
260 {
261     rtl::OString sValue("3.0625");
262     toDouble_test(sValue);
263 }
TEST_F(toDouble,toDouble_test_pi)264 TEST_F(toDouble, toDouble_test_pi)
265 {
266     // value from http://www.angio.net/pi/digits/50.txt
267     rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
268     toDouble_test(sValue);
269 }
270 
TEST_F(toDouble,toDouble_test_1)271 TEST_F(toDouble, toDouble_test_1)
272 {
273     rtl::OString sValue("1");
274     toDouble_test(sValue);
275 }
TEST_F(toDouble,toDouble_test_10)276 TEST_F(toDouble, toDouble_test_10)
277 {
278     rtl::OString sValue("10");
279     toDouble_test(sValue);
280 }
TEST_F(toDouble,toDouble_test_100)281 TEST_F(toDouble, toDouble_test_100)
282 {
283     rtl::OString sValue("100");
284     toDouble_test(sValue);
285 }
TEST_F(toDouble,toDouble_test_1000)286 TEST_F(toDouble, toDouble_test_1000)
287 {
288     rtl::OString sValue("1000");
289     toDouble_test(sValue);
290 }
TEST_F(toDouble,toDouble_test_10000)291 TEST_F(toDouble, toDouble_test_10000)
292 {
293     rtl::OString sValue("10000");
294     toDouble_test(sValue);
295 }
TEST_F(toDouble,toDouble_test_1e99)296 TEST_F(toDouble, toDouble_test_1e99)
297 {
298     rtl::OString sValue("1e99");
299     toDouble_test(sValue);
300 }
TEST_F(toDouble,toDouble_test_1e_n99)301 TEST_F(toDouble, toDouble_test_1e_n99)
302 {
303     rtl::OString sValue("1e-99");
304     toDouble_test(sValue);
305 }
TEST_F(toDouble,toDouble_test_1e308)306 TEST_F(toDouble, toDouble_test_1e308)
307 {
308     rtl::OString sValue("1e308");
309     toDouble_test(sValue);
310 }
311 
312 // -----------------------------------------------------------------------------
313 // - getToken (tests)
314 // -----------------------------------------------------------------------------
315 class getToken : public ::testing::Test
316 {
317 
318 public:
319 
320     // initialise your test code values here.
SetUp()321     void SetUp()
322         {
323         }
324 
TearDown()325     void TearDown()
326         {
327         }
328 }; // class getToken
329 
330 
TEST_F(getToken,getToken_000)331 TEST_F(getToken, getToken_000)
332 {
333     rtl::OString sTokenStr;
334 
335     sal_Int32 nIndex = 0;
336     do
337     {
338         rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
339     }
340     while ( nIndex >= 0 );
341     // printf("Index %d\n", nIndex);
342     // should not GPF
343 }
344 
TEST_F(getToken,getToken_001)345 TEST_F(getToken, getToken_001)
346 {
347     rtl::OString sTokenStr = "a;b";
348 
349     sal_Int32 nIndex = 0;
350 
351     rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
352     ASSERT_TRUE(sToken.equals("a") == sal_True) << "Token should be a 'a'";
353 
354     /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
355     ASSERT_TRUE(sToken.equals("b") == sal_True) << "Token should be a 'b'";
356     ASSERT_TRUE(nIndex == -1) << "index should be negative";
357 }
358 
TEST_F(getToken,getToken_002)359 TEST_F(getToken, getToken_002)
360 {
361     rtl::OString sTokenStr = "a;b.c";
362 
363     sal_Int32 nIndex = 0;
364 
365     rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
366     ASSERT_TRUE(sToken.equals("a") == sal_True) << "Token should be a 'a'";
367 
368     /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
369     ASSERT_TRUE(sToken.equals("b") == sal_True) << "Token should be a 'b'";
370 
371     /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
372     ASSERT_TRUE(sToken.equals("c") == sal_True) << "Token should be a 'c'";
373     ASSERT_TRUE(nIndex == -1) << "index should be negative";
374 }
375 
TEST_F(getToken,getToken_003)376 TEST_F(getToken, getToken_003)
377 {
378     rtl::OString sTokenStr = "a;;b";
379 
380     sal_Int32 nIndex = 0;
381 
382     rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
383     ASSERT_TRUE(sToken.equals("a") == sal_True) << "Token should be a 'a'";
384 
385     /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
386     ASSERT_TRUE(sToken.getLength() == 0) << "Token should be empty";
387 
388     /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
389     ASSERT_TRUE(sToken.equals("b") == sal_True) << "Token should be a 'b'";
390     ASSERT_TRUE(nIndex == -1) << "index should be negative";
391 }
392 
TEST_F(getToken,getToken_004)393 TEST_F(getToken, getToken_004)
394 {
395     rtl::OString sTokenStr = "longer.then.ever.";
396 
397     sal_Int32 nIndex = 0;
398 
399     rtl::OString sToken = sTokenStr.getToken( 0, '.', nIndex );
400     ASSERT_TRUE(sToken.equals("longer") == sal_True) << "Token should be 'longer'";
401 
402     /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
403     ASSERT_TRUE(sToken.equals("then") == sal_True) << "Token should be 'then'";
404 
405     /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
406     ASSERT_TRUE(sToken.equals("ever") == sal_True) << "Token should be 'ever'";
407 
408     /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
409     ASSERT_TRUE(sToken.getLength() == 0) << "Token should be empty";
410 
411     ASSERT_TRUE(nIndex == -1) << "index should be negative";
412 }
413 
414 // -----------------------------------------------------------------------------
415 // testing the method replaceAt( sal_Int32 index, sal_Int32 count,
416 // const OString& newStr )
417 // -----------------------------------------------------------------------------
418 
419 // Developer note: Mindy Liu, 2004-04-23
420 // stollen from sal/qa/rtl_strings/rtl_OString.cxx
421 
422 class replaceAt : public ::testing::Test
423 {
424 
425 public:
426     // initialise your test code values here.
SetUp()427     void SetUp()
428         {
429         }
430 
TearDown()431     void TearDown()
432         {
433         }
check_replaceAt(const rtl::OString * expVal,const rtl::OString * input,const rtl::OString * newStr,sal_Int32 index,sal_Int32 count)434     sal_Bool check_replaceAt( const rtl::OString* expVal, const rtl::OString* input,
435         const rtl::OString* newStr, sal_Int32  index, sal_Int32 count)
436     {
437     	 ::rtl::OString aStr1;
438         aStr1= input->replaceAt( index, count, *newStr );
439 
440         printf("the result OString is %s#\n", aStr1.getStr() );
441 
442         sal_Bool bRes = ( expVal->compareTo(aStr1) == 0 );
443         return bRes;
444     }
445 }; // class replaceAt
446 
TEST_F(replaceAt,replaceAt_001)447 TEST_F(replaceAt, replaceAt_001)
448 {
449     sal_Bool bRes = check_replaceAt(new rtl::OString("Java@Sun"),
450 		new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
451     ASSERT_TRUE(bRes == sal_True) << "string differs, replace whole string";
452 }
453 
TEST_F(replaceAt,replaceAt_002)454 TEST_F(replaceAt, replaceAt_002)
455 {
456     sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Java desktop system"),
457 		new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
458     ASSERT_TRUE(bRes == sal_True) << "index > length of input string";
459 }
460 
TEST_F(replaceAt,replaceAt_003)461 TEST_F(replaceAt, replaceAt_003)
462 {
463     sal_Bool bRes = check_replaceAt(new rtl::OString("SuJava desktop system"),
464 		new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
465     ASSERT_TRUE(bRes == sal_True) << "larger count";
466 }
467 
TEST_F(replaceAt,replaceAt_004)468 TEST_F(replaceAt, replaceAt_004)
469 {
470 
471     sal_Bool bRes = check_replaceAt(new rtl::OString("Java desktop system"),
472 		new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
473     ASSERT_TRUE(bRes == sal_True) << "navigate index";
474 }
TEST_F(replaceAt,replaceAt_005)475 TEST_F(replaceAt, replaceAt_005)
476 {
477 
478     sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Jesktop System"),
479 		new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
480     ASSERT_TRUE(bRes == sal_True) << "replace with null string";
481 }
482 
483 
484 } // namespace rtl_OString
485 
486 
main(int argc,char ** argv)487 int main(int argc, char **argv)
488 {
489     ::testing::InitGoogleTest(&argc, argv);
490     return RUN_ALL_TESTS();
491 }
492