summaryrefslogtreecommitdiffstats
path: root/sal/qa/rtl/strings/test_ostring.cxx
blob: b4fb201cafa3a360d73f3a61712d08b1c079b757 (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
/* -*- 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/.
 */

#include <sal/config.h>

#include <cppunit/TestAssert.h>
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <rtl/string.hxx>

namespace {

class Test: public CppUnit::TestFixture {
private:
    void testStartsWithIgnoreAsciiCase();
    void testCompareTo();
    void testUtf8StringLiterals();

    CPPUNIT_TEST_SUITE(Test);
    CPPUNIT_TEST(testStartsWithIgnoreAsciiCase);
    CPPUNIT_TEST(testCompareTo);
    CPPUNIT_TEST(testUtf8StringLiterals);
    CPPUNIT_TEST_SUITE_END();
};

void Test::testStartsWithIgnoreAsciiCase() {
    {
        OString r;
        CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase(std::string_view(), &r));
        CPPUNIT_ASSERT(r.isEmpty());
    }
    {
        OString r;
        CPPUNIT_ASSERT(OString("foo").startsWithIgnoreAsciiCase(std::string_view(), &r));
        CPPUNIT_ASSERT_EQUAL(OString("foo"), r);
    }
    {
        OString r;
        CPPUNIT_ASSERT(
            OString("foo").startsWithIgnoreAsciiCase("F", &r));
        CPPUNIT_ASSERT_EQUAL(OString("oo"), r);
    }
    {
        OString r("other");
        CPPUNIT_ASSERT(
            !OString("foo").startsWithIgnoreAsciiCase("bar", &r));
        CPPUNIT_ASSERT_EQUAL(OString("other"), r);
    }
    {
        OString r("other");
        CPPUNIT_ASSERT(
            !OString("foo").startsWithIgnoreAsciiCase("foobar", &r));
        CPPUNIT_ASSERT_EQUAL(OString("other"), r);
    }

    {
        OString r;
        CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase("", &r));
        CPPUNIT_ASSERT(r.isEmpty());
    }
    {
        OString r;
        CPPUNIT_ASSERT(OString("foo").startsWithIgnoreAsciiCase("", &r));
        CPPUNIT_ASSERT_EQUAL(OString("foo"), r);
    }
    {
        OString r;
        CPPUNIT_ASSERT(
            OString("foo").startsWithIgnoreAsciiCase("F", &r));
        CPPUNIT_ASSERT_EQUAL(OString("oo"), r);
    }
    {
        OString r("other");
        CPPUNIT_ASSERT(
            !OString("foo").startsWithIgnoreAsciiCase("bar", &r));
        CPPUNIT_ASSERT_EQUAL(OString("other"), r);
    }
    {
        OString r("other");
        CPPUNIT_ASSERT(
            !OString("foo").startsWithIgnoreAsciiCase("foobar", &r));
        CPPUNIT_ASSERT_EQUAL(OString("other"), r);
    }
}

void Test::testCompareTo()
{
    // test that embedded NUL does not stop the compare
    char str1[2] = { '\0', 'x' };
    char str2[2] = { '\0', 'y' };

    OString s1(str1, 2);
    OString s2(str2, 2);
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), s1.compareTo(s1));
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), s2.compareTo(s2));
    CPPUNIT_ASSERT(s1.compareTo(s2) < 0);
    CPPUNIT_ASSERT(s2.compareTo(s1) > 0);
    CPPUNIT_ASSERT(s1.compareTo(OString(s2 + "y")) < 0);
    CPPUNIT_ASSERT(s2.compareTo(OString(s1 + "x")) > 0);
    CPPUNIT_ASSERT(OString(s1 + "x").compareTo(s2) < 0);
    CPPUNIT_ASSERT(OString(s2 + "y").compareTo(s1) > 0);
}

void Test::testUtf8StringLiterals()
{
    const OString sIn(u8"ßa");
    CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sIn.getLength());
    CPPUNIT_ASSERT_EQUAL(195, int(static_cast<unsigned char>(sIn[0])));
    CPPUNIT_ASSERT_EQUAL(159, int(static_cast<unsigned char>(sIn[1])));
    CPPUNIT_ASSERT_EQUAL(97, int(static_cast<unsigned char>(sIn[2])));
}

CPPUNIT_TEST_SUITE_REGISTRATION(Test);

}

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