X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/fdfc5e18e7e0b8ebe1e29c819afe82381a713b21..e5d05b907c823c87f7d4f92e535c15a7ee0124f2:/tests/strings/strings.cpp?ds=sidebyside diff --git a/tests/strings/strings.cpp b/tests/strings/strings.cpp index 01c15182e7..87956929e2 100644 --- a/tests/strings/strings.cpp +++ b/tests/strings/strings.cpp @@ -61,6 +61,7 @@ private: CPPUNIT_TEST( ExplicitConversion ); CPPUNIT_TEST( IndexedAccess ); CPPUNIT_TEST( BeforeAndAfter ); + CPPUNIT_TEST( ScopedBuffers ); CPPUNIT_TEST_SUITE_END(); void String(); @@ -93,6 +94,7 @@ private: void ExplicitConversion(); void IndexedAccess(); void BeforeAndAfter(); + void ScopedBuffers(); DECLARE_NO_COPY_CLASS(StringTestCase) }; @@ -588,6 +590,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() ) @@ -605,6 +614,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 ); @@ -667,20 +683,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() @@ -871,3 +926,26 @@ void StringTestCase::BeforeAndAfter() CPPUNIT_ASSERT_EQUAL( L"\xe7a=l\xe0", s.AfterLast(';') ); } +void StringTestCase::ScopedBuffers() +{ + // wxString relies on efficient buffers, verify they work as they should + + const char *literal = "Hello World!"; + + // non-owned buffer points to the string passed to it + wxScopedCharBuffer sbuf = wxScopedCharBuffer::CreateNonOwned(literal); + CPPUNIT_ASSERT( sbuf.data() == literal ); + + // a copy of scoped non-owned buffer still points to the same string + wxScopedCharBuffer sbuf2(sbuf); + CPPUNIT_ASSERT( sbuf.data() == sbuf2.data() ); + + // but assigning it to wxCharBuffer makes a full copy + wxCharBuffer buf(sbuf); + CPPUNIT_ASSERT( buf.data() != literal ); + CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); + + wxCharBuffer buf2 = sbuf; + CPPUNIT_ASSERT( buf2.data() != literal ); + CPPUNIT_ASSERT_EQUAL( literal, buf.data() ); +}