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 namespace CppUnit
30 {
31 template<> struct assertion_traits<std::type_info>
32 {
equalCppUnit::assertion_traits33     static bool equal(std::type_info const & x, std::type_info const & y) { return x == y; }
34 
toStringCppUnit::assertion_traits35     static std::string toString(std::type_info const & x) { return x.name(); }
36 };
37 } // namespace
38 
39 namespace test::oustring {
40 
41 class StringConcat : public CppUnit::TestFixture
42 {
43 private:
44     void checkConcat();
45     void checkConcatAsciiL();
46     void checkEnsureCapacity();
47     void checkAppend();
48     void checkInvalid();
49 
50 CPPUNIT_TEST_SUITE(StringConcat);
51 CPPUNIT_TEST(checkConcat);
52 CPPUNIT_TEST(checkConcatAsciiL);
53 CPPUNIT_TEST(checkEnsureCapacity);
54 CPPUNIT_TEST(checkAppend);
55 CPPUNIT_TEST(checkInvalid);
56 CPPUNIT_TEST_SUITE_END();
57 };
58 
checkConcat()59 void test::oustring::StringConcat::checkConcat()
60 {
61 // All the extra () are to protect commas against being treated as separators of macro arguments.
62     CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString()));
63     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
64     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( OUString( "foo" ) + OUString( "bar" )));
65     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
66     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
67     CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
68     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< OUString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
69     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + "bar" ));
70     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringBuffer, const char[ 4 ] > )), typeid( OUStringBuffer( "foo" ) + "bar" ));
71     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( u"foo" ) + "bar" ));
72     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringLiteral<4>, const char[ 4 ] > )), typeid( OUStringLiteral<4>( u"foo" ) + "bar" ));
73         //TODO: the explicit OUStringLiteral<4> template argument in the unevaluated typeid context
74         // is needed by some GCC versions, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96878>
75         // "Failed class template argument deduction in unevaluated, parenthesized context"
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" ));
93 
94     CPPUNIT_ASSERT_EQUAL( OUString( "num10" ), OUString( OUString( "num" ) + OUString::number( 10 )));
95     CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUStringNumber< int > > )), 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, OUStringNumber< long long > > )), 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, OUStringNumber< unsigned long long > > )), 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, OUStringNumber< float > > )), 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, OUStringNumber< double > > )), typeid( OUString( "num" ) + OUString::number( 10.5 )));
104 }
105 
checkConcatAsciiL()106 void test::oustring::StringConcat::checkConcatAsciiL()
107 {
108     {
109         OUString s("foo");
110         s += "";
111         CPPUNIT_ASSERT_EQUAL(OUString("foo"), s);
112     }
113     {
114         OUString s("foo");
115         s += "bar";
116         CPPUNIT_ASSERT_EQUAL(OUString("foobar"), s);
117     }
118 }
119 
checkEnsureCapacity()120 void test::oustring::StringConcat::checkEnsureCapacity()
121 {
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 ));
126 
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 );
132 
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 );
143     str = oldStr;
144 
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
153     for( int i = 0;
154          i < 20;
155          ++i )
156         str->buffer[ str->length + i ] = '0';
157     str->length += 20;
158     rtl_uString_release( str );
159     rtl_uString_release( oldStr );
160 }
161 
checkAppend()162 void test::oustring::StringConcat::checkAppend()
163 {
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());
170 }
171 
172 #define INVALID_CONCAT( expression ) \
173     ( \
174     rtl_string_unittest_invalid_concat = false, \
175     ( void ) OUString( expression ), \
176     rtl_string_unittest_invalid_concat )
177 
checkInvalid()178 void test::oustring::StringConcat::checkInvalid()
179 {
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") ));
184     char d[] = "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 )));
196 
197 #if 0
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);
202 #endif
203 }
204 
205 } // namespace
206 
207 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat);
208 
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
210