1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
12 #include <sal/types.h>
13 #include <cppunit/TestAssert.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <rtl/string.hxx>
17 #include <rtl/ustring.hxx>
22 OUString
s_bar(u
"bar"_ustr
);
23 OUString
s_bars(u
"bars"_ustr
);
24 OUString
s_foo(u
"foo"_ustr
);
25 OUString
s_other(u
"other"_ustr
);
26 OUString
s_xa(u
"xa"_ustr
);
27 OUString
s_xx(u
"xx"_ustr
);
29 class Test
: public CppUnit::TestFixture
{
31 void stringReplaceFirst();
33 void stringReplaceAll();
35 void ustringReplaceFirst();
37 void ustringReplaceFirstAsciiL();
39 void ustringReplaceFirstToAsciiL();
41 void ustringReplaceFirstAsciiLAsciiL();
43 void ustringReplaceAll();
45 void ustringReplaceAllAsciiL();
47 void ustringReplaceAllToAsciiL();
49 void ustringReplaceAllAsciiLAsciiL();
51 CPPUNIT_TEST_SUITE(Test
);
52 CPPUNIT_TEST(stringReplaceFirst
);
53 CPPUNIT_TEST(stringReplaceAll
);
54 CPPUNIT_TEST(ustringReplaceFirst
);
55 CPPUNIT_TEST(ustringReplaceFirstAsciiL
);
56 CPPUNIT_TEST(ustringReplaceFirstToAsciiL
);
57 CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL
);
58 CPPUNIT_TEST(ustringReplaceAll
);
59 CPPUNIT_TEST(ustringReplaceAllAsciiL
);
60 CPPUNIT_TEST(ustringReplaceAllToAsciiL
);
61 CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL
);
62 CPPUNIT_TEST_SUITE_END();
65 void Test::stringReplaceFirst() {
68 "foobarfoo"_ostr
.replaceFirst("foo"_ostr
, "other"_ostr
));
72 "foobarfoo"_ostr
.replaceFirst("bars"_ostr
, "other"_ostr
));
78 "foobarfoo"_ostr
.replaceFirst("foo"_ostr
, "other"_ostr
, &n
));
79 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
86 "foobarfoo"_ostr
.replaceFirst("foo"_ostr
, "other"_ostr
, &n
));
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
94 "foobarfoo"_ostr
.replaceFirst("bar"_ostr
, "other"_ostr
, &n
));
95 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
99 void Test::stringReplaceAll() {
100 CPPUNIT_ASSERT_EQUAL(
101 "otherbarother"_ostr
,
102 "foobarfoo"_ostr
.replaceAll("foo"_ostr
, "other"_ostr
));
104 CPPUNIT_ASSERT_EQUAL(
106 "foobarfoo"_ostr
.replaceAll("bars"_ostr
, "other"_ostr
));
108 CPPUNIT_ASSERT_EQUAL(
109 "xxa"_ostr
, "xaa"_ostr
.replaceAll("xa"_ostr
, "xx"_ostr
));
112 void Test::ustringReplaceFirst() {
113 CPPUNIT_ASSERT_EQUAL(
115 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, s_other
));
117 CPPUNIT_ASSERT_EQUAL(
119 u
"foobarfoo"_ustr
.replaceFirst(s_bars
, s_other
));
123 CPPUNIT_ASSERT_EQUAL(
125 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, s_other
, &n
));
126 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
131 CPPUNIT_ASSERT_EQUAL(
133 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, s_other
, &n
));
134 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
139 CPPUNIT_ASSERT_EQUAL(
141 u
"foobarfoo"_ustr
.replaceFirst(s_bar
, s_other
, &n
));
142 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
146 void Test::ustringReplaceFirstAsciiL() {
147 CPPUNIT_ASSERT_EQUAL(
149 u
"foobarfoo"_ustr
.replaceFirst("foo", s_other
));
151 CPPUNIT_ASSERT_EQUAL(
153 u
"foobarfoo"_ustr
.replaceFirst("bars", s_other
));
157 CPPUNIT_ASSERT_EQUAL(
159 u
"foobarfoo"_ustr
.replaceFirst("foo", s_other
, &n
));
160 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
165 CPPUNIT_ASSERT_EQUAL(
167 u
"foobarfoo"_ustr
.replaceFirst("foo", s_other
, &n
));
168 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
173 CPPUNIT_ASSERT_EQUAL(
175 u
"foobarfoo"_ustr
.replaceFirst("bar", s_other
, &n
));
176 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
179 CPPUNIT_ASSERT_EQUAL(
180 OUString(), u
"xa"_ustr
.replaceFirst("xa", s_empty
));
183 void Test::ustringReplaceFirstToAsciiL() {
184 CPPUNIT_ASSERT_EQUAL(
186 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, "other"));
188 CPPUNIT_ASSERT_EQUAL(
190 u
"foobarfoo"_ustr
.replaceFirst(s_bars
, "other"));
194 CPPUNIT_ASSERT_EQUAL(
196 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, "other", &n
));
197 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
202 CPPUNIT_ASSERT_EQUAL(
204 u
"foobarfoo"_ustr
.replaceFirst(s_foo
, "other", &n
));
205 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
210 CPPUNIT_ASSERT_EQUAL(
212 u
"foobarfoo"_ustr
.replaceFirst(s_bar
, "other", &n
));
213 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
216 CPPUNIT_ASSERT_EQUAL(
217 OUString(), u
"xa"_ustr
.replaceFirst(s_xa
, ""));
220 void Test::ustringReplaceFirstAsciiLAsciiL() {
221 CPPUNIT_ASSERT_EQUAL(
224 replaceFirst("foo", "other")));
226 CPPUNIT_ASSERT_EQUAL(
229 replaceFirst("bars", "other")));
233 CPPUNIT_ASSERT_EQUAL(
236 replaceFirst("foo", "other", &n
)));
237 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
242 CPPUNIT_ASSERT_EQUAL(
245 replaceFirst("foo", "other", &n
)));
246 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
251 CPPUNIT_ASSERT_EQUAL(
254 replaceFirst("bar", "other", &n
)));
255 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
258 CPPUNIT_ASSERT_EQUAL(
259 OUString(), u
"xa"_ustr
.replaceFirst("xa", ""));
262 void Test::ustringReplaceAll() {
263 CPPUNIT_ASSERT_EQUAL(
264 u
"otherbarother"_ustr
,
265 u
"foobarfoo"_ustr
.replaceAll(s_foo
, s_other
));
267 CPPUNIT_ASSERT_EQUAL(
269 u
"foobarfoo"_ustr
.replaceAll(s_bars
, s_other
));
271 CPPUNIT_ASSERT_EQUAL(
273 u
"xaa"_ustr
.replaceAll(s_xa
, s_xx
));
275 CPPUNIT_ASSERT_EQUAL(
276 u
"foobarbaz"_ustr
, u
"foobarfoo"_ustr
.replaceAll(u
"foo", u
"baz", 1));
279 void Test::ustringReplaceAllAsciiL() {
280 CPPUNIT_ASSERT_EQUAL(
281 u
"otherbarother"_ustr
,
282 u
"foobarfoo"_ustr
.replaceAll("foo", s_other
));
284 CPPUNIT_ASSERT_EQUAL(
286 u
"foobarfoo"_ustr
.replaceAll("bars", s_other
));
288 CPPUNIT_ASSERT_EQUAL(
290 u
"xaa"_ustr
.replaceAll("xa", s_xx
));
292 CPPUNIT_ASSERT_EQUAL(
293 OUString(), u
"xa"_ustr
.replaceAll("xa", s_empty
));
296 void Test::ustringReplaceAllToAsciiL() {
297 CPPUNIT_ASSERT_EQUAL(
298 u
"otherbarother"_ustr
,
299 u
"foobarfoo"_ustr
.replaceAll(s_foo
, "other"));
301 CPPUNIT_ASSERT_EQUAL(
303 u
"foobarfoo"_ustr
.replaceAll(s_bars
, "other"));
305 CPPUNIT_ASSERT_EQUAL(
307 u
"xaa"_ustr
.replaceAll(s_xa
, "xx"));
309 CPPUNIT_ASSERT_EQUAL(
310 OUString(), u
"xa"_ustr
.replaceAll(s_xa
, ""));
313 void Test::ustringReplaceAllAsciiLAsciiL() {
314 CPPUNIT_ASSERT_EQUAL(
315 u
"otherbarother"_ustr
,
317 replaceAll("foo", "other")));
319 CPPUNIT_ASSERT_EQUAL(
322 replaceAll("bars", "other")));
324 CPPUNIT_ASSERT_EQUAL(
327 replaceAll("xa", "xx")));
329 CPPUNIT_ASSERT_EQUAL(
330 OUString(), u
"xa"_ustr
.replaceAll("xa", ""));
335 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */