| 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 support for detecting errors instead of getting compile errors |
| 11 |
#define RTL_STRING_UNITTEST_CONCAT |
| 12 |
extern bool rtl_string_unittest_invalid_concat; |
| 13 |
|
| 14 |
#include <sal/types.h> |
| 15 |
#include <cppunit/TestAssert.h> |
| 16 |
#include <cppunit/TestFixture.h> |
| 17 |
#include <cppunit/extensions/HelperMacros.h> |
| 18 |
|
| 19 |
#include <rtl/ustring.hxx> |
| 20 |
#include <rtl/ustrbuf.hxx> |
| 21 |
#include <rtl/string.hxx> |
| 22 |
#include <rtl/strbuf.hxx> |
| 23 |
|
| 24 |
#include <string> |
| 25 |
#include <typeinfo> |
| 26 |
|
| 27 |
using namespace rtl; |
| 28 |
|
| 29 |
template<> inline std::string CppUnit::assertion_traits<std::type_info>::toString( |
| 30 |
std::type_info const & x) |
| 31 |
{ |
| 32 |
return x.name(); |
| 33 |
} |
| 34 |
|
| 35 |
namespace test::oustring { |
| 36 |
|
| 37 |
class StringConcat : public CppUnit::TestFixture |
| 38 |
{ |
| 39 |
private: |
| 40 |
void checkConcat(); |
| 41 |
void checkConcatAsciiL(); |
| 42 |
void checkEnsureCapacity(); |
| 43 |
void checkAppend(); |
| 44 |
void checkInvalid(); |
| 45 |
|
| 46 |
CPPUNIT_TEST_SUITE(StringConcat); |
| 47 |
CPPUNIT_TEST(checkConcat); |
| 48 |
CPPUNIT_TEST(checkConcatAsciiL); |
| 49 |
CPPUNIT_TEST(checkEnsureCapacity); |
| 50 |
CPPUNIT_TEST(checkAppend); |
| 51 |
CPPUNIT_TEST(checkInvalid); |
| 52 |
CPPUNIT_TEST_SUITE_END(); |
| 53 |
}; |
| 54 |
|
| 55 |
void test::oustring::StringConcat::checkConcat() |
| 56 |
{ |
| 57 |
// All the extra () are to protect commas against being treated as separators of macro arguments. |
| 58 |
CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString())); |
| 59 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( u"foo"_ustr + u"bar"_ustr)); |
| 60 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( u"foo"_ustr + u"bar"_ustr)); |
| 61 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( u"foo"_ustr + "bar" )); |
| 62 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( u"foo"_ustr + "bar" )); |
| 63 |
CPPUNIT_ASSERT_EQUAL( u"foobarbaz"_ustr, OUString( u"foo"_ustr + "bar" + "baz" )); |
| 64 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< OUString, const char[ 4 ] >, const char[ 4 ] > )), typeid( u"foo"_ustr + "bar" + "baz" )); |
| 65 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( OUStringBuffer( "foo" ) + "bar" )); |
| 66 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringBuffer, const char[ 4 ] > )), typeid( OUStringBuffer( "foo" ) + "bar" )); |
| 67 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( OUStringLiteral( u"foo" ) + "bar" )); |
| 68 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringLiteral<4>, const char[ 4 ] > )), typeid( OUStringLiteral( u"foo" ) + "bar" )); |
| 69 |
const char d1[] = "xyz"; |
| 70 |
CPPUNIT_ASSERT_EQUAL( u"fooxyz"_ustr, OUString( u"foo"_ustr + d1 )); |
| 71 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( u"foo"_ustr + d1 )); |
| 72 |
const sal_Unicode* d2 = u"xyz"; |
| 73 |
CPPUNIT_ASSERT_EQUAL( u"fooxyz"_ustr, OUString( u"foo"_ustr + d2 )); |
| 74 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const sal_Unicode* > )), typeid( u"foo"_ustr + d2 )); |
| 75 |
const sal_Unicode d3[] = u"xyz"; |
| 76 |
sal_Unicode d4[5] = u"xyz"; // non-const array: text is copied only up to terminating null |
| 77 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( OUString::Concat( "foo" ) + "bar" )); |
| 78 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( "foo" ) + "bar" )); |
| 79 |
CPPUNIT_ASSERT_EQUAL( u"xyzbar"_ustr, OUString( OUString::Concat( d1 ) + "bar" )); |
| 80 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d1 ) + "bar" )); |
| 81 |
CPPUNIT_ASSERT_EQUAL( u"foobar"_ustr, OUString( OUString::Concat( u"foo" ) + "bar" )); |
| 82 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, const sal_Unicode[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( u"foo" ) + "bar" )); |
| 83 |
CPPUNIT_ASSERT_EQUAL( u"xyzbar"_ustr, OUString( OUString::Concat( d2 ) + "bar" )); |
| 84 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, const sal_Unicode* >, const char[ 4 ] > )), typeid( OUString::Concat( d2 ) + "bar" )); |
| 85 |
CPPUNIT_ASSERT_EQUAL( u"xyzbar"_ustr, OUString( OUString::Concat( d3 ) + "bar" )); |
| 86 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, const sal_Unicode[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d3 ) + "bar" )); |
| 87 |
CPPUNIT_ASSERT_EQUAL( u"xyzbar"_ustr, OUString( OUString::Concat( d4 ) + "bar" )); |
| 88 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< rtl::OUStringConcatMarker, sal_Unicode[ 5 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d4 ) + "bar" )); |
| 89 |
|
| 90 |
CPPUNIT_ASSERT_EQUAL( u"num10"_ustr, OUString( u"num"_ustr + OUString::number( 10 ))); |
| 91 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, StringNumber< sal_Unicode, RTL_USTR_MAX_VALUEOFINT32 > > )), typeid( u"num"_ustr + OUString::number( 10 ))); |
| 92 |
CPPUNIT_ASSERT_EQUAL( u"num10"_ustr, OUString( u"num"_ustr + OUString::number( 10L ))); |
| 93 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, StringNumber< sal_Unicode, RTL_USTR_MAX_VALUEOFINT64 > > )), typeid( u"num"_ustr + OUString::number( 10L ))); |
| 94 |
CPPUNIT_ASSERT_EQUAL( u"num10"_ustr, OUString( u"num"_ustr + OUString::number( 10ULL ))); |
| 95 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, StringNumber< sal_Unicode, RTL_USTR_MAX_VALUEOFUINT64 > > )), typeid( u"num"_ustr + OUString::number( 10ULL ))); |
| 96 |
CPPUNIT_ASSERT_EQUAL( u"num10.5"_ustr, OUString( u"num"_ustr + OUString::number( 10.5f ))); |
| 97 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( u"num"_ustr + OUString::number( 10.5f ))); |
| 98 |
CPPUNIT_ASSERT_EQUAL( u"num10.5"_ustr, OUString( u"num"_ustr + OUString::number( 10.5 ))); |
| 99 |
CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( u"num"_ustr + OUString::number( 10.5 ))); |
| 100 |
} |
| 101 |
|
| 102 |
void test::oustring::StringConcat::checkConcatAsciiL() |
| 103 |
{ |
| 104 |
{ |
| 105 |
OUString s(u"foo"_ustr); |
| 106 |
s += ""; |
| 107 |
CPPUNIT_ASSERT_EQUAL(u"foo"_ustr, s); |
| 108 |
} |
| 109 |
{ |
| 110 |
OUString s(u"foo"_ustr); |
| 111 |
s += "bar"; |
| 112 |
CPPUNIT_ASSERT_EQUAL(u"foobar"_ustr, s); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
void test::oustring::StringConcat::checkEnsureCapacity() |
| 117 |
{ |
| 118 |
rtl_uString* str = nullptr; |
| 119 |
rtl_uString_newFromLiteral( &str, "test", strlen( "test" ), 0 ); |
| 120 |
CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); |
| 121 |
CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount )); |
| 122 |
|
| 123 |
rtl_uString* oldStr = str; |
| 124 |
rtl_uString_ensureCapacity( &str, 4 ); // should be no-op |
| 125 |
CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); |
| 126 |
CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount )); |
| 127 |
CPPUNIT_ASSERT_EQUAL( str, oldStr ); |
| 128 |
|
| 129 |
rtl_uString_acquire( oldStr ); |
| 130 |
CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount )); |
| 131 |
rtl_uString_ensureCapacity( &str, 4 ); |
| 132 |
CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); |
| 133 |
CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount )); |
| 134 |
// a copy was forced because of refcount |
| 135 |
CPPUNIT_ASSERT( oldStr != str ); |
| 136 |
CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(0), rtl_ustr_compare( oldStr->buffer, str->buffer ) ); |
| 137 |
CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount )); |
| 138 |
rtl_uString_release( str ); |
| 139 |
str = oldStr; |
| 140 |
|
| 141 |
rtl_uString_acquire( oldStr ); |
| 142 |
rtl_uString_ensureCapacity( &str, 1024 ); |
| 143 |
CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4 |
| 144 |
CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount )); |
| 145 |
CPPUNIT_ASSERT( oldStr != str ); |
| 146 |
CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(0), rtl_ustr_compare( oldStr->buffer, str->buffer ) ); |
| 147 |
CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount )); |
| 148 |
// but there should be extra capacity |
| 149 |
for( int i = 0; |
| 150 |
i < 20; |
| 151 |
++i ) |
| 152 |
str->buffer[ str->length + i ] = '0'; |
| 153 |
str->length += 20; |
| 154 |
rtl_uString_release( str ); |
| 155 |
rtl_uString_release( oldStr ); |
| 156 |
} |
| 157 |
|
| 158 |
void test::oustring::StringConcat::checkAppend() |
| 159 |
{ |
| 160 |
OUString str( u"foo"_ustr ); |
| 161 |
str += OUStringLiteral( u"bar" ) + "baz"; |
| 162 |
CPPUNIT_ASSERT_EQUAL( u"foobarbaz"_ustr, str ); |
| 163 |
OUStringBuffer buf( "foo" ); |
| 164 |
buf.append( OUStringLiteral( u"bar" ) + "baz" ); |
| 165 |
CPPUNIT_ASSERT_EQUAL( u"foobarbaz"_ustr, buf.makeStringAndClear()); |
| 166 |
} |
| 167 |
|
| 168 |
#define INVALID_CONCAT( expression ) \ |
| 169 |
( \ |
| 170 |
rtl_string_unittest_invalid_concat = false, \ |
| 171 |
( void ) OUString( expression ), \ |
| 172 |
rtl_string_unittest_invalid_concat ) |
| 173 |
|
| 174 |
void test::oustring::StringConcat::checkInvalid() |
| 175 |
{ |
| 176 |
CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString())); |
| 177 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + "b"_ostr)); |
| 178 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + OStringBuffer( "b" ))); |
| 179 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + static_cast<const char*>("b") )); |
| 180 |
char d[] = "b"; |
| 181 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + d )); |
| 182 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + static_cast<char*>(d) )); |
| 183 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + OStringLiteral( "b" ))); |
| 184 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + OString::Concat( "b" ))); |
| 185 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + 1 )); |
| 186 |
rtl_String* rs = nullptr; |
| 187 |
rtl_uString* rus = nullptr; |
| 188 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"b"_ustr + rs )); |
| 189 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"b"_ustr + rus )); |
| 190 |
CPPUNIT_ASSERT( INVALID_CONCAT( u"a"_ustr + OString::number( 10 ))); |
| 191 |
CPPUNIT_ASSERT( INVALID_CONCAT( OUString::number( 0 ) + OString::number( 10 ))); |
| 192 |
|
| 193 |
#if 0 |
| 194 |
// Should fail to compile, to avoid use of OUStringConcat lvalues that |
| 195 |
// contain dangling references to temporaries: |
| 196 |
auto const conc = OUStringLiteral("foo") + "bar"; |
| 197 |
(void) OUString(conc); |
| 198 |
#endif |
| 199 |
} |
| 200 |
|
| 201 |
} // namespace |
| 202 |
|
| 203 |
CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat); |
| 204 |
|
| 205 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |