X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/39601a7f74796aae377ad1520595f083fdafd7b6..cd15bcaf50ead53ccf9d50965312f0dc754affb4:/tests/any/anytest.cpp diff --git a/tests/any/anytest.cpp b/tests/any/anytest.cpp index 74a9d9cd05..29dd5170a9 100644 --- a/tests/any/anytest.cpp +++ b/tests/any/anytest.cpp @@ -17,6 +17,8 @@ #include "wx/any.h" #include "wx/datetime.h" +#include "wx/object.h" +#include "wx/vector.h" #include @@ -31,18 +33,24 @@ public: private: CPPUNIT_TEST_SUITE( wxAnyTestCase ); + CPPUNIT_TEST( CheckType ); CPPUNIT_TEST( Equality ); CPPUNIT_TEST( As ); CPPUNIT_TEST( GetAs ); CPPUNIT_TEST( Null ); + CPPUNIT_TEST( wxVariantConversions ); CPPUNIT_TEST( CustomTemplateSpecialization ); + CPPUNIT_TEST( Misc ); CPPUNIT_TEST_SUITE_END(); + void CheckType(); void Equality(); void As(); void GetAs(); void Null(); + void wxVariantConversions(); void CustomTemplateSpecialization(); + void Misc(); wxDateTime m_testDateTime; @@ -65,6 +73,7 @@ private: wxAny m_anyWxObjectPtr1; wxAny m_anyVoidPtr1; wxAny m_anyDateTime1; + wxAny m_anyUniChar1; wxAny m_anySignedChar2; wxAny m_anySignedShort2; @@ -92,13 +101,14 @@ private: // register in the unnamed registry so that these tests are run by default CPPUNIT_TEST_SUITE_REGISTRATION( wxAnyTestCase ); -// 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( wxAnyTestCase, "wxAnyTestCase" ); // Let's use a number with first digit after decimal dot less than 5, // so that we don't have to worry about whether conversion from float // to int truncates or rounds. -const double TEST_FLOAT_CONST = 123.456; +const float TEST_FLOAT_CONST = 123.456f; +const double TEST_DOUBLE_CONST = 123.456; const double FEQ_DELTA = 0.001; @@ -127,8 +137,8 @@ wxAnyTestCase::wxAnyTestCase() m_anyCharString1("abc"), m_anyWcharString1(L"abc"), m_anyBool1(true), - m_anyFloatDouble1((float)TEST_FLOAT_CONST), - m_anyDoubleDouble1((double)TEST_FLOAT_CONST), + m_anyFloatDouble1(TEST_FLOAT_CONST), + m_anyDoubleDouble1(TEST_DOUBLE_CONST), m_anyWxObjectPtr1(dummyWxObjectPointer), m_anyVoidPtr1(dummyVoidPointer), m_anyDateTime1(wxDateTime::Now()) @@ -152,13 +162,31 @@ wxAnyTestCase::wxAnyTestCase() m_anyCharString2 = "abc"; m_anyWcharString2 = L"abc"; m_anyBool2 = true; - m_anyFloatDouble2 = (float)TEST_FLOAT_CONST; - m_anyDoubleDouble2 = (double)TEST_FLOAT_CONST; + m_anyFloatDouble2 = TEST_FLOAT_CONST; + m_anyDoubleDouble2 = TEST_DOUBLE_CONST; m_anyDateTime2 = m_testDateTime; + m_anyUniChar1 = wxUniChar('A'); m_anyWxObjectPtr2 = dummyWxObjectPointer; m_anyVoidPtr2 = dummyVoidPointer; } +void wxAnyTestCase::CheckType() +{ + wxAny nullAny; + CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(nullAny, wxString)); + + CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyCharString2, const char*)); + CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2, wxString)); + CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyCharString2, const wchar_t*)); + CPPUNIT_ASSERT(wxANY_CHECK_TYPE(m_anyWcharString2, const wchar_t*)); + CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2, wxString)); + CPPUNIT_ASSERT(!wxANY_CHECK_TYPE(m_anyWcharString2, const char*)); + + // HasSameType() + CPPUNIT_ASSERT( m_anyWcharString1.HasSameType(m_anyWcharString2) ); + CPPUNIT_ASSERT( !m_anyWcharString1.HasSameType(m_anyBool1) ); +} + void wxAnyTestCase::Equality() { // @@ -199,6 +227,12 @@ void wxAnyTestCase::Equality() CPPUNIT_ASSERT(wxANY_AS(m_anyWxObjectPtr2, wxObject*) == dummyWxObjectPointer); CPPUNIT_ASSERT(wxANY_AS(m_anyVoidPtr2, void*) == dummyVoidPointer); + + // Test sub-type system type compatibility + CPPUNIT_ASSERT(m_anySignedShort1.GetType()-> + IsSameType(m_anySignedLongLong1.GetType())); + CPPUNIT_ASSERT(m_anyUnsignedShort1.GetType()-> + IsSameType(m_anyUnsignedLongLong1.GetType())); } void wxAnyTestCase::As() @@ -232,15 +266,26 @@ void wxAnyTestCase::As() wxString k = wxANY_AS(m_anyStringString1, wxString); CPPUNIT_ASSERT(k == "abc"); wxString l = wxANY_AS(m_anyCharString1, wxString); + const char* cptr = wxANY_AS(m_anyCharString1, const char*); CPPUNIT_ASSERT(l == "abc"); + CPPUNIT_ASSERT(cptr); wxString m = wxANY_AS(m_anyWcharString1, wxString); + const wchar_t* wcptr = wxANY_AS(m_anyWcharString1, const wchar_t*); + CPPUNIT_ASSERT(wcptr); CPPUNIT_ASSERT(m == "abc"); bool n = wxANY_AS(m_anyBool1, bool); CPPUNIT_ASSERT(n); + + // Make sure the stored float that comes back is -identical-. + // So do not use delta comparison here. float o = wxANY_AS(m_anyFloatDouble1, float); - CPPUNIT_ASSERT_DOUBLES_EQUAL(o, TEST_FLOAT_CONST, FEQ_DELTA); + CPPUNIT_ASSERT_EQUAL(o, TEST_FLOAT_CONST); + double p = wxANY_AS(m_anyDoubleDouble1, double); - CPPUNIT_ASSERT_DOUBLES_EQUAL(p, TEST_FLOAT_CONST, FEQ_DELTA); + CPPUNIT_ASSERT_EQUAL(p, TEST_DOUBLE_CONST); + + wxUniChar chr = wxANY_AS(m_anyUniChar1, wxUniChar); + CPPUNIT_ASSERT(chr == 'A'); wxDateTime q = wxANY_AS(m_anyDateTime1, wxDateTime); CPPUNIT_ASSERT(q == m_testDateTime); wxObject* r = wxANY_AS(m_anyWxObjectPtr1, wxObject*); @@ -265,6 +310,7 @@ void wxAnyTestCase::GetAs() // Test dynamic conversion bool res; long l = 0; + short int si = 0; unsigned long ul = 0; wxString s; // Let's test against float instead of double, since the former @@ -274,9 +320,14 @@ void wxAnyTestCase::GetAs() bool b = false; // Conversions from signed long type + // The first check should be enough to make sure that the sub-type system + // has not failed. + res = m_anySignedLong1.GetAs(&si); + CPPUNIT_ASSERT(res); + CPPUNIT_ASSERT_EQUAL(si, 15); res = m_anySignedLong1.GetAs(&ul); CPPUNIT_ASSERT(res); - CPPUNIT_ASSERT_EQUAL(ul, static_cast(15)); + CPPUNIT_ASSERT_EQUAL(ul, 15UL); res = m_anySignedLong1.GetAs(&s); CPPUNIT_ASSERT(res); CPPUNIT_ASSERT(s == "15"); @@ -352,22 +403,50 @@ void wxAnyTestCase::GetAs() res = m_anyDoubleDouble1.GetAs(&s); CPPUNIT_ASSERT(res); double d2; - res = s.ToDouble(&d2); + res = s.ToCDouble(&d2); CPPUNIT_ASSERT(res); CPPUNIT_ASSERT_DOUBLES_EQUAL(d2, TEST_FLOAT_CONST, FEQ_DELTA); } + // -// Test user data type specialization of wxAnyValueTypeImpl +// Test user data type for wxAnyValueTypeImpl specialization +// any hand-built wxVariantData. Also for inplace allocation +// sanity checks. // +class MyClass; + +static wxVector gs_myClassInstances; + class MyClass { public: MyClass( int someValue = 32768 ) { + Init(); m_someValue = someValue; } + MyClass( const MyClass& other ) + { + Init(); + m_someValue = other.m_someValue; + } + virtual ~MyClass() + { + for ( size_t i=0; i class wxAnyValueTypeImpl : public wxAnyValueTypeImplBase @@ -429,5 +705,38 @@ void wxAnyTestCase::CustomTemplateSpecialization() CPPUNIT_ASSERT_EQUAL(str, myObject.ToString()); } +void wxAnyTestCase::Misc() +{ + // Do some (inplace) allocation sanity checks + { + + // Do it inside a scope so we can easily test instance count + // afterwards + MyClass myObject(15); + wxAny any = myObject; + + // There must be two instances - first in myObject, + // and second copied in any. + CPPUNIT_ASSERT_EQUAL(gs_myClassInstances.size(), 2); + + // Check that it is allocated in-place, as supposed + if ( sizeof(MyClass) <= WX_ANY_VALUE_BUFFER_SIZE ) + { + // Memory block of the instance second must be inside the any + size_t anyBegin = reinterpret_cast(&any); + size_t anyEnd = anyBegin + sizeof(wxAny); + size_t pos = reinterpret_cast(gs_myClassInstances[1]); + CPPUNIT_ASSERT( pos >= anyBegin ); + CPPUNIT_ASSERT( pos < anyEnd ); + } + + wxAny any2 = any; + CPPUNIT_ASSERT( wxANY_AS(any2, MyClass).GetValue() == 15 ); + } + + // Make sure allocations and deallocations match + CPPUNIT_ASSERT_EQUAL(gs_myClassInstances.size(), 0); +} + #endif // wxUSE_ANY