]> git.saurik.com Git - wxWidgets.git/blob - tests/scopeguard/scopeguardtest.cpp
added convenient wxON_BLOCK_EXIT_THISn() macros wrapping wxON_BLOCK_EXIT_OBJn(*this)
[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 // RCS-ID: $Id$
6 // Copyright: (c) 2005 Vadim Zeitlin
7 // Licence: wxWidgets licence
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
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_SUITE_END();
65
66 void Normal();
67 void Dismiss();
68 void BlockExit();
69 void BlockExitObj();
70 void BlockExitThis();
71
72 private:
73 void Zero() { m_count = 0; }
74 void Set(int n) { m_count = n; }
75 void Sum(int n, int m) { m_count = n + m; }
76
77 int m_count;
78 };
79
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase);
82
83 // also include in it's own registry so that these tests can be run alone
84 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase,
85 "ScopeGuardTestCase");
86
87 // ============================================================================
88 // ScopeGuardTestCase implementation
89 // ============================================================================
90
91 void ScopeGuardTestCase::Normal()
92 {
93 int n = 1,
94 m = 2;
95
96 {
97 gs_count = 1;
98 wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
99 incN = wxMakeGuard(Inc, &n),
100 incMby15 = wxMakeGuard(IncBy, &m, 15);
101
102 wxUnusedVar(incGlobal);
103 wxUnusedVar(incN);
104 wxUnusedVar(incMby15);
105
106 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
107 CPPUNIT_ASSERT_EQUAL( 1, n );
108 CPPUNIT_ASSERT_EQUAL( 2, m );
109 }
110
111 CPPUNIT_ASSERT_EQUAL( 2, gs_count );
112 CPPUNIT_ASSERT_EQUAL( 2, n );
113 CPPUNIT_ASSERT_EQUAL( 17, m );
114 }
115
116 void ScopeGuardTestCase::Dismiss()
117 {
118 int n = 1,
119 m = 2;
120
121 {
122 gs_count = 1;
123 wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
124 incN = wxMakeGuard(Inc, &n),
125 incMby15 = wxMakeGuard(IncBy, &m, 15);
126
127 incGlobal.Dismiss();
128 incN.Dismiss();
129 incMby15.Dismiss();
130
131 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
132 CPPUNIT_ASSERT_EQUAL( 1, n );
133 CPPUNIT_ASSERT_EQUAL( 2, m );
134 }
135
136 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
137 CPPUNIT_ASSERT_EQUAL( 1, n );
138 CPPUNIT_ASSERT_EQUAL( 2, m );
139 }
140
141 void ScopeGuardTestCase::BlockExit()
142 {
143 int n = 1,
144 m = 2;
145
146 {
147 gs_count = 1;
148
149 wxON_BLOCK_EXIT0(IncGlobal);
150 wxON_BLOCK_EXIT1(Inc, &n);
151 wxON_BLOCK_EXIT2(IncBy, &m, 15);
152
153 CPPUNIT_ASSERT_EQUAL( 1, gs_count );
154 CPPUNIT_ASSERT_EQUAL( 1, n );
155 CPPUNIT_ASSERT_EQUAL( 2, m );
156 }
157
158 CPPUNIT_ASSERT_EQUAL( 2, gs_count );
159 CPPUNIT_ASSERT_EQUAL( 2, n );
160 CPPUNIT_ASSERT_EQUAL( 17, m );
161 }
162
163 void ScopeGuardTestCase::BlockExitObj()
164 {
165 Counter count0(1),
166 count1(2),
167 count2(3);
168
169 {
170 wxON_BLOCK_EXIT_OBJ0(count0, Counter::Zero);
171 wxON_BLOCK_EXIT_OBJ1(count1, Counter::Set, 17);
172 wxON_BLOCK_EXIT_OBJ2(count2, Counter::Sum, 2, 3);
173
174 CPPUNIT_ASSERT_EQUAL( 1, count0.GetCount() );
175 CPPUNIT_ASSERT_EQUAL( 2, count1.GetCount() );
176 CPPUNIT_ASSERT_EQUAL( 3, count2.GetCount() );
177 }
178
179 CPPUNIT_ASSERT_EQUAL( 0, count0.GetCount() );
180 CPPUNIT_ASSERT_EQUAL( 17, count1.GetCount() );
181 CPPUNIT_ASSERT_EQUAL( 5, count2.GetCount() );
182 }
183
184 void ScopeGuardTestCase::BlockExitThis()
185 {
186 m_count = 1;
187
188 {
189 wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero);
190
191 CPPUNIT_ASSERT_EQUAL( 1, m_count );
192 }
193 CPPUNIT_ASSERT_EQUAL( 0, m_count );
194
195 {
196 wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set, 17);
197
198 CPPUNIT_ASSERT_EQUAL( 0, m_count );
199 }
200 CPPUNIT_ASSERT_EQUAL( 17, m_count );
201
202 {
203 wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum, 2, 3);
204 CPPUNIT_ASSERT_EQUAL( 17, m_count );
205 }
206 CPPUNIT_ASSERT_EQUAL( 5, m_count );
207 }
208