]>
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: wxWidgets licence
8 ///////////////////////////////////////////////////////////////////////////////
10 // ============================================================================
12 // ============================================================================
14 // ----------------------------------------------------------------------------
16 // ----------------------------------------------------------------------------
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_SUITE_END();
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
; }
80 // register in the unnamed registry so that these tests are run by default
81 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase
);
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");
87 // ============================================================================
88 // ScopeGuardTestCase implementation
89 // ============================================================================
91 void ScopeGuardTestCase::Normal()
98 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
99 incN
= wxMakeGuard(Inc
, &n
),
100 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
102 wxUnusedVar(incGlobal
);
104 wxUnusedVar(incMby15
);
106 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
107 CPPUNIT_ASSERT_EQUAL( 1, n
);
108 CPPUNIT_ASSERT_EQUAL( 2, m
);
111 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
112 CPPUNIT_ASSERT_EQUAL( 2, n
);
113 CPPUNIT_ASSERT_EQUAL( 17, m
);
116 void ScopeGuardTestCase::Dismiss()
123 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
124 incN
= wxMakeGuard(Inc
, &n
),
125 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
131 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
132 CPPUNIT_ASSERT_EQUAL( 1, n
);
133 CPPUNIT_ASSERT_EQUAL( 2, m
);
136 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
137 CPPUNIT_ASSERT_EQUAL( 1, n
);
138 CPPUNIT_ASSERT_EQUAL( 2, m
);
141 void ScopeGuardTestCase::BlockExit()
149 wxON_BLOCK_EXIT0(IncGlobal
);
150 wxON_BLOCK_EXIT1(Inc
, &n
);
151 wxON_BLOCK_EXIT2(IncBy
, &m
, 15);
153 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
154 CPPUNIT_ASSERT_EQUAL( 1, n
);
155 CPPUNIT_ASSERT_EQUAL( 2, m
);
158 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
159 CPPUNIT_ASSERT_EQUAL( 2, n
);
160 CPPUNIT_ASSERT_EQUAL( 17, m
);
163 void ScopeGuardTestCase::BlockExitObj()
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);
174 CPPUNIT_ASSERT_EQUAL( 1, count0
.GetCount() );
175 CPPUNIT_ASSERT_EQUAL( 2, count1
.GetCount() );
176 CPPUNIT_ASSERT_EQUAL( 3, count2
.GetCount() );
179 CPPUNIT_ASSERT_EQUAL( 0, count0
.GetCount() );
180 CPPUNIT_ASSERT_EQUAL( 17, count1
.GetCount() );
181 CPPUNIT_ASSERT_EQUAL( 5, count2
.GetCount() );
184 void ScopeGuardTestCase::BlockExitThis()
189 wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero
);
191 CPPUNIT_ASSERT_EQUAL( 1, m_count
);
193 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
196 wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set
, 17);
198 CPPUNIT_ASSERT_EQUAL( 0, m_count
);
200 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
203 wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum
, 2, 3);
204 CPPUNIT_ASSERT_EQUAL( 17, m_count
);
206 CPPUNIT_ASSERT_EQUAL( 5, m_count
);