Debian Codegraph @

sal/qa/rtl/strings/test_ostring.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <sal/config.h>
11
12 #include <cppunit/TestAssert.h>
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <rtl/string.hxx>
16
17 namespace {
18
19 class Test: public CppUnit::TestFixture {
20 private:
21 void testStartsWithIgnoreAsciiCase();
22 void testCompareTo();
23 void testUtf8StringLiterals();
24
25 CPPUNIT_TEST_SUITE(Test);
26 CPPUNIT_TEST(testStartsWithIgnoreAsciiCase);
27 CPPUNIT_TEST(testCompareTo);
28 CPPUNIT_TEST(testUtf8StringLiterals);
29 CPPUNIT_TEST_SUITE_END();
30 };
31
32 void Test::testStartsWithIgnoreAsciiCase() {
33 {
34 OString r;
35 CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase(std::string_view(), &r));
36 CPPUNIT_ASSERT(r.isEmpty());
37 }
38 {
39 OString r;
40 CPPUNIT_ASSERT("foo"_ostr.startsWithIgnoreAsciiCase(std::string_view(), &r));
41 CPPUNIT_ASSERT_EQUAL("foo"_ostr, r);
42 }
43 {
44 OString r;
45 CPPUNIT_ASSERT(
46 "foo"_ostr.startsWithIgnoreAsciiCase("F", &r));
47 CPPUNIT_ASSERT_EQUAL("oo"_ostr, r);
48 }
49 {
50 OString r("other"_ostr);
51 CPPUNIT_ASSERT(
52 !"foo"_ostr.startsWithIgnoreAsciiCase("bar", &r));
53 CPPUNIT_ASSERT_EQUAL("other"_ostr, r);
54 }
55 {
56 OString r("other"_ostr);
57 CPPUNIT_ASSERT(
58 !"foo"_ostr.startsWithIgnoreAsciiCase("foobar", &r));
59 CPPUNIT_ASSERT_EQUAL("other"_ostr, r);
60 }
61
62 {
63 OString r;
64 CPPUNIT_ASSERT(OString().startsWithIgnoreAsciiCase("", &r));
65 CPPUNIT_ASSERT(r.isEmpty());
66 }
67 {
68 OString r;
69 CPPUNIT_ASSERT("foo"_ostr.startsWithIgnoreAsciiCase("", &r));
70 CPPUNIT_ASSERT_EQUAL("foo"_ostr, r);
71 }
72 {
73 OString r;
74 CPPUNIT_ASSERT(
75 "foo"_ostr.startsWithIgnoreAsciiCase("F", &r));
76 CPPUNIT_ASSERT_EQUAL("oo"_ostr, r);
77 }
78 {
79 OString r("other"_ostr);
80 CPPUNIT_ASSERT(
81 !"foo"_ostr.startsWithIgnoreAsciiCase("bar", &r));
82 CPPUNIT_ASSERT_EQUAL("other"_ostr, r);
83 }
84 {
85 OString r("other"_ostr);
86 CPPUNIT_ASSERT(
87 !"foo"_ostr.startsWithIgnoreAsciiCase("foobar", &r));
88 CPPUNIT_ASSERT_EQUAL("other"_ostr, r);
89 }
90 }
91
92 void Test::testCompareTo()
93 {
94 // test that embedded NUL does not stop the compare
95 char str1[2] = { '\0', 'x' };
96 char str2[2] = { '\0', 'y' };
97
98 OString s1(str1, 2);
99 OString s2(str2, 2);
100 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), s1.compareTo(s1));
101 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), s2.compareTo(s2));
102 CPPUNIT_ASSERT(s1.compareTo(s2) < 0);
103 CPPUNIT_ASSERT(s2.compareTo(s1) > 0);
104 CPPUNIT_ASSERT(s1.compareTo(OString(s2 + "y")) < 0);
105 CPPUNIT_ASSERT(s2.compareTo(OString(s1 + "x")) > 0);
106 CPPUNIT_ASSERT(OString(s1 + "x").compareTo(s2) < 0);
107 CPPUNIT_ASSERT(OString(s2 + "y").compareTo(s1) > 0);
108 }
109
110 void Test::testUtf8StringLiterals()
111 {
112 OString sIn(u8"ßa"_ostr);
113 CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), sIn.getLength());
114 CPPUNIT_ASSERT_EQUAL(195, int(static_cast<unsigned char>(sIn[0])));
115 CPPUNIT_ASSERT_EQUAL(159, int(static_cast<unsigned char>(sIn[1])));
116 CPPUNIT_ASSERT_EQUAL(97, int(static_cast<unsigned char>(sIn[2])));
117 }
118
119 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
120
121 }
122
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */