summaryrefslogtreecommitdiff
path: root/comphelper/qa/string/test_string.cxx
blob: 21d84c9142fe428f6fa1934b5115d0a39633ca4c (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#include "precompiled_comphelper.hxx"
#include "sal/cppunit.h"

#include <comphelper/string.hxx>
#include <cppuhelper/implbase1.hxx>
#include <com/sun/star/i18n/CharType.hpp>

#include "rtl/string.hxx"
#include "rtl/ustring.hxx"

namespace {

class TestString: public CppUnit::TestFixture
{
public:
    void testSearchAndReplaceAsciiL();
    void testNatural();
    void testReplace();
    void testToken();
    void testDecimalStringToNumber();
    void testIsdigitAsciiString();
    void testIsalnumAsciiString();
    void testIsupperAsciiString();

    CPPUNIT_TEST_SUITE(TestString);
    CPPUNIT_TEST(testSearchAndReplaceAsciiL);
    CPPUNIT_TEST(testNatural);
    CPPUNIT_TEST(testReplace);
    CPPUNIT_TEST(testToken);
    CPPUNIT_TEST(testDecimalStringToNumber);
    CPPUNIT_TEST(testIsdigitAsciiString);
    CPPUNIT_TEST(testIsalnumAsciiString);
    CPPUNIT_TEST(testIsupperAsciiString);
    CPPUNIT_TEST_SUITE_END();
};

void TestString::testSearchAndReplaceAsciiL()
{
    rtl::OUString s1(RTL_CONSTASCII_USTRINGPARAM("foobarbar"));
    sal_Int32 n1;
    rtl::OUString s2(
        comphelper::string::searchAndReplaceAsciiL(
            s1, RTL_CONSTASCII_STRINGPARAM("bar"),
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baaz")), 0, &n1));
    CPPUNIT_ASSERT(
        s2 == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobaazbar")));
    CPPUNIT_ASSERT(n1 == 3);
    sal_Int32 n2;
    rtl::OUString s3(
        comphelper::string::searchAndReplaceAsciiL(
            s2, RTL_CONSTASCII_STRINGPARAM("bar"),
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bz")),
            n1 + RTL_CONSTASCII_LENGTH("baaz"), &n2));
    CPPUNIT_ASSERT(
        s3 == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobaazbz")));
    CPPUNIT_ASSERT(n2 == 7);
    sal_Int32 n3;
    rtl::OUString s4(
        comphelper::string::searchAndReplaceAsciiL(
            s3, RTL_CONSTASCII_STRINGPARAM("bar"),
            rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baz")),
            n2 + RTL_CONSTASCII_LENGTH("bz"), &n3));
    CPPUNIT_ASSERT(s4 == s3);
    CPPUNIT_ASSERT(n3 == -1);
}

void TestString::testDecimalStringToNumber()
{
    rtl::OUString s1(RTL_CONSTASCII_USTRINGPARAM("1234"));
    CPPUNIT_ASSERT_EQUAL((sal_uInt32)1234, comphelper::string::decimalStringToNumber(s1));
    s1 += rtl::OUString(static_cast<sal_Unicode>(0x07C6));
    CPPUNIT_ASSERT_EQUAL((sal_uInt32)12346, comphelper::string::decimalStringToNumber(s1));
    // Codepoints on 2 16bits words
    sal_uInt32 utf16String[] = { 0x1D7FE /* 8 */, 0x1D7F7 /* 1 */};
    s1 = rtl::OUString(utf16String, 2);
    CPPUNIT_ASSERT_EQUAL((sal_uInt32)81, comphelper::string::decimalStringToNumber(s1));
}

void TestString::testIsdigitAsciiString()
{
    rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("1234"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s1), true);

    rtl::OString s2(RTL_CONSTASCII_STRINGPARAM("1A34"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s2), false);

    rtl::OString s3;
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3), true);
}

void TestString::testIsalnumAsciiString()
{
    rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("1234"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s1), true);

    rtl::OString s2(RTL_CONSTASCII_STRINGPARAM("1A34"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s2), true);

    rtl::OString s3;
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s3), true);

    rtl::OString s4(RTL_CONSTASCII_STRINGPARAM("1A[4"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isalnumAsciiString(s4), false);
}

void TestString::testIsupperAsciiString()
{
    rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("1234"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isupperAsciiString(s1), false);

    rtl::OString s2(RTL_CONSTASCII_STRINGPARAM("aAbB"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isupperAsciiString(s2), false);

    rtl::OString s3(RTL_CONSTASCII_STRINGPARAM("AABB"));
    CPPUNIT_ASSERT_EQUAL(comphelper::string::isupperAsciiString(s3), true);
}

using namespace ::com::sun::star;

class testCollator : public cppu::WeakImplHelper1< i18n::XCollator >
{
public:
    virtual sal_Int32 SAL_CALL compareSubstring(
        const rtl::OUString& str1, sal_Int32 off1, sal_Int32 len1,
        const rtl::OUString& str2, sal_Int32 off2, sal_Int32 len2) throw(uno::RuntimeException)
    {
        return str1.copy(off1, len1).compareTo(str2.copy(off2, len2));
    }
    virtual sal_Int32 SAL_CALL compareString(
        const rtl::OUString& str1,
        const rtl::OUString& str2) throw(uno::RuntimeException)
    {
        return str1.compareTo(str2);
    }
    virtual sal_Int32 SAL_CALL loadDefaultCollator(const lang::Locale&, sal_Int32)
        throw(uno::RuntimeException) {return 0;}
    virtual sal_Int32 SAL_CALL loadCollatorAlgorithm(const rtl::OUString&,
        const lang::Locale&, sal_Int32) throw(uno::RuntimeException) {return 0;}
    virtual void SAL_CALL loadCollatorAlgorithmWithEndUserOption(const rtl::OUString&,
        const lang::Locale&, const uno::Sequence< sal_Int32 >&) throw(uno::RuntimeException) {}
    virtual uno::Sequence< rtl::OUString > SAL_CALL listCollatorAlgorithms(const lang::Locale&)
        throw(uno::RuntimeException)
    {
        return uno::Sequence< rtl::OUString >();
    }
    virtual uno::Sequence< sal_Int32 > SAL_CALL listCollatorOptions(const rtl::OUString&)
        throw(uno::RuntimeException)
    {
        return uno::Sequence< sal_Int32 >();
    }
};

#define IS_DIGIT(CHAR) (((CHAR) >= 48) && ((CHAR <= 57)))

class testBreakIterator : public cppu::WeakImplHelper1< i18n::XBreakIterator >
{
public:
    virtual sal_Int32 SAL_CALL nextCharacters( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& )
            throw(uno::RuntimeException) {return -1;}
    virtual sal_Int32 SAL_CALL previousCharacters( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16, sal_Int32, sal_Int32& )
            throw(uno::RuntimeException) {return -1;}

    virtual i18n::Boundary SAL_CALL previousWord( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16) throw(uno::RuntimeException)
        { return i18n::Boundary(); }
    virtual i18n::Boundary SAL_CALL nextWord( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16) throw(uno::RuntimeException)
        { return i18n::Boundary(); }
    virtual i18n::Boundary SAL_CALL getWordBoundary( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16, sal_Bool )
        throw(uno::RuntimeException)
        { return i18n::Boundary(); }

    virtual sal_Bool SAL_CALL isBeginWord( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException)
        { return false; }
    virtual sal_Bool SAL_CALL isEndWord( const rtl::OUString&, sal_Int32,
        const lang::Locale& , sal_Int16 ) throw(uno::RuntimeException)
        { return false; }
    virtual sal_Int16 SAL_CALL getWordType( const rtl::OUString&, sal_Int32,
        const lang::Locale& ) throw(uno::RuntimeException)
        { return 0; }

    virtual sal_Int32 SAL_CALL beginOfSentence( const rtl::OUString&, sal_Int32,
        const lang::Locale& ) throw(uno::RuntimeException)
        { return 0; }
    virtual sal_Int32 SAL_CALL endOfSentence( const rtl::OUString& rText, sal_Int32,
        const lang::Locale& ) throw(uno::RuntimeException)
        { return rText.getLength(); }

    virtual i18n::LineBreakResults SAL_CALL getLineBreak( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int32,
        const i18n::LineBreakHyphenationOptions&,
        const i18n::LineBreakUserOptions&)
        throw(uno::RuntimeException)
    {
        return i18n::LineBreakResults();
    }

    virtual sal_Int16 SAL_CALL getScriptType( const rtl::OUString&, sal_Int32 )
        throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL beginOfScript( const rtl::OUString&, sal_Int32,
        sal_Int16 ) throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL endOfScript( const rtl::OUString&, sal_Int32,
        sal_Int16 ) throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL previousScript( const rtl::OUString&, sal_Int32,
        sal_Int16 ) throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL nextScript( const rtl::OUString&, sal_Int32,
        sal_Int16 ) throw(uno::RuntimeException) { return -1; }

    virtual sal_Int32 SAL_CALL beginOfCharBlock( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL endOfCharBlock( const rtl::OUString& rText, sal_Int32 nStartPos,
        const lang::Locale&, sal_Int16 CharType ) throw(uno::RuntimeException)
    {
        const sal_Unicode *pStr = rText.getStr()+nStartPos;
        for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
        {
            if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
                return nI;
            else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
                return nI;
            ++pStr;
        }
        return -1;
    }
    virtual sal_Int32 SAL_CALL previousCharBlock( const rtl::OUString&, sal_Int32,
        const lang::Locale&, sal_Int16 ) throw(uno::RuntimeException) { return -1; }
    virtual sal_Int32 SAL_CALL nextCharBlock( const rtl::OUString& rText, sal_Int32 nStartPos,
        const lang::Locale&, sal_Int16 CharType ) throw(uno::RuntimeException)
    {
        const sal_Unicode *pStr = rText.getStr()+nStartPos;
        for (sal_Int32 nI = nStartPos; nI < rText.getLength(); ++nI)
        {
            if (CharType == i18n::CharType::DECIMAL_DIGIT_NUMBER && IS_DIGIT(*pStr))
                return nI;
            else if (CharType != i18n::CharType::DECIMAL_DIGIT_NUMBER && !IS_DIGIT(*pStr))
                return nI;
            ++pStr;
        }
        return -1;
    }
};

void TestString::testNatural()
{
    using namespace comphelper::string;

    uno::Reference< i18n::XCollator > xCollator(new testCollator);
    uno::Reference< i18n::XBreakIterator > xBI(new testBreakIterator);

// --- Some generic tests to ensure we do not alter original behavior
// outside what we want
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), xCollator, xBI, lang::Locale()) == 0
    );
    // Case sensitivity
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc"))), xCollator, xBI, lang::Locale()) < 0
    );
    // Reverse
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("ABC"))), xCollator, xBI, lang::Locale()) > 0
    );
    // First shorter
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongstring"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongerstring"))), xCollator, xBI, lang::Locale()) > 0
    );
    // Second shorter
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongerstring"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("alongstring"))), xCollator, xBI, lang::Locale()) < 0
    );
