bump product version to 4.1.6.2
[LibreOffice.git] / sal / qa / rtl / strings / test_oustring_concat.cxx
blob9cde7b80bc66d1ea0b8b2f96b31499863fe4705f
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 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 #ifdef RTL_FAST_STRING
56 #define TYPES_ASSERT_EQUAL( a, b ) CPPUNIT_ASSERT_EQUAL( a, b )
57 #else
58 #define TYPES_ASSERT_EQUAL( a, b )
59 #endif
60 void test::oustring::StringConcat::checkConcat()
62 // All the extra () are to protect commas againsts being treated as separators of macro arguments.
63 CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString()));
64 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
65 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( OUString( "foo" ) + OUString( "bar" )));
66 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
67 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
68 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
69 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< OUString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
70 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( "foo" ) + "bar" ));
71 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringLiteral, const char[ 4 ] > )), typeid( OUStringLiteral( "foo" ) + "bar" ));
72 const char d1[] = "xyz";
73 CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1 ));
74 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1 ));
75 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + OUString( "bar" )));
76 TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringBuffer, OUString > )), typeid( OUStringBuffer( "foo" ) + OUString( "bar" )));
79 void test::oustring::StringConcat::checkEnsureCapacity()
81 rtl_uString* str = NULL;
82 rtl_uString_newFromLiteral( &str, "test", strlen( "test" ), 0 );
83 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
84 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
86 rtl_uString* oldStr = str;
87 rtl_uString_ensureCapacity( &str, 4 ); // should be no-op
88 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
89 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
90 CPPUNIT_ASSERT( oldStr == str );
92 rtl_uString_acquire( oldStr );
93 CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
94 rtl_uString_ensureCapacity( &str, 4 );
95 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
96 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
97 // a copy was forced because of refcount
98 CPPUNIT_ASSERT( oldStr != str );
99 CPPUNIT_ASSERT( rtl_ustr_compare( oldStr->buffer, str->buffer ) == 0 );
100 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
101 rtl_uString_release( str );
102 str = oldStr;
104 rtl_uString_acquire( oldStr );
105 rtl_uString_ensureCapacity( &str, 1024 );
106 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
107 CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
108 CPPUNIT_ASSERT( oldStr != str );
109 CPPUNIT_ASSERT( rtl_ustr_compare( oldStr->buffer, str->buffer ) == 0 );
110 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
111 // but there should be extra capacity
112 for( int i = 0;
113 i < 20;
114 ++i )
115 str->buffer[ str->length + i ] = '0';
116 str->length += 20;
117 rtl_uString_release( str );
118 rtl_uString_release( oldStr );
121 void test::oustring::StringConcat::checkAppend()
123 OUString str( "foo" );
124 str += OUStringLiteral( "bar" ) + "baz";
125 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), str );
126 OUStringBuffer buf( "foo" );
127 buf.append( OUStringLiteral( "bar" ) + "baz" );
128 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf.makeStringAndClear());
131 #define INVALID_CONCAT( expression ) \
133 rtl_string_unittest_invalid_concat = false, \
134 ( void ) OUString( expression ), \
135 rtl_string_unittest_invalid_concat )
137 void test::oustring::StringConcat::checkInvalid()
139 #ifdef RTL_FAST_STRING
140 CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
141 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" )));
142 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" )));
143 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (const char*) "b" ));
144 char d[] = "b";
145 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d ));
146 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (char*)d ));
147 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" )));
148 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 ));
149 rtl_String* rs = NULL;
150 rtl_uString* rus = NULL;
151 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
152 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
153 #endif
157 }} // namespace
159 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat);
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */