Line data Source code
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 : bool rtl_string_unittest_invalid_concat = false;
13 :
14 : #include <sal/types.h>
15 : #include <cppunit/TestFixture.h>
16 : #include <cppunit/extensions/HelperMacros.h>
17 :
18 : #include <rtl/string.hxx>
19 : #include <rtl/strbuf.hxx>
20 : #include <rtl/ustring.hxx>
21 : #include <rtl/ustrbuf.hxx>
22 :
23 : #include <typeinfo>
24 :
25 : using namespace rtl;
26 :
27 : namespace std
28 : {
29 : template< typename charT, typename traits > std::basic_ostream<charT, traits> &
30 0 : operator <<(
31 : std::basic_ostream<charT, traits> & stream, const std::type_info& info )
32 : {
33 0 : return stream << info.name();
34 : }
35 : } // namespace
36 :
37 : namespace test { namespace ostring {
38 :
39 12 : class StringConcat : public CppUnit::TestFixture
40 : {
41 : private:
42 : void checkConcat();
43 : void checkEnsureCapacity();
44 : void checkAppend();
45 : void checkInvalid();
46 :
47 2 : CPPUNIT_TEST_SUITE(StringConcat);
48 1 : CPPUNIT_TEST(checkConcat);
49 1 : CPPUNIT_TEST(checkEnsureCapacity);
50 1 : CPPUNIT_TEST(checkAppend);
51 1 : CPPUNIT_TEST(checkInvalid);
52 2 : CPPUNIT_TEST_SUITE_END();
53 : };
54 :
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 1 : void test::ostring::StringConcat::checkConcat()
61 : {
62 : // All the extra () are to protect commas againsts being treated as separators of macro arguments.
63 1 : CPPUNIT_ASSERT_EQUAL( OString(), OString(OString() + OString()));
64 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + OString( "bar" )));
65 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, OString > )), typeid( OString( "foo" ) + OString( "bar" )));
66 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + "bar" ));
67 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" ));
68 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), OString( OString( "foo" ) + "bar" + "baz" ));
69 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringConcat< OString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" + "baz" ));
70 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + "bar" ));
71 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char[ 4 ] > )), typeid( OStringLiteral( "foo" ) + "bar" ));
72 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + (const char*)"bar" ));
73 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char* > )), typeid( OStringLiteral( "foo" ) + (const char*)"bar" ));
74 1 : const char d1[] = "xyz";
75 1 : char d2[] = "abc";
76 1 : const char* d3 = d1;
77 1 : char* d4 = d2;
78 1 : CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d1 ));
79 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + d1 ));
80 1 : CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d2 ));
81 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, char[ 4 ] > )), typeid( OString( "foo" ) + d2 ));
82 1 : CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d3 ));
83 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char* > )), typeid( OString( "foo" ) + d3 ));
84 1 : CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 ));
85 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 ));
86 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringBuffer( "foo" ) + OString( "bar" )));
87 1 : TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringBuffer, OString > )), typeid( OStringBuffer( "foo" ) + OString( "bar" )));
88 1 : }
89 : #undef typeid
90 :
91 1 : void test::ostring::StringConcat::checkEnsureCapacity()
92 : {
93 1 : rtl_String* str = NULL;
94 1 : rtl_string_newFromLiteral( &str, "test", strlen( "test" ), 0 );
95 1 : CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
96 1 : CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
97 :
98 1 : rtl_String* oldStr = str;
99 1 : rtl_string_ensureCapacity( &str, 4 ); // should be no-op
100 1 : CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
101 1 : CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
102 1 : CPPUNIT_ASSERT( oldStr == str );
103 :
104 1 : rtl_string_acquire( oldStr );
105 1 : CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
106 1 : rtl_string_ensureCapacity( &str, 4 );
107 1 : CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
108 1 : CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
109 : // a copy was forced because of refcount
110 1 : CPPUNIT_ASSERT( oldStr != str );
111 1 : CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
112 1 : CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
113 1 : rtl_string_release( str );
114 1 : str = oldStr;
115 :
116 1 : rtl_string_acquire( oldStr );
117 1 : rtl_string_ensureCapacity( &str, 1024 );
118 1 : CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
119 1 : CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
120 1 : CPPUNIT_ASSERT( oldStr != str );
121 1 : CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
122 1 : CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
123 1 : strcpy( str->buffer, "01234567890123456789" ); // but there should be extra capacity
124 1 : str->length += 20;
125 1 : rtl_string_release( str );
126 1 : rtl_string_release( oldStr );
127 1 : }
128 :
129 1 : void test::ostring::StringConcat::checkAppend()
130 : {
131 1 : OString str( "foo" );
132 1 : str += OStringLiteral( "bar" ) + "baz";
133 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), str );
134 1 : OStringBuffer buf( "foo" );
135 1 : buf.append( OStringLiteral( "bar" ) + "baz" );
136 1 : CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear());
137 1 : }
138 :
139 : #define INVALID_CONCAT( expression ) \
140 : ( \
141 : rtl_string_unittest_invalid_concat = false, \
142 : ( void ) OString( expression ), \
143 : rtl_string_unittest_invalid_concat )
144 :
145 1 : void test::ostring::StringConcat::checkInvalid()
146 : {
147 : #ifdef RTL_FAST_STRING
148 1 : CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString()));
149 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" )));
150 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" )));
151 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" )));
152 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 ));
153 1 : rtl_String* rs = NULL;
154 1 : rtl_uString* rus = NULL;
155 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
156 1 : CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
157 : #endif
158 1 : }
159 :
160 : }} // namespace
161 :
162 3 : CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat);
163 :
164 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|