1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
29 template< typename charT
, typename traits
> std::basic_ostream
<charT
, traits
> &
31 std::basic_ostream
<charT
, traits
> & stream
, const std::type_info
& info
)
33 return stream
<< info
.name();
37 namespace test
{ namespace oustring
{
39 class StringConcat
: public CppUnit::TestFixture
43 void checkEnsureCapacity();
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::oustring::StringConcat::checkConcat()
57 // All the extra () are to protect commas against being treated as separators of macro arguments.
58 CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString()));
59 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
60 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, OUString
> )), typeid( OUString( "foo" ) + OUString( "bar" )));
61 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
62 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
63 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
64 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringConcat
< OUString
, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
65 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( "foo" ) + "bar" ));
66 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringLiteral
, const char[ 4 ] > )), typeid( OUStringLiteral( "foo" ) + "bar" ));
67 const char d1
[] = "xyz";
68 CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1
));
69 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUString
, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1
));
70 CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + OUString( "bar" )));
71 CPPUNIT_ASSERT_EQUAL(( typeid( OUStringConcat
< OUStringBuffer
, OUString
> )), typeid( OUStringBuffer( "foo" ) + OUString( "bar" )));
74 void test::oustring::StringConcat::checkEnsureCapacity()
76 rtl_uString
* str
= NULL
;
77 rtl_uString_newFromLiteral( &str
, "test", strlen( "test" ), 0 );
78 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
79 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
81 rtl_uString
* oldStr
= str
;
82 rtl_uString_ensureCapacity( &str
, 4 ); // should be no-op
83 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
84 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
85 CPPUNIT_ASSERT( oldStr
== str
);
87 rtl_uString_acquire( oldStr
);
88 CPPUNIT_ASSERT_EQUAL( 2, int( str
->refCount
));
89 rtl_uString_ensureCapacity( &str
, 4 );
90 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
);
91 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
92 // a copy was forced because of refcount
93 CPPUNIT_ASSERT( oldStr
!= str
);
94 CPPUNIT_ASSERT( rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) == 0 );
95 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
96 rtl_uString_release( str
);
99 rtl_uString_acquire( oldStr
);
100 rtl_uString_ensureCapacity( &str
, 1024 );
101 CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str
->length
); // size is still 4
102 CPPUNIT_ASSERT_EQUAL( 1, int( str
->refCount
));
103 CPPUNIT_ASSERT( oldStr
!= str
);
104 CPPUNIT_ASSERT( rtl_ustr_compare( oldStr
->buffer
, str
->buffer
) == 0 );
105 CPPUNIT_ASSERT_EQUAL( 1, int( oldStr
->refCount
));
106 // but there should be extra capacity
110 str
->buffer
[ str
->length
+ i
] = '0';
112 rtl_uString_release( str
);
113 rtl_uString_release( oldStr
);
116 void test::oustring::StringConcat::checkAppend()
118 OUString
str( "foo" );
119 str
+= OUStringLiteral( "bar" ) + "baz";
120 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), str
);
121 OUStringBuffer
buf( "foo" );
122 buf
.append( OUStringLiteral( "bar" ) + "baz" );
123 CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf
.makeStringAndClear());
126 #define INVALID_CONCAT( expression ) \
128 rtl_string_unittest_invalid_concat = false, \
129 ( void ) OUString( expression ), \
130 rtl_string_unittest_invalid_concat )
132 void test::oustring::StringConcat::checkInvalid()
134 CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
135 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" )));
136 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" )));
137 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (const char*) "b" ));
139 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d
));
140 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (char*)d
));
141 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" )));
142 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 ));
143 rtl_String
* rs
= NULL
;
144 rtl_uString
* rus
= NULL
;
145 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs
));
146 CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus
));
151 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat
);
153 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */