1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <o3tl/string_view.hxx>
22 class Test
: public CppUnit::TestFixture
{
24 CPPUNIT_TEST_SUITE(Test
);
25 CPPUNIT_TEST(testCharLiteral
);
26 CPPUNIT_TEST(testChar16Literal
);
27 CPPUNIT_TEST(testChar32Literal
);
28 CPPUNIT_TEST(testWcharLiteral
);
29 CPPUNIT_TEST(testOperations
);
30 CPPUNIT_TEST_SUITE_END();
32 void testCharLiteral() {
33 char * const s1
= const_cast<char *>("foo");
34 o3tl::string_view
v1(s1
);
35 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v1
.size());
36 char const * const s2
= "foo";
37 o3tl::string_view
v2(s2
);
38 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v2
.size());
40 o3tl::string_view
v3(s3
);
41 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v3
.size());
42 o3tl::string_view
v4("foo");
43 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v4
.size());
46 void testChar16Literal() {
47 char16_t
* const s1
= const_cast<char16_t
*>(u
"foo");
48 o3tl::u16string_view
v1(s1
);
49 CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v1
.size());
50 char16_t
const * const s2
= u
"foo";
51 o3tl::u16string_view
v2(s2
);
52 CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v2
.size());
53 char16_t s3
[] = u
"foo";
54 o3tl::u16string_view
v3(s3
);
55 CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v3
.size());
56 o3tl::u16string_view
v4(u
"foo");
57 CPPUNIT_ASSERT_EQUAL(o3tl::u16string_view::size_type(3), v4
.size());
60 void testChar32Literal() {
61 char32_t
* const s1
= const_cast<char32_t
*>(U
"foo");
62 o3tl::u32string_view
v1(s1
);
63 CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v1
.size());
64 char32_t
const * const s2
= U
"foo";
65 o3tl::u32string_view
v2(s2
);
66 CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v2
.size());
67 char32_t s3
[] = U
"foo";
68 o3tl::u32string_view
v3(s3
);
69 CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v3
.size());
70 o3tl::u32string_view
v4(U
"foo");
71 CPPUNIT_ASSERT_EQUAL(o3tl::u32string_view::size_type(3), v4
.size());
74 void testWcharLiteral() {
75 wchar_t * const s1
= const_cast<wchar_t *>(L
"foo");
76 o3tl::wstring_view
v1(s1
);
77 CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v1
.size());
78 wchar_t const * const s2
= L
"foo";
79 o3tl::wstring_view
v2(s2
);
80 CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v2
.size());
81 wchar_t s3
[] = L
"foo";
82 o3tl::wstring_view
v3(s3
);
83 CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v3
.size());
84 o3tl::wstring_view
v4(L
"foo");
85 CPPUNIT_ASSERT_EQUAL(o3tl::wstring_view::size_type(3), v4
.size());
88 void testOperations() {
89 o3tl::string_view
const v("fox");
90 auto npos
= o3tl::string_view::npos
;
91 // o3tl::basic_string_view::npos will be (implicitly) inline with
92 // C++17, but for now can't be passed as 'const T& expected'
93 // argument into CppUnit::assertEquals, so take this detour
94 CPPUNIT_ASSERT_EQUAL('f', *v
.begin());
96 o3tl::string_view::difference_type(3), v
.end() - v
.begin());
97 CPPUNIT_ASSERT_EQUAL('f', *v
.cbegin());
99 o3tl::string_view::difference_type(3), v
.cend() - v
.cbegin());
100 CPPUNIT_ASSERT_EQUAL('x', *v
.rbegin());
101 CPPUNIT_ASSERT_EQUAL(
102 o3tl::string_view::difference_type(3), v
.rend() - v
.rbegin());
103 CPPUNIT_ASSERT_EQUAL('x', *v
.crbegin());
104 CPPUNIT_ASSERT_EQUAL(
105 o3tl::string_view::difference_type(3), v
.crend() - v
.crbegin());
106 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v
.size());
107 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v
.length());
108 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::npos
- 1, v
.max_size());
109 CPPUNIT_ASSERT(!v
.empty());
110 CPPUNIT_ASSERT_EQUAL('o', v
[1]);
112 v
.at(o3tl::string_view::npos
);
113 CPPUNIT_FAIL("missing exception");
114 } catch (std::out_of_range
&) {}
115 CPPUNIT_ASSERT_EQUAL('f', v
.at(0));
116 CPPUNIT_ASSERT_EQUAL('x', v
.at(2));
119 CPPUNIT_FAIL("missing exception");
120 } catch (std::out_of_range
&) {}
121 CPPUNIT_ASSERT_EQUAL('f', v
.front());
122 CPPUNIT_ASSERT_EQUAL('x', v
.back());
123 CPPUNIT_ASSERT_EQUAL('f', *v
.data());
125 o3tl::string_view
v1("fox");
127 CPPUNIT_ASSERT_EQUAL('x', v1
.front());
128 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v1
.size());
131 o3tl::string_view
v1("fox");
133 CPPUNIT_ASSERT_EQUAL('f', v1
.front());
134 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v1
.size());
137 o3tl::string_view
v1("fox");
138 o3tl::string_view
v2("giraffe");
140 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(7), v1
.size());
141 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(3), v2
.size());
145 auto n
= v
.copy(a
, 10, 1);
146 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(2), n
);
147 CPPUNIT_ASSERT_EQUAL('o', a
[0]);
148 CPPUNIT_ASSERT_EQUAL('x', a
[1]);
151 auto v1
= v
.substr(1);
152 CPPUNIT_ASSERT_EQUAL('o', v1
.front());
153 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(2), v1
.size());
155 CPPUNIT_ASSERT(v
.compare(o3tl::string_view("foo")) > 0);
156 CPPUNIT_ASSERT(v
.compare(0, 2, o3tl::string_view("foo")) < 0);
157 CPPUNIT_ASSERT_EQUAL(
158 0, v
.compare(0, 2, o3tl::string_view("foo"), 0, 2));
159 CPPUNIT_ASSERT_EQUAL(0, v
.compare("fox"));
160 CPPUNIT_ASSERT(v
.compare(1, 2, "abc") > 0);
161 CPPUNIT_ASSERT_EQUAL(0, v
.compare(1, 2, "oxx", 2));
162 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v
.find("ox"));
163 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v
.find('o'));
164 CPPUNIT_ASSERT_EQUAL(
165 o3tl::string_view::size_type(1), v
.find("oxx", 0, 2));
166 CPPUNIT_ASSERT_EQUAL(npos
, v
.find("oxx"));
167 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v
.rfind("ox"));
168 CPPUNIT_ASSERT_EQUAL(o3tl::string_view::size_type(1), v
.rfind('o'));
169 CPPUNIT_ASSERT_EQUAL(
170 o3tl::string_view::size_type(1),
171 v
.rfind("oxx", o3tl::string_view::npos
, 2));
172 CPPUNIT_ASSERT_EQUAL(npos
, v
.rfind("oxx"));
173 CPPUNIT_ASSERT_EQUAL(
174 o3tl::string_view::size_type(1), v
.find_first_of("nop"));
175 CPPUNIT_ASSERT_EQUAL(
176 o3tl::string_view::size_type(1), v
.find_first_of('o'));
177 CPPUNIT_ASSERT_EQUAL(
178 o3tl::string_view::size_type(1), v
.find_first_of("nofx", 0, 2));
179 CPPUNIT_ASSERT_EQUAL(
180 o3tl::string_view::size_type(0), v
.find_first_of("nofx"));
181 CPPUNIT_ASSERT_EQUAL(
182 o3tl::string_view::size_type(1), v
.find_last_of("nop"));
183 CPPUNIT_ASSERT_EQUAL(
184 o3tl::string_view::size_type(1), v
.find_last_of('o'));
185 CPPUNIT_ASSERT_EQUAL(
186 o3tl::string_view::size_type(1),
187 v
.find_last_of("nofx", o3tl::string_view::npos
, 2));
188 CPPUNIT_ASSERT_EQUAL(
189 o3tl::string_view::size_type(2), v
.find_last_of("nofx"));
190 CPPUNIT_ASSERT_EQUAL(
191 o3tl::string_view::size_type(1), v
.find_first_not_of("fx"));
192 CPPUNIT_ASSERT_EQUAL(
193 o3tl::string_view::size_type(1), v
.find_first_not_of('f'));
194 CPPUNIT_ASSERT_EQUAL(
195 o3tl::string_view::size_type(1), v
.find_first_not_of("fxo", 0, 2));
196 CPPUNIT_ASSERT_EQUAL(npos
, v
.find_first_not_of("fxo"));
197 CPPUNIT_ASSERT_EQUAL(
198 o3tl::string_view::size_type(1), v
.find_last_not_of("fx"));
199 CPPUNIT_ASSERT_EQUAL(
200 o3tl::string_view::size_type(1), v
.find_last_not_of('x'));
201 CPPUNIT_ASSERT_EQUAL(
202 o3tl::string_view::size_type(1),
203 v
.find_last_not_of("fxo", o3tl::string_view::npos
, 2));
204 CPPUNIT_ASSERT_EQUAL(npos
, v
.find_last_not_of("fxo"));
208 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */