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( 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 CPPUNIT_ASSERT_EQUAL( u
"foobar"_ustr
, OUString( OUString::Concat( "foo" ) + "bar" ));
77 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( "foo" ) + "bar" ));
78 CPPUNIT_ASSERT_EQUAL( u
"xyzbar"_ustr
, OUString( OUString::Concat( d1
) + "bar" ));
79 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( d1
) + "bar" ));
80 CPPUNIT_ASSERT_EQUAL( u
"foobar"_ustr
, OUString( OUString::Concat( u
"foo" ) + "bar" ));
81 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const sal_Unicode
[ 4 ] >, const char[ 4 ] > )), typeid( OUString::Concat( u
"foo" ) + "bar" ));
82 CPPUNIT_ASSERT_EQUAL( u
"xyzbar"_ustr
, OUString( OUString::Concat( d2
) + "bar" ));
83 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< rtl::OUStringConcatMarker
, const sal_Unicode
* >, const char[ 4 ] > )), typeid( OUString::Concat( d2
) + "bar" ));
84 CPPUNIT_ASSERT_EQUAL( u
"xyzbar"_ustr
, OUString( OUString::Concat( d3
) + "bar" ));
85 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
"num10"_ustr
, OUString( u
"num"_ustr
+ OUString::number( 10 )));
88 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFINT32
> > )), typeid( u
"num"_ustr
+ OUString::number( 10 )));
89 CPPUNIT_ASSERT_EQUAL( u
"num10"_ustr
, OUString( u
"num"_ustr
+ OUString::number( 10L )));
90 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFINT64
> > )), typeid( u
"num"_ustr
+ OUString::number( 10L )));
91 CPPUNIT_ASSERT_EQUAL( u
"num10"_ustr
, OUString( u
"num"_ustr
+ OUString::number( 10ULL )));
92 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, StringNumber
< sal_Unicode
, RTL_USTR_MAX_VALUEOFUINT64
> > )), typeid( u
"num"_ustr
+ OUString::number( 10ULL )));
93 CPPUNIT_ASSERT_EQUAL( u
"num10.5"_ustr
, OUString( u
"num"_ustr
+ OUString::number( 10.5f
)));
94 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( u
"num"_ustr
+ OUString::number( 10.5f
)));
95 CPPUNIT_ASSERT_EQUAL( u
"num10.5"_ustr
, OUString( u
"num"_ustr
+ OUString::number( 10.5 )));
96 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( u
"num"_ustr
+ OUString::number( 10.5 )));
99 void test::oustring::StringConcat::checkConcatAsciiL()
102 OUString
s(u
"foo"_ustr
);
104 CPPUNIT_ASSERT_EQUAL(u
"foo"_ustr
, s
);
107 OUString
s(u
"foo"_ustr
);
109 CPPUNIT_ASSERT_EQUAL(u
"foobar"_ustr
, s
);
113 void test::oustring::StringConcat::checkEnsureCapacity()
115 rtl_uString
* str
= nullptr;
116 rtl_uString_newFromLiteral( &str
, "test", strlen( "test" ), 0 );
117 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
118 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
120 rtl_uString
* oldStr
= str
;
121 rtl_uString_ensureCapacity( &str
, 4 ); // should be no-op
122 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
123 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
124 CPPUNIT_ASSERT_EQUAL( str
, oldStr
);
126 rtl_uString_acquire( oldStr
);
127 CPPUNIT_ASSERT_EQUAL( 2, int( str
->refCount
));
128 rtl_uString_ensureCapacity( &str
, 4 );
129 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
130 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
131 // a copy was forced because of refcount
132 CPPUNIT_ASSERT( oldStr
!= str
);
133 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(0), rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) );
134 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
135 rtl_uString_release( str
);
138 rtl_uString_acquire( oldStr
);
139 rtl_uString_ensureCapacity( &str
, 1024 );
140 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
); // size is still 4
141 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
142 CPPUNIT_ASSERT( oldStr
!= str
);
143 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(0), rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) );
144 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
145 // but there should be extra capacity
149 str
->buffer
[ str
->length
+ i
] = '0';
151 rtl_uString_release( str
);
152 rtl_uString_release( oldStr
);
155 void test::oustring::StringConcat::checkAppend()
157 OUString
str( u
"foo"_ustr
);
158 str
+= OUStringLiteral( u
"bar" ) + "baz";
159 CPPUNIT_ASSERT_EQUAL( u
"foobarbaz"_ustr
, str
);
160 OUStringBuffer
buf( "foo" );
161 buf
.append( OUStringLiteral( u
"bar" ) + "baz" );
162 CPPUNIT_ASSERT_EQUAL( u
"foobarbaz"_ustr
, buf
.makeStringAndClear());
165 #define INVALID_CONCAT( expression ) \
167 rtl_string_unittest_invalid_concat = false, \
168 ( void ) OUString( expression ), \
169 rtl_string_unittest_invalid_concat )
171 void test::oustring::StringConcat::checkInvalid()
173 CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
174 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ "b"_ostr
));
175 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ OStringBuffer( "b" )));
176 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ static_cast<const char*>("b") ));
178 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ d
));
179 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ static_cast<char*>(d
) ));
180 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ OStringLiteral( "b" )));
181 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ OString::Concat( "b" )));
182 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ 1 ));
183 rtl_String
* rs
= nullptr;
184 rtl_uString
* rus
= nullptr;
185 CPPUNIT_ASSERT( INVALID_CONCAT( u
"b"_ustr
+ rs
));
186 CPPUNIT_ASSERT( INVALID_CONCAT( u
"b"_ustr
+ rus
));
187 CPPUNIT_ASSERT( INVALID_CONCAT( u
"a"_ustr
+ OString::number( 10 )));
188 CPPUNIT_ASSERT( INVALID_CONCAT( OUString::number( 0 ) + OString::number( 10 )));
191 // Should fail to compile, to avoid use of OUStringConcat lvalues that
192 // contain dangling references to temporaries:
193 auto const conc
= OUStringLiteral("foo") + "bar";
194 (void) OUString(conc
);
200 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat
);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */