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.h>
17 #include <rtl/string.hxx>
18 #include <rtl/ustring.h>
19 #include <rtl/ustring.hxx>
24 OUString
s_bar("bar");
25 OUString
s_bars("bars");
26 OUString
s_foo("foo");
27 OUString
s_other("other");
31 class Test
: public CppUnit::TestFixture
{
33 void stringReplaceFirst();
35 void stringReplaceAll();
37 void ustringReplaceFirst();
39 void ustringReplaceFirstAsciiL();
41 void ustringReplaceFirstToAsciiL();
43 void ustringReplaceFirstAsciiLAsciiL();
45 void ustringReplaceAll();
47 void ustringReplaceAllAsciiL();
49 void ustringReplaceAllToAsciiL();
51 void ustringReplaceAllAsciiLAsciiL();
53 CPPUNIT_TEST_SUITE(Test
);
54 CPPUNIT_TEST(stringReplaceFirst
);
55 CPPUNIT_TEST(stringReplaceAll
);
56 CPPUNIT_TEST(ustringReplaceFirst
);
57 CPPUNIT_TEST(ustringReplaceFirstAsciiL
);
58 CPPUNIT_TEST(ustringReplaceFirstToAsciiL
);
59 CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL
);
60 CPPUNIT_TEST(ustringReplaceAll
);
61 CPPUNIT_TEST(ustringReplaceAllAsciiL
);
62 CPPUNIT_TEST(ustringReplaceAllToAsciiL
);
63 CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL
);
64 CPPUNIT_TEST_SUITE_END();
67 void Test::stringReplaceFirst() {
69 OString("otherbarfoo"),
70 OString("foobarfoo").replaceFirst("foo", "other"));
74 OString("foobarfoo").replaceFirst("bars", "other"));
79 OString("otherbarfoo"),
80 OString("foobarfoo").replaceFirst("foo", "other", &n
));
81 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
87 OString("foobarother"),
88 OString("foobarfoo").replaceFirst("foo", "other", &n
));
89 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
96 OString("foobarfoo").replaceFirst("bar", "other", &n
));
97 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
101 void Test::stringReplaceAll() {
102 CPPUNIT_ASSERT_EQUAL(
103 OString("otherbarother"),
104 OString("foobarfoo").replaceAll("foo", "other"));
106 CPPUNIT_ASSERT_EQUAL(
107 OString("foobarfoo"),
108 OString("foobarfoo").replaceAll("bars", "other"));
110 CPPUNIT_ASSERT_EQUAL(
111 OString("xxa"), OString("xaa").replaceAll("xa", "xx"));
114 void Test::ustringReplaceFirst() {
115 CPPUNIT_ASSERT_EQUAL(
116 OUString("otherbarfoo"),
117 OUString("foobarfoo").replaceFirst(s_foo
, s_other
));
119 CPPUNIT_ASSERT_EQUAL(
120 OUString("foobarfoo"),
121 OUString("foobarfoo").replaceFirst(s_bars
, s_other
));
125 CPPUNIT_ASSERT_EQUAL(
126 OUString("otherbarfoo"),
127 OUString("foobarfoo").replaceFirst(s_foo
, s_other
, &n
));
128 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
133 CPPUNIT_ASSERT_EQUAL(
134 OUString("foobarother"),
135 OUString("foobarfoo").replaceFirst(s_foo
, s_other
, &n
));
136 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
141 CPPUNIT_ASSERT_EQUAL(
142 OUString("foobarfoo"),
143 OUString("foobarfoo").replaceFirst(s_bar
, s_other
, &n
));
144 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
148 void Test::ustringReplaceFirstAsciiL() {
149 CPPUNIT_ASSERT_EQUAL(
150 OUString("otherbarfoo"),
151 OUString("foobarfoo").replaceFirst("foo", s_other
));
153 CPPUNIT_ASSERT_EQUAL(
154 OUString("foobarfoo"),
155 OUString("foobarfoo").replaceFirst("bars", s_other
));
159 CPPUNIT_ASSERT_EQUAL(
160 OUString("otherbarfoo"),
161 OUString("foobarfoo").replaceFirst("foo", s_other
, &n
));
162 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
167 CPPUNIT_ASSERT_EQUAL(
168 OUString("foobarother"),
169 OUString("foobarfoo").replaceFirst("foo", s_other
, &n
));
170 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
175 CPPUNIT_ASSERT_EQUAL(
176 OUString("foobarfoo"),
177 OUString("foobarfoo").replaceFirst("bar", s_other
, &n
));
178 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
181 CPPUNIT_ASSERT_EQUAL(
182 OUString(), OUString("xa").replaceFirst("xa", s_empty
));
185 void Test::ustringReplaceFirstToAsciiL() {
186 CPPUNIT_ASSERT_EQUAL(
187 OUString("otherbarfoo"),
188 OUString("foobarfoo").replaceFirst(s_foo
, "other"));
190 CPPUNIT_ASSERT_EQUAL(
191 OUString("foobarfoo"),
192 OUString("foobarfoo").replaceFirst(s_bars
, "other"));
196 CPPUNIT_ASSERT_EQUAL(
197 OUString("otherbarfoo"),
198 OUString("foobarfoo").replaceFirst(s_foo
, "other", &n
));
199 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
204 CPPUNIT_ASSERT_EQUAL(
205 OUString("foobarother"),
206 OUString("foobarfoo").replaceFirst(s_foo
, "other", &n
));
207 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
212 CPPUNIT_ASSERT_EQUAL(
213 OUString("foobarfoo"),
214 OUString("foobarfoo").replaceFirst(s_bar
, "other", &n
));
215 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
218 CPPUNIT_ASSERT_EQUAL(
219 OUString(), OUString("xa").replaceFirst(s_xa
, ""));
222 void Test::ustringReplaceFirstAsciiLAsciiL() {
223 CPPUNIT_ASSERT_EQUAL(
224 OUString("otherbarfoo"),
225 (OUString("foobarfoo").
226 replaceFirst("foo", "other")));
228 CPPUNIT_ASSERT_EQUAL(
229 OUString("foobarfoo"),
230 (OUString("foobarfoo").
231 replaceFirst("bars", "other")));
235 CPPUNIT_ASSERT_EQUAL(
236 OUString("otherbarfoo"),
237 (OUString("foobarfoo").
238 replaceFirst("foo", "other", &n
)));
239 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n
);
244 CPPUNIT_ASSERT_EQUAL(
245 OUString("foobarother"),
246 (OUString("foobarfoo").
247 replaceFirst("foo", "other", &n
)));
248 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n
);
253 CPPUNIT_ASSERT_EQUAL(
254 OUString("foobarfoo"),
255 (OUString("foobarfoo").
256 replaceFirst("bar", "other", &n
)));
257 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n
);
260 CPPUNIT_ASSERT_EQUAL(
261 OUString(), OUString("xa").replaceFirst("xa", ""));
264 void Test::ustringReplaceAll() {
265 CPPUNIT_ASSERT_EQUAL(
266 OUString("otherbarother"),
267 OUString("foobarfoo").replaceAll(s_foo
, s_other
));
269 CPPUNIT_ASSERT_EQUAL(
270 OUString("foobarfoo"),
271 OUString("foobarfoo").replaceAll(s_bars
, s_other
));
273 CPPUNIT_ASSERT_EQUAL(
275 OUString("xaa").replaceAll(s_xa
, s_xx
));
277 CPPUNIT_ASSERT_EQUAL(
278 OUString("foobarbaz"), OUString("foobarfoo").replaceAll(u
"foo", u
"baz", 1));
281 void Test::ustringReplaceAllAsciiL() {
282 CPPUNIT_ASSERT_EQUAL(
283 OUString("otherbarother"),
284 OUString("foobarfoo").replaceAll("foo", s_other
));
286 CPPUNIT_ASSERT_EQUAL(
287 OUString("foobarfoo"),
288 OUString("foobarfoo").replaceAll("bars", s_other
));
290 CPPUNIT_ASSERT_EQUAL(
292 OUString("xaa").replaceAll("xa", s_xx
));
294 CPPUNIT_ASSERT_EQUAL(
295 OUString(), OUString("xa").replaceAll("xa", s_empty
));
298 void Test::ustringReplaceAllToAsciiL() {
299 CPPUNIT_ASSERT_EQUAL(
300 OUString("otherbarother"),
301 OUString("foobarfoo").replaceAll(s_foo
, "other"));
303 CPPUNIT_ASSERT_EQUAL(
304 OUString("foobarfoo"),
305 OUString("foobarfoo").replaceAll(s_bars
, "other"));
307 CPPUNIT_ASSERT_EQUAL(
309 OUString("xaa").replaceAll(s_xa
, "xx"));
311 CPPUNIT_ASSERT_EQUAL(
312 OUString(), OUString("xa").replaceAll(s_xa
, ""));
315 void Test::ustringReplaceAllAsciiLAsciiL() {
316 CPPUNIT_ASSERT_EQUAL(
317 OUString("otherbarother"),
318 (OUString("foobarfoo").
319 replaceAll("foo", "other")));
321 CPPUNIT_ASSERT_EQUAL(
322 OUString("foobarfoo"),
323 (OUString("foobarfoo").
324 replaceAll("bars", "other")));
326 CPPUNIT_ASSERT_EQUAL(
329 replaceAll("xa", "xx")));
331 CPPUNIT_ASSERT_EQUAL(
332 OUString(), OUString("xa").replaceAll("xa", ""));
337 CPPUNIT_TEST_SUITE_REGISTRATION(Test
);
339 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */