1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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
;
14 #include <sal/types.h>
15 #include <cppunit/TestAssert.h>
16 #include <cppunit/TestFixture.h>
17 #include <cppunit/extensions/HelperMacros.h>
19 #include <rtl/ustring.hxx>
20 #include <rtl/ustrbuf.hxx>
21 #include <rtl/string.hxx>
22 #include <rtl/strbuf.hxx>
29 template<> inline std::string
CppUnit::assertion_traits
<std::type_info
>::toString(
30 std::type_info
const & x
)
35 namespace test::oustring
{
37 class StringConcat
: public CppUnit::TestFixture
41 void checkConcatAsciiL();
42 void checkEnsureCapacity();
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();
55 void test::oustring::StringConcat::checkConcat()
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( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
60 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( OUString( "foo" ) + OUString( "bar" )));
61 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
62 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
63 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
64 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< OUString
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
65 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + "bar" ));
66 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringBuffer
, const char[ 4 ] > )), typeid( OUStringBuffer( "foo" ) + "bar" ));
67 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( u
"foo" ) + "bar" ));
68 #if defined __GNUC__ && __GNUC__ <= 11 && !defined __clang__
69 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringLiteral
<4>, const char[ 4 ] > )), typeid( OUStringLiteral
<4>( u
"foo" ) + "bar" ));
70 // the explicit OUStringLiteral<4> template argument in the unevaluated typeid context
71 // is needed by some GCC versions, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878>
72 // "Failed class template argument deduction in unevaluated, parenthesized context"
74 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringLiteral
<4>, const char[ 4 ] > )), typeid( OUStringLiteral( u
"foo" ) + "bar" ));
76 const char d1
[] = "xyz";
77 CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1
));
78 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1
));
79 const sal_Unicode
* d2
= u
"xyz";
80 CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d2
));
81 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, const sal_Unicode
* > )), typeid( OUString( "foo" ) + d2
));
82 const sal_Unicode d3
[] = u
"xyz";
83 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString::Concat( "foo" ) + "bar" ));
84 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( "foo" ) + "bar" ));
85 CPPUNIT_ASSERT_EQUAL( OUString( "xyzbar" ), OUString( OUString::Concat( d1
) + "bar" ));
86 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d1
) + "bar" ));
87 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString::Concat( u
"foo" ) + "bar" ));
88 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const sal_Unicode
[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( u
"foo" ) + "bar" ));
89 CPPUNIT_ASSERT_EQUAL( OUString( "xyzbar" ), OUString( OUString::Concat( d2
) + "bar" ));
90 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const sal_Unicode
* >, const char[ 4 ] > )), typeid( OUString::Concat( d2
) + "bar" ));
91 CPPUNIT_ASSERT_EQUAL( OUString( "xyzbar" ), OUString( OUString::Concat( d3
) + "bar" ));
92 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const sal_Unicode
[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d3
) + "bar" ));
94 CPPUNIT_ASSERT_EQUAL( OUString( "num10" ), OUString( OUString( "num" ) + OUString::number( 10 )));
95 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFINT32
> > )), typeid( OUString( "num" ) + OUString::number( 10 )));
96 CPPUNIT_ASSERT_EQUAL( OUString( "num10" ), OUString( OUString( "num" ) + OUString::number( 10L )));
97 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFINT64
> > )), typeid( OUString( "num" ) + OUString::number( 10L )));
98 CPPUNIT_ASSERT_EQUAL( OUString( "num10" ), OUString( OUString( "num" ) + OUString::number( 10ULL )));
99 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFUINT64
> > )), typeid( OUString( "num" ) + OUString::number( 10ULL )));
100 CPPUNIT_ASSERT_EQUAL( OUString( "num10.5" ), OUString( OUString( "num" ) + OUString::number( 10.5f
)));
101 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( OUString( "num" ) + OUString::number( 10.5f
)));
102 CPPUNIT_ASSERT_EQUAL( OUString( "num10.5" ), OUString( OUString( "num" ) + OUString::number( 10.5 )));
103 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( OUString( "num" ) + OUString::number( 10.5 )));
106 void test::oustring::StringConcat::checkConcatAsciiL()
111 CPPUNIT_ASSERT_EQUAL(OUString("foo"), s
);
116 CPPUNIT_ASSERT_EQUAL(OUString("foobar"), s
);
120 void test::oustring::StringConcat::checkEnsureCapacity()
122 rtl_uString
* str
= nullptr;
123 rtl_uString_newFromLiteral( &str
, "test", strlen( "test" ), 0 );
124 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
125 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
127 rtl_uString
* oldStr
= str
;
128 rtl_uString_ensureCapacity( &str
, 4 ); // should be no-op
129 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
130 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
131 CPPUNIT_ASSERT_EQUAL( str
, oldStr
);
133 rtl_uString_acquire( oldStr
);
134 CPPUNIT_ASSERT_EQUAL( 2, int( str
->refCount
));
135 rtl_uString_ensureCapacity( &str
, 4 );
136 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
137 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
138 // a copy was forced because of refcount
139 CPPUNIT_ASSERT( oldStr
!= str
);
140 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(0), rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) );
141 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
142 rtl_uString_release( str
);
145 rtl_uString_acquire( oldStr
);
146 rtl_uString_ensureCapacity( &str
, 1024 );
147 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
); // size is still 4
148 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
149 CPPUNIT_ASSERT( oldStr
!= str
);
150 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(0), rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) );
151 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
152 // but there should be extra capacity
156 str
->buffer
[ str
->length
+ i
] = '0';
158 rtl_uString_release( str
);
159 rtl_uString_release( oldStr
);
162 void test::oustring::StringConcat::checkAppend()
164 OUString
str( "foo" );
165 str
+= OUStringLiteral( u
"bar" ) + "baz";
166 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), str
);
167 OUStringBuffer
buf( "foo" );
168 buf
.append( OUStringLiteral( u
"bar" ) + "baz" );
169 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf
.makeStringAndClear());
172 #define INVALID_CONCAT( expression ) \
174 rtl_string_unittest_invalid_concat = false, \
175 ( void ) OUString( expression ), \
176 rtl_string_unittest_invalid_concat )
178 void test::oustring::StringConcat::checkInvalid()
180 CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
181 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" )));
182 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" )));
183 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + static_cast<const char*>("b") ));
185 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d
));
186 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + static_cast<char*>(d
) ));
187 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" )));
188 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString::Concat( "b" )));
189 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 ));
190 rtl_String
* rs
= nullptr;
191 rtl_uString
* rus
= nullptr;
192 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs
));
193 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus
));
194 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString::number( 10 )));
195 CPPUNIT_ASSERT( INVALID_CONCAT( OUString::number( 0 ) + OString::number( 10 )));
198 // Should fail to compile, to avoid use of OUStringConcat lvalues that
199 // contain dangling references to temporaries:
200 auto const conc
= OUStringLiteral("foo") + "bar";
201 (void) OUString(conc
);
207 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat
);
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */