Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sal / qa / rtl / strings / test_oustring_concat.cxx
blob1af3f60dd3aa67628380c846e9c5f8b619042451
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <rtl/ustring.hxx>
19 #include <rtl/ustrbuf.hxx>
20 #include <rtl/string.hxx>
21 #include <rtl/strbuf.hxx>
23 #include <typeinfo>
25 using namespace rtl;
27 namespace std
29 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
30 operator <<(
31 std::basic_ostream<charT, traits> & stream, const std::type_info& info )
33 return stream << info.name();
35 } // namespace
37 namespace test { namespace oustring {
39 class StringConcat : public CppUnit::TestFixture
41 private:
42 void checkConcat();
43 void checkConcatAsciiL();
44 void checkEnsureCapacity();
45 void checkAppend();
46 void checkInvalid();
48 CPPUNIT_TEST_SUITE(StringConcat);
49 CPPUNIT_TEST(checkConcat);
50 CPPUNIT_TEST(checkConcatAsciiL);
51 CPPUNIT_TEST(checkEnsureCapacity);
52 CPPUNIT_TEST(checkAppend);
53 CPPUNIT_TEST(checkInvalid);
54 CPPUNIT_TEST_SUITE_END();
57 void test::oustring::StringConcat::checkConcat()
59 // All the extra () are to protect commas against being treated as separators of macro arguments.
60 CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString()));
61 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
62 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( OUString( "foo" ) + OUString( "bar" )));
63 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
64 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
65 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
66 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< OUString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
67 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( "foo" ) + "bar" ));
68 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringLiteral, const char[ 4 ] > )), typeid( OUStringLiteral( "foo" ) + "bar" ));
69 const char d1[] = "xyz";
70 CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1 ));
71 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1 ));
74 void test::oustring::StringConcat::checkConcatAsciiL()
77 OUString s("foo");
78 CPPUNIT_ASSERT_EQUAL(OUString("foo"), s += "");
81 OUString s("foo");
82 CPPUNIT_ASSERT_EQUAL(OUString("foobar"), s += "bar");
86 void test::oustring::StringConcat::checkEnsureCapacity()
88 rtl_uString* str = nullptr;
89 rtl_uString_newFromLiteral( &str, "test", strlen( "test" ), 0 );
90 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
91 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
93 rtl_uString* oldStr = str;
94 rtl_uString_ensureCapacity( &str, 4 ); // should be no-op
95 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
96 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
97 CPPUNIT_ASSERT_EQUAL( str, oldStr );
99 rtl_uString_acquire( oldStr );
100 CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
101 rtl_uString_ensureCapacity( &str, 4 );
102 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
103 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
104 // a copy was forced because of refcount
105 CPPUNIT_ASSERT( oldStr != str );
106 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(0), rtl_ustr_compare( oldStr->buffer, str->buffer ) );
107 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
108 rtl_uString_release( str );
109 str = oldStr;
111 rtl_uString_acquire( oldStr );
112 rtl_uString_ensureCapacity( &str, 1024 );
113 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
114 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
115 CPPUNIT_ASSERT( oldStr != str );
116 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32>(0), rtl_ustr_compare( oldStr->buffer, str->buffer ) );
117 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
118 // but there should be extra capacity
119 for( int i = 0;
120 i < 20;
121 ++i )
122 str->buffer[ str->length + i ] = '0';
123 str->length += 20;
124 rtl_uString_release( str );
125 rtl_uString_release( oldStr );
128 void test::oustring::StringConcat::checkAppend()
130 OUString str( "foo" );
131 str += OUStringLiteral( "bar" ) + "baz";
132 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), str );
133 OUStringBuffer buf( "foo" );
134 buf.append( OUStringLiteral( "bar" ) + "baz" );
135 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf.makeStringAndClear());
138 #define INVALID_CONCAT( expression ) \
140 rtl_string_unittest_invalid_concat = false, \
141 ( void ) OUString( expression ), \
142 rtl_string_unittest_invalid_concat )
144 void test::oustring::StringConcat::checkInvalid()
146 CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
147 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OUStringBuffer( "b" )));
148 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" )));
149 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" )));
150 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + static_cast<const char*>("b") ));
151 char d[] = "b";
152 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d ));
153 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + static_cast<char*>(d) ));
154 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" )));
155 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 ));
156 rtl_String* rs = nullptr;
157 rtl_uString* rus = nullptr;
158 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
159 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
161 #if 0
162 // Should fail to compile, to avoid use of OUStringConcat lvalues that
163 // contain dangling references to temporaries:
164 auto const conc = OUStringLiteral("foo") + "bar";
165 (void) OUString(conc);
166 #endif
169 }} // namespace
171 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat);
173 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */