diff options
author | Luboš Luňák <l.lunak@suse.cz> | 2012-12-05 21:55:29 +0100 |
---|---|---|
committer | Luboš Luňák <l.lunak@suse.cz> | 2012-12-06 13:35:19 +0100 |
commit | f0d75874c3106b9e25e36ff338c7935813ad0c11 (patch) | |
tree | 4e669cf12da7f09e54281ace7d8bfc18ba83510d | |
parent | 9b7ec76111ed966e93f6e0802aa6c8e021dcd4a8 (diff) |
unittest for fast string operator+ not allowing unwanted combinations
Change-Id: I5891efcec7db373407a661636108fd979839db52
-rw-r--r-- | sal/inc/rtl/stringconcat.hxx | 17 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_ostring_concat.cxx | 31 | ||||
-rw-r--r-- | sal/qa/rtl/strings/test_oustring_concat.cxx | 36 |
3 files changed, 84 insertions, 0 deletions
diff --git a/sal/inc/rtl/stringconcat.hxx b/sal/inc/rtl/stringconcat.hxx index 2b4fc31f60c6..a6e3467209e3 100644 --- a/sal/inc/rtl/stringconcat.hxx +++ b/sal/inc/rtl/stringconcat.hxx @@ -260,6 +260,23 @@ typename internal::Enable< OUStringConcat< T1, T2 >, ToStringHelper< T1 >::allow return OUStringConcat< T1, T2 >( left, right ); } +#ifdef RTL_STRING_UNITTEST_CONCAT +// Special overload to catch the remaining invalid combinations. The helper struct must +// be used to make this operator+ overload a worse choice than all the existing overloads above. +struct StringConcatInvalid + { + template< typename T > + StringConcatInvalid( const T& ) {} + }; +template< typename T > +inline +int operator+( const StringConcatInvalid&, const T& ) + { + rtl_string_unittest_invalid_concat = true; + return 0; // doesn't matter + } +#endif + } // namespace #endif diff --git a/sal/qa/rtl/strings/test_ostring_concat.cxx b/sal/qa/rtl/strings/test_ostring_concat.cxx index 4c0630c56351..a2b172c902a8 100644 --- a/sal/qa/rtl/strings/test_ostring_concat.cxx +++ b/sal/qa/rtl/strings/test_ostring_concat.cxx @@ -7,12 +7,18 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +// activate support for detecting errors instead of getting compile errors +#define RTL_STRING_UNITTEST_CONCAT +bool rtl_string_unittest_invalid_concat = false; + #include <sal/types.h> #include <cppunit/TestFixture.h> #include <cppunit/extensions/HelperMacros.h> #include <rtl/string.hxx> #include <rtl/strbuf.hxx> +#include <rtl/ustring.hxx> +#include <rtl/ustrbuf.hxx> #include <typeinfo> @@ -36,11 +42,13 @@ private: void checkConcat(); void checkEnsureCapacity(); void checkAppend(); + void checkInvalid(); CPPUNIT_TEST_SUITE(StringConcat); CPPUNIT_TEST(checkConcat); CPPUNIT_TEST(checkEnsureCapacity); CPPUNIT_TEST(checkAppend); +CPPUNIT_TEST(checkInvalid); CPPUNIT_TEST_SUITE_END(); }; @@ -75,6 +83,8 @@ void test::ostring::StringConcat::checkConcat() TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, const char* > )), typeid( OString( "foo" ) + d3 )); CPPUNIT_ASSERT_EQUAL( OString( "fooabc" ), OString( OString( "foo" ) + d4 )); TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OString, char* > )), typeid( OString( "foo" ) + d4 )); + CPPUNIT_ASSERT_EQUAL( OString( "foobar" ), OString( OStringBuffer( "foo" ) + OString( "bar" ))); + TYPES_ASSERT_EQUAL(( typeid( OStringConcat< OStringBuffer, OString > )), typeid( OStringBuffer( "foo" ) + OString( "bar" ))); } #undef typeid @@ -126,6 +136,27 @@ void test::ostring::StringConcat::checkAppend() CPPUNIT_ASSERT_EQUAL( OString( "foobarbaz" ), buf.makeStringAndClear()); } +#define INVALID_CONCAT( expression ) \ + ( \ + rtl_string_unittest_invalid_concat = false, \ + ( void ) OString( expression ), \ + rtl_string_unittest_invalid_concat ) + +void test::ostring::StringConcat::checkInvalid() +{ +#ifdef RTL_FAST_STRING + CPPUNIT_ASSERT( !INVALID_CONCAT( OString() + OString())); + CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUString( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringBuffer( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + OUStringLiteral( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OString( "a" ) + 1 )); + rtl_String* rs = NULL; + rtl_uString* rus = NULL; + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs )); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus )); +#endif +} + }} // namespace CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringConcat); diff --git a/sal/qa/rtl/strings/test_oustring_concat.cxx b/sal/qa/rtl/strings/test_oustring_concat.cxx index 8d98232a71a2..c5b549757ad7 100644 --- a/sal/qa/rtl/strings/test_oustring_concat.cxx +++ b/sal/qa/rtl/strings/test_oustring_concat.cxx @@ -7,12 +7,18 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +// activate support for detecting errors instead of getting compile errors +#define RTL_STRING_UNITTEST_CONCAT +extern bool rtl_string_unittest_invalid_concat; + #include <sal/types.h> #include <cppunit/TestFixture.h> #include <cppunit/extensions/HelperMacros.h> #include <rtl/ustring.hxx> #include <rtl/ustrbuf.hxx> +#include <rtl/string.hxx> +#include <rtl/strbuf.hxx> #include <typeinfo> @@ -36,11 +42,13 @@ private: void checkConcat(); void checkEnsureCapacity(); void checkAppend(); + void checkInvalid(); CPPUNIT_TEST_SUITE(StringConcat); CPPUNIT_TEST(checkConcat); CPPUNIT_TEST(checkEnsureCapacity); CPPUNIT_TEST(checkAppend); +CPPUNIT_TEST(checkInvalid); CPPUNIT_TEST_SUITE_END(); }; @@ -64,6 +72,8 @@ void test::oustring::StringConcat::checkConcat() const char d1[] = "xyz"; CPPUNIT_ASSERT_EQUAL( OUString( "fooxyz" ), OUString( OUString( "foo" ) + d1 )); TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUString, const char[ 4 ] > )), typeid( OUString( "foo" ) + d1 )); + CPPUNIT_ASSERT_EQUAL( OUString( "foobar" ), OUString( OUStringBuffer( "foo" ) + OUString( "bar" ))); + TYPES_ASSERT_EQUAL(( typeid( OUStringConcat< OUStringBuffer, OUString > )), typeid( OUStringBuffer( "foo" ) + OUString( "bar" ))); } void test::oustring::StringConcat::checkEnsureCapacity() @@ -118,6 +128,32 @@ void test::oustring::StringConcat::checkAppend() CPPUNIT_ASSERT_EQUAL( OUString( "foobarbaz" ), buf.makeStringAndClear()); } +#define INVALID_CONCAT( expression ) \ + ( \ + rtl_string_unittest_invalid_concat = false, \ + ( void ) OUString( expression ), \ + rtl_string_unittest_invalid_concat ) + +void test::oustring::StringConcat::checkInvalid() +{ +#ifdef RTL_FAST_STRING + CPPUNIT_ASSERT( !INVALID_CONCAT( OUString() + OUString())); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OString( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringBuffer( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (const char*) "b" )); + char d[] = "b"; + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + d )); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + (char*)d )); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + OStringLiteral( "b" ))); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "a" ) + 1 )); + rtl_String* rs = NULL; + rtl_uString* rus = NULL; + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rs )); + CPPUNIT_ASSERT( INVALID_CONCAT( OUString( "b" ) + rus )); +#endif + +} + }} // namespace CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringConcat); |