]>
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
6 // Copyright: (c) 2005 Vadim Zeitlin
7 // Licence: wxWindows licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
24 #include "wx/string.h"
25 #include "wx/scopeguard.h"
27 // ----------------------------------------------------------------------------
28 // helper stuff: something to do on scope exit
29 // ----------------------------------------------------------------------------
31 static int gs_count
= 0;
33 static void IncGlobal() { gs_count
++; }
34 static void Inc(int *n
) { (*n
)++; }
35 static void IncBy(int *n
, int m
) { (*n
) += m
; }
40 Counter(int n
) : m_count(n
) { }
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
; }
46 int GetCount() const { return m_count
; }
52 // ----------------------------------------------------------------------------
54 // ----------------------------------------------------------------------------
56 class ScopeGuardTestCase
: public CppUnit::TestCase
59 CPPUNIT_TEST_SUITE(ScopeGuardTestCase
);
61 CPPUNIT_TEST(Dismiss
);
62 CPPUNIT_TEST(BlockExit
);
63 CPPUNIT_TEST(BlockExitObj
);
64 CPPUNIT_TEST(BlockExitThis
);
65 CPPUNIT_TEST(BlockExitSetVar
);
66 CPPUNIT_TEST_SUITE_END();
73 void BlockExitSetVar();
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
; }
83 // register in the unnamed registry so that these tests are run by default
84 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase
);
86 // also include in it's own registry so that these tests can be run alone
87 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase
,
88 "ScopeGuardTestCase");
90 // ============================================================================
91 // ScopeGuardTestCase implementation
92 // ============================================================================
94 void ScopeGuardTestCase::Normal()
101 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
102 incN
= wxMakeGuard(Inc
, &n
),
103 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
105 wxUnusedVar(incGlobal
);
107 wxUnusedVar(incMby15
);
109 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
110 CPPUNIT_ASSERT_EQUAL( 1, n
);
111 CPPUNIT_ASSERT_EQUAL( 2, m
);
114 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
115 CPPUNIT_ASSERT_EQUAL( 2, n
);
116 CPPUNIT_ASSERT_EQUAL( 17, m
);
119 void ScopeGuardTestCase::Dismiss()
126 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
127 incN
= wxMakeGuard(Inc
, &n
),
128 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
134 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
135 CPPUNIT_ASSERT_EQUAL( 1, n
);
136 CPPUNIT_ASSERT_EQUAL( 2, m
);
139 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
140 CPPUNIT_ASSERT_EQUAL( 1, n
);
141 CPPUNIT_ASSERT_EQUAL( 2, m
);
144 void ScopeGuardTestCase::BlockExit()
152 wxON_BLOCK_EXIT0(IncGlobal
);
153 wxON_BLOCK_EXIT1(Inc
, &n
);
154 wxON_BLOCK_EXIT2(IncBy
, &m
, 15);
156 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
157 CPPUNIT_ASSERT_EQUAL( 1, n
);
158 CPPUNIT_ASSERT_EQUAL( 2, m
);
161 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
162 CPPUNIT_ASSERT_EQUAL( 2, n
);
163 CPPUNIT_ASSERT_EQUAL( 17, m
);
166 void ScopeGuardTestCase::BlockExitObj()
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);
177 CPPUNIT_ASSERT_EQUAL( 1, count0
.GetCount() );
178 CPPUNIT_ASSERT_EQUAL( 2, count1
.GetCount() );
179 CPPUNIT_ASSERT_EQUAL( 3, count2
.GetCount() );
182 CPPUNIT_ASSERT_EQUAL( 0, count0
.GetCount() );
183 CPPUNIT_ASSERT_EQUAL( 17, count1
.GetCount() );
184 CPPUNIT_ASSERT_EQUAL( 5, count2
.GetCount() );
187 void ScopeGuardTestCase::BlockExitThis()
192 wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero
);
194 CPPUNIT_ASSERT_EQUAL( 1, m_count
);
196 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
199 wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set
, 17);
201 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
203 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
206 wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum
, 2, 3);
207 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
209 CPPUNIT_ASSERT_EQUAL( 5, m_count
);
212 void ScopeGuardTestCase::BlockExitSetVar()
216 wxON_BLOCK_EXIT_SET(m_count
, 17);
218 CPPUNIT_ASSERT_EQUAL( 1, m_count
);
220 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
225 wxON_BLOCK_EXIT_SET(count
, 17);
227 CPPUNIT_ASSERT_EQUAL( 1, count
);
229 CPPUNIT_ASSERT_EQUAL( 17, count
);
234 wxON_BLOCK_EXIT_SET(s
, "bye");
236 CPPUNIT_ASSERT_EQUAL( "hi", s
);
238 CPPUNIT_ASSERT_EQUAL( "bye", s
);
240 ScopeGuardTestCase
*p
= this;
242 wxON_BLOCK_EXIT_NULL(p
);
246 CPPUNIT_ASSERT( !p
);