From 4f7ee81a089e9701be439bd6e2bd94dfb2934eec Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 11 Sep 2005 11:47:11 +0000 Subject: [PATCH] added ToLong/ULong/Double() tests git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@35466 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- tests/strings/strings.cpp | 97 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index c62abad45d..6abc91a11b 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -53,6 +53,9 @@ private: CPPUNIT_TEST( CaseChanges ); CPPUNIT_TEST( Compare ); CPPUNIT_TEST( CompareNoCase ); + CPPUNIT_TEST( ToLong ); + CPPUNIT_TEST( ToULong ); + CPPUNIT_TEST( ToDouble ); CPPUNIT_TEST_SUITE_END(); void String(); @@ -75,6 +78,9 @@ private: void CaseChanges(); void Compare(); void CompareNoCase(); + void ToLong(); + void ToULong(); + void ToDouble(); #if wxUSE_WCHAR_T // test if converting s using the given encoding gives ws and vice versa @@ -635,3 +641,94 @@ void StringTestCase::CompareNoCase() CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); } +void StringTestCase::ToLong() +{ + long l; + static const struct ToLongData + { + const wxChar *str; + long value; + bool ok; + } longData[] = + { + { _T("1"), 1, true }, + { _T("0"), 0, true }, + { _T("a"), 0, false }, + { _T("12345"), 12345, true }, + { _T("-1"), -1, true }, + { _T("--1"), 0, false }, + }; + + size_t n; + for ( n = 0; n < WXSIZEOF(longData); n++ ) + { + const ToLongData& ld = longData[n]; + CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToLong(&l) ); + if ( ld.ok ) + CPPUNIT_ASSERT_EQUAL( ld.value, l ); + } +} + +void StringTestCase::ToULong() +{ + unsigned long ul; + static const struct ToULongData + { + const wxChar *str; + unsigned long value; + bool ok; + } ulongData[] = + { + { _T("1"), 1, true }, + { _T("0"), 0, true }, + { _T("a"), 0, false }, + { _T("12345"), 12345, true }, + // this is surprizing but consistent with strtoul() behaviour + { _T("-1"), ULONG_MAX, true }, + }; + + size_t n; + for ( n = 0; n < WXSIZEOF(ulongData); n++ ) + { + const ToULongData& uld = ulongData[n]; + CPPUNIT_ASSERT_EQUAL( uld.ok, wxString(uld.str).ToULong(&ul) ); + if ( uld.ok ) + CPPUNIT_ASSERT_EQUAL( uld.value, ul ); + } +} + +void StringTestCase::ToDouble() +{ + double d; + static const struct ToDoubleData + { + const wxChar *str; + double value; + bool ok; + } doubleData[] = + { + { _T("1"), 1, true }, + { _T("1.23"), 1.23, true }, + { _T(".1"), .1, true }, + { _T("1."), 1, true }, + { _T("1.."), 0, false }, + { _T("0"), 0, true }, + { _T("a"), 0, false }, + { _T("12345"), 12345, true }, + { _T("-1"), -1, true }, + { _T("--1"), 0, false }, + }; + + // we need to use decimal point, not comma or whatever is its value for the + // current locale + wxSetlocale(LC_ALL, _T("C")); + + size_t n; + for ( n = 0; n < WXSIZEOF(doubleData); n++ ) + { + const ToDoubleData& ld = doubleData[n]; + CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); + if ( ld.ok ) + CPPUNIT_ASSERT_EQUAL( ld.value, d ); + } +} -- 2.45.2