LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/rtl/strings - test_ostring_concat.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 86 88 97.7 %
Date: 2012-12-17 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 againsts 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           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringBuffer( "foo" ) + OString( "bar" )));
      87           2 :     TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringBuffer, OString > )), typeid( OStringBuffer( "foo" ) + OString( "bar" )));
      88           2 : }
      89             : #undef typeid
      90             : 
      91           2 : void test::ostring::StringConcat::checkEnsureCapacity()
      92             : {
      93           2 :     rtl_String* str = NULL;
      94           2 :     rtl_string_newFromLiteral( &str, "test", strlen( "test" ), 0 );
      95           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
      96           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
      97             : 
      98           2 :     rtl_String* oldStr = str;
      99           2 :     rtl_string_ensureCapacity( &str, 4 ); // should be no-op
     100           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
     101           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     102           2 :     CPPUNIT_ASSERT( oldStr == str );
     103             : 
     104           2 :     rtl_string_acquire( oldStr );
     105           2 :     CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
     106           2 :     rtl_string_ensureCapacity( &str, 4 );
     107           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
     108           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     109             :     // a copy was forced because of refcount
     110           2 :     CPPUNIT_ASSERT( oldStr != str );
     111           2 :     CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
     112           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     113           2 :     rtl_string_release( str );
     114           2 :     str = oldStr;
     115             : 
     116           2 :     rtl_string_acquire( oldStr );
     117           2 :     rtl_string_ensureCapacity( &str, 1024 );
     118           2 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
     119           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     120           2 :     CPPUNIT_ASSERT( oldStr != str );
     121           2 :     CPPUNIT_ASSERT( strcmp( oldStr->buffer, str->buffer ) == 0 );
     122           2 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     123           2 :     strcpy( str->buffer, "01234567890123456789" ); // but there should be extra capacity
     124           2 :     str->length += 20;
     125           2 :     rtl_string_release( str );
     126           2 :     rtl_string_release( oldStr );
     127           2 : }
     128             : 
     129           2 : void test::ostring::StringConcat::checkAppend()
     130             : {
     131           2 :     OString str( "foo" );
     132           2 :     str += OStringLiteral( "bar" ) + "baz";
     133           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), str );
     134           2 :     OStringBuffer buf( "foo" );
     135           2 :     buf.append( OStringLiteral( "bar" ) + "baz" );
     136           2 :     CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear());
     137           2 : }
     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           2 : void test::ostring::StringConcat::checkInvalid()
     146             : {
     147             : #ifdef RTL_FAST_STRING
     148           2 :     CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString()));
     149           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" )));
     150           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" )));
     151           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" )));
     152           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 ));
     153           2 :     rtl_String* rs = NULL;
     154           2 :     rtl_uString* rus = NULL;
     155           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
     156           2 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
     157             : #endif
     158           2 : }
     159             : 
     160             : }} // namespace
     161             : 
     162           6 : CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat);
     163             : 
     164             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10