Unit tests update

This commit is contained in:
Markus Gans 2018-03-05 00:25:05 +01:00
parent f343945ff7
commit 2459126924
2 changed files with 189 additions and 22 deletions

View File

@ -161,7 +161,10 @@ FString::FString (const wchar_t s[])
, bufsize(0)
, c_string(0)
{
if ( s )
if ( ! s )
return;
if ( s[0] )
_assign (s);
}
@ -177,13 +180,9 @@ FString::FString (const std::string& s)
const wchar_t* wc_string;
wc_string = c_to_wc_str(s.c_str());
if ( wc_string )
{
_assign( wc_string );
delete[] wc_string;
}
}
//----------------------------------------------------------------------
FString::FString (const char s[])
@ -195,15 +194,14 @@ FString::FString (const char s[])
if ( ! s )
return;
if ( ! s[0] )
return;
const wchar_t* wc_string;
wc_string = c_to_wc_str(s);
if ( wc_string )
{
_assign (wc_string);
delete[] wc_string;
}
}
//----------------------------------------------------------------------
FString::FString (const wchar_t c)
@ -879,9 +877,12 @@ sInt16 FString::toShort() const
register long num;
num = toLong();
if ( num > SHRT_MAX || num < SHRT_MIN )
if ( num > SHRT_MAX )
throw std::overflow_error ("overflow");
if ( num < SHRT_MIN )
throw std::underflow_error ("underflow");
return sInt16(num);
}
@ -889,7 +890,7 @@ sInt16 FString::toShort() const
uInt16 FString::toUShort() const
{
register uLong num;
num = uLong(toLong());
num = uLong(toULong());
if ( num > USHRT_MAX )
throw std::overflow_error ("overflow");
@ -903,9 +904,12 @@ int FString::toInt() const
register long num;
num = toLong();
if ( num > INT_MAX || num < INT_MIN )
if ( num > INT_MAX )
throw std::overflow_error ("overflow");
if ( num < INT_MIN )
throw std::underflow_error ("underflow");
return int(num);
}
@ -913,7 +917,7 @@ int FString::toInt() const
uInt FString::toUInt() const
{
register uLong num;
num = uLong(toLong());
num = uLong(toULong());
if ( num > UINT_MAX )
throw std::overflow_error ("overflow");
@ -963,6 +967,9 @@ long FString::toLong() const
if ( num > tenth_limit
|| (num == tenth_limit && d > tenth_limit_digit) )
{
if ( neg )
throw std::underflow_error ("underflow");
else
throw std::overflow_error ("overflow");
}
@ -1000,7 +1007,11 @@ uLong FString::toULong() const
if ( ! *p )
throw std::invalid_argument ("empty value");
if ( *p == L'+' )
if ( *p == L'-' )
{
throw std::underflow_error ("underflow");
}
else if ( *p == L'+' )
{
p++;
}
@ -1031,9 +1042,12 @@ float FString::toFloat() const
register double num;
num = toDouble();
if ( num > double(FLT_MAX) || num < double(FLT_MIN) )
if ( num > double(FLT_MAX) )
throw std::overflow_error ("overflow");
if ( num < double(-FLT_MAX) )
throw std::underflow_error ("underflow");
return float(num);
}

View File

