Branch data 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 : : #include <ostream>
30 : :
31 : : #include <sal/types.h>
32 : : #include "cppunit/TestAssert.h"
33 : : #include "cppunit/TestFixture.h"
34 : : #include "cppunit/extensions/HelperMacros.h"
35 : : #include "rtl/oustringostreaminserter.hxx"
36 : : #include "rtl/string.h"
37 : : #include "rtl/string.hxx"
38 : : #include "rtl/ustring.h"
39 : : #include "rtl/ustring.hxx"
40 : :
41 : : namespace rtl {
42 : :
43 : : template< typename charT, typename traits > std::basic_ostream<charT, traits> &
44 : 0 : operator <<(
45 : : std::basic_ostream<charT, traits> & stream, rtl::OString const & string)
46 : : {
47 : 0 : return stream << string.getStr();
48 : : // best effort; potentially loses data due to embedded null characters
49 : : }
50 : :
51 : : }
52 : :
53 : : namespace {
54 : :
55 [ - + ]: 120 : class Test: public CppUnit::TestFixture {
56 : : private:
57 : : void stringReplaceFirst();
58 : :
59 : : void stringReplaceAll();
60 : :
61 : : void ustringReplaceFirst();
62 : :
63 : : void ustringReplaceFirstAsciiL();
64 : :
65 : : void ustringReplaceFirstAsciiLAsciiL();
66 : :
67 : : void ustringReplaceAll();
68 : :
69 : : void ustringReplaceAllAsciiL();
70 : :
71 : : void ustringReplaceAllAsciiLAsciiL();
72 : :
73 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE(Test);
[ + - ][ + - ]
[ # # ]
74 [ + - ][ + - ]: 5 : CPPUNIT_TEST(stringReplaceFirst);
[ + - ][ + - ]
[ + - ][ + - ]
75 [ + - ][ + - ]: 5 : CPPUNIT_TEST(stringReplaceAll);
[ + - ][ + - ]
[ + - ][ + - ]
76 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceFirst);
[ + - ][ + - ]
[ + - ][ + - ]
77 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceFirstAsciiL);
[ + - ][ + - ]
[ + - ][ + - ]
78 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceFirstAsciiLAsciiL);
[ + - ][ + - ]
[ + - ][ + - ]
79 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceAll);
[ + - ][ + - ]
[ + - ][ + - ]
80 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceAllAsciiL);
[ + - ][ + - ]
[ + - ][ + - ]
81 [ + - ][ + - ]: 5 : CPPUNIT_TEST(ustringReplaceAllAsciiLAsciiL);
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ]
82 [ + - ][ + - ]: 10 : CPPUNIT_TEST_SUITE_END();
[ + - ][ + - ]
[ + - ][ + - ]
[ + - ][ + - ]
83 : : };
84 : :
85 : 5 : void Test::stringReplaceFirst() {
86 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
87 : : rtl::OString("otherbarfoo"),
88 [ + - ]: 5 : rtl::OString("foobarfoo").replaceFirst("foo", "other"));
89 : :
90 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
91 : : rtl::OString("foobarfoo"),
92 [ + - ]: 5 : rtl::OString("foobarfoo").replaceFirst("bars", "other"));
93 : :
94 : : {
95 : 5 : sal_Int32 n = 0;
96 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
97 : : rtl::OString("otherbarfoo"),
98 [ + - ]: 5 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
99 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
[ + - ][ + - ]
[ + - ]
100 : : }
101 : :
102 : : {
103 : 5 : sal_Int32 n = 1;
104 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
105 : : rtl::OString("foobarother"),
106 [ + - ]: 5 : rtl::OString("foobarfoo").replaceFirst("foo", "other", &n));
107 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
[ + - ][ + - ]
[ + - ]
108 : : }
109 : :
110 : : {
111 : 5 : sal_Int32 n = 4;
112 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
113 : : rtl::OString("foobarfoo"),
114 [ + - ]: 5 : rtl::OString("foobarfoo").replaceFirst("bar", "other", &n));
115 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
[ + - ][ + - ]
[ + - ]
116 : : }
117 : 5 : }
118 : :
119 : 5 : void Test::stringReplaceAll() {
120 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
121 : : rtl::OString("otherbarother"),
122 [ + - ]: 5 : rtl::OString("foobarfoo").replaceAll("foo", "other"));
123 : :
124 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
125 : : rtl::OString("foobarfoo"),
126 [ + - ]: 5 : rtl::OString("foobarfoo").replaceAll("bars", "other"));
127 : :
128 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
129 [ + - ]: 5 : rtl::OString("xxa"), rtl::OString("xaa").replaceAll("xa", "xx"));
130 : 5 : }
131 : :
132 : 5 : void Test::ustringReplaceFirst() {
133 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
134 : : rtl::OUString("otherbarfoo"),
135 : : rtl::OUString("foobarfoo").replaceFirst(
136 : : rtl::OUString("foo"),
137 [ + - ]: 5 : rtl::OUString("other")));
138 : :
139 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
140 : : rtl::OUString("foobarfoo"),
141 : : rtl::OUString("foobarfoo").replaceFirst(
142 : : rtl::OUString("bars"),
143 [ + - ]: 5 : rtl::OUString("other")));
144 : :
145 : : {
146 : 5 : sal_Int32 n = 0;
147 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
148 : : rtl::OUString("otherbarfoo"),
149 : : (rtl::OUString("foobarfoo").
150 : : replaceFirst(
151 : : rtl::OUString("foo"),
152 [ + - ]: 5 : rtl::OUString("other"), &n)));
153 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
[ + - ][ + - ]
[ + - ]
154 : : }
155 : :
156 : : {
157 : 5 : sal_Int32 n = 1;
158 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
159 : : rtl::OUString("foobarother"),
160 : : (rtl::OUString("foobarfoo").
161 : : replaceFirst(
162 : : rtl::OUString("foo"),
163 [ + - ]: 5 : rtl::OUString("other"), &n)));
164 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
[ + - ][ + - ]
[ + - ]
165 : : }
166 : :
167 : : {
168 : 5 : sal_Int32 n = 4;
169 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
170 : : rtl::OUString("foobarfoo"),
171 : : (rtl::OUString("foobarfoo").
172 : : replaceFirst(
173 : : rtl::OUString("bar"),
174 [ + - ]: 5 : rtl::OUString("other"), &n)));
175 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
[ + - ][ + - ]
[ + - ]
176 : : }
177 : 5 : }
178 : :
179 : 5 : void Test::ustringReplaceFirstAsciiL() {
180 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
181 : : rtl::OUString("otherbarfoo"),
182 : : (rtl::OUString("foobarfoo").
183 : : replaceFirst("foo",
184 [ + - ]: 5 : rtl::OUString("other"))));
185 : :
186 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
187 : : rtl::OUString("foobarfoo"),
188 : : (rtl::OUString("foobarfoo").
189 : : replaceFirst("bars",
190 [ + - ]: 5 : rtl::OUString("other"))));
191 : :
192 : : {
193 : 5 : sal_Int32 n = 0;
194 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
195 : : rtl::OUString("otherbarfoo"),
196 : : (rtl::OUString("foobarfoo").
197 : : replaceFirst("foo",
198 [ + - ]: 5 : rtl::OUString("other"), &n)));
199 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
[ + - ][ + - ]
[ + - ]
200 : : }
201 : :
202 : : {
203 : 5 : sal_Int32 n = 1;
204 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
205 : : rtl::OUString("foobarother"),
206 : : (rtl::OUString("foobarfoo").
207 : : replaceFirst("foo",
208 [ + - ]: 5 : rtl::OUString("other"), &n)));
209 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
[ + - ][ + - ]
[ + - ]
210 : : }
211 : :
212 : : {
213 : 5 : sal_Int32 n = 4;
214 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
215 : : rtl::OUString("foobarfoo"),
216 : : (rtl::OUString("foobarfoo").
217 : : replaceFirst("bar",
218 [ + - ]: 5 : rtl::OUString("other"), &n)));
219 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
[ + - ][ + - ]
[ + - ]
220 : : }
221 : 5 : }
222 : :
223 : 5 : void Test::ustringReplaceFirstAsciiLAsciiL() {
224 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
225 : : rtl::OUString("otherbarfoo"),
226 : : (rtl::OUString("foobarfoo").
227 [ + - ]: 5 : replaceFirst("foo", "other")));
228 : :
229 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
230 : : rtl::OUString("foobarfoo"),
231 : : (rtl::OUString("foobarfoo").
232 [ + - ]: 5 : replaceFirst("bars", "other")));
233 : :
234 : : {
235 : 5 : sal_Int32 n = 0;
236 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
237 : : rtl::OUString("otherbarfoo"),
238 : : (rtl::OUString("foobarfoo").
239 [ + - ]: 5 : replaceFirst("foo", "other", &n)));
240 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(0), n);
[ + - ][ + - ]
[ + - ]
241 : : }
242 : :
243 : : {
244 : 5 : sal_Int32 n = 1;
245 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
246 : : rtl::OUString("foobarother"),
247 : : (rtl::OUString("foobarfoo").
248 [ + - ]: 5 : replaceFirst("foo", "other", &n)));
249 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(6), n);
[ + - ][ + - ]
[ + - ]
250 : : }
251 : :
252 : : {
253 : 5 : sal_Int32 n = 4;
254 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
255 : : rtl::OUString("foobarfoo"),
256 : : (rtl::OUString("foobarfoo").
257 [ + - ]: 5 : replaceFirst("bar", "other", &n)));
258 [ + - ][ + - ]: 5 : CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), n);
[ + - ][ + - ]
[ + - ]
259 : : }
260 : 5 : }
261 : :
262 : 5 : void Test::ustringReplaceAll() {
263 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
264 : : rtl::OUString("otherbarother"),
265 : : rtl::OUString("foobarfoo").replaceAll(
266 : : rtl::OUString("foo"),
267 [ + - ]: 5 : rtl::OUString("other")));
268 : :
269 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
270 : : rtl::OUString("foobarfoo"),
271 : : rtl::OUString("foobarfoo").replaceAll(
272 : : rtl::OUString("bars"),
273 [ + - ]: 5 : rtl::OUString("other")));
274 : :
275 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
276 : : rtl::OUString("xxa"),
277 : : rtl::OUString("xaa").replaceAll(
278 : : rtl::OUString("xa"),
279 [ + - ]: 5 : rtl::OUString("xx")));
280 : 5 : }
281 : :
282 : 5 : void Test::ustringReplaceAllAsciiL() {
283 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
284 : : rtl::OUString("otherbarother"),
285 : : (rtl::OUString("foobarfoo").
286 : : replaceAll("foo",
287 [ + - ]: 5 : rtl::OUString("other"))));
288 : :
289 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
290 : : rtl::OUString("foobarfoo"),
291 : : (rtl::OUString("foobarfoo").
292 : : replaceAll("bars",
293 [ + - ]: 5 : rtl::OUString("other"))));
294 : :
295 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
296 : : rtl::OUString("xxa"),
297 : : rtl::OUString("xaa").replaceAll(
298 : : "xa",
299 [ + - ]: 5 : rtl::OUString("xx")));
300 : 5 : }
301 : :
302 : 5 : void Test::ustringReplaceAllAsciiLAsciiL() {
303 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
304 : : rtl::OUString("otherbarother"),
305 : : (rtl::OUString("foobarfoo").
306 [ + - ]: 5 : replaceAll("foo", "other")));
307 : :
308 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
309 : : rtl::OUString("foobarfoo"),
310 : : (rtl::OUString("foobarfoo").
311 [ + - ]: 5 : replaceAll("bars", "other")));
312 : :
313 [ + - ][ + - ]: 10 : CPPUNIT_ASSERT_EQUAL(
[ + - ][ + - ]
314 : : rtl::OUString("xxa"),
315 : : (rtl::OUString("xaa").
316 [ + - ]: 5 : replaceAll("xa", "xx")));
317 : 5 : }
318 : :
319 : : }
320 : :
321 [ + - ][ + - ]: 15 : CPPUNIT_TEST_SUITE_REGISTRATION(Test);
322 : :
323 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|