Add test for absence of events from wxSpinCtrlDouble ctor.
[wxWidgets.git] / tests / misc / misctests.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/misc/misctests.cpp
3 // Purpose: test miscellaneous stuff
4 // Author: Peter Most, Vadim Zeitlin
5 // Created: 2008-07-10
6 // Copyright: (c) 2008 Peter Most
7 // (c) 2009 Vadim Zeitlin
8 ///////////////////////////////////////////////////////////////////////////////
9
10 // ----------------------------------------------------------------------------
11 // headers
12 // ----------------------------------------------------------------------------
13
14 #include "testprec.h"
15
16 #ifdef __BORLANDC__
17 #pragma hdrstop
18 #endif
19
20 #include "wx/defs.h"
21
22 // just some classes using wxRTTI for wxStaticCast() test
23 #include "wx/tarstrm.h"
24 #include "wx/zipstrm.h"
25
26 // ----------------------------------------------------------------------------
27 // test class
28 // ----------------------------------------------------------------------------
29
30 class MiscTestCase : public CppUnit::TestCase
31 {
32 public:
33 MiscTestCase() { }
34
35 private:
36 CPPUNIT_TEST_SUITE( MiscTestCase );
37 CPPUNIT_TEST( Assert );
38 CPPUNIT_TEST( Delete );
39 CPPUNIT_TEST( StaticCast );
40 CPPUNIT_TEST_SUITE_END();
41
42 void Assert();
43 void Delete();
44 void StaticCast();
45
46 DECLARE_NO_COPY_CLASS(MiscTestCase)
47 };
48
49 // register in the unnamed registry so that these tests are run by default
50 CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase );
51
52 // also include in its own registry so that these tests can be run alone
53 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase, "MiscTestCase" );
54
55 namespace
56 {
57
58 bool AssertIfOdd(int n)
59 {
60 wxCHECK_MSG( !(n % 2), false, "parameter must be even" );
61
62 return true;
63 }
64
65 } // anonymous namespace
66
67 void MiscTestCase::Assert()
68 {
69 AssertIfOdd(0);
70 WX_ASSERT_FAILS_WITH_ASSERT(AssertIfOdd(1));
71
72 // doesn't fail any more
73 wxAssertHandler_t oldHandler = wxSetAssertHandler(NULL);
74 AssertIfOdd(17);
75 wxSetAssertHandler(oldHandler);
76 }
77
78 void MiscTestCase::Delete()
79 {
80 // Allocate some arbitrary memory to get a valid pointer:
81 long *pointer = new long;
82 CPPUNIT_ASSERT( pointer != NULL );
83
84 // Check that wxDELETE sets the pointer to NULL:
85 wxDELETE( pointer );
86 CPPUNIT_ASSERT( pointer == NULL );
87
88 // Allocate some arbitrary array to get a valid pointer:
89 long *array = new long[ 3 ];
90 CPPUNIT_ASSERT( array != NULL );
91
92 // Check that wxDELETEA sets the pointer to NULL:
93 wxDELETE( array );
94 CPPUNIT_ASSERT( array == NULL );
95
96 // this results in compilation error, as it should
97 #if 0
98 struct SomeUnknownStruct *p = NULL;
99 wxDELETE(p);
100 #endif
101 }
102
103 namespace
104 {
105
106 // helper function used just to avoid warnings about value computed not being
107 // used in WX_ASSERT_FAILS_WITH_ASSERT() in StaticCast() below
108 bool IsNull(void *p)
109 {
110 return p == NULL;
111 }
112
113 } // anonymous namespace
114
115 void MiscTestCase::StaticCast()
116 {
117 #if wxUSE_TARSTREAM
118 wxTarEntry tarEntry;
119 CPPUNIT_ASSERT( wxStaticCast(&tarEntry, wxArchiveEntry) );
120
121 wxArchiveEntry *entry = &tarEntry;
122 CPPUNIT_ASSERT( wxStaticCast(entry, wxTarEntry) );
123
124 #if wxUSE_ZIPSTREAM
125 wxZipEntry zipEntry;
126 entry = &zipEntry;
127 CPPUNIT_ASSERT( wxStaticCast(entry, wxZipEntry) );
128 WX_ASSERT_FAILS_WITH_ASSERT( IsNull(wxStaticCast(&zipEntry, wxTarEntry)) );
129 #endif // wxUSE_ZIPSTREAM
130
131 WX_ASSERT_FAILS_WITH_ASSERT( IsNull(wxStaticCast(entry, wxTarEntry)) );
132 #endif // wxUSE_TARSTREAM
133 }
134