@ -49,6 +49,7 @@ class FStringTest : public CPPUNIT_NS::TestFixture
{
public:
FStringTest()
: s(0)
{ }
void setUp();
@ -72,7 +73,10 @@ class FStringTest : public CPPUNIT_NS::TestFixture
void streamExtractionTest();
void subscriptOperatorTest();
void functionCallOperatorTest();
void formatTest();
void convertToNumberTest();
void exceptionTest();
void trimTest();
private:
FString* s;
@ -97,7 +101,10 @@ class FStringTest : public CPPUNIT_NS::TestFixture
CPPUNIT_TEST (streamExtractionTest);
CPPUNIT_TEST (subscriptOperatorTest);
CPPUNIT_TEST (functionCallOperatorTest);
CPPUNIT_TEST (formatTest);
CPPUNIT_TEST (convertToNumberTest);
CPPUNIT_TEST (exceptionTest);
CPPUNIT_TEST (trimTest);
// End of test suite definition
CPPUNIT_TEST_SUITE_END();
@ -691,6 +698,10 @@ void FStringTest::streamInsertionTest()
out << sInt16(INT_LEAST16_MIN);
CPPUNIT_ASSERT ( out == L"-32768" );
out.clear();
out << uInt16(UINT_LEAST16_MAX);
CPPUNIT_ASSERT ( out == L"65535" );
out.clear();
out << int(1234567);
CPPUNIT_ASSERT ( out == L"1234567" );
@ -808,6 +819,72 @@ void FStringTest::functionCallOperatorTest()
CPPUNIT_ASSERT ( copy == "teststring" );
}
//----------------------------------------------------------------------
void FStringTest::formatTest()
{
FString str;
int num = 3;
char location[] = "zoo";
str.sprintf ("There are %d lions in the %s", num, location);
CPPUNIT_ASSERT ( str == "There are 3 lions in the zoo" );
str.sprintf (L"It costs only %d cent", 50);
CPPUNIT_ASSERT ( str == "It costs only 50 cent" );
std::setlocale (LC_NUMERIC, "C");
FString fnum1, fnum2;
#if defined(__LP64__) || defined(_LP64)
// 64-bit architecture
fnum1.setFormatedNumber(0xffffffffffffffff, '\'');
CPPUNIT_ASSERT ( fnum1 == "18'446'744'073'709'551'615" );
fnum2.setFormatedNumber(-9223372036854775807);
CPPUNIT_ASSERT ( fnum2 == "-9 223 372 036 854 775 807" );
#else
// 32-bit architecture
fnum1.setFormatedNumber(0xffffffff, '\'');
CPPUNIT_ASSERT ( fnum1 == "4'294'967'295" );
fnum2.setFormatedNumber(-2147483647);
CPPUNIT_ASSERT ( fnum2 == "-2 147 483 647" );
#endif
}
//----------------------------------------------------------------------
void FStringTest::convertToNumberTest()
{
FString str = "-127";
CPPUNIT_ASSERT ( str.toShort() == -127 );
str = "255";
CPPUNIT_ASSERT ( str.toUShort() == 255 );
str = "-32768";
CPPUNIT_ASSERT ( str.toInt() == -32768 );
str = "65535";
CPPUNIT_ASSERT ( str.toUInt() == 65535 );
str = "-2147483647";
CPPUNIT_ASSERT ( str.toLong() == -2147483647 );
str = "4294967295";
CPPUNIT_ASSERT ( str.toULong() == 4294967295 );
str = "3.14159";
CPPUNIT_ASSERT ( str.toFloat() == 3.14159f );
str = "-3.14159";
CPPUNIT_ASSERT ( str.toFloat() == -3.14159f );
str = "3.141592653589793238";
CPPUNIT_ASSERT ( str.toDouble() == 3.141592653589793238 );
str = "-3.141592653589793238";
CPPUNIT_ASSERT ( str.toDouble() == -3.141592653589793238 );
}
//----------------------------------------------------------------------
void FStringTest::exceptionTest()
{
@ -819,6 +896,82 @@ void FStringTest::exceptionTest()
CPPUNIT_ASSERT_THROW ( FString("abc")[-1]
, std::out_of_range );
CPPUNIT_ASSERT_THROW ( FString("99999").toShort()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-99999").toShort()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString("99999").toUShort()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-1").toUShort()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString("9999999999").toInt()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-9999999999").toInt()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString("9999999999").toUInt()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-1").toUInt()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString("9999999999999999999").toLong()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-9999999999999999999").toLong()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString("99999999999999999999").toULong()
, std::overflow_error );
CPPUNIT_ASSERT_THROW ( FString("-1").toULong()
, std::underflow_error );
CPPUNIT_ASSERT_THROW ( FString().toLong()
, std::invalid_argument );
CPPUNIT_ASSERT_THROW ( FString("").toLong()
, std::invalid_argument );
CPPUNIT_ASSERT_THROW ( FString("one").toLong()
, std::invalid_argument );
CPPUNIT_ASSERT_THROW ( FString().toULong()
, std::invalid_argument );
CPPUNIT_ASSERT_THROW ( FString("").toULong()
, std::invalid_argument );
CPPUNIT_ASSERT_THROW ( FString("one").toULong()
, std::invalid_argument );
}
//----------------------------------------------------------------------
void FStringTest::trimTest()
{
const FString& trim_str1 = L"\r\n\t A string \n\t";
CPPUNIT_ASSERT ( trim_str1.rtrim() == L"\r\n\t A string" );
CPPUNIT_ASSERT ( trim_str1.ltrim() == L"A string \n\t" );
CPPUNIT_ASSERT ( trim_str1.trim() == L"A string" );
const FString& trim_str2 = L"\n \n\n";
CPPUNIT_ASSERT ( trim_str2.rtrim().isEmpty() );
CPPUNIT_ASSERT ( trim_str2.rtrim().isNull() );
CPPUNIT_ASSERT ( trim_str2.rtrim().getLength() == 0 );
CPPUNIT_ASSERT ( trim_str2.ltrim().isEmpty() );
CPPUNIT_ASSERT ( trim_str2.ltrim().isNull() );
CPPUNIT_ASSERT ( trim_str2.ltrim().getLength() == 0 );
CPPUNIT_ASSERT ( trim_str2.trim().isEmpty() );
CPPUNIT_ASSERT ( trim_str2.trim().isNull() );
CPPUNIT_ASSERT ( trim_str2.trim().getLength() == 0 );
}
// Put the test suite in the registry