LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_oustring_stringliterals.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 91 91 100.0 %
Date: 2014-04-11 Functions: 14 14 100.0 %
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 the extra needed ctor
      11             : #define RTL_STRING_UNITTEST
      12             : extern bool rtl_string_unittest_const_literal;
      13             : extern bool rtl_string_unittest_invalid_conversion;
      14             : extern bool rtl_string_unittest_const_literal_function;
      15             : extern bool rtl_string_unittest_non_const_literal_function;
      16             : 
      17             : #include <sal/types.h>
      18             : #include <cppunit/TestFixture.h>
      19             : #include <cppunit/extensions/HelperMacros.h>
      20             : #include "rtl/string.h"
      21             : #include "rtl/ustring.hxx"
      22             : #include "rtl/ustrbuf.hxx"
      23             : 
      24             : namespace test { namespace oustring {
      25             : 
      26          15 : class StringLiterals: public CppUnit::TestFixture
      27             : {
      28             : private:
      29             :     void checkCtors();
      30             :     void checkUsage();
      31             :     void checkExtraIntArgument();
      32             :     void checkNonconstChar();
      33             :     void checkBuffer();
      34             : 
      35             :     void testcall( const char str[] );
      36             : 
      37           2 : CPPUNIT_TEST_SUITE(StringLiterals);
      38           1 : CPPUNIT_TEST(checkCtors);
      39           1 : CPPUNIT_TEST(checkUsage);
      40           1 : CPPUNIT_TEST(checkExtraIntArgument);
      41           1 : CPPUNIT_TEST(checkNonconstChar);
      42           1 : CPPUNIT_TEST(checkBuffer);
      43           2 : CPPUNIT_TEST_SUITE_END();
      44             : };
      45             : 
      46             : // reset the flag, evaluate the expression and return
      47             : // whether the string literal ctor was used (i.e. whether the conversion was valid)
      48             : #define VALID_CONVERSION( expression ) \
      49             :     ( \
      50             :     rtl_string_unittest_invalid_conversion = false, \
      51             :     ( void ) rtl::OUString( expression ), \
      52             :     ( void ) rtl::OUStringBuffer( expression ), \
      53             :     !rtl_string_unittest_invalid_conversion )
      54             : 
      55           1 : void test::oustring::StringLiterals::checkCtors()
      56             : {
      57           1 :     CPPUNIT_ASSERT( VALID_CONVERSION( "test" ));
      58           1 :     const char good1[] = "test";
      59           1 :     CPPUNIT_ASSERT( VALID_CONVERSION( good1 ));
      60             : 
      61           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( (const char*) "test" ));
      62           1 :     const char* bad1 = good1;
      63           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad1 ));
      64           1 :     char bad2[] = "test";
      65           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad2 ));
      66           1 :     char* bad3 = bad2;
      67           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad3 ));
      68           1 :     const char* bad4[] = { "test1" };
      69           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad4[ 0 ] ));
      70           1 :     testcall( good1 );
      71             : 
      72             : // This one is technically broken, since the first element is 6 characters test\0\0,
      73             : // but there does not appear a way to detect this by compile time (runtime will assert()).
      74             : // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
      75           1 :     const char bad5[][ 6 ] = { "test", "test2" };
      76             : //    CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] ));
      77           1 :     CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 1 ] ));
      78             : 
      79             : // Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used.
      80           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( "" ));
      81           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "ab" ), rtl::OUString( "ab" ));
      82             : #if 0
      83             : // Also check that embedded \0 is included.
      84             : // In fact, allowing this is probably just trouble, so this now asserts.
      85             :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "\0" ), rtl::OUString( "\0" ));
      86             :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "a\0b" ), rtl::OUString( "a\0b" ));
      87             : #endif
      88           1 : }
      89             : 
      90           1 : void test::oustring::StringLiterals::testcall( const char str[] )
      91             : {
      92           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( str )));
      93           1 : }
      94             : 
      95           1 : void test::oustring::StringLiterals::checkUsage()
      96             : {
      97             : // simply check that all string literal based calls work as expected
      98             : // also check that they really use string literal overload and do not convert to OUString
      99           1 :     rtl::OUString foo( "foo" );
     100           2 :     rtl::OUString FoO( "FoO" );
     101           2 :     rtl::OUString foobarfoo( "foobarfoo" );
     102           2 :     rtl::OUString foobar( "foobar" );
     103           2 :     rtl::OUString FooBaRfoo( "FooBaRfoo" );
     104           2 :     rtl::OUString FooBaR( "FooBaR" );
     105           2 :     rtl::OUString bar( "bar" );
     106           2 :     rtl::OUString test( "test" );
     107             : 
     108           1 :     rtl_string_unittest_const_literal = false; // start checking for OUString conversions
     109           1 :     CPPUNIT_ASSERT_EQUAL( foo, rtl::OUString() = "foo" );
     110           1 :     CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( "fOo" ));
     111           1 :     CPPUNIT_ASSERT( foobarfoo.match( "bar", 3 ));
     112           1 :     CPPUNIT_ASSERT( foobar.match( "foo" ));
     113           1 :     CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 ));
     114           1 :     CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" ));
     115           1 :     CPPUNIT_ASSERT( foobar.startsWith( "foo" ));
     116           1 :     CPPUNIT_ASSERT( FooBaR.startsWithIgnoreAsciiCase( "foo" ));
     117           1 :     CPPUNIT_ASSERT( foobar.endsWith( "bar" ));
     118           1 :     CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" ));
     119           1 :     CPPUNIT_ASSERT( foo == "foo" );
     120           1 :     CPPUNIT_ASSERT( "foo" == foo );
     121           1 :     CPPUNIT_ASSERT( foo != "bar" );
     122           1 :     CPPUNIT_ASSERT( "foo" != bar );
     123           1 :     CPPUNIT_ASSERT( foobarfoo.indexOf( "foo", 1 ) == 6 );
     124           1 :     CPPUNIT_ASSERT( foobarfoo.lastIndexOf( "foo" ) == 6 );
     125           1 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", test ) == "testbarfoo" );
     126           1 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", "test" ) == "testbarfoo" );
     127           1 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", test ) == "testbartest" );
     128           1 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", "test" ) == "testbartest" );
     129           1 :     CPPUNIT_ASSERT( foo.reverseCompareTo( "foo" ) == 0 );
     130             :     // if this is not true, some of the calls above converted to OUString
     131           2 :     CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false );
     132           1 : }
     133             : 
     134           1 : void test::oustring::StringLiterals::checkExtraIntArgument()
     135             : {
     136             :     // This makes sure that using by mistake RTL_CONSTASCII_STRINGPARAM does not trigger a different
     137             :     // overload, i.e. the second argument to match() in this case is the indexFrom argument,
     138             :     // but with the macro it would contain the length of the string. Therefore
     139             :     // match( RTL_CONSTASCII_STRINGPARAM( "bar" )) would be match( "bar", 3 ), which would be
     140             :     // true when called for OUString( "foobar" ). But this should not happen because of the
     141             :     // &foo[0] trick in the RTL_CONSTASCII_STRINGPARAM macro.
     142           1 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( "bar" ));
     143           1 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( RTL_CONSTASCII_STRINGPARAM( "bar" )));
     144           1 : }
     145             : 
     146           1 : void test::oustring::StringLiterals::checkNonconstChar()
     147             : { // check that non-const char[] data do not trigger string literal overloads
     148           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), rtl::OUString( "footest" ).replaceAll( "test", "bar" ));
     149           1 :     char test[] = "test";
     150           1 :     char bar[] = "bar";
     151           1 :     const char consttest[] = "test";
     152           1 :     const char constbar[] = "bar";
     153           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, bar )));
     154           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( consttest, bar )));
     155           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, constbar )));
     156           1 :     CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest, constbar ));
     157           1 : }
     158             : 
     159           1 : void test::oustring::StringLiterals::checkBuffer()
     160             : {
     161           1 :     rtl::OUStringBuffer buf;
     162           1 :     buf.append( "foo" );
     163           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf.toString());
     164           1 :     buf.append( "bar" );
     165           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf.toString());
     166           1 :     buf.insert( 3, "baz" );
     167           1 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf.toString());
     168           1 :     char d[] = "d";
     169           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUString( d ))));
     170           1 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUStringBuffer( d ))));
     171           1 : }
     172             : 
     173             : }} // namespace
     174             : 
     175           3 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals);
     176             : 
     177             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10