CppunitTest_sw_tiledrendering: factor out common code
[LibreOffice.git] / o3tl / qa / test-string_view.cxx
blobcb96f15e5c5ec8fa272e601391c250a0be8d6d62
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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 <string_view>
14 #include <cppunit/TestAssert.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <o3tl/safeint.hxx>
19 #include <o3tl/string_view.hxx>
20 #include <rtl/string.hxx>
21 #include <rtl/ustring.hxx>
22 #include <test/cppunitasserthelper.hxx>
24 namespace
26 OString ostringEmpty() { return {}; } // avoid loplugin:stringview
27 OString ostringDoof() { return "doof"_ostr; } // avoid loplugin:stringview
28 OString ostringFoo() { return "foo"_ostr; } // avoid loplugin:stringview
29 OString ostringFoobars() { return "foobars"_ostr; } // avoid loplugin:stringview
30 OString ostringFood() { return "food"_ostr; } // avoid loplugin:stringview
31 OString ostringOof() { return "oof"_ostr; } // avoid loplugin:stringview
32 OString ostringSraboof() { return "sraboof"_ostr; } // avoid loplugin:stringview
33 OUString oustringEmpty() { return {}; } // avoid loplugin:stringview
34 OUString oustringDoof() { return u"doof"_ustr; } // avoid loplugin:stringview
35 OUString oustringFoo() { return u"foo"_ustr; } // avoid loplugin:stringview
36 OUString oustringFoobars() { return u"foobars"_ustr; } // avoid loplugin:stringview
37 OUString oustringFood() { return u"food"_ustr; } // avoid loplugin:stringview
38 OUString oustringOof() { return u"oof"_ustr; } // avoid loplugin:stringview
39 OUString oustringSraboof() { return u"sraboof"_ustr; } // avoid loplugin:stringview
41 class Test : public CppUnit::TestFixture
43 private:
44 CPPUNIT_TEST_SUITE(Test);
45 CPPUNIT_TEST(testStartsWith);
46 CPPUNIT_TEST(testStartsWithRest);
47 CPPUNIT_TEST(testEndsWith);
48 CPPUNIT_TEST(testEndsWithRest);
49 CPPUNIT_TEST(testEqualsIgnoreAsciiCase);
50 CPPUNIT_TEST(testGetToken);
51 CPPUNIT_TEST(testIterateCodePoints);
52 CPPUNIT_TEST_SUITE_END();
54 void testStartsWith()
56 using namespace std::string_view_literals;
57 CPPUNIT_ASSERT(o3tl::starts_with(""sv, ""sv));
58 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, "foo"sv));
59 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ""sv));
60 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, "foo"sv));
61 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "food"sv));
62 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "foobars"sv));
63 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, 'f'));
64 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, 'f'));
65 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, 'g'));
66 CPPUNIT_ASSERT(o3tl::starts_with(""sv, ""));
67 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, "foo"));
68 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ""));
69 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, "foo"));
70 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "food"));
71 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "foobars"));
72 CPPUNIT_ASSERT(o3tl::starts_with(""sv, ostringEmpty()));
73 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, ostringFoo()));
74 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ostringEmpty()));
75 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ostringFoo()));
76 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, ostringFood()));
77 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, ostringFoobars()));
78 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, u""sv));
79 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u"foo"sv));
80 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u""sv));
81 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u"foo"sv));
82 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"food"sv));
83 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"foobars"sv));
84 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u'f'));
85 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u'f'));
86 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u'g'));
87 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, u""));
88 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u"foo"));
89 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u""));
90 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u"foo"));
91 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"food"));
92 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"foobars"));
93 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, oustringEmpty()));
94 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, oustringFoo()));
95 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, oustringEmpty()));
96 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, oustringFoo()));
97 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, oustringFood()));
98 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, oustringFoobars()));
101 void testStartsWithRest()
103 using namespace std::string_view_literals;
105 std::string_view rest;
106 CPPUNIT_ASSERT(o3tl::starts_with(""sv, ""sv, &rest));
107 CPPUNIT_ASSERT_EQUAL(""sv, rest);
110 std::string_view rest;
111 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, "foo"sv, &rest));
112 CPPUNIT_ASSERT_EQUAL(""sv, rest);
115 std::string_view rest;
116 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ""sv, &rest));
117 CPPUNIT_ASSERT_EQUAL("foobar"sv, rest);
120 std::string_view rest;
121 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, "foo"sv, &rest));
122 CPPUNIT_ASSERT_EQUAL("bar"sv, rest);
125 std::string_view rest;
126 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "food"sv, &rest));
127 CPPUNIT_ASSERT_EQUAL(""sv, rest);
130 std::string_view rest;
131 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "foobars"sv, &rest));
132 CPPUNIT_ASSERT_EQUAL(""sv, rest);
135 std::string_view rest;
136 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, 'f', &rest));
137 CPPUNIT_ASSERT_EQUAL(""sv, rest);
140 std::string_view rest;
141 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, 'f', &rest));
142 CPPUNIT_ASSERT_EQUAL("oobar"sv, rest);
145 std::string_view rest;
146 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, 'g', &rest));
147 CPPUNIT_ASSERT_EQUAL(""sv, rest);
150 std::string_view rest;
151 CPPUNIT_ASSERT(o3tl::starts_with(""sv, "", &rest));
152 CPPUNIT_ASSERT_EQUAL(""sv, rest);
155 std::string_view rest;
156 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, "foo", &rest));
157 CPPUNIT_ASSERT_EQUAL(""sv, rest);
160 std::string_view rest;
161 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, "", &rest));
162 CPPUNIT_ASSERT_EQUAL("foobar"sv, rest);
165 std::string_view rest;
166 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, "foo", &rest));
167 CPPUNIT_ASSERT_EQUAL("bar"sv, rest);
170 std::string_view rest;
171 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "food", &rest));
172 CPPUNIT_ASSERT_EQUAL(""sv, rest);
175 std::string_view rest;
176 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, "foobars", &rest));
177 CPPUNIT_ASSERT_EQUAL(""sv, rest);
180 std::string_view rest;
181 CPPUNIT_ASSERT(o3tl::starts_with(""sv, ostringEmpty(), &rest));
182 CPPUNIT_ASSERT_EQUAL(""sv, rest);
185 std::string_view rest;
186 CPPUNIT_ASSERT(!o3tl::starts_with(""sv, ostringFoo(), &rest));
187 CPPUNIT_ASSERT_EQUAL(""sv, rest);
190 std::string_view rest;
191 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ostringEmpty(), &rest));
192 CPPUNIT_ASSERT_EQUAL("foobar"sv, rest);
195 std::string_view rest;
196 CPPUNIT_ASSERT(o3tl::starts_with("foobar"sv, ostringFoo(), &rest));
197 CPPUNIT_ASSERT_EQUAL("bar"sv, rest);
200 std::string_view rest;
201 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, ostringFood(), &rest));
202 CPPUNIT_ASSERT_EQUAL(""sv, rest);
205 std::string_view rest;
206 CPPUNIT_ASSERT(!o3tl::starts_with("foobar"sv, ostringFoobars(), &rest));
207 CPPUNIT_ASSERT_EQUAL(""sv, rest);
210 std::u16string_view rest;
211 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, u""sv, &rest));
212 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
215 std::u16string_view rest;
216 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u"foo"sv, &rest));
217 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
220 std::u16string_view rest;
221 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u""sv, &rest));
222 CPPUNIT_ASSERT_EQUAL(u"foobar"sv, rest);
225 std::u16string_view rest;
226 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u"foo"sv, &rest));
227 CPPUNIT_ASSERT_EQUAL(u"bar"sv, rest);
230 std::u16string_view rest;
231 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"food"sv, &rest));
232 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
235 std::u16string_view rest;
236 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"foobars"sv, &rest));
237 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
240 std::u16string_view rest;
241 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u'f', &rest));
242 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
245 std::u16string_view rest;
246 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u'f', &rest));
247 CPPUNIT_ASSERT_EQUAL(u"oobar"sv, rest);
250 std::u16string_view rest;
251 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u'g', &rest));
252 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
255 std::u16string_view rest;
256 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, u"", &rest));
257 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
260 std::u16string_view rest;
261 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, u"foo", &rest));
262 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
265 std::u16string_view rest;
266 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u"", &rest));
267 CPPUNIT_ASSERT_EQUAL(u"foobar"sv, rest);
270 std::u16string_view rest;
271 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, u"foo", &rest));
272 CPPUNIT_ASSERT_EQUAL(u"bar"sv, rest);
275 std::u16string_view rest;
276 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"food", &rest));
277 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
280 std::u16string_view rest;
281 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, u"foobars", &rest));
282 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
285 std::u16string_view rest;
286 CPPUNIT_ASSERT(o3tl::starts_with(u""sv, oustringEmpty(), &rest));
287 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
290 std::u16string_view rest;
291 CPPUNIT_ASSERT(!o3tl::starts_with(u""sv, oustringFoo(), &rest));
292 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
295 std::u16string_view rest;
296 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, oustringEmpty(), &rest));
297 CPPUNIT_ASSERT_EQUAL(u"foobar"sv, rest);
300 std::u16string_view rest;
301 CPPUNIT_ASSERT(o3tl::starts_with(u"foobar"sv, oustringFoo(), &rest));
302 CPPUNIT_ASSERT_EQUAL(u"bar"sv, rest);
305 std::u16string_view rest;
306 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, oustringFood(), &rest));
307 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
310 std::u16string_view rest;
311 CPPUNIT_ASSERT(!o3tl::starts_with(u"foobar"sv, oustringFoobars(), &rest));
312 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
316 void testEndsWith()
318 using namespace std::string_view_literals;
319 CPPUNIT_ASSERT(o3tl::ends_with(""sv, ""sv));
320 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, "oof"sv));
321 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ""sv));
322 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, "oof"sv));
323 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "doof"sv));
324 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "sraboof"sv));
325 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, 'f'));
326 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, 'f'));
327 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, 'g'));
328 CPPUNIT_ASSERT(o3tl::ends_with(""sv, ""));
329 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, "oof"));
330 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ""));
331 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, "oof"));
332 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "doof"));
333 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "sraboof"));
334 CPPUNIT_ASSERT(o3tl::ends_with(""sv, ostringEmpty()));
335 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, ostringOof()));
336 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ostringEmpty()));
337 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ostringOof()));
338 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, ostringDoof()));
339 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, ostringSraboof()));
340 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, u""sv));
341 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u"oof"sv));
342 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u""sv));
343 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u"oof"sv));
344 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"doof"sv));
345 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"sraboof"sv));
346 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u'f'));
347 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u'f'));
348 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u'g'));
349 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, u""));
350 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u"oof"));
351 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u""));
352 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u"oof"));
353 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"doof"));
354 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"sraboof"));
355 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, oustringEmpty()));
356 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, oustringOof()));
357 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, oustringEmpty()));
358 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, oustringOof()));
359 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, oustringDoof()));
360 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, oustringSraboof()));
363 void testEndsWithRest()
365 using namespace std::string_view_literals;
367 std::string_view rest;
368 CPPUNIT_ASSERT(o3tl::ends_with(""sv, ""sv, &rest));
369 CPPUNIT_ASSERT_EQUAL(""sv, rest);
372 std::string_view rest;
373 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, "oof"sv, &rest));
374 CPPUNIT_ASSERT_EQUAL(""sv, rest);
377 std::string_view rest;
378 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ""sv, &rest));
379 CPPUNIT_ASSERT_EQUAL("raboof"sv, rest);
382 std::string_view rest;
383 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, "oof"sv, &rest));
384 CPPUNIT_ASSERT_EQUAL("rab"sv, rest);
387 std::string_view rest;
388 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "doof"sv, &rest));
389 CPPUNIT_ASSERT_EQUAL(""sv, rest);
392 std::string_view rest;
393 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "sraboof"sv, &rest));
394 CPPUNIT_ASSERT_EQUAL(""sv, rest);
397 std::string_view rest;
398 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, 'f', &rest));
399 CPPUNIT_ASSERT_EQUAL(""sv, rest);
402 std::string_view rest;
403 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, 'f', &rest));
404 CPPUNIT_ASSERT_EQUAL("raboo"sv, rest);
407 std::string_view rest;
408 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, 'g', &rest));
409 CPPUNIT_ASSERT_EQUAL(""sv, rest);
412 std::string_view rest;
413 CPPUNIT_ASSERT(o3tl::ends_with(""sv, "", &rest));
414 CPPUNIT_ASSERT_EQUAL(""sv, rest);
417 std::string_view rest;
418 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, "oof", &rest));
419 CPPUNIT_ASSERT_EQUAL(""sv, rest);
422 std::string_view rest;
423 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, "", &rest));
424 CPPUNIT_ASSERT_EQUAL("raboof"sv, rest);
427 std::string_view rest;
428 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, "oof", &rest));
429 CPPUNIT_ASSERT_EQUAL("rab"sv, rest);
432 std::string_view rest;
433 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "doof", &rest));
434 CPPUNIT_ASSERT_EQUAL(""sv, rest);
437 std::string_view rest;
438 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, "sraboof", &rest));
439 CPPUNIT_ASSERT_EQUAL(""sv, rest);
442 std::string_view rest;
443 CPPUNIT_ASSERT(o3tl::ends_with(""sv, ostringEmpty(), &rest));
444 CPPUNIT_ASSERT_EQUAL(""sv, rest);
447 std::string_view rest;
448 CPPUNIT_ASSERT(!o3tl::ends_with(""sv, ostringOof(), &rest));
449 CPPUNIT_ASSERT_EQUAL(""sv, rest);
452 std::string_view rest;
453 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ostringEmpty(), &rest));
454 CPPUNIT_ASSERT_EQUAL("raboof"sv, rest);
457 std::string_view rest;
458 CPPUNIT_ASSERT(o3tl::ends_with("raboof"sv, ostringOof(), &rest));
459 CPPUNIT_ASSERT_EQUAL("rab"sv, rest);
462 std::string_view rest;
463 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, ostringDoof(), &rest));
464 CPPUNIT_ASSERT_EQUAL(""sv, rest);
467 std::string_view rest;
468 CPPUNIT_ASSERT(!o3tl::ends_with("raboof"sv, ostringSraboof(), &rest));
469 CPPUNIT_ASSERT_EQUAL(""sv, rest);
472 std::u16string_view rest;
473 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, u""sv, &rest));
474 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
477 std::u16string_view rest;
478 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u"oof"sv, &rest));
479 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
482 std::u16string_view rest;
483 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u""sv, &rest));
484 CPPUNIT_ASSERT_EQUAL(u"raboof"sv, rest);
487 std::u16string_view rest;
488 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u"oof"sv, &rest));
489 CPPUNIT_ASSERT_EQUAL(u"rab"sv, rest);
492 std::u16string_view rest;
493 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"doof"sv, &rest));
494 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
497 std::u16string_view rest;
498 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"sraboof"sv, &rest));
499 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
502 std::u16string_view rest;
503 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u'f', &rest));
504 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
507 std::u16string_view rest;
508 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u'f', &rest));
509 CPPUNIT_ASSERT_EQUAL(u"raboo"sv, rest);
512 std::u16string_view rest;
513 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u'g', &rest));
514 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
517 std::u16string_view rest;
518 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, u"", &rest));
519 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
522 std::u16string_view rest;
523 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, u"oof", &rest));
524 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
527 std::u16string_view rest;
528 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u"", &rest));
529 CPPUNIT_ASSERT_EQUAL(u"raboof"sv, rest);
532 std::u16string_view rest;
533 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, u"oof", &rest));
534 CPPUNIT_ASSERT_EQUAL(u"rab"sv, rest);
537 std::u16string_view rest;
538 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"doof", &rest));
539 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
542 std::u16string_view rest;
543 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, u"sraboof", &rest));
544 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
547 std::u16string_view rest;
548 CPPUNIT_ASSERT(o3tl::ends_with(u""sv, oustringEmpty(), &rest));
549 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
552 std::u16string_view rest;
553 CPPUNIT_ASSERT(!o3tl::ends_with(u""sv, oustringOof(), &rest));
554 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
557 std::u16string_view rest;
558 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, oustringEmpty(), &rest));
559 CPPUNIT_ASSERT_EQUAL(u"raboof"sv, rest);
562 std::u16string_view rest;
563 CPPUNIT_ASSERT(o3tl::ends_with(u"raboof"sv, oustringOof(), &rest));
564 CPPUNIT_ASSERT_EQUAL(u"rab"sv, rest);
567 std::u16string_view rest;
568 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, oustringDoof(), &rest));
569 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
572 std::u16string_view rest;
573 CPPUNIT_ASSERT(!o3tl::ends_with(u"raboof"sv, oustringSraboof(), &rest));
574 CPPUNIT_ASSERT_EQUAL(u""sv, rest);
578 void testEqualsIgnoreAsciiCase()
580 using namespace std::string_view_literals;
581 CPPUNIT_ASSERT(o3tl::equalsIgnoreAsciiCase(u"test"sv, u"test"sv));
582 CPPUNIT_ASSERT(!o3tl::equalsIgnoreAsciiCase(u"test"sv, u"test2"sv));
584 CPPUNIT_ASSERT_EQUAL(0, o3tl::compareToIgnoreAsciiCase(u"test"sv, u"test"sv));
585 CPPUNIT_ASSERT_GREATER(0, o3tl::compareToIgnoreAsciiCase(u"zest"sv, u"test"sv));
586 CPPUNIT_ASSERT_LESS(0, o3tl::compareToIgnoreAsciiCase(u"test"sv, u"test2"sv));
589 void testGetToken()
592 // Explicit initialization of suTokenStr to avoid an unhelpful loplugin:stringviewvar;
593 // it is the o3tl::getToken overload taking OUString that we want to test here:
594 OUString suTokenStr(u""_ustr);
595 sal_Int32 nIndex = 0;
598 o3tl::getToken(suTokenStr, 0, ';', nIndex);
599 } while (nIndex >= 0);
600 // should not GPF
603 OUString suTokenStr(u"a;b"_ustr);
605 sal_Int32 nIndex = 0;
607 std::u16string_view suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
608 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'a'", std::u16string_view(u"a"),
609 suToken);
611 suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
612 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'b'", std::u16string_view(u"b"),
613 suToken);
614 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1),
615 nIndex);
618 std::u16string_view suTokenStr(u"a;b.c");
620 sal_Int32 nIndex = 0;
622 std::u16string_view suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
623 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'a'", std::u16string_view(u"a"),
624 suToken);
626 suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
627 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'b'", std::u16string_view(u"b"),
628 suToken);
630 suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
631 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'c'", std::u16string_view(u"c"),
632 suToken);
633 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1),
634 nIndex);
637 std::u16string_view suTokenStr(u"a;;b");
639 sal_Int32 nIndex = 0;
641 std::u16string_view suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
642 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'a'", std::u16string_view(u"a"),
643 suToken);
645 suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
646 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.empty());
648 suToken = o3tl::getToken(suTokenStr, 0, ';', nIndex);
649 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be a 'b'", std::u16string_view(u"b"),
650 suToken);
651 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1),
652 nIndex);
655 std::u16string_view suTokenStr(u"longer.then.ever.");
657 sal_Int32 nIndex = 0;
659 std::u16string_view suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
660 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be 'longer'", std::u16string_view(u"longer"),
661 suToken);
663 suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
664 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be 'then'", std::u16string_view(u"then"),
665 suToken);
667 suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
668 CPPUNIT_ASSERT_EQUAL_MESSAGE("Token should be 'ever'", std::u16string_view(u"ever"),
669 suToken);
671 suToken = o3tl::getToken(suTokenStr, 0, '.', nIndex);
672 CPPUNIT_ASSERT_MESSAGE("Token should be empty", suToken.empty());
674 CPPUNIT_ASSERT_EQUAL_MESSAGE("index should be negative", static_cast<sal_Int32>(-1),
675 nIndex);
678 std::u16string_view ab(u"ab");
679 sal_Int32 n = 0;
680 CPPUNIT_ASSERT_EQUAL_MESSAGE("token should be 'ab'", ab, o3tl::getToken(ab, 0, '-', n));
681 CPPUNIT_ASSERT_EQUAL_MESSAGE("n should be -1", static_cast<sal_Int32>(-1), n);
682 CPPUNIT_ASSERT_MESSAGE("token should be empty", o3tl::getToken(ab, 0, '-', n).empty());
685 std::u16string_view suTokenStr;
686 auto pTokenStr = suTokenStr.data();
687 sal_uInt64 n64 = reinterpret_cast<sal_uInt64>(pTokenStr) / sizeof(sal_Unicode);
688 // Point either to 0x0, or to some random address -4GiB away from this string
689 sal_Int32 n = n64 > o3tl::make_unsigned(SAL_MAX_INT32) ? -SAL_MAX_INT32
690 : -static_cast<sal_Int32>(n64);
691 o3tl::getToken(suTokenStr, 0, ';', n);
692 // should not GPF with negative index
695 CPPUNIT_ASSERT_MESSAGE("compareToAscii",
696 u"aaa"_ustr.compareToAscii("aa")
697 > 0); // just for comparison to following line
698 CPPUNIT_ASSERT_MESSAGE("compareToAscii", o3tl::compareToAscii(u"aaa", "aa") > 0);
700 OUString aa(u"aa"_ustr);
701 CPPUNIT_ASSERT_MESSAGE("compareToAscii",
702 aa.compareToAscii("aaa")
703 < 0); // just for comparison to following line
704 CPPUNIT_ASSERT_MESSAGE("compareToAscii", o3tl::compareToAscii(u"aa", "aaa") < 0);
706 CPPUNIT_ASSERT_MESSAGE(
707 "equalsIgnoreAsciiCase",
708 aa.equalsIgnoreAsciiCase("AA")); // just for comparison to following line
709 CPPUNIT_ASSERT_MESSAGE("equalsIgnoreAsciiCase",
710 o3tl::equalsIgnoreAsciiCase(u"aa", "AA"));
712 CPPUNIT_ASSERT_MESSAGE(
713 "matchIgnoreAsciiCase",
714 aa.matchIgnoreAsciiCase("a")); // just for comparison to following line
715 CPPUNIT_ASSERT_MESSAGE("matchIgnoreAsciiCase", o3tl::matchIgnoreAsciiCase(u"aa", "a"));
717 CPPUNIT_ASSERT_MESSAGE(
718 "endsWithIgnoreAsciiCase",
719 aa.endsWithIgnoreAsciiCase("a")); // just for comparison to following line
720 CPPUNIT_ASSERT_MESSAGE("endsWithIgnoreAsciiCase",
721 o3tl::endsWithIgnoreAsciiCase(u"aa", "a"));
722 CPPUNIT_ASSERT_MESSAGE("endsWithIgnoreAsciiCase",
723 o3tl::endsWithIgnoreAsciiCase(u"aa", u"a"));
727 void testIterateCodePoints()
730 sal_Int32 i = 1;
731 auto const c = o3tl::iterateCodePoints(u"\U00010000", &i, 1);
732 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), i);
733 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0xDC00), c);
736 sal_Int32 i = 2;
737 auto const c = o3tl::iterateCodePoints(u"a\U00010000", &i, -1);
738 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), i);
739 CPPUNIT_ASSERT_EQUAL(sal_uInt32(0x10000), c);
744 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
747 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */