Debian Codegraph @

sal/qa/rtl/strings/test_strings_replace.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 <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>
18
19 namespace {
20
21 OUString s_empty;
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);
28
29 class Test: public CppUnit::TestFixture {
30 private:
31 void stringReplaceFirst();
32
33 void stringReplaceAll();
34
35 void ustringReplaceFirst();
36
37 void ustringReplaceFirstAsciiL();
38
39 void ustringReplaceFirstToAsciiL();
40
41 void ustringReplaceFirstAsciiLAsciiL();
42
43 void ustringReplaceAll();
44
45 void ustringReplaceAllAsciiL();
46
47 void ustringReplaceAllToAsciiL();
48
49 void ustringReplaceAllAsciiLAsciiL();
50
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();
63 };
64
65 void Test::stringReplaceFirst() {
66 CPPUNIT_ASSERT_EQUAL(
67 "otherbarfoo"_ostr,
68 "foobarfoo"_ostr.replaceFirst("foo"_ostr, "other"_ostr));
69
70 CPPUNIT_ASSERT_EQUAL(
71 "foobarfoo"_ostr,
72 "foobarfoo"_ostr.replaceFirst("bars"_ostr, "other"_ostr));
73
74 {
75 sal_Int32 n = 0;
76 CPPUNIT_ASSERT_EQUAL(
77 "otherbarfoo"_ostr,
78 "foobarfoo"_ostr.replaceFirst("foo"_ostr, "other"_ostr, &n));
79 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
80 }
81
82 {
83 sal_Int32 n = 1;
84 CPPUNIT_ASSERT_EQUAL(
85 "foobarother"_ostr,
86 "foobarfoo"_ostr.replaceFirst("foo"_ostr, "other"_ostr, &n));
87 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
88 }
89
90 {
91 sal_Int32 n = 4;
92 CPPUNIT_ASSERT_EQUAL(
93 "foobarfoo"_ostr,
94 "foobarfoo"_ostr.replaceFirst("bar"_ostr, "other"_ostr, &n));
95 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
96 }
97 }
98
99 void Test::stringReplaceAll() {
100 CPPUNIT_ASSERT_EQUAL(
101 "otherbarother"_ostr,
102 "foobarfoo"_ostr.replaceAll("foo"_ostr, "other"_ostr));
103
104 CPPUNIT_ASSERT_EQUAL(
105 "foobarfoo"_ostr,
106 "foobarfoo"_ostr.replaceAll("bars"_ostr, "other"_ostr));
107
108 CPPUNIT_ASSERT_EQUAL(
109 "xxa"_ostr, "xaa"_ostr.replaceAll("xa"_ostr, "xx"_ostr));
110 }
111
112 void Test::ustringReplaceFirst() {
113 CPPUNIT_ASSERT_EQUAL(
114 u"otherbarfoo"_ustr,
115 u"foobarfoo"_ustr.replaceFirst(s_foo, s_other));
116
117 CPPUNIT_ASSERT_EQUAL(
118 u"foobarfoo"_ustr,
119 u"foobarfoo"_ustr.replaceFirst(s_bars, s_other));
120
121 {
122 sal_Int32 n = 0;
123 CPPUNIT_ASSERT_EQUAL(
124 u"otherbarfoo"_ustr,
125 u"foobarfoo"_ustr.replaceFirst(s_foo, s_other, &n));
126 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
127 }
128
129 {
130 sal_Int32 n = 1;
131 CPPUNIT_ASSERT_EQUAL(
132 u"foobarother"_ustr,
133 u"foobarfoo"_ustr.replaceFirst(s_foo, s_other, &n));
134 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
135 }
136
137 {
138 sal_Int32 n = 4;
139 CPPUNIT_ASSERT_EQUAL(
140 u"foobarfoo"_ustr,
141 u"foobarfoo"_ustr.replaceFirst(s_bar, s_other, &n));
142 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
143 }
144 }
145
146 void Test::ustringReplaceFirstAsciiL() {
147 CPPUNIT_ASSERT_EQUAL(
148 u"otherbarfoo"_ustr,
149 u"foobarfoo"_ustr.replaceFirst("foo", s_other));
150
151 CPPUNIT_ASSERT_EQUAL(
152 u"foobarfoo"_ustr,
153 u"foobarfoo"_ustr.replaceFirst("bars", s_other));
154
155 {
156 sal_Int32 n = 0;
157 CPPUNIT_ASSERT_EQUAL(
158 u"otherbarfoo"_ustr,
159 u"foobarfoo"_ustr.replaceFirst("foo", s_other, &n));
160 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
161 }
162
163 {
164 sal_Int32 n = 1;
165 CPPUNIT_ASSERT_EQUAL(
166 u"foobarother"_ustr,
167 u"foobarfoo"_ustr.replaceFirst("foo", s_other, &n));
168 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
169 }
170
171 {
172 sal_Int32 n = 4;
173 CPPUNIT_ASSERT_EQUAL(
174 u"foobarfoo"_ustr,
175 u"foobarfoo"_ustr.replaceFirst("bar", s_other, &n));
176 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
177 }
178
179 CPPUNIT_ASSERT_EQUAL(
180 OUString(), u"xa"_ustr.replaceFirst("xa", s_empty));
181 }
182
183 void Test::ustringReplaceFirstToAsciiL() {
184 CPPUNIT_ASSERT_EQUAL(
185 u"otherbarfoo"_ustr,
186 u"foobarfoo"_ustr.replaceFirst(s_foo, "other"));
187
188 CPPUNIT_ASSERT_EQUAL(
189 u"foobarfoo"_ustr,
190 u"foobarfoo"_ustr.replaceFirst(s_bars, "other"));
191
192 {
193 sal_Int32 n = 0;
194 CPPUNIT_ASSERT_EQUAL(
195 u"otherbarfoo"_ustr,
196 u"foobarfoo"_ustr.replaceFirst(s_foo, "other", &n));
197 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
198 }
199
200 {
201 sal_Int32 n = 1;
202 CPPUNIT_ASSERT_EQUAL(
203 u"foobarother"_ustr,
204 u"foobarfoo"_ustr.replaceFirst(s_foo, "other", &n));
205 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
206 }
207
208 {
209 sal_Int32 n = 4;
210 CPPUNIT_ASSERT_EQUAL(
211 u"foobarfoo"_ustr,
212 u"foobarfoo"_ustr.replaceFirst(s_bar, "other", &n));
213 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
214 }
215
216 CPPUNIT_ASSERT_EQUAL(
217 OUString(), u"xa"_ustr.replaceFirst(s_xa, ""));
218 }
219
220 void Test::ustringReplaceFirstAsciiLAsciiL() {
221 CPPUNIT_ASSERT_EQUAL(
222 u"otherbarfoo"_ustr,
223 (u"foobarfoo"_ustr.
224 replaceFirst("foo", "other")));
225
226 CPPUNIT_ASSERT_EQUAL(
227 u"foobarfoo"_ustr,
228 (u"foobarfoo"_ustr.
229 replaceFirst("bars", "other")));
230
231 {
232 sal_Int32 n = 0;
233 CPPUNIT_ASSERT_EQUAL(
234 u"otherbarfoo"_ustr,
235 (u"foobarfoo"_ustr.
236 replaceFirst("foo", "other", &n)));
237 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
238 }
239
240 {
241 sal_Int32 n = 1;
242 CPPUNIT_ASSERT_EQUAL(
243 u"foobarother"_ustr,
244 (u"foobarfoo"_ustr.
245 replaceFirst("foo", "other", &n)));
246 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
247 }
248
249 {
250 sal_Int32 n = 4;
251 CPPUNIT_ASSERT_EQUAL(
252 u"foobarfoo"_ustr,
253 (u"foobarfoo"_ustr.
254 replaceFirst("bar", "other", &n)));
255 CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
256 }
257
258 CPPUNIT_ASSERT_EQUAL(
259 OUString(), u"xa"_ustr.replaceFirst("xa", ""));
260 }
261
262 void Test::ustringReplaceAll() {
263 CPPUNIT_ASSERT_EQUAL(
264 u"otherbarother"_ustr,
265 u"foobarfoo"_ustr.replaceAll(s_foo, s_other));
266
267 CPPUNIT_ASSERT_EQUAL(
268 u"foobarfoo"_ustr,
269 u"foobarfoo"_ustr.replaceAll(s_bars, s_other));
270
271 CPPUNIT_ASSERT_EQUAL(
272 u"xxa"_ustr,
273 u"xaa"_ustr.replaceAll(s_xa, s_xx));
274
275 CPPUNIT_ASSERT_EQUAL(
276 u"foobarbaz"_ustr, u"foobarfoo"_ustr.replaceAll(u"foo", u"baz", 1));
277 }
278
279 void Test::ustringReplaceAllAsciiL() {
280 CPPUNIT_ASSERT_EQUAL(
281 u"otherbarother"_ustr,
282 u"foobarfoo"_ustr.replaceAll("foo", s_other));
283
284 CPPUNIT_ASSERT_EQUAL(
285 u"foobarfoo"_ustr,
286 u"foobarfoo"_ustr.replaceAll("bars", s_other));
287
288 CPPUNIT_ASSERT_EQUAL(
289 u"xxa"_ustr,
290 u"xaa"_ustr.replaceAll("xa", s_xx));
291
292 CPPUNIT_ASSERT_EQUAL(
293 OUString(), u"xa"_ustr.replaceAll("xa", s_empty));
294 }
295
296 void Test::ustringReplaceAllToAsciiL() {
297 CPPUNIT_ASSERT_EQUAL(
298 u"otherbarother"_ustr,
299 u"foobarfoo"_ustr.replaceAll(s_foo, "other"));
300
301 CPPUNIT_ASSERT_EQUAL(
302 u"foobarfoo"_ustr,
303 u"foobarfoo"_ustr.replaceAll(s_bars, "other"));
304
305 CPPUNIT_ASSERT_EQUAL(
306 u"xxa"_ustr,
307 u"xaa"_ustr.replaceAll(s_xa, "xx"));
308
309 CPPUNIT_ASSERT_EQUAL(
310 OUString(), u"xa"_ustr.replaceAll(s_xa, ""));
311 }
312
313 void Test::ustringReplaceAllAsciiLAsciiL() {
314 CPPUNIT_ASSERT_EQUAL(
315 u"otherbarother"_ustr,
316 (u"foobarfoo"_ustr.
317 replaceAll("foo", "other")));
318
319 CPPUNIT_ASSERT_EQUAL(
320 u"foobarfoo"_ustr,
321 (u"foobarfoo"_ustr.
322 replaceAll("bars", "other")));
323
324 CPPUNIT_ASSERT_EQUAL(
325 u"xxa"_ustr,
326 (u"xaa"_ustr.
327 replaceAll("xa", "xx")));
328
329 CPPUNIT_ASSERT_EQUAL(
330 OUString(), u"xa"_ustr.replaceAll("xa", ""));
331 }
332
333 }
334
335 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
336
337 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */