LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_oustring_concat.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 80 82 97.6 %
Date: 2014-04-11 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             : extern bool rtl_string_unittest_invalid_concat;
      13             : 
      14             : #include <sal/types.h>
      15             : #include <cppunit/TestFixture.h>
      16             : #include <cppunit/extensions/HelperMacros.h>
      17             : 
      18             : #include <rtl/ustring.hxx>
      19             : #include <rtl/ustrbuf.hxx>
      20             : #include <rtl/string.hxx>
      21             : #include <rtl/strbuf.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 oustring {
      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::oustring::StringConcat::checkConcat()
      61             : {
      62             : // All the extra () are to protect commas against being treated as separators of macro arguments.
      63           1 :     CPPUNIT_ASSERT_EQUAL( OUString(), OUString(OUString() + OUString()));
      64           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + OUString( "bar" )));
      65           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, OUString > )), typeid( OUString( "foo" ) + OUString( "bar" )));
      66           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUString( "foo" ) + "bar" ));
      67           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" ));
      68           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), OUString( OUString( "foo" ) + "bar" + "baz" ));
      69           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringConcat< OUString, const char[ 4 ] >, const char[ 4 ] > )), typeid( OUString( "foo" ) + "bar" + "baz" ));
      70           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringLiteral( "foo" ) + "bar" ));
      71           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringLiteral, const char[ 4 ] > )), typeid( OUStringLiteral( "foo" ) + "bar" ));
      72           1 :     const char d1[] = "xyz";
      73           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1 ));
      74           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1 ));
      75           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + OUString( "bar" )));
      76           1 :     TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringBuffer, OUString > )), typeid( OUStringBuffer( "foo" ) + OUString( "bar" )));
      77           1 : }
      78             : 
      79           1 : void test::oustring::StringConcat::checkEnsureCapacity()
      80             : {
      81           1 :     rtl_uString* str = NULL;
      82           1 :     rtl_uString_newFromLiteral( &str, "test", strlen( "test" ), 0 );
      83           1 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
      84           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
      85             : 
      86           1 :     rtl_uString* oldStr = str;
      87           1 :     rtl_uString_ensureCapacity( &str, 4 ); // should be no-op
      88           1 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
      89           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
      90           1 :     CPPUNIT_ASSERT( oldStr == str );
      91             : 
      92           1 :     rtl_uString_acquire( oldStr );
      93           1 :     CPPUNIT_ASSERT_EQUAL( 2, int( str->refCount ));
      94           1 :     rtl_uString_ensureCapacity( &str, 4 );
      95           1 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length );
      96           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
      97             :     // a copy was forced because of refcount
      98           1 :     CPPUNIT_ASSERT( oldStr != str );
      99           1 :     CPPUNIT_ASSERT( rtl_ustr_compare( oldStr->buffer, str->buffer ) == 0 );
     100           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     101           1 :     rtl_uString_release( str );
     102           1 :     str = oldStr;
     103             : 
     104           1 :     rtl_uString_acquire( oldStr );
     105           1 :     rtl_uString_ensureCapacity( &str, 1024 );
     106           1 :     CPPUNIT_ASSERT_EQUAL( sal_Int32( 4 ), str->length ); // size is still 4
     107           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( str->refCount ));
     108           1 :     CPPUNIT_ASSERT( oldStr != str );
     109           1 :     CPPUNIT_ASSERT( rtl_ustr_compare( oldStr->buffer, str->buffer ) == 0 );
     110           1 :     CPPUNIT_ASSERT_EQUAL( 1, int( oldStr->refCount ));
     111             :     // but there should be extra capacity
     112          21 :     for( int i = 0;
     113             :          i < 20;
     114             :          ++i )
     115          20 :         str->buffer[ str->length + i ] = '0';
     116           1 :     str->length += 20;
     117           1 :     rtl_uString_release( str );
     118           1 :     rtl_uString_release( oldStr );
     119           1 : }
     120             : 
     121           1 : void test::oustring::StringConcat::checkAppend()
     122             : {
     123           1 :     OUString str( "foo" );
     124           1 :     str += OUStringLiteral( "bar" ) + "baz";
     125           1 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), str );
     126           2 :     OUStringBuffer buf( "foo" );
     127           1 :     buf.append( OUStringLiteral( "bar" ) + "baz" );
     128           2 :     CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf.makeStringAndClear());
     129           1 : }
     130             : 
     131             : #define INVALID_CONCAT( expression ) \
     132             :     ( \
     133             :     rtl_string_unittest_invalid_concat = false, \
     134             :     ( void ) OUString( expression ), \
     135             :     rtl_string_unittest_invalid_concat )
     136             : 
     137           1 : void test::oustring::StringConcat::checkInvalid()
     138             : {
     139             : #ifdef RTL_FAST_STRING
     140           1 :     CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString()));
     141           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" )));
     142           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" )));
     143           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (const char*) "b" ));
     144           1 :     char d[] = "b";
     145           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d ));
     146           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (char*)d ));
     147           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" )));
     148           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 ));
     149           1 :     rtl_String* rs = NULL;
     150           1 :     rtl_uString* rus = NULL;
     151           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs ));
     152           1 :     CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus ));
     153             : #endif
     154             : 
     155           1 : }
     156             : 
     157             : }} // namespace
     158             : 
     159           3 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat);
     160             : 
     161             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10