Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / sal / qa / rtl / strings / test_ostring_concat.cxx
blob80fa62df6be5903068850e19be406de867476af3
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
13 #include <sal/types.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
17 #include <rtl/string.hxx>
18 #include <rtl/strbuf.hxx>
19 #include <rtl/ustring.hxx>
20 #include <rtl/ustrbuf.hxx>
22 #include <typeinfo>
24 bool rtl_string_unittest_invalid_concat = false;
26 using namespace rtl;
28 namespace std
30 template< typename charT, typename traits > std::basic_ostream<charT, traits> &
31 operator <<(
32 std::basic_ostream<charT, traits> & stream, const std::type_info& info )
34 return stream << info.name();
36 } // namespace
38 namespace test { namespace ostring {
40 class StringConcat : public CppUnit::TestFixture
42 private:
43 void checkConcat();
44 void checkEnsureCapacity();
45 void checkAppend();
46 void checkInvalid();
48 CPPUNIT_TEST_SUITE(StringConcat);
49 CPPUNIT_TEST(checkConcat);
50 CPPUNIT_TEST(checkEnsureCapacity);
51 CPPUNIT_TEST(checkAppend);
52 CPPUNIT_TEST(checkInvalid);
53 CPPUNIT_TEST_SUITE_END();
56 void test::ostring::StringConcat::checkConcat()
58 // All the extra () are to protect commas against being treated as separators of macro arguments.
59 CPPUNIT_ASSERT_EQUAL( OString(), OString(OString() + OString()));
60 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + OString( "bar" )));
61 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, OString > )), typeid( OString( "foo" ) + OString( "bar" )));
62 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + "bar" ));
63 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" ));
64 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), OString( OString( "foo" ) + "bar" + "baz" ));
65 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringConcat< OString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" + "baz" ));
66 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + "bar" ));
67 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char[ 4 ] > )), typeid( OStringLiteral( "foo" ) + "bar" ));
68 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + static_cast<const char*>("bar") ));
69 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char* > )), typeid( OStringLiteral( "foo" ) + static_cast<const char*>("bar") ));
70 const char d1[] = "xyz";
71 char d2[] = "abc";
72 const char* d3 = d1;
73 char* d4 = d2;
74 CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d1 ));
75 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + d1 ));
76 CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d2 ));
77 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char[ 4 ] > )), typeid( OString( "foo" ) + d2 ));
78 CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d3 ));
79 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char* > )), typeid( OString( "foo" ) + d3 ));
80 CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 ));
81 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 ));
84 void test::ostring::StringConcat::checkEnsureCapacity()
86 rtl_String* str = nullptr;
87 rtl_string_newFromLiteral( &str, "test", strlen( "test" ), 0 );
88 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
89 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
91 rtl_String* oldStr = str;
92 rtl_string_ensureCapacity( &str, 4 ); // should be no-op
93 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
94 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
95 CPPUNIT_ASSERT_EQUAL( str, oldStr );
97 rtl_string_acquire( oldStr );
98 CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
99 rtl_string_ensureCapacity( &str, 4 );
100 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
101 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
102 // a copy was forced because of refcount
103 CPPUNIT_ASSERT( oldStr != str );
104 CPPUNIT_ASSERT_EQUAL( 0, strcmp( oldStr->buffer, str->buffer ) );
105 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
106 rtl_string_release( str );
107 str = oldStr;
109 rtl_string_acquire( oldStr );
110 rtl_string_ensureCapacity( &str, 1024 );
111 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
112 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
113 CPPUNIT_ASSERT( oldStr != str );
114 CPPUNIT_ASSERT_EQUAL( 0, strcmp( oldStr->buffer, str->buffer ) );
115 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
116 strcpy( str->buffer, "01234567890123456789" ); // but there should be extra capacity
117 str->length += 20;
118 rtl_string_release( str );
119 rtl_string_release( oldStr );
122 void test::ostring::StringConcat::checkAppend()
124 OString str( "foo" );
125 str += OStringLiteral( "bar" ) + "baz";
126 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), str );
127 OStringBuffer buf( "foo" );
128 buf.append( OStringLiteral( "bar" ) + "baz" );
129 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear());
132 #define INVALID_CONCAT( expression ) \
134 rtl_string_unittest_invalid_concat = false, \
135 ( void ) OString( expression ), \
136 rtl_string_unittest_invalid_concat )
138 void test::ostring::StringConcat::checkInvalid()
140 CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString()));
141 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OStringBuffer( "b" )));
142 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" )));
143 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" )));
144 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" )));
145 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 ));
146 rtl_String* rs = nullptr;
147 rtl_uString* rus = nullptr;
148 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
149 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
151 #if 0
152 // Should fail to compile, to avoid use of OStringConcat lvalues that
153 // contain dangling references to temporaries:
154 auto const conc = OStringLiteral("foo") + "bar";
155 (void) OString(conc);
156 #endif
159 }} // namespace
161 CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat);
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */