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

Generated by: LCOV version 1.11