LCOV - code coverage report
Current view: top level - libreoffice/sal/qa/rtl/strings - test_oustring_stringliterals.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 91 91 100.0 %
Date: 2012-12-17 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             :  * Version: MPL 1.1 / GPLv3+ / LGPLv3+
       4             :  *
       5             :  * The contents of this file are subject to the Mozilla Public License Version
       6             :  * 1.1 (the "License"); you may not use this file except in compliance with
       7             :  * the License or as specified alternatively below. You may obtain a copy of
       8             :  * the License at http://www.mozilla.org/MPL/
       9             :  *
      10             :  * Software distributed under the License is distributed on an "AS IS" basis,
      11             :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12             :  * for the specific language governing rights and limitations under the
      13             :  * License.
      14             :  *
      15             :  * Major Contributor(s):
      16             :  * Copyright (C) 2012 Lubos Lunak <l.lunak@suse.cz> (initial developer)
      17             :  *
      18             :  * All Rights Reserved.
      19             :  *
      20             :  * For minor contributions see the git repository.
      21             :  *
      22             :  * Alternatively, the contents of this file may be used under the terms of
      23             :  * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
      24             :  * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
      25             :  * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
      26             :  * instead of those above.
      27             :  */
      28             : 
      29             : // activate the extra needed ctor
      30             : #define RTL_STRING_UNITTEST
      31             : extern bool rtl_string_unittest_const_literal;
      32             : extern bool rtl_string_unittest_invalid_conversion;
      33             : extern bool rtl_string_unittest_const_literal_function;
      34             : extern bool rtl_string_unittest_non_const_literal_function;
      35             : 
      36             : #include <sal/types.h>
      37             : #include <cppunit/TestFixture.h>
      38             : #include <cppunit/extensions/HelperMacros.h>
      39             : #include "rtl/string.h"
      40             : #include "rtl/ustring.hxx"
      41             : #include "rtl/ustrbuf.hxx"
      42             : 
      43             : namespace test { namespace oustring {
      44             : 
      45          30 : class StringLiterals: public CppUnit::TestFixture
      46             : {
      47             : private:
      48             :     void checkCtors();
      49             :     void checkUsage();
      50             :     void checkExtraIntArgument();
      51             :     void checkNonconstChar();
      52             :     void checkBuffer();
      53             : 
      54             :     void testcall( const char str[] );
      55             : 
      56           4 : CPPUNIT_TEST_SUITE(StringLiterals);
      57           2 : CPPUNIT_TEST(checkCtors);
      58           2 : CPPUNIT_TEST(checkUsage);
      59           2 : CPPUNIT_TEST(checkExtraIntArgument);
      60           2 : CPPUNIT_TEST(checkNonconstChar);
      61           2 : CPPUNIT_TEST(checkBuffer);
      62           4 : CPPUNIT_TEST_SUITE_END();
      63             : };
      64             : 
      65             : // reset the flag, evaluate the expression and return
      66             : // whether the string literal ctor was used (i.e. whether the conversion was valid)
      67             : #define VALID_CONVERSION( expression ) \
      68             :     ( \
      69             :     rtl_string_unittest_invalid_conversion = false, \
      70             :     ( void ) rtl::OUString( expression ), \
      71             :     ( void ) rtl::OUStringBuffer( expression ), \
      72             :     !rtl_string_unittest_invalid_conversion )
      73             : 
      74           2 : void test::oustring::StringLiterals::checkCtors()
      75             : {
      76           2 :     CPPUNIT_ASSERT( VALID_CONVERSION( "test" ));
      77           2 :     const char good1[] = "test";
      78           2 :     CPPUNIT_ASSERT( VALID_CONVERSION( good1 ));
      79             : 
      80           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( (const char*) "test" ));
      81           2 :     const char* bad1 = good1;
      82           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad1 ));
      83           2 :     char bad2[] = "test";
      84           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad2 ));
      85           2 :     char* bad3 = bad2;
      86           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad3 ));
      87           2 :     const char* bad4[] = { "test1" };
      88           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( bad4[ 0 ] ));
      89           2 :     testcall( good1 );
      90             : 
      91             : // This one is technically broken, since the first element is 6 characters test\0\0,
      92             : // but there does not appear a way to detect this by compile time (runtime will assert()).
      93             : // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
      94           2 :     const char bad5[][ 6 ] = { "test", "test2" };
      95             : //    CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] ));
      96           2 :     CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 1 ] ));
      97             : 
      98             : // Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used.
      99           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "" )));
     100           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "ab" ), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ab" )));
     101             : #if 0
     102             : // Also check that embedded \0 is included.
     103             : // In fact, allowing this is probably just trouble, so this now asserts.
     104             :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "\0" ), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "\0" )));
     105             :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "a\0b" ), rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "a\0b" )));
     106             : #endif
     107           2 : }
     108             : 
     109           2 : void test::oustring::StringLiterals::testcall( const char str[] )
     110             : {
     111           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( str )));
     112           2 : }
     113             : 
     114           2 : void test::oustring::StringLiterals::checkUsage()
     115             : {
     116             : // simply check that all string literal based calls work as expected
     117             : // also check that they really use string literal overload and do not convert to OUString
     118           2 :     rtl::OUString foo( "foo" );
     119           2 :     rtl::OUString FoO( "FoO" );
     120           2 :     rtl::OUString foobarfoo( "foobarfoo" );
     121           2 :     rtl::OUString foobar( "foobar" );
     122           2 :     rtl::OUString FooBaRfoo( "FooBaRfoo" );
     123           2 :     rtl::OUString FooBaR( "FooBaR" );
     124           2 :     rtl::OUString bar( "bar" );
     125           2 :     rtl::OUString test( "test" );
     126             : 
     127           2 :     rtl_string_unittest_const_literal = false; // start checking for OUString conversions
     128           2 :     CPPUNIT_ASSERT_EQUAL( foo, rtl::OUString() = "foo" );
     129           2 :     CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( "fOo" ));
     130           2 :     CPPUNIT_ASSERT( foobarfoo.match( "bar", 3 ));
     131           2 :     CPPUNIT_ASSERT( foobar.match( "foo" ));
     132           2 :     CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 ));
     133           2 :     CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" ));
     134           2 :     CPPUNIT_ASSERT( foobar.startsWith( "foo" ));
     135           2 :     CPPUNIT_ASSERT( FooBaR.startsWithIgnoreAsciiCase( "foo" ));
     136           2 :     CPPUNIT_ASSERT( foobar.endsWith( "bar" ));
     137           2 :     CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" ));
     138           2 :     CPPUNIT_ASSERT( foo == "foo" );
     139           2 :     CPPUNIT_ASSERT( "foo" == foo );
     140           2 :     CPPUNIT_ASSERT( foo != "bar" );
     141           2 :     CPPUNIT_ASSERT( "foo" != bar );
     142           2 :     CPPUNIT_ASSERT( foobarfoo.indexOf( "foo", 1 ) == 6 );
     143           2 :     CPPUNIT_ASSERT( foobarfoo.lastIndexOf( "foo" ) == 6 );
     144           2 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", test ) == "testbarfoo" );
     145           2 :     CPPUNIT_ASSERT( foobarfoo.replaceFirst( "foo", "test" ) == "testbarfoo" );
     146           2 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", test ) == "testbartest" );
     147           2 :     CPPUNIT_ASSERT( foobarfoo.replaceAll( "foo", "test" ) == "testbartest" );
     148             :     // if this is not true, some of the calls above converted to OUString
     149           2 :     CPPUNIT_ASSERT( rtl_string_unittest_const_literal == false );
     150           2 : }
     151             : 
     152           2 : void test::oustring::StringLiterals::checkExtraIntArgument()
     153             : {
     154             :     // This makes sure that using by mistake RTL_CONSTASCII_STRINGPARAM does not trigger a different
     155             :     // overload, i.e. the second argument to match() in this case is the indexFrom argument,
     156             :     // but with the macro it would contain the length of the string. Therefore
     157             :     // match( RTL_CONSTASCII_STRINGPARAM( "bar" )) would be match( "bar", 3 ), which would be
     158             :     // true when called for OUString( "foobar" ). But this should not happen because of the
     159             :     // &foo[0] trick in the RTL_CONSTASCII_STRINGPARAM macro.
     160           2 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( "bar" ));
     161           2 :     CPPUNIT_ASSERT( !rtl::OUString("foobar").match( RTL_CONSTASCII_STRINGPARAM( "bar" )));
     162           2 : }
     163             : 
     164           2 : void test::oustring::StringLiterals::checkNonconstChar()
     165             : { // check that non-const char[] data do not trigger string literal overloads
     166           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), rtl::OUString( "footest" ).replaceAll( "test", "bar" ));
     167           2 :     char test[] = "test";
     168           2 :     char bar[] = "bar";
     169           2 :     const char consttest[] = "test";
     170           2 :     const char constbar[] = "bar";
     171           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, bar )));
     172           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( consttest, bar )));
     173           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test, constbar )));
     174           2 :     CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest, constbar ));
     175           2 : }
     176             : 
     177           2 : void test::oustring::StringLiterals::checkBuffer()
     178             : {
     179           2 :     rtl::OUStringBuffer buf;
     180           2 :     buf.append( "foo" );
     181           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf.toString());
     182           2 :     buf.append( "bar" );
     183           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf.toString());
     184           2 :     buf.insert( 3, "baz" );
     185           2 :     CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf.toString());
     186           2 :     char d[] = "d";
     187           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUString( d ))));
     188           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.append( rtl::OUStringBuffer( d ))));
     189           2 :     CPPUNIT_ASSERT( !VALID_CONVERSION( buf.insert( 0, d )));
     190           2 : }
     191             : 
     192             : }} // namespace
     193             : 
     194           6 : CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals);
     195             : 
     196             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10