Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * Version: MPL 1.1 / GPLv3+ / LGPLv3+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * [ Copyright (C) 2012 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 : * (initial developer) ]
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 : * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 : * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include "sal/config.h"
31 :
32 : #include <sal/types.h>
33 : #include "cppunit/TestAssert.h"
34 : #include "cppunit/TestFixture.h"
35 : #include "cppunit/extensions/HelperMacros.h"
36 : #include "rtl/string.h"
37 : #include "rtl/string.hxx"
38 : #include "rtl/ustring.h"
39 : #include "rtl/ustring.hxx"
40 :
41 : namespace {
42 :
43 24 : class Test: public CppUnit::TestFixture {
44 : private:
45 : void stringReplaceFirst();
46 :
47 : void stringReplaceAll();
48 :
49 : void ustringReplaceFirst();
50 :
51 : void ustringReplaceFirstAsciiL();
52 :
53 : void ustringReplaceFirstAsciiLAsciiL();
54 :
55 : void ustringReplaceAll();
56 :
57 : void ustringReplaceAllAsciiL();
58 :
59 : void ustringReplaceAllAsciiLAsciiL();
60 :
61 2 : CPPUNIT_TEST_SUITE(Test);
62 1 : CPPUNIT_TEST(stringReplaceFirst);
63 1 : CPPUNIT_TEST(stringReplaceAll);
64 1 : CPPUNIT_TEST(ustringReplaceFirst);
65 1 : CPPUNIT_TEST(ustringReplaceFirstAsciiL);
66 1 : CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
67 1 : CPPUNIT_TEST(ustringReplaceAll);
68 1 : CPPUNIT_TEST(ustringReplaceAllAsciiL);
69 1 : CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
70 2 : CPPUNIT_TEST_SUITE_END();
71 : };
72 :
73 1 : void Test::stringReplaceFirst() {
74 2 : CPPUNIT_ASSERT_EQUAL(
75 : rtl::OString("otherbarfoo"),
76 1 : rtl::OString("foobarfoo").replaceFirst("foo", "other"));
77 :
78 2 : CPPUNIT_ASSERT_EQUAL(
79 : rtl::OString("foobarfoo"),
80 1 : rtl::OString("foobarfoo").replaceFirst("bars", "other"));
81 :
82 : {
83 1 : sal_Int32 n = 0;
84 2 : CPPUNIT_ASSERT_EQUAL(
85 : rtl::OString("otherbarfoo"),
86 1 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
87 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
88 : }
89 :
90 : {
91 1 : sal_Int32 n = 1;
92 2 : CPPUNIT_ASSERT_EQUAL(
93 : rtl::OString("foobarother"),
94 1 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
95 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
96 : }
97 :
98 : {
99 1 : sal_Int32 n = 4;
100 2 : CPPUNIT_ASSERT_EQUAL(
101 : rtl::OString("foobarfoo"),
102 1 : rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
103 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
104 : }
105 1 : }
106 :
107 1 : void Test::stringReplaceAll() {
108 2 : CPPUNIT_ASSERT_EQUAL(
109 : rtl::OString("otherbarother"),
110 1 : rtl::OString("foobarfoo").replaceAll("foo", "other"));
111 :
112 2 : CPPUNIT_ASSERT_EQUAL(
113 : rtl::OString("foobarfoo"),
114 1 : rtl::OString("foobarfoo").replaceAll("bars", "other"));
115 :
116 2 : CPPUNIT_ASSERT_EQUAL(
117 1 : rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
118 1 : }
119 :
120 1 : void Test::ustringReplaceFirst() {
121 2 : CPPUNIT_ASSERT_EQUAL(
122 : rtl::OUString("otherbarfoo"),
123 : rtl::OUString("foobarfoo").replaceFirst(
124 : rtl::OUString("foo"),
125 1 : rtl::OUString("other")));
126 :
127 2 : CPPUNIT_ASSERT_EQUAL(
128 : rtl::OUString("foobarfoo"),
129 : rtl::OUString("foobarfoo").replaceFirst(
130 : rtl::OUString("bars"),
131 1 : rtl::OUString("other")));
132 :
133 : {
134 1 : sal_Int32 n = 0;
135 2 : CPPUNIT_ASSERT_EQUAL(
136 : rtl::OUString("otherbarfoo"),
137 : (rtl::OUString("foobarfoo").
138 : replaceFirst(
139 : rtl::OUString("foo"),
140 1 : rtl::OUString("other"), &n)));
141 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
142 : }
143 :
144 : {
145 1 : sal_Int32 n = 1;
146 2 : CPPUNIT_ASSERT_EQUAL(
147 : rtl::OUString("foobarother"),
148 : (rtl::OUString("foobarfoo").
149 : replaceFirst(
150 : rtl::OUString("foo"),
151 1 : rtl::OUString("other"), &n)));
152 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
153 : }
154 :
155 : {
156 1 : sal_Int32 n = 4;
157 2 : CPPUNIT_ASSERT_EQUAL(
158 : rtl::OUString("foobarfoo"),
159 : (rtl::OUString("foobarfoo").
160 : replaceFirst(
161 : rtl::OUString("bar"),
162 1 : rtl::OUString("other"), &n)));
163 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
164 : }
165 1 : }
166 :
167 1 : void Test::ustringReplaceFirstAsciiL() {
168 2 : CPPUNIT_ASSERT_EQUAL(
169 : rtl::OUString("otherbarfoo"),
170 : (rtl::OUString("foobarfoo").
171 : replaceFirst("foo",
172 1 : rtl::OUString("other"))));
173 :
174 2 : CPPUNIT_ASSERT_EQUAL(
175 : rtl::OUString("foobarfoo"),
176 : (rtl::OUString("foobarfoo").
177 : replaceFirst("bars",
178 1 : rtl::OUString("other"))));
179 :
180 : {
181 1 : sal_Int32 n = 0;
182 2 : CPPUNIT_ASSERT_EQUAL(
183 : rtl::OUString("otherbarfoo"),
184 : (rtl::OUString("foobarfoo").
185 : replaceFirst("foo",
186 1 : rtl::OUString("other"), &n)));
187 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
188 : }
189 :
190 : {
191 1 : sal_Int32 n = 1;
192 2 : CPPUNIT_ASSERT_EQUAL(
193 : rtl::OUString("foobarother"),
194 : (rtl::OUString("foobarfoo").
195 : replaceFirst("foo",
196 1 : rtl::OUString("other"), &n)));
197 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
198 : }
199 :
200 : {
201 1 : sal_Int32 n = 4;
202 2 : CPPUNIT_ASSERT_EQUAL(
203 : rtl::OUString("foobarfoo"),
204 : (rtl::OUString("foobarfoo").
205 : replaceFirst("bar",
206 1 : rtl::OUString("other"), &n)));
207 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
208 : }
209 1 : }
210 :
211 1 : void Test::ustringReplaceFirstAsciiLAsciiL() {
212 2 : CPPUNIT_ASSERT_EQUAL(
213 : rtl::OUString("otherbarfoo"),
214 : (rtl::OUString("foobarfoo").
215 1 : replaceFirst("foo", "other")));
216 :
217 2 : CPPUNIT_ASSERT_EQUAL(
218 : rtl::OUString("foobarfoo"),
219 : (rtl::OUString("foobarfoo").
220 1 : replaceFirst("bars", "other")));
221 :
222 : {
223 1 : sal_Int32 n = 0;
224 2 : CPPUNIT_ASSERT_EQUAL(
225 : rtl::OUString("otherbarfoo"),
226 : (rtl::OUString("foobarfoo").
227 1 : replaceFirst("foo", "other", &n)));
228 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
229 : }
230 :
231 : {
232 1 : sal_Int32 n = 1;
233 2 : CPPUNIT_ASSERT_EQUAL(
234 : rtl::OUString("foobarother"),
235 : (rtl::OUString("foobarfoo").
236 1 : replaceFirst("foo", "other", &n)));
237 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
238 : }
239 :
240 : {
241 1 : sal_Int32 n = 4;
242 2 : CPPUNIT_ASSERT_EQUAL(
243 : rtl::OUString("foobarfoo"),
244 : (rtl::OUString("foobarfoo").
245 1 : replaceFirst("bar", "other", &n)));
246 1 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
247 : }
248 1 : }
249 :
250 1 : void Test::ustringReplaceAll() {
251 2 : CPPUNIT_ASSERT_EQUAL(
252 : rtl::OUString("otherbarother"),
253 : rtl::OUString("foobarfoo").replaceAll(
254 : rtl::OUString("foo"),
255 1 : rtl::OUString("other")));
256 :
257 2 : CPPUNIT_ASSERT_EQUAL(
258 : rtl::OUString("foobarfoo"),
259 : rtl::OUString("foobarfoo").replaceAll(
260 : rtl::OUString("bars"),
261 1 : rtl::OUString("other")));
262 :
263 2 : CPPUNIT_ASSERT_EQUAL(
264 : rtl::OUString("xxa"),
265 : rtl::OUString("xaa").replaceAll(
266 : rtl::OUString("xa"),
267 1 : rtl::OUString("xx")));
268 1 : }
269 :
270 1 : void Test::ustringReplaceAllAsciiL() {
271 2 : CPPUNIT_ASSERT_EQUAL(
272 : rtl::OUString("otherbarother"),
273 : (rtl::OUString("foobarfoo").
274 : replaceAll("foo",
275 1 : rtl::OUString("other"))));
276 :
277 2 : CPPUNIT_ASSERT_EQUAL(
278 : rtl::OUString("foobarfoo"),
279 : (rtl::OUString("foobarfoo").
280 : replaceAll("bars",
281 1 : rtl::OUString("other"))));
282 :
283 2 : CPPUNIT_ASSERT_EQUAL(
284 : rtl::OUString("xxa"),
285 : rtl::OUString("xaa").replaceAll(
286 : "xa",
287 1 : rtl::OUString("xx")));
288 1 : }
289 :
290 1 : void Test::ustringReplaceAllAsciiLAsciiL() {
291 2 : CPPUNIT_ASSERT_EQUAL(
292 : rtl::OUString("otherbarother"),
293 : (rtl::OUString("foobarfoo").
294 1 : replaceAll("foo", "other")));
295 :
296 2 : CPPUNIT_ASSERT_EQUAL(
297 : rtl::OUString("foobarfoo"),
298 : (rtl::OUString("foobarfoo").
299 1 : replaceAll("bars", "other")));
300 :
301 2 : CPPUNIT_ASSERT_EQUAL(
302 : rtl::OUString("xxa"),
303 : (rtl::OUString("xaa").
304 1 : replaceAll("xa", "xx")));
305 1 : }
306 :
307 : }
308 :
309 3 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
310 :
311 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|