X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/817d057840b17a106f7b66e64ec2a7526e42dd20..ecde8361cce9ed8a51b41b619b7975a5abd8252c:/tests/strings/strings.cpp diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index 0967c6249a..b29747338a 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -59,6 +59,8 @@ private: CPPUNIT_TEST( CStrDataOperators ); CPPUNIT_TEST( CStrDataImplicitConversion ); CPPUNIT_TEST( ExplicitConversion ); + CPPUNIT_TEST( IndexedAccess ); + CPPUNIT_TEST( BeforeAndAfter ); CPPUNIT_TEST_SUITE_END(); void String(); @@ -89,6 +91,8 @@ private: void CStrDataOperators(); void CStrDataImplicitConversion(); void ExplicitConversion(); + void IndexedAccess(); + void BeforeAndAfter(); DECLARE_NO_COPY_CLASS(StringTestCase) }; @@ -314,7 +318,7 @@ void StringTestCase::Replace() { \ wxString s(o,olen); \ s.replace( pos , len , replacement ); \ - CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ + CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ } TEST_NULLCHARREPLACE( _T("null\0char"), 9, 5, 1, _T("d"), @@ -324,7 +328,7 @@ void StringTestCase::Replace() { \ wxString s(o,olen); \ s.Replace( olds, news, all ); \ - CPPUNIT_ASSERT( s == wxString(r,rlen) ); \ + CPPUNIT_ASSERT_EQUAL( wxString(r,rlen), s ); \ } TEST_WXREPLACE( _T("null\0char"), 9, _T("c"), _T("de"), true, @@ -334,6 +338,10 @@ void StringTestCase::Replace() _T("null\0char"), 9 ); TEST_WXREPLACE( "life", 4, "f", "", false, "lie", 3 ); + TEST_WXREPLACE( "life", 4, "f", "", true, "lie", 3 ); + TEST_WXREPLACE( "life", 4, "fe", "ve", true, "live", 4 ); + TEST_WXREPLACE( "xx", 2, "x", "yy", true, "yyyy", 4 ); + TEST_WXREPLACE( "xxx", 3, "xx", "z", true, "zx", 2 ); #undef TEST_WXREPLACE #undef TEST_NULLCHARREPLACE @@ -527,7 +535,7 @@ enum Number_Unsigned = 2, // if not specified, works for signed conversion Number_Signed = 4, // if not specified, works for unsigned Number_LongLong = 8, // only for long long tests - Number_Long = 16, // only for long tests + Number_Long = 16 // only for long tests }; static const struct ToLongData @@ -580,6 +588,13 @@ 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: + + CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToCLong(&l) ); + if ( ld.IsOk() ) + CPPUNIT_ASSERT_EQUAL( ld.LValue(), l ); CPPUNIT_ASSERT_EQUAL( ld.IsOk(), wxString(ld.str).ToLong(&l) ); if ( ld.IsOk() ) @@ -597,6 +612,13 @@ void StringTestCase::ToULong() if ( ld.flags & (Number_LongLong | Number_Signed) ) continue; + // NOTE: unless you're using some exotic locale, ToCLong and ToLong + // should behave the same for our test data set: + + 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 ); @@ -659,20 +681,59 @@ void StringTestCase::ToDouble() { _T("12345"), 12345, true }, { _T("-1"), -1, true }, { _T("--1"), 0, false }, + { _T("-3E-5"), -3E-5, true }, + { _T("-3E-abcde5"), 0, false }, }; - // we need to use decimal point, not comma or whatever is its value for the - // current locale - wxSetlocale(LC_ALL, "C"); + // test ToCDouble() first: size_t n; for ( n = 0; n < WXSIZEOF(doubleData); n++ ) { const ToDoubleData& ld = doubleData[n]; + CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToCDouble(&d) ); + if ( ld.ok ) + CPPUNIT_ASSERT_EQUAL( ld.value, d ); + } + + + // test ToDouble() now: + // NOTE: for the test to be reliable, we need to set the locale explicitely + // 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; + + // don't load default catalog, it may be unavailable: + CPPUNIT_ASSERT( locale->Init(wxLANGUAGE_FRENCH, wxLOCALE_CONV_ENCODING) ); + + static const struct ToDoubleData doubleData2[] = + { + { _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 }, + { _T("-3E-5"), -3E-5, true }, + { _T("-3E-abcde5"), 0, false }, + }; + + for ( n = 0; n < WXSIZEOF(doubleData2); n++ ) + { + const ToDoubleData& ld = doubleData2[n]; CPPUNIT_ASSERT_EQUAL( ld.ok, wxString(ld.str).ToDouble(&d) ); if ( ld.ok ) CPPUNIT_ASSERT_EQUAL( ld.value, d ); } + + delete locale; } void StringTestCase::StringBuf() @@ -784,11 +845,14 @@ void StringTestCase::CStrDataOperators() CPPUNIT_ASSERT( s.c_str()[0] == 'h' ); CPPUNIT_ASSERT( s.c_str()[1] == 'e' ); - CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); + + // IMPORTANT: at least with the CRT coming with MSVC++ 2008 trying to access + // the final character results in an assert failure (with debug CRT) + //CPPUNIT_ASSERT( s.c_str()[5] == '\0' ); CPPUNIT_ASSERT( *s.c_str() == 'h' ); CPPUNIT_ASSERT( *(s.c_str() + 2) == 'l' ); - CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); + //CPPUNIT_ASSERT( *(s.c_str() + 5) == '\0' ); } bool CheckStrChar(const wxString& expected, char *s) @@ -826,3 +890,37 @@ void StringTestCase::ExplicitConversion() CPPUNIT_ASSERT( CheckStrConstWChar(s, s.wc_str()) ); CPPUNIT_ASSERT( CheckStrWChar(s, s.wchar_str()) ); } + +void StringTestCase::IndexedAccess() +{ + wxString s("bar"); + CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); + + // this tests for a possible bug in UTF-8 based wxString implementation: + // the 3rd character of the underlying byte string is going to change, but + // the 3rd character of wxString should remain the same + s[0] = L'\xe9'; + CPPUNIT_ASSERT_EQUAL( 'r', (char)s[2] ); +} + +void StringTestCase::BeforeAndAfter() +{ + const wxString s(L"letter=\xe9;\xe7a=l\xe0"); + + CPPUNIT_ASSERT_EQUAL( "letter", s.BeforeFirst('=') ); + CPPUNIT_ASSERT_EQUAL( s, s.BeforeFirst('!') ); + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeFirst(';') ); + + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9;\xe7a", s.BeforeLast('=') ); + CPPUNIT_ASSERT_EQUAL( "", s.BeforeLast('!') ); + CPPUNIT_ASSERT_EQUAL( L"letter=\xe9", s.BeforeLast(';') ); + + CPPUNIT_ASSERT_EQUAL( L"\xe9;\xe7a=l\xe0", 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( s, s.AfterLast('!') ); + CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') ); +} +