X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/cbab1556578ae39b1f00e0c4f6ca8016ac8124ac..26df5dd39a8c09397b19cd98ea51600af5a1dea4:/tests/strings/strings.cpp?ds=sidebyside diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index bc8a02817c..af18513045 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -53,6 +53,7 @@ private: CPPUNIT_TEST( ToULongLong ); #endif // wxLongLong_t CPPUNIT_TEST( ToDouble ); + CPPUNIT_TEST( FromDouble ); CPPUNIT_TEST( StringBuf ); CPPUNIT_TEST( UTF8Buf ); CPPUNIT_TEST( CStrDataTernaryOperator ); @@ -85,6 +86,7 @@ private: void ToULongLong(); #endif // wxLongLong_t void ToDouble(); + void FromDouble(); void StringBuf(); void UTF8Buf(); void CStrDataTernaryOperator(); @@ -102,7 +104,7 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( StringTestCase ); -// also include in it's own registry so that these tests can be run alone +// also include in its own registry so that these tests can be run alone CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( StringTestCase, "StringTestCase" ); StringTestCase::StringTestCase() @@ -453,6 +455,10 @@ void StringTestCase::Compare() CPPUNIT_ASSERT( s1 != neq2 ); CPPUNIT_ASSERT( s1 != neq3 ); CPPUNIT_ASSERT( s1 != neq4 ); + + CPPUNIT_ASSERT( wxString("\n").Cmp(" ") < 0 ); + CPPUNIT_ASSERT( wxString("'").Cmp("!") > 0 ); + CPPUNIT_ASSERT( wxString("!").Cmp("z") < 0 ); } void StringTestCase::CompareNoCase() @@ -500,6 +506,10 @@ void StringTestCase::CompareNoCase() CPPUNIT_CNCNEQ_ASSERT( s1, neq ); CPPUNIT_CNCNEQ_ASSERT( s1, neq2 ); CPPUNIT_CNCNEQ_ASSERT( s1, neq3 ); + + CPPUNIT_ASSERT( wxString("\n").CmpNoCase(" ") < 0 ); + CPPUNIT_ASSERT( wxString("'").CmpNoCase("!") > 0); + CPPUNIT_ASSERT( wxString("!").Cmp("Z") < 0 ); } void StringTestCase::Contains() @@ -590,7 +600,7 @@ void StringTestCase::ToLong() if ( ld.flags & (Number_LongLong | Number_Unsigned) ) continue; - + // NOTE: unless you're using some exotic locale, ToCLong and ToLong // should behave the same for our test data set: @@ -631,7 +641,7 @@ void StringTestCase::ToULong() CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCULong(&ul) ); if ( ld.IsOk() ) CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); - + CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToULong(&ul) ); if ( ld.IsOk() ) CPPUNIT_ASSERT_EQUAL( ld.ULValue(), ul ); @@ -711,17 +721,17 @@ void StringTestCase::ToDouble() // test ToDouble() now: - // NOTE: for the test to be reliable, we need to set the locale explicitely + // NOTE: for the test to be reliable, we need to set the locale explicitly // so that we know the decimal point character to use if (!wxLocale::IsAvailable(wxLANGUAGE_FRENCH)) return; // you should have french support installed to continue this test! - wxLocale *locale = new wxLocale; - + wxLocale locale; + // don't load default catalog, it may be unavailable: - CPPUNIT_ASSERT( locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING) ); - + CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) ); + static const struct ToDoubleData doubleData2[] = { { wxT("1"), 1, true }, @@ -745,8 +755,53 @@ void StringTestCase::ToDouble() if ( ld.ok ) CPPUNIT_ASSERT_EQUAL( ld.value, d ); } - - delete locale; +} + +void StringTestCase::FromDouble() +{ + static const struct FromDoubleTestData + { + double value; + int prec; + const char *str; + } testData[] = + { + { 1.23, -1, "1.23" }, + // NB: there are no standards about the minimum exponent width + // and newer MSVC versions use 3 digits as minimum exponent + // width while GNU libc uses 2 digits as minimum width... +#ifdef wxUSING_VC_CRT_IO + { -3e-10, -1, "-3e-010" }, +#else + { -3e-10, -1, "-3e-10" }, +#endif + { -0.45678, -1, "-0.45678" }, + { 1.2345678, 0, "1" }, + { 1.2345678, 1, "1.2" }, + { 1.2345678, 2, "1.23" }, + { 1.2345678, 3, "1.235" }, + }; + + for ( unsigned n = 0; n < WXSIZEOF(testData); n++ ) + { + const FromDoubleTestData& td = testData[n]; + CPPUNIT_ASSERT_EQUAL( td.str, wxString::FromCDouble(td.value, td.prec) ); + } + + if ( !wxLocale::IsAvailable(wxLANGUAGE_FRENCH) ) + return; + + wxLocale locale; + CPPUNIT_ASSERT( locale.Init(wxLANGUAGE_FRENCH, wxLOCALE_DONT_LOAD_DEFAULT) ); + + for ( unsigned m = 0; m < WXSIZEOF(testData); m++ ) + { + const FromDoubleTestData& td = testData[m]; + + wxString str(td.str); + str.Replace(".", ","); + CPPUNIT_ASSERT_EQUAL( str, wxString::FromDouble(td.value, td.prec) ); + } } void StringTestCase::StringBuf() @@ -918,23 +973,48 @@ void StringTestCase::IndexedAccess() void StringTestCase::BeforeAndAfter() { - const wxString s(L"letter=\xe9;\xe7a=l\xe0"); + // Construct a string with 2 equal signs in it by concatenating its three + // parts: before the first "=", in between the two "="s and after the last + // one. This allows to avoid duplicating the string contents (which has to + // be different for Unicode and ANSI builds) in the tests below. +#if wxUSE_UNICODE + #define FIRST_PART L"letter" + #define MIDDLE_PART L"\xe9;\xe7a" + #define LAST_PART L"l\xe0" +#else // !wxUSE_UNICODE + #define FIRST_PART "letter" + #define MIDDLE_PART "e;ca" + #define LAST_PART "la" +#endif // wxUSE_UNICODE/!wxUSE_UNICODE - CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); - CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';') ); + const wxString s(FIRST_PART wxT("=") MIDDLE_PART wxT("=") LAST_PART); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=') ); - CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); - CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';') ); + wxString r; - CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", s.AfterFirst('=') ); + CPPUNIT_ASSERT_EQUAL( FIRST_PART, s.BeforeFirst('=', &r) ); + CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, r ); + + CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!', &r) ); + CPPUNIT_ASSERT_EQUAL( "", r ); + + + CPPUNIT_ASSERT_EQUAL( FIRST_PART wxT("=") MIDDLE_PART, s.BeforeLast('=', &r) ); + CPPUNIT_ASSERT_EQUAL( LAST_PART, r ); + + CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!', &r) ); + CPPUNIT_ASSERT_EQUAL( s, r ); + + + CPPUNIT_ASSERT_EQUAL( MIDDLE_PART wxT("=") LAST_PART, s.AfterFirst('=') ); CPPUNIT_ASSERT_EQUAL( "", s.AfterFirst('!') ); - CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterFirst(';') ); - CPPUNIT_ASSERT_EQUAL( L"l\xe0", s.AfterLast('=') ); + + CPPUNIT_ASSERT_EQUAL( LAST_PART, s.AfterLast('=') ); CPPUNIT_ASSERT_EQUAL( s, s.AfterLast('!') ); - CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') ); + + #undef LAST_PART + #undef MIDDLE_PART + #undef FIRST_PART } void StringTestCase::ScopedBuffers()