Fix crash when auto-sizing a wxDataViewCtrl column.
[wxWidgets.git] / tests / scopeguard / scopeguardtest.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: tests/scopeguard/scopeguardtest.cpp
3 // Purpose: Test wxScopeGuard and related macros
4 // Author: Vadim Zeitlin
5 // Copyright: (c) 2005 Vadim Zeitlin
6 // Licence: wxWindows licence
7 ///////////////////////////////////////////////////////////////////////////////
8
9 // ============================================================================
10 // declarations
11 // ============================================================================
12
13 // ----------------------------------------------------------------------------
14 // headers
15 // ----------------------------------------------------------------------------
16
17 #include "testprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #include "wx/string.h"
24 #include "wx/scopeguard.h"
25
26 // ----------------------------------------------------------------------------
27 // helper stuff: something to do on scope exit
28 // ----------------------------------------------------------------------------
29
30 static int gs_count = 0;
31
32 static void IncGlobal() { gs_count++; }
33 static void Inc(int *n) { (*n)++; }
34 static void IncBy(int *n, int m) { (*n) += m; }
35
36 class Counter
37 {
38 public:
39 Counter(int n) : m_count(n) { }
40
41 void Zero() { m_count = 0; }
42 void Set(int n) { m_count = n; }
43 void Sum(int n, int m) { m_count = n + m; }
44
45 int GetCount() const { return m_count; }
46
47 private:
48 int m_count;
49 };
50
51 // ----------------------------------------------------------------------------
52 // test class
53 // ----------------------------------------------------------------------------
54
55 class ScopeGuardTestCase : public CppUnit::TestCase
56 {
57 public:
58 CPPUNIT_TEST_SUITE(ScopeGuardTestCase);
59 CPPUNIT_TEST(Normal);
60 CPPUNIT_TEST(Dismiss);
61 CPPUNIT_TEST(BlockExit);
62 CPPUNIT_TEST(BlockExitObj);
63 CPPUNIT_TEST(BlockExitThis);
64 CPPUNIT_TEST(BlockExitSetVar);
65 CPPUNIT_TEST_SUITE_END();
66
67 void Normal();
68 void Dismiss();
69 void BlockExit();
70 void BlockExitObj();
71 void BlockExitThis();
72 void BlockExitSetVar();
73
74 private:
75 void Zero() { m_count = 0; }
76 void Set(int n) { m_count = n; }
77 void Sum(int n, int m) { m_count = n + m; }
78
79 int m_count;
80 };
81
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase);
84
85 // also include in its own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase,
87 "ScopeGuardTestCase");
88
89 // ============================================================================
90 // ScopeGuardTestCase implementation
91 // ============================================================================
92
93 void ScopeGuardTestCase::Normal()
94 {
95 int n = 1,
96 m = 2;
97
98 {
99 gs_count = 1;
100 wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
101 incN = wxMakeGuard(Inc, &n),
102 incMby15 = wxMakeGuard(IncBy, &m, 15);
103
104 wxUnusedVar(incGlobal);
105 wxUnusedVar(incN);
106 wxUnusedVar(incMby15);
107
108 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
109 CPPUNIT_ASSERT_EQUAL( 1, n );
110 CPPUNIT_ASSERT_EQUAL( 2, m );
111 }
112
113 CPPUNIT_ASSERT_EQUAL( 2, gs_count );
114 CPPUNIT_ASSERT_EQUAL( 2, n );
115 CPPUNIT_ASSERT_EQUAL( 17, m );
116 }
117
118 void ScopeGuardTestCase::Dismiss()
119 {
120 int n = 1,
121 m = 2;
122
123 {
124 gs_count = 1;
125 wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
126 incN = wxMakeGuard(Inc, &n),
127 incMby15 = wxMakeGuard(IncBy, &m, 15);
128
129 incGlobal.Dismiss();
130 incN.Dismiss();
131 incMby15.Dismiss();
132
133 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
134 CPPUNIT_ASSERT_EQUAL( 1, n );
135 CPPUNIT_ASSERT_EQUAL( 2, m );
136 }
137
138 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
139 CPPUNIT_ASSERT_EQUAL( 1, n );
140 CPPUNIT_ASSERT_EQUAL( 2, m );
141 }
142
143 void ScopeGuardTestCase::BlockExit()
144 {
145 int n = 1,
146 m = 2;
147
148 {
149 gs_count = 1;
150
151 wxON_BLOCK_EXIT0(IncGlobal);
152 wxON_BLOCK_EXIT1(Inc, &n);
153 wxON_BLOCK_EXIT2(IncBy, &m, 15);
154
155 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
156 CPPUNIT_ASSERT_EQUAL( 1, n );
157 CPPUNIT_ASSERT_EQUAL( 2, m );
158 }
159
160 CPPUNIT_ASSERT_EQUAL( 2, gs_count );
161 CPPUNIT_ASSERT_EQUAL( 2, n );
162 CPPUNIT_ASSERT_EQUAL( 17, m );
163 }
164
165 void ScopeGuardTestCase::BlockExitObj()
166 {
167 Counter count0(1),
168 count1(2),
169 count2(3);
170
171 {
172 wxON_BLOCK_EXIT_OBJ0(count0, Counter::Zero);
173 wxON_BLOCK_EXIT_OBJ1(count1, Counter::Set, 17);
174 wxON_BLOCK_EXIT_OBJ2(count2, Counter::Sum, 2, 3);
175
176 CPPUNIT_ASSERT_EQUAL( 1, count0.GetCount() );
177 CPPUNIT_ASSERT_EQUAL( 2, count1.GetCount() );
178 CPPUNIT_ASSERT_EQUAL( 3, count2.GetCount() );
179 }
180
181 CPPUNIT_ASSERT_EQUAL( 0, count0.GetCount() );
182 CPPUNIT_ASSERT_EQUAL( 17, count1.GetCount() );
183 CPPUNIT_ASSERT_EQUAL( 5, count2.GetCount() );
184 }
185
186 void ScopeGuardTestCase::BlockExitThis()
187 {
188 m_count = 1;
189
190 {
191 wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero);
192
193 CPPUNIT_ASSERT_EQUAL( 1, m_count );
194 }
195 CPPUNIT_ASSERT_EQUAL( 0, m_count );
196
197 {
198 wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set, 17);
199
200 CPPUNIT_ASSERT_EQUAL( 0, m_count );
201 }
202 CPPUNIT_ASSERT_EQUAL( 17, m_count );
203
204 {
205 wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum, 2, 3);
206 CPPUNIT_ASSERT_EQUAL( 17, m_count );
207 }
208 CPPUNIT_ASSERT_EQUAL( 5, m_count );
209 }
210
211 void ScopeGuardTestCase::BlockExitSetVar()
212 {
213 m_count = 1;
214 {
215 wxON_BLOCK_EXIT_SET(m_count, 17);
216
217 CPPUNIT_ASSERT_EQUAL( 1, m_count );
218 }
219 CPPUNIT_ASSERT_EQUAL( 17, m_count );
220
221
222 int count = 1;
223 {
224 wxON_BLOCK_EXIT_SET(count, 17);
225
226 CPPUNIT_ASSERT_EQUAL( 1, count );
227 }
228 CPPUNIT_ASSERT_EQUAL( 17, count );
229
230
231 wxString s("hi");
232 {
233 wxON_BLOCK_EXIT_SET(s, "bye");
234
235 CPPUNIT_ASSERT_EQUAL( "hi", s );
236 }
237 CPPUNIT_ASSERT_EQUAL( "bye", s );
238
239 ScopeGuardTestCase *p = this;
240 {
241 wxON_BLOCK_EXIT_NULL(p);
242
243 CPPUNIT_ASSERT( p );
244 }
245 CPPUNIT_ASSERT( !p );
246 }