summaryrefslogtreecommitdiff
path: root/sal/qa/rtl/ostring/rtl_OString2.cxx
blob: 1e22a96b5e485a7fc8eaec4f71e657af069b2fb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <sal/types.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/plugin/TestPlugIn.h>

#include <rtl/string.hxx>

#include "valueequal.hxx"

namespace rtl_OString
{

class valueOf : public CppUnit::TestFixture
{
    void valueOf_float_test_impl(float _nValue)
        {
            rtl::OString sValue;
            sValue = rtl::OString::valueOf( _nValue );
            printf("nFloat := %.9f  sValue := %s\n", _nValue, sValue.getStr());

            float nValueATOF = static_cast<float>(atof( sValue.getStr() ));

            bool bEqualResult = is_float_equal(_nValue, nValueATOF);
            CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
        }

    void valueOf_float_test(float _nValue)
        {
            valueOf_float_test_impl(_nValue);

            // test also the negative part.
            float nNegativeValue = -_nValue;
            valueOf_float_test_impl(nNegativeValue);
        }

public:
    // insert your test code here.
    void valueOf_float_test_001()
    {
        // this is demonstration code
        // CPPUNIT_ASSERT_MESSAGE("a message", 1 == 1);
        float nValue = 3.0f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_002()
    {
        float nValue = 3.5f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_003()
    {
        float nValue = 3.0625f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_004()
    {
        float nValue = 3.502525f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_005()
    {
        float nValue = 3.141592f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_006()
    {
        float nValue = 3.5025255f;
        valueOf_float_test(nValue);
    }

    void valueOf_float_test_007()
    {
        float nValue = 3.0039062f;
        valueOf_float_test(nValue);
    }

private:

    void valueOf_double_test_impl(double _nValue)
        {
            rtl::OString sValue;
            sValue = rtl::OString::valueOf( _nValue );
            printf("nDouble := %.20f  sValue := %s\n", _nValue, sValue.getStr());

            double nValueATOF = atof( sValue.getStr() );

            bool bEqualResult = is_double_equal(_nValue, nValueATOF);
            CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
        }

    void valueOf_double_test(double _nValue)
        {
            valueOf_double_test_impl(_nValue);

            // test also the negative part.
            double nNegativeValue = -_nValue;
            valueOf_double_test_impl(nNegativeValue);
        }
public:

    // valueOf double
    void valueOf_double_test_001()
        {
            double nValue = 3.0;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_002()
        {
            double nValue = 3.5;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_003()
        {
            double nValue = 3.0625;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_004()
        {
            double nValue = 3.1415926535;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_005()
        {
            double nValue = 3.141592653589793;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_006()
        {
            double nValue = 3.1415926535897932;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_007()
        {
            double nValue = 3.14159265358979323;
            valueOf_double_test(nValue);
        }
    void valueOf_double_test_008()
        {
            double nValue = 3.141592653589793238462643;
            valueOf_double_test(nValue);
        }

    // Change the following lines only, if you add, remove or rename
    // member functions of the current class,
    // because these macros are need by auto register mechanism.

    CPPUNIT_TEST_SUITE(valueOf);
    CPPUNIT_TEST(valueOf_float_test_001);
    CPPUNIT_TEST(valueOf_float_test_002);
    CPPUNIT_TEST(valueOf_float_test_003);
    CPPUNIT_TEST(valueOf_float_test_004);
    CPPUNIT_TEST(valueOf_float_test_005);
    CPPUNIT_TEST(valueOf_float_test_006);
    CPPUNIT_TEST(valueOf_float_test_007);

    CPPUNIT_TEST(valueOf_double_test_001);
    CPPUNIT_TEST(valueOf_double_test_002);
    CPPUNIT_TEST(valueOf_double_test_003);
    CPPUNIT_TEST(valueOf_double_test_004);
    CPPUNIT_TEST(valueOf_double_test_005);
    CPPUNIT_TEST(valueOf_double_test_006);
    CPPUNIT_TEST(valueOf_double_test_007);
    CPPUNIT_TEST(valueOf_double_test_008);
    CPPUNIT_TEST_SUITE_END();
}; // class valueOf

// - toDouble (tests)

class toDouble : public CppUnit::TestFixture
{

public:

    toDouble()
        {
            // testPrecision a;
        }

    void toDouble_test_impl(rtl::OString const& _sValue)
        {
            double nValueATOF = atof( _sValue.getStr() );

            // rtl::OUString suValue = rtl::OUString::createFromAscii( _sValue.getStr() );
            double nValueToDouble = _sValue.toDouble();

            bool bEqualResult = is_double_equal(nValueToDouble, nValueATOF);
            CPPUNIT_ASSERT_MESSAGE("Values are not equal.", bEqualResult == true);
        }

    void toDouble_test(rtl::OString const& _sValue)
        {
            toDouble_test_impl(_sValue);

            // test also the negative part.
            rtl::OString sNegativValue("-");
            sNegativValue += _sValue;
            toDouble_test_impl(sNegativValue);
        }

    // insert your test code here.
    void toDouble_selftest()
        {
            printf("Start selftest:\n");
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.01) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.0001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.00001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000001) == false);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000001) == false);
            // we check til 14 values after comma
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.00000000000001) == true);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.000000000000001) == true);
            CPPUNIT_ASSERT (is_double_equal(1.0, 1.0000000000000001) == true);
            printf("Selftest done.\n");
        }

    void toDouble_test_3()
        {
            rtl::OString sValue("3");
            toDouble_test(sValue);
        }
    void toDouble_test_3_5()
        {
            rtl::OString sValue("3.5");
            toDouble_test(sValue);
        }
    void toDouble_test_3_0625()
        {
            rtl::OString sValue("3.0625");
            toDouble_test(sValue);
        }
    void toDouble_test_pi()
        {
            // value from http://www.angio.net/pi/digits/50.txt
            rtl::OString sValue("3.141592653589793238462643383279502884197169399375");
            toDouble_test(sValue);
        }

    void toDouble_test_1()
        {
            rtl::OString sValue("1");
            toDouble_test(sValue);
        }
    void toDouble_test_10()
        {
            rtl::OString sValue("10");
            toDouble_test(sValue);
        }
    void toDouble_test_100()
        {
            rtl::OString sValue("100");
            toDouble_test(sValue);
        }
    void toDouble_test_1000()
        {
            rtl::OString sValue("1000");
            toDouble_test(sValue);
        }
    void toDouble_test_10000()
        {
            rtl::OString sValue("10000");
            toDouble_test(sValue);
        }
    void toDouble_test_1e99()
        {
            rtl::OString sValue("1e99");
            toDouble_test(sValue);
        }
    void toDouble_test_1e_n99()
        {
            rtl::OString sValue("1e-99");
            toDouble_test(sValue);
        }
    void toDouble_test_1e308()
        {
            rtl::OString sValue("1e308");
            toDouble_test(sValue);
        }

    // Change the following lines only, if you add, remove or rename
    // member functions of the current class,
    // because these macros are need by auto register mechanism.

    CPPUNIT_TEST_SUITE(toDouble);
    CPPUNIT_TEST(toDouble_selftest);

    CPPUNIT_TEST(toDouble_test_3);
    CPPUNIT_TEST(toDouble_test_3_5);
    CPPUNIT_TEST(toDouble_test_3_0625);
    CPPUNIT_TEST(toDouble_test_pi);
    CPPUNIT_TEST(toDouble_test_1);
    CPPUNIT_TEST(toDouble_test_10);
    CPPUNIT_TEST(toDouble_test_100);
    CPPUNIT_TEST(toDouble_test_1000);
    CPPUNIT_TEST(toDouble_test_10000);
    CPPUNIT_TEST(toDouble_test_1e99);
    CPPUNIT_TEST(toDouble_test_1e_n99);
    CPPUNIT_TEST(toDouble_test_1e308);
    CPPUNIT_TEST_SUITE_END();
}; // class toDouble

// - getToken (tests)

class getToken : public CppUnit::TestFixture
{

public:
    void getToken_000()
        {
            rtl::OString sTokenStr;

            sal_Int32 nIndex = 0;
            do
            {
                rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
            }
            while ( nIndex >= 0 );
            // printf("Index %d\n", nIndex);
            // should not GPF
        }

    void getToken_001()
        {
            rtl::OString sTokenStr = "a;b";

            sal_Int32 nIndex = 0;

            rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
            CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
        }

    void getToken_002()
        {
            rtl::OString sTokenStr = "a;b.c";

            sal_Int32 nIndex = 0;

            rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'c'", sToken.equals("c") == sal_True);
            CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
        }

    void getToken_003()
        {
            rtl::OString sTokenStr = "a;;b";

            sal_Int32 nIndex = 0;

            rtl::OString sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'a'", sToken.equals("a") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.isEmpty());

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, ';', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be a 'b'", sToken.equals("b") == sal_True);
            CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
        }

    void getToken_004()
        {
            rtl::OString sTokenStr = "longer.then.ever.";

            sal_Int32 nIndex = 0;

            rtl::OString sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be 'longer'", sToken.equals("longer") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be 'then'", sToken.equals("then") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be 'ever'", sToken.equals("ever") == sal_True);

            /* rtl::OString */ sToken = sTokenStr.getToken( 0, '.', nIndex );
            CPPUNIT_ASSERT_MESSAGE("Token should be empty", sToken.isEmpty());

            CPPUNIT_ASSERT_MESSAGE("index should be negative", nIndex == -1);
        }

    CPPUNIT_TEST_SUITE(getToken);
    CPPUNIT_TEST(getToken_000);
    CPPUNIT_TEST(getToken_001);
    CPPUNIT_TEST(getToken_002);
    CPPUNIT_TEST(getToken_003);
    CPPUNIT_TEST(getToken_004);
    CPPUNIT_TEST_SUITE_END();
}; // class getToken

// testing the method replaceAt( sal_Int32 index, sal_Int32 count,
// const OString& newStr )

// Developer note: Mindy Liu, 2004-04-23
// stolen from sal/qa/rtl_strings/rtl_OString.cxx

class replaceAt : public CppUnit::TestFixture
{

public:
    sal_Bool check_replaceAt( const rtl::OString* expVal, const rtl::OString* input,
        const rtl::OString* newStr, sal_Int32  index, sal_Int32 count)
    {
         ::rtl::OString aStr1;
        aStr1= input->replaceAt( index, count, *newStr );

        printf("the result OString is %s#\n", aStr1.getStr() );

        sal_Bool bRes = ( expVal->compareTo(aStr1) == 0 );
        return bRes;
    }

   void replaceAt_001()
        {
        sal_Bool bRes = check_replaceAt(new rtl::OString("Java@Sun"),
            new rtl::OString("Sun java"), new rtl::OString("Java@Sun"), 0, 8 );
            CPPUNIT_ASSERT_MESSAGE("string differs, replace whole string", bRes == sal_True);
        }

    void replaceAt_002()
        {
            sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Java desktop system"),
            new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 10, 8 );
            CPPUNIT_ASSERT_MESSAGE("index > length of input string", bRes == sal_True);
        }

    void replaceAt_003()
        {
            sal_Bool bRes = check_replaceAt(new rtl::OString("SuJava desktop system"),
            new rtl::OString("Sun "), new rtl::OString("Java desktop system"), 2, 64 );
            CPPUNIT_ASSERT_MESSAGE("larger count", bRes == sal_True);
        }

    void replaceAt_004()
        {

            sal_Bool bRes = check_replaceAt(new rtl::OString("Java desktop system"),
            new rtl::OString("Sun "), new rtl::OString("Java desktop system"), -4, 8 );
            CPPUNIT_ASSERT_MESSAGE("navigate index", bRes == sal_True);
        }
    void replaceAt_005()
        {

            sal_Bool bRes = check_replaceAt(new rtl::OString("Sun Jesktop System"),
            new rtl::OString("Sun Java Desktop System"), new rtl::OString(""), 5, 5 );
            CPPUNIT_ASSERT_MESSAGE("replace with null string", bRes == sal_True);
        }

    CPPUNIT_TEST_SUITE(replaceAt);
    CPPUNIT_TEST(replaceAt_001);
    CPPUNIT_TEST(replaceAt_002);
    CPPUNIT_TEST(replaceAt_003);
    CPPUNIT_TEST(replaceAt_004);
    CPPUNIT_TEST(replaceAt_005);
    CPPUNIT_TEST_SUITE_END();
}; // class replaceAt

CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::valueOf);
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::toDouble);
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::getToken);
CPPUNIT_TEST_SUITE_REGISTRATION(rtl_OString::replaceAt);

} // namespace rtl_OString

// this macro creates an empty function, which will called by the RegisterAllFunctions()
// to let the user the possibility to also register some functions by hand.
CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */