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