bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / strings / test_ostring_concat.cxx
blob4eb64d3e2278c673ec8f07be028dacc85773e606
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 bool rtl_string_unittest_invalid_concat = false;
14 #include <sal/types.h>
15 #include <cppunit/TestFixture.h>
16 #include <cppunit/extensions/HelperMacros.h>
18 #include <rtl/string.hxx>
19 #include <rtl/strbuf.hxx>
20 #include <rtl/ustring.hxx>
21 #include <rtl/ustrbuf.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 ostring {
39 class StringConcat : public CppUnit::TestFixture
41 private:
42 void checkConcat();
43 void checkEnsureCapacity();
44 void checkAppend();
45 void checkInvalid();
47 CPPUNIT_TEST_SUITE(StringConcat);
48 CPPUNIT_TEST(checkConcat);
49 CPPUNIT_TEST(checkEnsureCapacity);
50 CPPUNIT_TEST(checkAppend);
51 CPPUNIT_TEST(checkInvalid);
52 CPPUNIT_TEST_SUITE_END();
55 void test::ostring::StringConcat::checkConcat()
57 // All the extra () are to protect commas against being treated as separators of macro arguments.
58 CPPUNIT_ASSERT_EQUAL( OString(), OString(OString() + OString()));
59 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + OString( "bar" )));
60 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, OString > )), typeid( OString( "foo" ) + OString( "bar" )));
61 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + "bar" ));
62 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" ));
63 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), OString( OString( "foo" ) + "bar" + "baz" ));
64 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringConcat< OString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" + "baz" ));
65 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + "bar" ));
66 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char[ 4 ] > )), typeid( OStringLiteral( "foo" ) + "bar" ));
67 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + (const char*)"bar" ));
68 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char* > )), typeid( OStringLiteral( "foo" ) + (const char*)"bar" ));
69 const char d1[] = "xyz";
70 char d2[] = "abc";
71 const char* d3 = d1;
72 char* d4 = d2;
73 CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d1 ));
74 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + d1 ));
75 CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d2 ));
76 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char[ 4 ] > )), typeid( OString( "foo" ) + d2 ));
77 CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d3 ));
78 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char* > )), typeid( OString( "foo" ) + d3 ));
79 CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 ));
80 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 ));
81 #ifdef __GNUC__
82 CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringBuffer( "foo" ) + OString( "bar" )));
83 CPPUNIT_ASSERT_EQUAL(( typeid( OStringConcat< OStringBuffer, OString > )), typeid( OStringBuffer( "foo" ) + OString( "bar" )));
84 #endif
87 void test::ostring::StringConcat::checkEnsureCapacity()
89 rtl_String* str = NULL;
90 rtl_string_newFromLiteral( &str, "test", strlen( "test" ), 0 );
91 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
92 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
94 rtl_String* oldStr = str;
95 rtl_string_ensureCapacity( &str, 4 ); // should be no-op
96 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
97 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
98 CPPUNIT_ASSERT( oldStr == str );
100 rtl_string_acquire( oldStr );
101 CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
102 rtl_string_ensureCapacity( &str, 4 );
103 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
104 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
105 // a copy was forced because of refcount
106 CPPUNIT_ASSERT( oldStr != str );
107 CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
108 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
109 rtl_string_release( str );
110 str = oldStr;
112 rtl_string_acquire( oldStr );
113 rtl_string_ensureCapacity( &str, 1024 );
114 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
115 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
116 CPPUNIT_ASSERT( oldStr != str );
117 CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
118 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
119 strcpy( str->buffer, "01234567890123456789" ); // but there should be extra capacity
120 str->length += 20;
121 rtl_string_release( str );
122 rtl_string_release( oldStr );
125 void test::ostring::StringConcat::checkAppend()
127 OString str( "foo" );
128 str += OStringLiteral( "bar" ) + "baz";
129 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), str );
130 OStringBuffer buf( "foo" );
131 buf.append( OStringLiteral( "bar" ) + "baz" );
132 CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear());
135 #define INVALID_CONCAT( expression ) \
137 rtl_string_unittest_invalid_concat = false, \
138 ( void ) OString( expression ), \
139 rtl_string_unittest_invalid_concat )
141 void test::ostring::StringConcat::checkInvalid()
143 CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString()));
144 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" )));
145 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" )));
146 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" )));
147 CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 ));
148 rtl_String* rs = NULL;
149 rtl_uString* rus = NULL;
150 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
151 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
154 }} // namespace
156 CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat);
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */