LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_ostring_concat.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 86 88 97.7 %
Date: 2014-11-03 Functions: 12 13 92.3 %
Legend: Lines: hit not hit

          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          24 : class StringConcat : public CppUnit::TestFixture
      40             : {
      41             : private:
      42             :     void checkConcat();
      43             :     void checkEnsureCapacity();
      44             :     void checkAppend();
      45             :     void checkInvalid();
      46             : 
      47           4 : CPPUNIT_TEST_SUITE(StringConcat);
      48           2 : CPPUNIT_TEST(checkConcat);
      49           2 : CPPUNIT_TEST(checkEnsureCapacity);
      50           2 : CPPUNIT_TEST(checkAppend);
      51           2 : CPPUNIT_TEST(checkInvalid);
      52           4 : 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           2 : void test::ostring::StringConcat::checkConcat()
      61             : {
      62             : // All the extra () are to protect commas against being treated as separators of macro arguments.
      63           2 :     CPPUNIT_ASSERT_EQUAL( OString(), OString(OString() + OString()));
      64           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + OString( "bar" )));
      65           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, OString > )), typeid( OString( "foo" ) + OString( "bar" )));
      66           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OString( "foo" ) + "bar" ));
      67           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" ));
      68           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), OString( OString( "foo" ) + "bar" + "baz" ));
      69           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringConcat< OString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OString( "foo" ) + "bar" + "baz" ));
      70           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + "bar" ));
      71           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char[ 4 ] > )), typeid( OStringLiteral( "foo" ) + "bar" ));
      72           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringLiteral( "foo" ) + (const char*)"bar" ));
      73           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringLiteral, const char* > )), typeid( OStringLiteral( "foo" ) + (const char*)"bar" ));
      74           2 :     const char d1[] = "xyz";
      75           2 :     char d2[] = "abc";
      76           2 :     const char* d3 = d1;
      77           2 :     char* d4 = d2;
      78           2 :     CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d1 ));
      79           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char[ 4 ] > )), typeid( OString( "foo" ) + d1 ));
      80           2 :     CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d2 ));
      81           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, char[ 4 ] > )), typeid( OString( "foo" ) + d2 ));
      82           2 :     CPPUNIT_ASSERT_EQUAL( OString( "fooxyz" ), OString( OString( "foo" ) + d3 ));
      83           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char* > )), typeid( OString( "foo" ) + d3 ));
      84           2 :     CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 ));
      85           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 ));
      86             : #ifdef __GNUC__
      87           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringBuffer( "foo" ) + OString( "bar" )));
      88           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringBuffer, OString > )), typeid( OStringBuffer( "foo" ) + OString( "bar" )));
      89             : #endif
      90           2 : }
      91             : #undef typeid
      92             : 
      93           2 : void test::ostring::StringConcat::checkEnsureCapacity()
      94             : {
      95           2 :     rtl_String* str = NULL;
      96           2 :     rtl_string_newFromLiteral( &str, "test", strlen( "test" ), 0 );
      97           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
      98           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
      99             : 
     100           2 :     rtl_String* oldStr = str;
     101           2 :     rtl_string_ensureCapacity( &str, 4 ); // should be no-op
     102           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
     103           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     104           2 :     CPPUNIT_ASSERT( oldStr == str );
     105             : 
     106           2 :     rtl_string_acquire( oldStr );
     107           2 :     CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
     108           2 :     rtl_string_ensureCapacity( &str, 4 );
     109           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
     110           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     111             :     // a copy was forced because of refcount
     112           2 :     CPPUNIT_ASSERT( oldStr != str );
     113           2 :     CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
     114           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     115           2 :     rtl_string_release( str );
     116           2 :     str = oldStr;
     117             : 
     118           2 :     rtl_string_acquire( oldStr );
     119           2 :     rtl_string_ensureCapacity( &str, 1024 );
     120           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
     121           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     122           2 :     CPPUNIT_ASSERT( oldStr != str );
     123           2 :     CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
     124           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     125           2 :     strcpy( str->buffer, "01234567890123456789" ); // but there should be extra capacity
     126           2 :     str->length += 20;
     127           2 :     rtl_string_release( str );
     128           2 :     rtl_string_release( oldStr );
     129           2 : }
     130             : 
     131           2 : void test::ostring::StringConcat::checkAppend()
     132             : {
     133           2 :     OString str( "foo" );
     134           2 :     str += OStringLiteral( "bar" ) + "baz";
     135           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), str );
     136           4 :     OStringBuffer buf( "foo" );
     137           2 :     buf.append( OStringLiteral( "bar" ) + "baz" );
     138           4 :     CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear());
     139           2 : }
     140             : 
     141             : #define INVALID_CONCAT( expression ) \
     142             :     ( \
     143             :     rtl_string_unittest_invalid_concat = false, \
     144             :     ( void ) OString( expression ), \
     145             :     rtl_string_unittest_invalid_concat )
     146             : 
     147           2 : void test::ostring::StringConcat::checkInvalid()
     148             : {
     149             : #ifdef RTL_FAST_STRING
     150           2 :     CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString()));
     151           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" )));
     152           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" )));
     153           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" )));
     154           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 ));
     155           2 :     rtl_String* rs = NULL;
     156           2 :     rtl_uString* rus = NULL;
     157           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
     158           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
     159             : #endif
     160           2 : }
     161             : 
     162             : }} // namespace
     163             : 
     164           6 : CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat);
     165             : 
     166             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10