]>
git.saurik.com Git - wxWidgets.git/blob - 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 ///////////////////////////////////////////////////////////////////////////////
9 // ============================================================================
11 // ============================================================================
13 // ----------------------------------------------------------------------------
15 // ----------------------------------------------------------------------------
23 #include "wx/string.h"
24 #include "wx/scopeguard.h"
26 // ----------------------------------------------------------------------------
27 // helper stuff: something to do on scope exit
28 // ----------------------------------------------------------------------------
30 static int gs_count
= 0;
32 static void IncGlobal() { gs_count
++; }
33 static void Inc(int *n
) { (*n
)++; }
34 static void IncBy(int *n
, int m
) { (*n
) += m
; }
39 Counter(int n
) : m_count(n
) { }
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
; }
45 int GetCount() const { return m_count
; }
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 class ScopeGuardTestCase
: public CppUnit::TestCase
58 CPPUNIT_TEST_SUITE(ScopeGuardTestCase
);
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();
72 void BlockExitSetVar();
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
; }
82 // register in the unnamed registry so that these tests are run by default
83 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase
);
85 // also include in its own registry so that these tests can be run alone
86 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase
,
87 "ScopeGuardTestCase");
89 // ============================================================================
90 // ScopeGuardTestCase implementation
91 // ============================================================================
93 void ScopeGuardTestCase::Normal()
100 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
101 incN
= wxMakeGuard(Inc
, &n
),
102 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
104 wxUnusedVar(incGlobal
);
106 wxUnusedVar(incMby15
);
108 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
109 CPPUNIT_ASSERT_EQUAL( 1, n
);
110 CPPUNIT_ASSERT_EQUAL( 2, m
);
113 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
114 CPPUNIT_ASSERT_EQUAL( 2, n
);
115 CPPUNIT_ASSERT_EQUAL( 17, m
);
118 void ScopeGuardTestCase::Dismiss()
125 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
126 incN
= wxMakeGuard(Inc
, &n
),
127 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
133 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
134 CPPUNIT_ASSERT_EQUAL( 1, n
);
135 CPPUNIT_ASSERT_EQUAL( 2, m
);
138 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
139 CPPUNIT_ASSERT_EQUAL( 1, n
);
140 CPPUNIT_ASSERT_EQUAL( 2, m
);
143 void ScopeGuardTestCase::BlockExit()
151 wxON_BLOCK_EXIT0(IncGlobal
);
152 wxON_BLOCK_EXIT1(Inc
, &n
);
153 wxON_BLOCK_EXIT2(IncBy
, &m
, 15);
155 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
156 CPPUNIT_ASSERT_EQUAL( 1, n
);
157 CPPUNIT_ASSERT_EQUAL( 2, m
);
160 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
161 CPPUNIT_ASSERT_EQUAL( 2, n
);
162 CPPUNIT_ASSERT_EQUAL( 17, m
);
165 void ScopeGuardTestCase::BlockExitObj()
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);
176 CPPUNIT_ASSERT_EQUAL( 1, count0
.GetCount() );
177 CPPUNIT_ASSERT_EQUAL( 2, count1
.GetCount() );
178 CPPUNIT_ASSERT_EQUAL( 3, count2
.GetCount() );
181 CPPUNIT_ASSERT_EQUAL( 0, count0
.GetCount() );
182 CPPUNIT_ASSERT_EQUAL( 17, count1
.GetCount() );
183 CPPUNIT_ASSERT_EQUAL( 5, count2
.GetCount() );
186 void ScopeGuardTestCase::BlockExitThis()
191 wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero
);
193 CPPUNIT_ASSERT_EQUAL( 1, m_count
);
195 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
198 wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set
, 17);
200 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
202 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
205 wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum
, 2, 3);
206 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
208 CPPUNIT_ASSERT_EQUAL( 5, m_count
);
211 void ScopeGuardTestCase::BlockExitSetVar()
215 wxON_BLOCK_EXIT_SET(m_count
, 17);
217 CPPUNIT_ASSERT_EQUAL( 1, m_count
);
219 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
224 wxON_BLOCK_EXIT_SET(count
, 17);
226 CPPUNIT_ASSERT_EQUAL( 1, count
);
228 CPPUNIT_ASSERT_EQUAL( 17, count
);
233 wxON_BLOCK_EXIT_SET(s
, "bye");
235 CPPUNIT_ASSERT_EQUAL( "hi", s
);
237 CPPUNIT_ASSERT_EQUAL( "bye", s
);
239 ScopeGuardTestCase
*p
= this;
241 wxON_BLOCK_EXIT_NULL(p
);
245 CPPUNIT_ASSERT( !p
);