LCOV - code coverage report
Current view: top level - sal/qa/rtl/strings - test_oustring_stringliterals.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 91 91 100.0 %
Date: 2014-11-03 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          30 : 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           4 : CPPUNIT_TEST_SUITE(StringLiterals);
      38           2 : CPPUNIT_TEST(checkCtors);
      39           2 : CPPUNIT_TEST(checkUsage);
      40           2 : CPPUNIT_TEST(checkExtraIntArgument);
      41           2 : CPPUNIT_TEST(checkNonconstChar);
      42           2 : CPPUNIT_TEST(checkBuffer);
      43           4 : 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           2 : void test::oustring::StringLiterals::checkCtors()
      56             : {
      57           2 :     CPPUNIT_ASSERT( VALID_CONVERSION( "test" ));
      58           2 :     const char good1[] = "test";
      59           2 :     CPPUNIT_ASSERT( VALID_CONVERSION( good1 ));
      60             : 
      61           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( (const char*) "test" ));
      62           2 :     const char* bad1 = good1;
      63           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad1 ));
      64           2 :     char bad2[] = "test";
      65           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad2 ));
      66           2 :     char* bad3 = bad2;
      67           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad3 ));
      68           2 :     const char* bad4[] = { "test1" };
      69           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad4[ 0 ] ));
      70           2 :     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           2 :     const char bad5[][ 6 ] = { "test", "test2" };
      76             : //    CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] ));
      77           2 :     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           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( "" ));
      81           2 :     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           2 : }
      89             : 
      90           2 : void test::oustring::StringLiterals::testcall( const char str[] )
      91             : {
      92           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( str )));
      93           2 : }
      94             : 
      95           2 : 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           2 :     rtl::OUString foo( "foo" );
     100           4 :     rtl::OUString FoO( "FoO" );
     101           4 :     rtl::OUString foobarfoo( "foobarfoo" );
     102           4 :     rtl::OUString foobar( "foobar" );
     103           4 :     rtl::OUString FooBaRfoo( "FooBaRfoo" );
     104           4 :     rtl::OUString FooBaR( "FooBaR" );
     105           4 :     rtl::OUString bar( "bar" );
     106           4 :     rtl::OUString test( "test" );
     107             : 
     108           2 :     rtl_string_unittest_const_literal = false; // start checking for OUString conversions
     109           2 :     CPPUNIT_ASSERT_EQUAL( foo, rtl::OUString() = "foo" );
     110           2 :     CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( "fOo" ));
     111           2 :     CPPUNIT_ASSERT( foobarfoo.match( "bar", 3 ));
     112           2 :     CPPUNIT_ASSERT( foobar.match( "foo" ));
     113           2 :     CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 ));
     114           2 :     CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" ));
     115           2 :     CPPUNIT_ASSERT( foobar.startsWith( "foo" ));
     116           2 :     CPPUNIT_ASSERT( FooBaR.startsWithIgnoreAsciiCase( "foo" ));
     117           2 :     CPPUNIT_ASSERT( foobar.endsWith( "bar" ));
     118           2 :     CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" ));
     119           2 :     CPPUNIT_ASSERT( foo == "foo" );
     120           2 :     CPPUNIT_ASSERT( "foo" == foo );
     121           2 :     CPPUNIT_ASSERT( foo != "bar" );
     122           2 :     CPPUNIT_ASSERT( "foo" != bar );
     123           2 :     CPPUNIT_ASSERT( foobarfoo.indexOf( "foo", 1 ) == 6 );
     124           2 :     CPPUNIT_ASSERT( foobarfoo.lastIndexOf( "foo" ) == 6 );
     125           2 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", test ) == "testbarfoo" );
     126           2 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", "test" ) == "testbarfoo" );
     127           2 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", test ) == "testbartest" );
     128           2 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", "test" ) == "testbartest" );
     129           2 :     CPPUNIT_ASSERT( foo.reverseCompareTo( "foo" ) == 0 );
     130             :     // if this is not true, some of the calls above converted to OUString
     131           4 :     CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false );
     132           2 : }
     133             : 
     134           2 : 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           2 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( "bar" ));
     143           2 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( RTL_CONSTASCII_STRINGPARAM( "bar" )));
     144           2 : }
     145             : 
     146           2 : void test::oustring::StringLiterals::checkNonconstChar()
     147             : { // check that non-const char[] data do not trigger string literal overloads
     148           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), rtl::OUString( "footest" ).replaceAll( "test", "bar" ));
     149           2 :     char test[] = "test";
     150           2 :     char bar[] = "bar";
     151           2 :     const char consttest[] = "test";
     152           2 :     const char constbar[] = "bar";
     153           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, bar )));
     154           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( consttest, bar )));
     155           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, constbar )));
     156           2 :     CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest, constbar ));
     157           2 : }
     158             : 
     159           2 : void test::oustring::StringLiterals::checkBuffer()
     160             : {
     161           2 :     rtl::OUStringBuffer buf;
     162           2 :     buf.append( "foo" );
     163           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf.toString());
     164           2 :     buf.append( "bar" );
     165           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf.toString());
     166           2 :     buf.insert( 3, "baz" );
     167           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf.toString());
     168           2 :     char d[] = "d";
     169           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUString( d ))));
     170           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUStringBuffer( d ))));
     171           2 : }
     172             : 
     173             : }} // namespace
     174             : 
     175           6 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals);
     176             : 
     177             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10