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

Generated by: LCOV version 1.10