]>
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_SUITE_END();
71 // register in the unnamed registry so that these tests are run by default
72 CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase
);
74 // also include in it's own registry so that these tests can be run alone
75 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase
,
76 "ScopeGuardTestCase");
78 // ============================================================================
79 // ScopeGuardTestCase implementation
80 // ============================================================================
82 void ScopeGuardTestCase::Normal()
89 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
90 incN
= wxMakeGuard(Inc
, &n
),
91 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
93 wxUnusedVar(incGlobal
);
95 wxUnusedVar(incMby15
);
97 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
98 CPPUNIT_ASSERT_EQUAL( 1, n
);
99 CPPUNIT_ASSERT_EQUAL( 2, m
);
102 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
103 CPPUNIT_ASSERT_EQUAL( 2, n
);
104 CPPUNIT_ASSERT_EQUAL( 17, m
);
107 void ScopeGuardTestCase::Dismiss()
114 wxScopeGuard incGlobal
= wxMakeGuard(IncGlobal
),
115 incN
= wxMakeGuard(Inc
, &n
),
116 incMby15
= wxMakeGuard(IncBy
, &m
, 15);
122 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
123 CPPUNIT_ASSERT_EQUAL( 1, n
);
124 CPPUNIT_ASSERT_EQUAL( 2, m
);
127 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
128 CPPUNIT_ASSERT_EQUAL( 1, n
);
129 CPPUNIT_ASSERT_EQUAL( 2, m
);
132 void ScopeGuardTestCase::BlockExit()
140 wxON_BLOCK_EXIT0(IncGlobal
);
141 wxON_BLOCK_EXIT1(Inc
, &n
);
142 wxON_BLOCK_EXIT2(IncBy
, &m
, 15);
144 CPPUNIT_ASSERT_EQUAL( 1, gs_count
);
145 CPPUNIT_ASSERT_EQUAL( 1, n
);
146 CPPUNIT_ASSERT_EQUAL( 2, m
);
149 CPPUNIT_ASSERT_EQUAL( 2, gs_count
);
150 CPPUNIT_ASSERT_EQUAL( 2, n
);
151 CPPUNIT_ASSERT_EQUAL( 17, m
);
154 void ScopeGuardTestCase::BlockExitObj()
161 wxON_BLOCK_EXIT_OBJ0(count0
, Counter::Zero
);
162 wxON_BLOCK_EXIT_OBJ1(count1
, Counter::Set
, 17);
163 wxON_BLOCK_EXIT_OBJ2(count2
, Counter::Sum
, 2, 3);
165 CPPUNIT_ASSERT_EQUAL( 1, count0
.GetCount() );
166 CPPUNIT_ASSERT_EQUAL( 2, count1
.GetCount() );
167 CPPUNIT_ASSERT_EQUAL( 3, count2
.GetCount() );
170 CPPUNIT_ASSERT_EQUAL( 0, count0
.GetCount() );
171 CPPUNIT_ASSERT_EQUAL( 17, count1
.GetCount() );
172 CPPUNIT_ASSERT_EQUAL( 5, count2
.GetCount() );