1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/misctests.cpp
3 // Purpose: test miscellaneous stuff
4 // Author: Peter Most, Vadim Zeitlin
7 // Copyright: (c) 2008 Peter Most
8 // (c) 2009 Vadim Zeitlin
9 ///////////////////////////////////////////////////////////////////////////////
11 // ----------------------------------------------------------------------------
13 // ----------------------------------------------------------------------------
23 // just some classes using wxRTTI for wxStaticCast() test
24 #include "wx/tarstrm.h"
25 #include "wx/zipstrm.h"
27 // ----------------------------------------------------------------------------
29 // ----------------------------------------------------------------------------
31 class MiscTestCase
: public CppUnit::TestCase
37 CPPUNIT_TEST_SUITE( MiscTestCase
);
38 CPPUNIT_TEST( Assert
);
39 CPPUNIT_TEST( Delete
);
40 CPPUNIT_TEST( StaticCast
);
41 CPPUNIT_TEST_SUITE_END();
47 DECLARE_NO_COPY_CLASS(MiscTestCase
)
50 // register in the unnamed registry so that these tests are run by default
51 CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase
);
53 // also include in it's own registry so that these tests can be run alone
54 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase
, "MiscTestCase" );
59 bool AssertIfOdd(int n
)
61 wxASSERT_MSG( !(n
% 2), "parameter must be even" );
65 } // anonymous namespace
67 void MiscTestCase::Assert()
70 WX_ASSERT_FAILS_WITH_ASSERT(AssertIfOdd(1));
72 // doesn't fail any more
73 wxAssertHandler_t oldHandler
= wxSetAssertHandler(NULL
);
75 wxSetAssertHandler(oldHandler
);
78 void MiscTestCase::Delete()
80 // Allocate some arbitrary memory to get a valid pointer:
81 long *pointer
= new long;
82 CPPUNIT_ASSERT( pointer
!= NULL
);
84 // Check that wxDELETE sets the pointer to NULL:
86 CPPUNIT_ASSERT( pointer
== NULL
);
88 // Allocate some arbitrary array to get a valid pointer:
89 long *array
= new long[ 3 ];
90 CPPUNIT_ASSERT( array
!= NULL
);
92 // Check that wxDELETEA sets the pointer to NULL:
94 CPPUNIT_ASSERT( array
== NULL
);
96 // this results in compilation error, as it should
98 struct SomeUnknownStruct
*p
= NULL
;
103 void MiscTestCase::StaticCast()
106 CPPUNIT_ASSERT( wxStaticCast(&tarEntry
, wxArchiveEntry
) );
108 wxArchiveEntry
*entry
= &tarEntry
;
109 CPPUNIT_ASSERT( wxStaticCast(entry
, wxTarEntry
) );
113 CPPUNIT_ASSERT( wxStaticCast(entry
, wxZipEntry
) );
115 WX_ASSERT_FAILS_WITH_ASSERT( wxStaticCast(entry
, wxTarEntry
) );
116 WX_ASSERT_FAILS_WITH_ASSERT( wxStaticCast(&zipEntry
, wxTarEntry
) );