// -- Here we go on natural order, each one is followed by classic compare and the reverse comparison
    // That's why we originally made the patch
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10"))), xCollator, xBI, lang::Locale()) < 0
    );
    // Original behavior
    CPPUNIT_ASSERT(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10")))) > 0
    );
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 10"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("Heading 9"))), xCollator, xBI, lang::Locale()) > 0
    );
    // Harder
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th"))), xCollator, xBI, lang::Locale()) < 0
    );
    CPPUNIT_ASSERT(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th")))) > 0
    );
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 10th"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("July, the 4th"))), xCollator, xBI, lang::Locale()) > 0
    );
    // Hardest
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010"))), xCollator, xBI, lang::Locale()) < 0
    );
    CPPUNIT_ASSERT(
        rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08"))).compareTo(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010")))) > 0
    );
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc010"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("abc08"))), xCollator, xBI, lang::Locale()) > 0
    );
    CPPUNIT_ASSERT(
        compareNatural(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("apple10apple"))), rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(("apple10apple"))), xCollator, xBI, lang::Locale()) == 0
    );
}

void TestString::testReplace()
{
    ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("aaa"));
    ::rtl::OString aOut;

    aOut = ::comphelper::string::replace(aIn,
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("aa")),
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("b")));
    CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ba")));

    aOut = ::comphelper::string::replace(aIn,
        rtl::OString(),
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("whatever")));
    CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("aaa")));

    aOut = ::comphelper::string::replace(aIn,
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")),
        rtl::OString());
    CPPUNIT_ASSERT(aOut.isEmpty());

    aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa foo aaa foo bbb"));

    aOut = ::comphelper::string::replace(aIn,
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("foo")),
        rtl::OString(RTL_CONSTASCII_STRINGPARAM("bar")));
    CPPUNIT_ASSERT(aOut.equalsL(
        RTL_CONSTASCII_STRINGPARAM("aaa bar aaa bar bbb")));

    aOut = ::comphelper::string::replace(aIn,
        rtl::OString(' '),
        rtl::OString());
    CPPUNIT_ASSERT(aOut.equalsL(
        RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb")));
}

void TestString::testToken()
{
    ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12"));
    ::rtl::OString aOut;

    aOut = ::comphelper::string::getToken(aIn, -1, '.');
    CPPUNIT_ASSERT(aOut.isEmpty());

    aOut = ::comphelper::string::getToken(aIn, 0, '.');
    CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("10")));

    aOut = ::comphelper::string::getToken(aIn, 1, '.');
    CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("11")));

    aOut = ::comphelper::string::getToken(aIn, 2, '.');
    CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("12")));

    aOut = ::comphelper::string::getToken(aIn, 3, '.');
    CPPUNIT_ASSERT(aOut.isEmpty());
}

CPPUNIT_TEST_SUITE_REGISTRATION(TestString);

}

CPPUNIT_PLUGIN_IMPLEMENT();

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