tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sal / qa / rtl / strings / test_strings_replace.cxx
blob5417a5fa92880f28a412296a10a644e8e24237b2
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.hxx>
17 #include <rtl/ustring.hxx>
19 namespace {
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);
29 class Test: public CppUnit::TestFixture {
30 private:
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() {
66 CPPUNIT_ASSERT_EQUAL(
67 "otherbarfoo"_ostr,
68 "foobarfoo"_ostr.replaceFirst("foo"_ostr, "other"_ostr));
70 CPPUNIT_ASSERT_EQUAL(
71 "foobarfoo"_ostr,
72 "foobarfoo"_ostr.replaceFirst("bars"_ostr, "other"_ostr));
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);
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);
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);
99 void Test::stringReplaceAll() {
100 CPPUNIT_ASSERT_EQUAL(
101 "otherbarother"_ostr,
102 "foobarfoo"_ostr.replaceAll("foo"_ostr, "other"_ostr));
104 CPPUNIT_ASSERT_EQUAL(
105 "foobarfoo"_ostr,
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(
114 u"otherbarfoo"_ustr,
115 u"foobarfoo"_ustr.replaceFirst(s_foo, s_other));
117 CPPUNIT_ASSERT_EQUAL(
118 u"foobarfoo"_ustr,
119 u"foobarfoo"_ustr.replaceFirst(s_bars, s_other));
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);
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);
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);
146 void Test::ustringReplaceFirstAsciiL() {
147 CPPUNIT_ASSERT_EQUAL(
148 u"otherbarfoo"_ustr,
149 u"foobarfoo"_ustr.replaceFirst("foo", s_other));
151 CPPUNIT_ASSERT_EQUAL(
152 u"foobarfoo"_ustr,
153 u"foobarfoo"_ustr.replaceFirst("bars", s_other));
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);
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);
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);
179 CPPUNIT_ASSERT_EQUAL(
180 OUString(), u"xa"_ustr.replaceFirst("xa", s_empty));
183 void Test::ustringReplaceFirstToAsciiL() {
184 CPPUNIT_ASSERT_EQUAL(
185 u"otherbarfoo"_ustr,
186 u"foobarfoo"_ustr.replaceFirst(s_foo, "other"));
188 CPPUNIT_ASSERT_EQUAL(
189 u"foobarfoo"_ustr,
190 u"foobarfoo"_ustr.replaceFirst(s_bars, "other"));
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);
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);
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);
216 CPPUNIT_ASSERT_EQUAL(
217 OUString(), u"xa"_ustr.replaceFirst(s_xa, ""));
220 void Test::ustringReplaceFirstAsciiLAsciiL() {
221 CPPUNIT_ASSERT_EQUAL(
222 u"otherbarfoo"_ustr,
223 (u"foobarfoo"_ustr.
224 replaceFirst("foo", "other")));
226 CPPUNIT_ASSERT_EQUAL(
227 u"foobarfoo"_ustr,
228 (u"foobarfoo"_ustr.
229 replaceFirst("bars", "other")));
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);
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);
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);
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(
268 u"foobarfoo"_ustr,
269 u"foobarfoo"_ustr.replaceAll(s_bars, s_other));
271 CPPUNIT_ASSERT_EQUAL(
272 u"xxa"_ustr,
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(
285 u"foobarfoo"_ustr,
286 u"foobarfoo"_ustr.replaceAll("bars", s_other));
288 CPPUNIT_ASSERT_EQUAL(
289 u"xxa"_ustr,
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(
302 u"foobarfoo"_ustr,
303 u"foobarfoo"_ustr.replaceAll(s_bars, "other"));
305 CPPUNIT_ASSERT_EQUAL(
306 u"xxa"_ustr,
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,
316 (u"foobarfoo"_ustr.
317 replaceAll("foo", "other")));
319 CPPUNIT_ASSERT_EQUAL(
320 u"foobarfoo"_ustr,
321 (u"foobarfoo"_ustr.
322 replaceAll("bars", "other")));
324 CPPUNIT_ASSERT_EQUAL(
325 u"xxa"_ustr,
326 (u"xaa"_ustr.
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: */