Kaydet (Commit) f0d75874 authored tarafından Luboš Luňák's avatar Luboš Luňák

unittest for fast string operator+ not allowing unwanted combinations

Change-Id: I5891efcec7db373407a661636108fd979839db52
üst 9b7ec761
......@@ -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
......
......@@ -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);
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment