From 2bc3fe4dd1c6c46a94dd43fbe01e7951f850af77 Mon Sep 17 00:00:00 2001 From: Markus Gans Date: Fri, 2 Mar 2018 00:02:54 +0100 Subject: [PATCH] Unit tests update --- src/fstring.cpp | 10 ++--- src/test/fstring-test.cpp | 92 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 10 deletions(-) diff --git a/src/fstring.cpp b/src/fstring.cpp index 4deee9d4..626b502c 100644 --- a/src/fstring.cpp +++ b/src/fstring.cpp @@ -368,10 +368,9 @@ const FString& FString::operator += (const wchar_t s[]) //---------------------------------------------------------------------- const FString& FString::operator += (const std::string& s) { - const wchar_t* wc_string = c_to_wc_str(s.c_str()); - - if ( wc_string ) + if ( ! s.empty() ) { + const wchar_t* wc_string = c_to_wc_str(s.c_str()); _insert (length, uInt(s.length()), wc_string); delete[] wc_string; } @@ -382,10 +381,9 @@ const FString& FString::operator += (const std::string& s) //---------------------------------------------------------------------- const FString& FString::operator += (const char s[]) { - const wchar_t* wc_string = c_to_wc_str(s); - - if ( wc_string ) + if ( s ) { + const wchar_t* wc_string = c_to_wc_str(s); _insert (length, uInt(std::wcslen(wc_string)), wc_string); delete[] wc_string; } diff --git a/src/test/fstring-test.cpp b/src/test/fstring-test.cpp index c2321152..b3742252 100644 --- a/src/test/fstring-test.cpp +++ b/src/test/fstring-test.cpp @@ -59,6 +59,8 @@ class FStringTest : public CPPUNIT_NS::TestFixture void initLengthTest(); void copyConstructorTest(); void assignmentTest(); + void additionAssignmentTest(); + void additionTest(); void caseTest(); void equalTest(); void lessEqualTest(); @@ -78,6 +80,8 @@ class FStringTest : public CPPUNIT_NS::TestFixture CPPUNIT_TEST (initLengthTest); CPPUNIT_TEST (copyConstructorTest); CPPUNIT_TEST (assignmentTest); + CPPUNIT_TEST (additionAssignmentTest); + CPPUNIT_TEST (additionTest); CPPUNIT_TEST (caseTest); CPPUNIT_TEST (equalTest); CPPUNIT_TEST (notEqualTest); @@ -174,7 +178,7 @@ void FStringTest::initLengthTest() CPPUNIT_ASSERT ( s2.getLength() == 10 ); CPPUNIT_ASSERT ( ! s2.isNull() ); CPPUNIT_ASSERT ( s2.isEmpty() ); - + FString s4(0, '-'); CPPUNIT_ASSERT ( s4.getLength() == 0 ); CPPUNIT_ASSERT ( s4.isNull() ); @@ -184,7 +188,7 @@ void FStringTest::initLengthTest() CPPUNIT_ASSERT ( s5.getLength() == 0 ); CPPUNIT_ASSERT ( s5.isNull() ); CPPUNIT_ASSERT ( s5.isEmpty() ); - + FString s6(x1, '-'); CPPUNIT_ASSERT ( s6.getLength() == 10 ); CPPUNIT_ASSERT ( ! s6.isNull() ); @@ -271,7 +275,7 @@ void FStringTest::assignmentTest() s1 = s5; CPPUNIT_ASSERT ( s1 == L"abc" ); CPPUNIT_ASSERT ( s1.getLength() == 3 ); - + const char s6[] = "def"; s1 = s6; CPPUNIT_ASSERT ( s1 == L"def" ); @@ -281,13 +285,93 @@ void FStringTest::assignmentTest() s1 = s7; CPPUNIT_ASSERT ( s1 == L"#" ); CPPUNIT_ASSERT ( s1.getLength() == 1 ); - + const char s8 = '%'; s1 = s8; CPPUNIT_ASSERT ( s1 == L"%" ); CPPUNIT_ASSERT ( s1.getLength() == 1 ); } +//---------------------------------------------------------------------- +void FStringTest::additionAssignmentTest() +{ + FString s1; + s1 += FString("abc"); + CPPUNIT_ASSERT ( s1 == L"abc" ); + s1 += FString("def"); + CPPUNIT_ASSERT ( s1 == L"abcdef" ); + + s1.clear(); + CPPUNIT_ASSERT ( s1.isNull() ); + CPPUNIT_ASSERT ( s1.isEmpty() ); + s1 += std::wstring(L"abc"); + CPPUNIT_ASSERT ( s1 == L"abc" ); + s1 += std::wstring(L"def"); + CPPUNIT_ASSERT ( s1 == L"abcdef" ); + + s1.clear(); + s1 += const_cast(L"abc"); + CPPUNIT_ASSERT ( s1 == L"abc" ); + s1 += const_cast(L"def"); + CPPUNIT_ASSERT ( s1 == L"abcdef" ); + + s1.clear(); + s1 += std::string("abc"); + CPPUNIT_ASSERT ( s1 == L"abc" ); + s1 += std::string("def"); + CPPUNIT_ASSERT ( s1 == L"abcdef" ); + + s1.clear(); + s1 += const_cast("abc"); + CPPUNIT_ASSERT ( s1 == L"abc" ); + s1 += const_cast("def"); + CPPUNIT_ASSERT ( s1 == L"abcdef" ); + + s1.clear(); + s1 += wchar_t('a'); + CPPUNIT_ASSERT ( s1 == L"a" ); + s1 += wchar_t('b'); + CPPUNIT_ASSERT ( s1 == L"ab" ); + + s1.clear(); + s1 += char('a'); + CPPUNIT_ASSERT ( s1 == L"a" ); + s1 += char('b'); + CPPUNIT_ASSERT ( s1 == L"ab" ); +} + +//---------------------------------------------------------------------- +void FStringTest::additionTest() +{ + FString s1("abc"); + FString s2 = s1 + FString("def"); + CPPUNIT_ASSERT ( s2 == L"abcdef" ); + + s2.clear(); + s2 = s1 + std::wstring(L"def"); + CPPUNIT_ASSERT ( s2 == L"abcdef" ); + + s2.clear(); + s2 = s1 + const_cast(L"def"); + CPPUNIT_ASSERT ( s2 == L"abcdef" ); + + s2.clear(); + s2 = s1 + std::string("def"); + CPPUNIT_ASSERT ( s2 == L"abcdef" ); + + s2.clear(); + s2 = s1 + const_cast("def"); + CPPUNIT_ASSERT ( s2 == L"abcdef" ); + + s2.clear(); + s2 = s1 + wchar_t(L'd'); + CPPUNIT_ASSERT ( s2 == L"abcd" ); + + s2.clear(); + s2 = s1 + char('d'); + CPPUNIT_ASSERT ( s2 == L"abcd" ); +} + //---------------------------------------------------------------------- void FStringTest::caseTest() {