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

Generated by: LCOV version 1.11