bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / strings / test_strings_replace.cxx
blob38c15e68ffaee7f50d81c4517fff549469f2ca2a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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"
21 namespace {
23 class Test: public CppUnit::TestFixture {
24 private:
25 void stringReplaceFirst();
27 void stringReplaceAll();
29 void ustringReplaceFirst();
31 void ustringReplaceFirstAsciiL();
33 void ustringReplaceFirstAsciiLAsciiL();
35 void ustringReplaceAll();
37 void ustringReplaceAllAsciiL();
39 void ustringReplaceAllAsciiLAsciiL();
41 CPPUNIT_TEST_SUITE(Test);
42 CPPUNIT_TEST(stringReplaceFirst);
43 CPPUNIT_TEST(stringReplaceAll);
44 CPPUNIT_TEST(ustringReplaceFirst);
45 CPPUNIT_TEST(ustringReplaceFirstAsciiL);
46 CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
47 CPPUNIT_TEST(ustringReplaceAll);
48 CPPUNIT_TEST(ustringReplaceAllAsciiL);
49 CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
50 CPPUNIT_TEST_SUITE_END();
53 void Test::stringReplaceFirst() {
54 CPPUNIT_ASSERT_EQUAL(
55 rtl::OString("otherbarfoo"),
56 rtl::OString("foobarfoo").replaceFirst("foo", "other"));
58 CPPUNIT_ASSERT_EQUAL(
59 rtl::OString("foobarfoo"),
60 rtl::OString("foobarfoo").replaceFirst("bars", "other"));
63 sal_Int32 n = 0;
64 CPPUNIT_ASSERT_EQUAL(
65 rtl::OString("otherbarfoo"),
66 rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
67 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
71 sal_Int32 n = 1;
72 CPPUNIT_ASSERT_EQUAL(
73 rtl::OString("foobarother"),
74 rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
75 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
79 sal_Int32 n = 4;
80 CPPUNIT_ASSERT_EQUAL(
81 rtl::OString("foobarfoo"),
82 rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
83 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
87 void Test::stringReplaceAll() {
88 CPPUNIT_ASSERT_EQUAL(
89 rtl::OString("otherbarother"),
90 rtl::OString("foobarfoo").replaceAll("foo", "other"));
92 CPPUNIT_ASSERT_EQUAL(
93 rtl::OString("foobarfoo"),
94 rtl::OString("foobarfoo").replaceAll("bars", "other"));
96 CPPUNIT_ASSERT_EQUAL(
97 rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
100 void Test::ustringReplaceFirst() {
101 CPPUNIT_ASSERT_EQUAL(
102 rtl::OUString("otherbarfoo"),
103 rtl::OUString("foobarfoo").replaceFirst(
104 rtl::OUString("foo"),
105 rtl::OUString("other")));
107 CPPUNIT_ASSERT_EQUAL(
108 rtl::OUString("foobarfoo"),
109 rtl::OUString("foobarfoo").replaceFirst(
110 rtl::OUString("bars"),
111 rtl::OUString("other")));
114 sal_Int32 n = 0;
115 CPPUNIT_ASSERT_EQUAL(
116 rtl::OUString("otherbarfoo"),
117 (rtl::OUString("foobarfoo").
118 replaceFirst(
119 rtl::OUString("foo"),
120 rtl::OUString("other"), &n)));
121 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
125 sal_Int32 n = 1;
126 CPPUNIT_ASSERT_EQUAL(
127 rtl::OUString("foobarother"),
128 (rtl::OUString("foobarfoo").
129 replaceFirst(
130 rtl::OUString("foo"),
131 rtl::OUString("other"), &n)));
132 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
136 sal_Int32 n = 4;
137 CPPUNIT_ASSERT_EQUAL(
138 rtl::OUString("foobarfoo"),
139 (rtl::OUString("foobarfoo").
140 replaceFirst(
141 rtl::OUString("bar"),
142 rtl::OUString("other"), &n)));
143 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
147 void Test::ustringReplaceFirstAsciiL() {
148 CPPUNIT_ASSERT_EQUAL(
149 rtl::OUString("otherbarfoo"),
150 (rtl::OUString("foobarfoo").
151 replaceFirst("foo",
152 rtl::OUString("other"))));
154 CPPUNIT_ASSERT_EQUAL(
155 rtl::OUString("foobarfoo"),
156 (rtl::OUString("foobarfoo").
157 replaceFirst("bars",
158 rtl::OUString("other"))));
161 sal_Int32 n = 0;
162 CPPUNIT_ASSERT_EQUAL(
163 rtl::OUString("otherbarfoo"),
164 (rtl::OUString("foobarfoo").
165 replaceFirst("foo",
166 rtl::OUString("other"), &n)));
167 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
171 sal_Int32 n = 1;
172 CPPUNIT_ASSERT_EQUAL(
173 rtl::OUString("foobarother"),
174 (rtl::OUString("foobarfoo").
175 replaceFirst("foo",
176 rtl::OUString("other"), &n)));
177 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
181 sal_Int32 n = 4;
182 CPPUNIT_ASSERT_EQUAL(
183 rtl::OUString("foobarfoo"),
184 (rtl::OUString("foobarfoo").
185 replaceFirst("bar",
186 rtl::OUString("other"), &n)));
187 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
190 CPPUNIT_ASSERT_EQUAL(
191 rtl::OUString(), rtl::OUString("xa").replaceFirst("xa", OUString()));
194 void Test::ustringReplaceFirstAsciiLAsciiL() {
195 CPPUNIT_ASSERT_EQUAL(
196 rtl::OUString("otherbarfoo"),
197 (rtl::OUString("foobarfoo").
198 replaceFirst("foo", "other")));
200 CPPUNIT_ASSERT_EQUAL(
201 rtl::OUString("foobarfoo"),
202 (rtl::OUString("foobarfoo").
203 replaceFirst("bars", "other")));
206 sal_Int32 n = 0;
207 CPPUNIT_ASSERT_EQUAL(
208 rtl::OUString("otherbarfoo"),
209 (rtl::OUString("foobarfoo").
210 replaceFirst("foo", "other", &n)));
211 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
215 sal_Int32 n = 1;
216 CPPUNIT_ASSERT_EQUAL(
217 rtl::OUString("foobarother"),
218 (rtl::OUString("foobarfoo").
219 replaceFirst("foo", "other", &n)));
220 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
224 sal_Int32 n = 4;
225 CPPUNIT_ASSERT_EQUAL(
226 rtl::OUString("foobarfoo"),
227 (rtl::OUString("foobarfoo").
228 replaceFirst("bar", "other", &n)));
229 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
232 CPPUNIT_ASSERT_EQUAL(
233 rtl::OUString(), rtl::OUString("xa").replaceFirst("xa", ""));
236 void Test::ustringReplaceAll() {
237 CPPUNIT_ASSERT_EQUAL(
238 rtl::OUString("otherbarother"),
239 rtl::OUString("foobarfoo").replaceAll(
240 rtl::OUString("foo"),
241 rtl::OUString("other")));
243 CPPUNIT_ASSERT_EQUAL(
244 rtl::OUString("foobarfoo"),
245 rtl::OUString("foobarfoo").replaceAll(
246 rtl::OUString("bars"),
247 rtl::OUString("other")));
249 CPPUNIT_ASSERT_EQUAL(
250 rtl::OUString("xxa"),
251 rtl::OUString("xaa").replaceAll(
252 rtl::OUString("xa"),
253 rtl::OUString("xx")));
256 void Test::ustringReplaceAllAsciiL() {
257 CPPUNIT_ASSERT_EQUAL(
258 rtl::OUString("otherbarother"),
259 (rtl::OUString("foobarfoo").
260 replaceAll("foo",
261 rtl::OUString("other"))));
263 CPPUNIT_ASSERT_EQUAL(
264 rtl::OUString("foobarfoo"),
265 (rtl::OUString("foobarfoo").
266 replaceAll("bars",
267 rtl::OUString("other"))));
269 CPPUNIT_ASSERT_EQUAL(
270 rtl::OUString("xxa"),
271 rtl::OUString("xaa").replaceAll(
272 "xa",
273 rtl::OUString("xx")));
275 CPPUNIT_ASSERT_EQUAL(
276 rtl::OUString(), rtl::OUString("xa").replaceAll("xa", OUString()));
279 void Test::ustringReplaceAllAsciiLAsciiL() {
280 CPPUNIT_ASSERT_EQUAL(
281 rtl::OUString("otherbarother"),
282 (rtl::OUString("foobarfoo").
283 replaceAll("foo", "other")));
285 CPPUNIT_ASSERT_EQUAL(
286 rtl::OUString("foobarfoo"),
287 (rtl::OUString("foobarfoo").
288 replaceAll("bars", "other")));
290 CPPUNIT_ASSERT_EQUAL(
291 rtl::OUString("xxa"),
292 (rtl::OUString("xaa").
293 replaceAll("xa", "xx")));
295 CPPUNIT_ASSERT_EQUAL(
296 rtl::OUString(), rtl::OUString("xa").replaceAll("xa", ""));
301 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
303 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */