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 48 : 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 4 : CPPUNIT_TEST_SUITE(Test);
62 2 : CPPUNIT_TEST(stringReplaceFirst);
63 2 : CPPUNIT_TEST(stringReplaceAll);
64 2 : CPPUNIT_TEST(ustringReplaceFirst);
65 2 : CPPUNIT_TEST(ustringReplaceFirstAsciiL);
66 2 : CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
67 2 : CPPUNIT_TEST(ustringReplaceAll);
68 2 : CPPUNIT_TEST(ustringReplaceAllAsciiL);
69 2 : CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
70 4 : CPPUNIT_TEST_SUITE_END();
71 : };
72 :
73 2 : void Test::stringReplaceFirst() {
74 4 : CPPUNIT_ASSERT_EQUAL(
75 : rtl::OString("otherbarfoo"),
76 2 : rtl::OString("foobarfoo").replaceFirst("foo", "other"));
77 :
78 4 : CPPUNIT_ASSERT_EQUAL(
79 : rtl::OString("foobarfoo"),
80 2 : rtl::OString("foobarfoo").replaceFirst("bars", "other"));
81 :
82 : {
83 2 : sal_Int32 n = 0;
84 4 : CPPUNIT_ASSERT_EQUAL(
85 : rtl::OString("otherbarfoo"),
86 2 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
87 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
88 : }
89 :
90 : {
91 2 : sal_Int32 n = 1;
92 4 : CPPUNIT_ASSERT_EQUAL(
93 : rtl::OString("foobarother"),
94 2 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
95 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
96 : }
97 :
98 : {
99 2 : sal_Int32 n = 4;
100 4 : CPPUNIT_ASSERT_EQUAL(
101 : rtl::OString("foobarfoo"),
102 2 : rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
103 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
104 : }
105 2 : }
106 :
107 2 : void Test::stringReplaceAll() {
108 4 : CPPUNIT_ASSERT_EQUAL(
109 : rtl::OString("otherbarother"),
110 2 : rtl::OString("foobarfoo").replaceAll("foo", "other"));
111 :
112 4 : CPPUNIT_ASSERT_EQUAL(
113 : rtl::OString("foobarfoo"),
114 2 : rtl::OString("foobarfoo").replaceAll("bars", "other"));
115 :
116 4 : CPPUNIT_ASSERT_EQUAL(
117 2 : rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
118 2 : }
119 :
120 2 : void Test::ustringReplaceFirst() {
121 4 : CPPUNIT_ASSERT_EQUAL(
122 : rtl::OUString("otherbarfoo"),
123 : rtl::OUString("foobarfoo").replaceFirst(
124 : rtl::OUString("foo"),
125 2 : rtl::OUString("other")));
126 :
127 4 : CPPUNIT_ASSERT_EQUAL(
128 : rtl::OUString("foobarfoo"),
129 : rtl::OUString("foobarfoo").replaceFirst(
130 : rtl::OUString("bars"),
131 2 : rtl::OUString("other")));
132 :
133 : {
134 2 : sal_Int32 n = 0;
135 4 : CPPUNIT_ASSERT_EQUAL(
136 : rtl::OUString("otherbarfoo"),
137 : (rtl::OUString("foobarfoo").
138 : replaceFirst(
139 : rtl::OUString("foo"),
140 2 : rtl::OUString("other"), &n)));
141 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
142 : }
143 :
144 : {
145 2 : sal_Int32 n = 1;
146 4 : CPPUNIT_ASSERT_EQUAL(
147 : rtl::OUString("foobarother"),
148 : (rtl::OUString("foobarfoo").
149 : replaceFirst(
150 : rtl::OUString("foo"),
151 2 : rtl::OUString("other"), &n)));
152 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
153 : }
154 :
155 : {
156 2 : sal_Int32 n = 4;
157 4 : CPPUNIT_ASSERT_EQUAL(
158 : rtl::OUString("foobarfoo"),
159 : (rtl::OUString("foobarfoo").
160 : replaceFirst(
161 : rtl::OUString("bar"),
162 2 : rtl::OUString("other"), &n)));
163 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
164 : }
165 2 : }
166 :
167 2 : void Test::ustringReplaceFirstAsciiL() {
168 4 : CPPUNIT_ASSERT_EQUAL(
169 : rtl::OUString("otherbarfoo"),
170 : (rtl::OUString("foobarfoo").
171 : replaceFirst("foo",
172 2 : rtl::OUString("other"))));
173 :
174 4 : CPPUNIT_ASSERT_EQUAL(
175 : rtl::OUString("foobarfoo"),
176 : (rtl::OUString("foobarfoo").
177 : replaceFirst("bars",
178 2 : rtl::OUString("other"))));
179 :
180 : {
181 2 : sal_Int32 n = 0;
182 4 : CPPUNIT_ASSERT_EQUAL(
183 : rtl::OUString("otherbarfoo"),
184 : (rtl::OUString("foobarfoo").
185 : replaceFirst("foo",
186 2 : rtl::OUString("other"), &n)));
187 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
188 : }
189 :
190 : {
191 2 : sal_Int32 n = 1;
192 4 : CPPUNIT_ASSERT_EQUAL(
193 : rtl::OUString("foobarother"),
194 : (rtl::OUString("foobarfoo").
195 : replaceFirst("foo",
196 2 : rtl::OUString("other"), &n)));
197 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
198 : }
199 :
200 : {
201 2 : sal_Int32 n = 4;
202 4 : CPPUNIT_ASSERT_EQUAL(
203 : rtl::OUString("foobarfoo"),
204 : (rtl::OUString("foobarfoo").
205 : replaceFirst("bar",
206 2 : rtl::OUString("other"), &n)));
207 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
208 : }
209 2 : }
210 :
211 2 : void Test::ustringReplaceFirstAsciiLAsciiL() {
212 4 : CPPUNIT_ASSERT_EQUAL(
213 : rtl::OUString("otherbarfoo"),
214 : (rtl::OUString("foobarfoo").
215 2 : replaceFirst("foo", "other")));
216 :
217 4 : CPPUNIT_ASSERT_EQUAL(
218 : rtl::OUString("foobarfoo"),
219 : (rtl::OUString("foobarfoo").
220 2 : replaceFirst("bars", "other")));
221 :
222 : {
223 2 : sal_Int32 n = 0;
224 4 : CPPUNIT_ASSERT_EQUAL(
225 : rtl::OUString("otherbarfoo"),
226 : (rtl::OUString("foobarfoo").
227 2 : replaceFirst("foo", "other", &n)));
228 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
229 : }
230 :
231 : {
232 2 : sal_Int32 n = 1;
233 4 : CPPUNIT_ASSERT_EQUAL(
234 : rtl::OUString("foobarother"),
235 : (rtl::OUString("foobarfoo").
236 2 : replaceFirst("foo", "other", &n)));
237 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
238 : }
239 :
240 : {
241 2 : sal_Int32 n = 4;
242 4 : CPPUNIT_ASSERT_EQUAL(
243 : rtl::OUString("foobarfoo"),
244 : (rtl::OUString("foobarfoo").
245 2 : replaceFirst("bar", "other", &n)));
246 2 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
247 : }
248 2 : }
249 :
250 2 : void Test::ustringReplaceAll() {
251 4 : CPPUNIT_ASSERT_EQUAL(
252 : rtl::OUString("otherbarother"),
253 : rtl::OUString("foobarfoo").replaceAll(
254 : rtl::OUString("foo"),
255 2 : rtl::OUString("other")));
256 :
257 4 : CPPUNIT_ASSERT_EQUAL(
258 : rtl::OUString("foobarfoo"),
259 : rtl::OUString("foobarfoo").replaceAll(
260 : rtl::OUString("bars"),
261 2 : rtl::OUString("other")));
262 :
263 4 : CPPUNIT_ASSERT_EQUAL(
264 : rtl::OUString("xxa"),
265 : rtl::OUString("xaa").replaceAll(
266 : rtl::OUString("xa"),
267 2 : rtl::OUString("xx")));
268 2 : }
269 :
270 2 : void Test::ustringReplaceAllAsciiL() {
271 4 : CPPUNIT_ASSERT_EQUAL(
272 : rtl::OUString("otherbarother"),
273 : (rtl::OUString("foobarfoo").
274 : replaceAll("foo",
275 2 : rtl::OUString("other"))));
276 :
277 4 : CPPUNIT_ASSERT_EQUAL(
278 : rtl::OUString("foobarfoo"),
279 : (rtl::OUString("foobarfoo").
280 : replaceAll("bars",
281 2 : rtl::OUString("other"))));
282 :
283 4 : CPPUNIT_ASSERT_EQUAL(
284 : rtl::OUString("xxa"),
285 : rtl::OUString("xaa").replaceAll(
286 : "xa",
287 2 : rtl::OUString("xx")));
288 2 : }
289 :
290 2 : void Test::ustringReplaceAllAsciiLAsciiL() {
291 4 : CPPUNIT_ASSERT_EQUAL(
292 : rtl::OUString("otherbarother"),
293 : (rtl::OUString("foobarfoo").
294 2 : replaceAll("foo", "other")));
295 :
296 4 : CPPUNIT_ASSERT_EQUAL(
297 : rtl::OUString("foobarfoo"),
298 : (rtl::OUString("foobarfoo").
299 2 : replaceAll("bars", "other")));
300 :
301 4 : CPPUNIT_ASSERT_EQUAL(
302 : rtl::OUString("xxa"),
303 : (rtl::OUString("xaa").
304 2 : replaceAll("xa", "xx")));
305 2 : }
306 :
307 : }
308 :
309 6 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
310 :
311 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|