| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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 |
*/ |
| 9 |
|
| 10 |
// activate the extra needed ctor |
| 11 |
#define RTL_STRING_UNITTEST |
| 12 |
|
| 13 |
#include <sal/config.h> |
| 14 |
|
| 15 |
#include <string> |
| 16 |
#include <string_view> |
| 17 |
#include <utility> |
| 18 |
|
| 19 |
#include <o3tl/cppunittraitshelper.hxx> |
| 20 |
#include <sal/types.h> |
| 21 |
#include <cppunit/TestFixture.h> |
| 22 |
#include <cppunit/extensions/HelperMacros.h> |
| 23 |
#include <rtl/ustring.hxx> |
| 24 |
#include <rtl/ustrbuf.hxx> |
| 25 |
|
| 26 |
extern bool rtl_string_unittest_const_literal; |
| 27 |
bool rtl_string_unittest_invalid_conversion; |
| 28 |
|
| 29 |
namespace test::oustring { |
| 30 |
|
| 31 |
class StringLiterals: public CppUnit::TestFixture |
| 32 |
{ |
| 33 |
private: |
| 34 |
void checkCtors(); |
| 35 |
void checkConstexprCtor(); |
| 36 |
void checkUsage(); |
| 37 |
void checkBuffer(); |
| 38 |
void checkOUStringLiteral(); |
| 39 |
void checkOUStringChar(); |
| 40 |
void checkUtf16(); |
| 41 |
void checkEmbeddedNul(); |
| 42 |
void checkOstr(); |
| 43 |
|
| 44 |
void testcall( const char str[] ); |
| 45 |
|
| 46 |
// Check that OUStringLiteral ctor is actually constexpr: |
| 47 |
static constexpr rtlunittest::OUStringLiteral dummy{u"dummy"}; |
| 48 |
|
| 49 |
CPPUNIT_TEST_SUITE(StringLiterals); |
| 50 |
CPPUNIT_TEST(checkCtors); |
| 51 |
CPPUNIT_TEST(checkConstexprCtor); |
| 52 |
CPPUNIT_TEST(checkUsage); |
| 53 |
CPPUNIT_TEST(checkBuffer); |
| 54 |
CPPUNIT_TEST(checkOUStringLiteral); |
| 55 |
CPPUNIT_TEST(checkOUStringChar); |
| 56 |
CPPUNIT_TEST(checkUtf16); |
| 57 |
CPPUNIT_TEST(checkEmbeddedNul); |
| 58 |
CPPUNIT_TEST(checkOstr); |
| 59 |
CPPUNIT_TEST_SUITE_END(); |
| 60 |
}; |
| 61 |
|
| 62 |
// reset the flag, evaluate the expression and return |
| 63 |
// whether the string literal ctor was used (i.e. whether the conversion was valid) |
| 64 |
template<typename T> static bool VALID_CONVERSION( T && expression ) |
| 65 |
{ |
| 66 |
rtl_string_unittest_invalid_conversion = false; |
| 67 |
// OK to std::forward expression twice; what is relevant in both ctor calls |
| 68 |
// is not the content of the passed argument (which is ignored anyway by the |
| 69 |
// special RTL_STRING_UNITTEST ctors) but only its type: |
| 70 |
( void ) rtl::OUString( std::forward<T>(expression) ); |
| 71 |
( void ) rtl::OUStringBuffer( std::forward<T>(expression) ); |
| 72 |
return !rtl_string_unittest_invalid_conversion; |
| 73 |
} |
| 74 |
template<typename T> static bool VALID_CONVERSION_CALL( T f ) |
| 75 |
{ |
| 76 |
rtl_string_unittest_invalid_conversion = false; |
| 77 |
( void ) rtl::OUString( f() ); |
| 78 |
( void ) rtl::OUStringBuffer( f() ); |
| 79 |
return !rtl_string_unittest_invalid_conversion; |
| 80 |
} |
| 81 |
|
| 82 |
void test::oustring::StringLiterals::checkCtors() |
| 83 |
{ |
| 84 |
CPPUNIT_ASSERT( VALID_CONVERSION( "test" )); |
| 85 |
const char good1[] = "test"; |
| 86 |
CPPUNIT_ASSERT( VALID_CONVERSION( good1 )); |
| 87 |
|
| 88 |
CPPUNIT_ASSERT( !VALID_CONVERSION( static_cast<const char*>("test") )); |
| 89 |
const char* bad1 = good1; |
| 90 |
CPPUNIT_ASSERT( !VALID_CONVERSION( bad1 )); |
| 91 |
char bad2[] = "test"; |
| 92 |
CPPUNIT_ASSERT( !VALID_CONVERSION( bad2 )); |
| 93 |
char* bad3 = bad2; |
| 94 |
CPPUNIT_ASSERT( !VALID_CONVERSION( bad3 )); |
| 95 |
const char* bad4[] = { "test1" }; |
| 96 |
CPPUNIT_ASSERT( !VALID_CONVERSION( bad4[ 0 ] )); |
| 97 |
testcall( good1 ); |
| 98 |
|
| 99 |
// This one is technically broken, since the first element is 6 characters test\0\0, |
| 100 |
// but there does not appear a way to detect this by compile time (runtime will assert()). |
| 101 |
// RTL_CONSTASCII_USTRINGPARAM() has the same flaw. |
| 102 |
const char bad5[][ 6 ] = { "test", "test2" }; |
| 103 |
// CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] )); |
| 104 |
CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 1 ] )); |
| 105 |
|
| 106 |
// Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used. |
| 107 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( "" )); |
| 108 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "ab" ), rtl::OUString( "ab" )); |
| 109 |
#if 0 |
| 110 |
// Also check that embedded \0 is included. |
| 111 |
// In fact, allowing this is probably just trouble, so this now asserts. |
| 112 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "\0" ), rtl::OUString( "\0" )); |
| 113 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "a\0b" ), rtl::OUString( "a\0b" )); |
| 114 |
#endif |
| 115 |
} |
| 116 |
|
| 117 |
void test::oustring::StringLiterals::testcall( const char str[] ) |
| 118 |
{ |
| 119 |
CPPUNIT_ASSERT( |
| 120 |
!VALID_CONVERSION_CALL([&str]() { return rtl::OUString(str); })); |
| 121 |
} |
| 122 |
|
| 123 |
void test::oustring::StringLiterals::checkConstexprCtor() |
| 124 |
{ |
| 125 |
static constinit rtl::OUString s(dummy); |
| 126 |
CPPUNIT_ASSERT_EQUAL(oslInterlockedCount(0x40000000), s.pData->refCount); |
| 127 |
// SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx) |
| 128 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength()); |
| 129 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("dummy"), s); |
| 130 |
CPPUNIT_ASSERT_EQUAL( |
| 131 |
static_cast<void const *>(dummy.getStr()), static_cast<void const *>(s.getStr())); |
| 132 |
} |
| 133 |
|
| 134 |
void test::oustring::StringLiterals::checkUsage() |
| 135 |
{ |
| 136 |
// simply check that all string literal based calls work as expected |
| 137 |
// also check that they really use string literal overload and do not convert to OUString |
| 138 |
rtl::OUString foo( "foo" ); |
| 139 |
rtl::OUString FoO( "FoO" ); |
| 140 |
rtl::OUString foobarfoo( "foobarfoo" ); |
| 141 |
rtl::OUString foobar( "foobar" ); |
| 142 |
rtl::OUString FooBaRfoo( "FooBaRfoo" ); |
| 143 |
rtl::OUString FooBaR( "FooBaR" ); |
| 144 |
rtl::OUString bar( "bar" ); |
| 145 |
rtl::OUString test( "test" ); |
| 146 |
|
| 147 |
rtl_string_unittest_const_literal = false; // start checking for OUString conversions |
| 148 |
CPPUNIT_ASSERT_EQUAL( foo, rtl::OUString() = "foo" ); |
| 149 |
CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( "fOo" )); |
| 150 |
CPPUNIT_ASSERT( foobarfoo.match( "bar", 3 )); |
| 151 |
CPPUNIT_ASSERT( foobar.match( "foo" )); |
| 152 |
CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 )); |
| 153 |
CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" )); |
| 154 |
CPPUNIT_ASSERT( foobar.startsWith( "foo" )); |
| 155 |
CPPUNIT_ASSERT( FooBaR.startsWithIgnoreAsciiCase( "foo" )); |
| 156 |
CPPUNIT_ASSERT( foobar.endsWith( "bar" )); |
| 157 |
CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" )); |
| 158 |
CPPUNIT_ASSERT( bool(foo == "foo") ); |
| 159 |
CPPUNIT_ASSERT( bool("foo" == foo) ); |
| 160 |
CPPUNIT_ASSERT( foo != "bar" ); |
| 161 |
CPPUNIT_ASSERT( "foo" != bar ); |
| 162 |
CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(6), foobarfoo.indexOf( "foo", 1 ) ); |
| 163 |
CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(6), foobarfoo.lastIndexOf( "foo" ) ); |
| 164 |
CPPUNIT_ASSERT( bool(foobarfoo.replaceFirst( "foo", test ) == "testbarfoo") ); |
| 165 |
CPPUNIT_ASSERT( bool(foobarfoo.replaceFirst( "foo", "test" ) == "testbarfoo") ); |
| 166 |
CPPUNIT_ASSERT( bool(foobarfoo.replaceAll( "foo", test ) == "testbartest") ); |
| 167 |
CPPUNIT_ASSERT( bool(foobarfoo.replaceAll( "foo", "test" ) == "testbartest") ); |
| 168 |
CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(0), foo.reverseCompareTo( "foo" ) ); |
| 169 |
// if this is not true, some of the calls above converted to OUString |
| 170 |
CPPUNIT_ASSERT( !rtl_string_unittest_const_literal ); |
| 171 |
} |
| 172 |
|
| 173 |
void test::oustring::StringLiterals::checkBuffer() |
| 174 |
{ |
| 175 |
rtl::OUStringBuffer buf; |
| 176 |
buf.append( "foo" ); |
| 177 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf.toString()); |
| 178 |
buf.append( "bar" ); |
| 179 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf.toString()); |
| 180 |
buf.insert( 3, "baz" ); |
| 181 |
CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf.toString()); |
| 182 |
char d[] = "d"; |
| 183 |
CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUString( d )))); |
| 184 |
CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUStringBuffer( d )))); |
| 185 |
} |
| 186 |
|
| 187 |
namespace { |
| 188 |
|
| 189 |
rtl::OUString conditional(bool flag) { |
| 190 |
return rtl::OUString::Concat(flag ? std::u16string_view(u"a") : std::u16string_view(u"bb")) |
| 191 |
+ "c"; |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
void test::oustring::StringLiterals::checkOUStringLiteral() |
| 197 |
{ |
| 198 |
CPPUNIT_ASSERT(bool(conditional(true) == "ac")); |
| 199 |
CPPUNIT_ASSERT(bool(conditional(false) == "bbc")); |
| 200 |
|
| 201 |
static constexpr rtlunittest::OUStringLiteral s1lit(u"abc"); |
| 202 |
rtl::OUString s1(s1lit); |
| 203 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abc"), s1); |
| 204 |
static constexpr rtlunittest::OUStringLiteral de(u"de"); |
| 205 |
s1 = de; |
| 206 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("de"), s1); |
| 207 |
s1 += rtlunittest::OUStringLiteral(u"fde"); |
| 208 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("defde"), s1); |
| 209 |
CPPUNIT_ASSERT_EQUAL( |
| 210 |
sal_Int32(0), |
| 211 |
s1.reverseCompareTo(rtlunittest::OUStringLiteral(u"defde"))); |
| 212 |
CPPUNIT_ASSERT( |
| 213 |
s1.equalsIgnoreAsciiCase(rtlunittest::OUStringLiteral(u"DEFDE"))); |
| 214 |
CPPUNIT_ASSERT(s1.match(rtlunittest::OUStringLiteral(u"fde"), 2)); |
| 215 |
CPPUNIT_ASSERT( |
| 216 |
s1.matchIgnoreAsciiCase(rtlunittest::OUStringLiteral(u"FDE"), 2)); |
| 217 |
rtl::OUString s2; |
| 218 |
CPPUNIT_ASSERT(s1.startsWith(rtlunittest::OUStringLiteral(u"de"), &s2)); |
| 219 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("fde"), s2); |
| 220 |
CPPUNIT_ASSERT( |
| 221 |
s1.startsWithIgnoreAsciiCase( |
| 222 |
rtlunittest::OUStringLiteral(u"DEFD"), &s2)); |
| 223 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("e"), s2); |
| 224 |
CPPUNIT_ASSERT(s1.endsWith(rtlunittest::OUStringLiteral(u"de"), &s2)); |
| 225 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("def"), s2); |
| 226 |
CPPUNIT_ASSERT( |
| 227 |
s1.endsWithIgnoreAsciiCase(rtlunittest::OUStringLiteral(u"EFDE"), &s2)); |
| 228 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("d"), s2); |
| 229 |
static constexpr rtlunittest::OUStringLiteral defde(u"defde"); |
| 230 |
CPPUNIT_ASSERT(bool(s1 == defde)); |
| 231 |
CPPUNIT_ASSERT(bool(defde == s1)); |
| 232 |
static constexpr rtlunittest::OUStringLiteral abc(u"abc"); |
| 233 |
CPPUNIT_ASSERT(s1 != abc); |
| 234 |
CPPUNIT_ASSERT(abc != s1); |
| 235 |
CPPUNIT_ASSERT_EQUAL( |
| 236 |
sal_Int32(3), s1.indexOf(rtlunittest::OUStringLiteral(u"de"), 1)); |
| 237 |
CPPUNIT_ASSERT_EQUAL( |
| 238 |
sal_Int32(3), s1.lastIndexOf(rtlunittest::OUStringLiteral(u"de"))); |
| 239 |
sal_Int32 i = 0; |
| 240 |
CPPUNIT_ASSERT_EQUAL( |
| 241 |
rtl::OUString("abcfde"), |
| 242 |
s1.replaceFirst( |
| 243 |
rtlunittest::OUStringLiteral(u"de"), rtl::OUString("abc"), &i)); |
| 244 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 245 |
CPPUNIT_ASSERT_EQUAL( |
| 246 |
rtl::OUString("abcfde"), |
| 247 |
s1.replaceFirst( |
| 248 |
rtl::OUString("de"), rtlunittest::OUStringLiteral(u"abc"), &i)); |
| 249 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 250 |
CPPUNIT_ASSERT_EQUAL( |
| 251 |
rtl::OUString("abcfde"), |
| 252 |
s1.replaceFirst( |
| 253 |
rtlunittest::OUStringLiteral(u"de"), |
| 254 |
rtlunittest::OUStringLiteral(u"abc"), &i)); |
| 255 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 256 |
CPPUNIT_ASSERT_EQUAL( |
| 257 |
rtl::OUString("abcfde"), |
| 258 |
s1.replaceFirst(rtlunittest::OUStringLiteral(u"de"), "abc", &i)); |
| 259 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 260 |
CPPUNIT_ASSERT_EQUAL( |
| 261 |
rtl::OUString("abcfde"), |
| 262 |
s1.replaceFirst("de", rtlunittest::OUStringLiteral(u"abc"), &i)); |
| 263 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 264 |
CPPUNIT_ASSERT_EQUAL( |
| 265 |
rtl::OUString("abcfabc"), |
| 266 |
s1.replaceAll( |
| 267 |
rtlunittest::OUStringLiteral(u"de"), rtl::OUString("abc"))); |
| 268 |
CPPUNIT_ASSERT_EQUAL( |
| 269 |
rtl::OUString("abcfabc"), |
| 270 |
s1.replaceAll( |
| 271 |
rtl::OUString("de"), rtlunittest::OUStringLiteral(u"abc"))); |
| 272 |
CPPUNIT_ASSERT_EQUAL( |
| 273 |
rtl::OUString("abcfabc"), |
| 274 |
s1.replaceAll( |
| 275 |
rtlunittest::OUStringLiteral(u"de"), |
| 276 |
rtlunittest::OUStringLiteral(u"abc"))); |
| 277 |
CPPUNIT_ASSERT_EQUAL( |
| 278 |
rtl::OUString("abcfabc"), |
| 279 |
s1.replaceAll(rtlunittest::OUStringLiteral(u"de"), "abc")); |
| 280 |
CPPUNIT_ASSERT_EQUAL( |
| 281 |
rtl::OUString("abcfabc"), |
| 282 |
s1.replaceAll("de", rtlunittest::OUStringLiteral(u"abc"))); |
| 283 |
CPPUNIT_ASSERT_EQUAL( |
| 284 |
rtl::OUString("abcdef"), |
| 285 |
rtl::OUString( |
| 286 |
rtl::OUString("abc") + rtlunittest::OUStringLiteral(u"def"))); |
| 287 |
CPPUNIT_ASSERT_EQUAL( |
| 288 |
rtl::OUString("abcdef"), |
| 289 |
rtl::OUString( |
| 290 |
rtlunittest::OUStringLiteral(u"abc") + rtl::OUString("def"))); |
| 291 |
rtl::OUStringBuffer b(rtlunittest::OUStringLiteral(u"abc")); |
| 292 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abc"), b.toString()); |
| 293 |
b.append(rtlunittest::OUStringLiteral(u"def")); |
| 294 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abcdef"), b.toString()); |
| 295 |
b.insert(2, rtlunittest::OUStringLiteral(u"gabab")); |
| 296 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abgababcdef"), b.toString()); |
| 297 |
CPPUNIT_ASSERT_EQUAL( |
| 298 |
sal_Int32(3), b.indexOf(rtlunittest::OUStringLiteral(u"ab"), 1)); |
| 299 |
CPPUNIT_ASSERT_EQUAL( |
| 300 |
sal_Int32(5), b.lastIndexOf(rtlunittest::OUStringLiteral(u"ab"))); |
| 301 |
} |
| 302 |
|
| 303 |
void test::oustring::StringLiterals::checkOUStringChar() |
| 304 |
{ |
| 305 |
auto l1 = rtlunittest::OUStringChar('A'); |
| 306 |
CPPUNIT_ASSERT_EQUAL(u'A', l1.c); |
| 307 |
|
| 308 |
char const c2 = 'A'; |
| 309 |
auto l2 = rtlunittest::OUStringChar(c2); |
| 310 |
CPPUNIT_ASSERT_EQUAL(u'A', l2.c); |
| 311 |
|
| 312 |
char c3 = 'A'; auto l3 = rtlunittest::OUStringChar(c3); |
| 313 |
CPPUNIT_ASSERT_EQUAL(u'A', l3.c); |
| 314 |
|
| 315 |
auto l4 = rtlunittest::OUStringChar(u'A'); |
| 316 |
CPPUNIT_ASSERT_EQUAL(u'A', l4.c); |
| 317 |
|
| 318 |
sal_Unicode const c5 = 0x100; |
| 319 |
auto l5 = rtlunittest::OUStringChar(c5); |
| 320 |
CPPUNIT_ASSERT_EQUAL(c5, l5.c); |
| 321 |
|
| 322 |
rtl::OUString s1{rtlunittest::OUStringChar('A')}; |
| 323 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1.getLength()); |
| 324 |
CPPUNIT_ASSERT_EQUAL(u'A', s1[0]); |
| 325 |
|
| 326 |
CPPUNIT_ASSERT_EQUAL( |
| 327 |
true, rtl::OUString("A") == rtlunittest::OUStringChar('A')); |
| 328 |
CPPUNIT_ASSERT_EQUAL( |
| 329 |
false, rtl::OUString("AB") == rtlunittest::OUStringChar('A')); |
| 330 |
CPPUNIT_ASSERT_EQUAL( |
| 331 |
false, rtl::OUString("A") != rtlunittest::OUStringChar('A')); |
| 332 |
CPPUNIT_ASSERT_EQUAL( |
| 333 |
true, rtl::OUString("AB") != rtlunittest::OUStringChar('A')); |
| 334 |
|
| 335 |
rtl::OUString s2("A" + rtlunittest::OUStringChar('b')); |
| 336 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s2.getLength()); |
| 337 |
CPPUNIT_ASSERT_EQUAL(u'A', s2[0]); |
| 338 |
CPPUNIT_ASSERT_EQUAL(u'b', s2[1]); |
| 339 |
} |
| 340 |
|
| 341 |
void test::oustring::StringLiterals::checkUtf16() { |
| 342 |
rtl::OUString s1(u"abc"_ustr); |
| 343 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abc"), s1); |
| 344 |
s1 = u"de"_ustr; |
| 345 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("de"), s1); |
| 346 |
s1 += u"fde"; |
| 347 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("defde"), s1); |
| 348 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s1.reverseCompareTo(u"defde")); |
| 349 |
CPPUNIT_ASSERT(s1.equalsIgnoreAsciiCase(u"DEFDE")); |
| 350 |
CPPUNIT_ASSERT(s1.match(u"fde", 2)); |
| 351 |
CPPUNIT_ASSERT(s1.matchIgnoreAsciiCase(u"FDE", 2)); |
| 352 |
rtl::OUString s2; |
| 353 |
CPPUNIT_ASSERT(s1.startsWith(u"de", &s2)); |
| 354 |
CPPUNIT_ASSERT_EQUAL(u"fde"_ustr, s2); |
| 355 |
CPPUNIT_ASSERT(s1.startsWithIgnoreAsciiCase(u"DEFD", &s2)); |
| 356 |
CPPUNIT_ASSERT_EQUAL(u"e"_ustr, s2); |
| 357 |
CPPUNIT_ASSERT(s1.endsWith(u"de", &s2)); |
| 358 |
CPPUNIT_ASSERT_EQUAL(u"def"_ustr, s2); |
| 359 |
CPPUNIT_ASSERT(s1.endsWithIgnoreAsciiCase(u"EFDE", &s2)); |
| 360 |
CPPUNIT_ASSERT_EQUAL(u"d"_ustr, s2); |
| 361 |
CPPUNIT_ASSERT(bool(s1 == u"defde")); |
| 362 |
CPPUNIT_ASSERT(bool(u"defde" == s1)); |
| 363 |
CPPUNIT_ASSERT(s1 != u"abc"); |
| 364 |
CPPUNIT_ASSERT(u"abc" != s1); |
| 365 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), s1.indexOf(u"de", 1)); |
| 366 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), s1.lastIndexOf(u"de")); |
| 367 |
sal_Int32 i = 0; |
| 368 |
CPPUNIT_ASSERT_EQUAL( |
| 369 |
u"abcfde"_ustr, |
| 370 |
s1.replaceFirst(u"de", rtl::OUString("abc"), &i)); |
| 371 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 372 |
CPPUNIT_ASSERT_EQUAL( |
| 373 |
u"abcfde"_ustr, |
| 374 |
s1.replaceFirst(rtl::OUString("de"), u"abc", &i)); |
| 375 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 376 |
CPPUNIT_ASSERT_EQUAL( |
| 377 |
u"abcfde"_ustr, s1.replaceFirst(u"de", u"abc", &i)); |
| 378 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 379 |
CPPUNIT_ASSERT_EQUAL( |
| 380 |
u"abcfde"_ustr, s1.replaceFirst(u"de", "abc", &i)); |
| 381 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 382 |
CPPUNIT_ASSERT_EQUAL( |
| 383 |
u"abcfde"_ustr, s1.replaceFirst("de", u"abc", &i)); |
| 384 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), i); |
| 385 |
CPPUNIT_ASSERT_EQUAL( |
| 386 |
u"abcfabc"_ustr, s1.replaceAll(u"de", rtl::OUString("abc"))); |
| 387 |
CPPUNIT_ASSERT_EQUAL( |
| 388 |
u"abcfabc"_ustr, s1.replaceAll(rtl::OUString("de"), u"abc")); |
| 389 |
CPPUNIT_ASSERT_EQUAL( |
| 390 |
u"abcfabc"_ustr, s1.replaceAll(u"de", u"abc")); |
| 391 |
CPPUNIT_ASSERT_EQUAL( |
| 392 |
u"abcfabc"_ustr, s1.replaceAll(u"de", "abc")); |
| 393 |
CPPUNIT_ASSERT_EQUAL( |
| 394 |
u"abcfabc"_ustr, s1.replaceAll("de", u"abc")); |
| 395 |
CPPUNIT_ASSERT_EQUAL( |
| 396 |
rtl::OUString("abcdef"), rtl::OUString(rtl::OUString("abc") + u"def")); |
| 397 |
CPPUNIT_ASSERT_EQUAL( |
| 398 |
rtl::OUString("abcdef"), rtl::OUString(u"abc" + rtl::OUString("def"))); |
| 399 |
rtl::OUStringBuffer b(u"abc"); |
| 400 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abc"), b.toString()); |
| 401 |
b.append(u"def"); |
| 402 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abcdef"), b.toString()); |
| 403 |
b.insert(2, u"gabab"); |
| 404 |
CPPUNIT_ASSERT_EQUAL(rtl::OUString("abgababcdef"), b.toString()); |
| 405 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(3), b.indexOf(u"ab", 1)); |
| 406 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(5), b.lastIndexOf(u"ab")); |
| 407 |
} |
| 408 |
|
| 409 |
void test::oustring::StringLiterals::checkEmbeddedNul() { |
| 410 |
using namespace std::literals; |
| 411 |
rtl::OUString const s("foobar"); |
| 412 |
static constexpr char16_t const a[] = u"foo\0hidden"; |
| 413 |
char16_t const * const p = a; |
| 414 |
CPPUNIT_ASSERT(s.startsWith(a)); |
| 415 |
CPPUNIT_ASSERT(s.startsWith(p)); |
| 416 |
CPPUNIT_ASSERT(s.startsWith(u"foo\0hidden")); |
| 417 |
// For Clang against libstdc++, this would hit not-yet-fixed |
| 418 |
// <https://github.com/llvm/llvm-project/issues/24502> "eagerly-instantiated entities whose |
| 419 |
// templates are defined after the first point of instantiation don't get instantiated at all" |
| 420 |
// (see the mailing list thread starting at |
| 421 |
// <https://gcc.gnu.org/pipermail/libstdc++/2021-November/053548.html> "std::basic_string<_Tp> |
| 422 |
// constructor point of instantiation woes?"): |
| 423 |
#if !(defined __clang__ && defined __GLIBCXX__) |
| 424 |
CPPUNIT_ASSERT(!s.startsWith(u"foo\0hidden"s)); |
| 425 |
#endif |
| 426 |
CPPUNIT_ASSERT(!s.startsWith(u"foo\0hidden"sv)); |
| 427 |
/*TODO:*/ |
| 428 |
CPPUNIT_ASSERT(!s.startsWith(rtlunittest::OUStringLiteral(a))); |
| 429 |
CPPUNIT_ASSERT(!s.startsWith(rtlunittest::OUStringLiteral(u"foo\0hidden"))); |
| 430 |
/*TODO*/ |
| 431 |
} |
| 432 |
|
| 433 |
void test::oustring::StringLiterals::checkOstr() { |
| 434 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(0), u""_ustr.getLength()); |
| 435 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(6), u"foobar"_ustr.getLength()); |
| 436 |
CPPUNIT_ASSERT_EQUAL(sal_Int32(7), u"foo\0bar"_ustr.getLength()); |
| 437 |
CPPUNIT_ASSERT_EQUAL(u""_ustr, rtl::OUString(""_tstr)); |
| 438 |
CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, rtl::OUString("foobar"_tstr)); |
| 439 |
// Unlike in a OString context, the below "foo\0bar"_tstr in a OUString context would trigger |
| 440 |
// the assert(c!='\0') in Copy in sal/rtl/strtmpl.hxx: |
| 441 |
// CPPUNIT_ASSERT_EQUAL(u"foo\0bar"_ustr, rtl::OUString("foo\0bar"_tstr)); |
| 442 |
} |
| 443 |
|
| 444 |
} // namespace |
| 445 |
|
| 446 |
CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals); |
| 447 |
|
| 448 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |