]>
Commit | Line | Data |
---|---|---|
604fba2d VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: tests/misc/misctests.cpp | |
3 | // Purpose: test miscellaneous stuff | |
657a8a35 | 4 | // Author: Peter Most, Vadim Zeitlin |
604fba2d VZ |
5 | // Created: 2008-07-10 |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2008 Peter Most | |
657a8a35 | 8 | // (c) 2009 Vadim Zeitlin |
604fba2d VZ |
9 | /////////////////////////////////////////////////////////////////////////////// |
10 | ||
11 | // ---------------------------------------------------------------------------- | |
12 | // headers | |
13 | // ---------------------------------------------------------------------------- | |
14 | ||
15 | #include "testprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
21 | #include "wx/defs.h" | |
22 | ||
cc14bd00 VZ |
23 | // just some classes using wxRTTI for wxStaticCast() test |
24 | #include "wx/tarstrm.h" | |
25 | #include "wx/zipstrm.h" | |
26 | ||
604fba2d VZ |
27 | // ---------------------------------------------------------------------------- |
28 | // test class | |
29 | // ---------------------------------------------------------------------------- | |
30 | ||
31 | class MiscTestCase : public CppUnit::TestCase | |
32 | { | |
33 | public: | |
34 | MiscTestCase() { } | |
35 | ||
36 | private: | |
37 | CPPUNIT_TEST_SUITE( MiscTestCase ); | |
657a8a35 | 38 | CPPUNIT_TEST( Assert ); |
604fba2d | 39 | CPPUNIT_TEST( Delete ); |
cc14bd00 | 40 | CPPUNIT_TEST( StaticCast ); |
604fba2d VZ |
41 | CPPUNIT_TEST_SUITE_END(); |
42 | ||
657a8a35 | 43 | void Assert(); |
604fba2d | 44 | void Delete(); |
cc14bd00 | 45 | void StaticCast(); |
604fba2d VZ |
46 | |
47 | DECLARE_NO_COPY_CLASS(MiscTestCase) | |
48 | }; | |
49 | ||
50 | // register in the unnamed registry so that these tests are run by default | |
51 | CPPUNIT_TEST_SUITE_REGISTRATION( MiscTestCase ); | |
52 | ||
53 | // also include in it's own registry so that these tests can be run alone | |
54 | CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( MiscTestCase, "MiscTestCase" ); | |
55 | ||
657a8a35 VZ |
56 | namespace |
57 | { | |
58 | ||
a17023d3 | 59 | bool AssertIfOdd(int n) |
657a8a35 VZ |
60 | { |
61 | wxASSERT_MSG( !(n % 2), "parameter must be even" ); | |
a17023d3 | 62 | return true; |
657a8a35 VZ |
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 | ||
604fba2d VZ |
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 | ||
cc14bd00 VZ |
103 | void MiscTestCase::StaticCast() |
104 | { | |
105 | wxTarEntry tarEntry; | |
106 | CPPUNIT_ASSERT( wxStaticCast(&tarEntry, wxArchiveEntry) ); | |
107 | ||
108 | wxArchiveEntry *entry = &tarEntry; | |
109 | CPPUNIT_ASSERT( wxStaticCast(entry, wxTarEntry) ); | |
110 | ||
111 | wxZipEntry zipEntry; | |
112 | entry = &zipEntry; | |
113 | CPPUNIT_ASSERT( wxStaticCast(entry, wxZipEntry) ); | |
114 | ||
115 | WX_ASSERT_FAILS_WITH_ASSERT( wxStaticCast(entry, wxTarEntry) ); | |
116 | WX_ASSERT_FAILS_WITH_ASSERT( wxStaticCast(&zipEntry, wxTarEntry) ); | |
117 | } | |
118 |