scopeguardtest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. ///////////////////////////////////////////////////////////////////////////////
  8. // ============================================================================
  9. // declarations
  10. // ============================================================================
  11. // ----------------------------------------------------------------------------
  12. // headers
  13. // ----------------------------------------------------------------------------
  14. #include "testprec.h"
  15. #ifdef __BORLANDC__
  16. #pragma hdrstop
  17. #endif
  18. #include "wx/string.h"
  19. #include "wx/scopeguard.h"
  20. // ----------------------------------------------------------------------------
  21. // helper stuff: something to do on scope exit
  22. // ----------------------------------------------------------------------------
  23. static int gs_count = 0;
  24. static void IncGlobal() { gs_count++; }
  25. static void Inc(int *n) { (*n)++; }
  26. static void IncBy(int *n, int m) { (*n) += m; }
  27. class Counter
  28. {
  29. public:
  30. Counter(int n) : m_count(n) { }
  31. void Zero() { m_count = 0; }
  32. void Set(int n) { m_count = n; }
  33. void Sum(int n, int m) { m_count = n + m; }
  34. int GetCount() const { return m_count; }
  35. private:
  36. int m_count;
  37. };
  38. // ----------------------------------------------------------------------------
  39. // test class
  40. // ----------------------------------------------------------------------------
  41. class ScopeGuardTestCase : public CppUnit::TestCase
  42. {
  43. public:
  44. CPPUNIT_TEST_SUITE(ScopeGuardTestCase);
  45. CPPUNIT_TEST(Normal);
  46. CPPUNIT_TEST(Dismiss);
  47. CPPUNIT_TEST(BlockExit);
  48. CPPUNIT_TEST(BlockExitObj);
  49. CPPUNIT_TEST(BlockExitThis);
  50. CPPUNIT_TEST(BlockExitSetVar);
  51. CPPUNIT_TEST_SUITE_END();
  52. void Normal();
  53. void Dismiss();
  54. void BlockExit();
  55. void BlockExitObj();
  56. void BlockExitThis();
  57. void BlockExitSetVar();
  58. private:
  59. void Zero() { m_count = 0; }
  60. void Set(int n) { m_count = n; }
  61. void Sum(int n, int m) { m_count = n + m; }
  62. int m_count;
  63. };
  64. // register in the unnamed registry so that these tests are run by default
  65. CPPUNIT_TEST_SUITE_REGISTRATION(ScopeGuardTestCase);
  66. // also include in its own registry so that these tests can be run alone
  67. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ScopeGuardTestCase,
  68. "ScopeGuardTestCase");
  69. // ============================================================================
  70. // ScopeGuardTestCase implementation
  71. // ============================================================================
  72. void ScopeGuardTestCase::Normal()
  73. {
  74. int n = 1,
  75. m = 2;
  76. {
  77. gs_count = 1;
  78. wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
  79. incN = wxMakeGuard(Inc, &n),
  80. incMby15 = wxMakeGuard(IncBy, &m, 15);
  81. wxUnusedVar(incGlobal);
  82. wxUnusedVar(incN);
  83. wxUnusedVar(incMby15);
  84. CPPUNIT_ASSERT_EQUAL( 1, gs_count );
  85. CPPUNIT_ASSERT_EQUAL( 1, n );
  86. CPPUNIT_ASSERT_EQUAL( 2, m );
  87. }
  88. CPPUNIT_ASSERT_EQUAL( 2, gs_count );
  89. CPPUNIT_ASSERT_EQUAL( 2, n );
  90. CPPUNIT_ASSERT_EQUAL( 17, m );
  91. }
  92. void ScopeGuardTestCase::Dismiss()
  93. {
  94. int n = 1,
  95. m = 2;
  96. {
  97. gs_count = 1;
  98. wxScopeGuard incGlobal = wxMakeGuard(IncGlobal),
  99. incN = wxMakeGuard(Inc, &n),
  100. incMby15 = wxMakeGuard(IncBy, &m, 15);
  101. incGlobal.Dismiss();
  102. incN.Dismiss();
  103. incMby15.Dismiss();
  104. CPPUNIT_ASSERT_EQUAL( 1, gs_count );
  105. CPPUNIT_ASSERT_EQUAL( 1, n );
  106. CPPUNIT_ASSERT_EQUAL( 2, m );
  107. }
  108. CPPUNIT_ASSERT_EQUAL( 1, gs_count );
  109. CPPUNIT_ASSERT_EQUAL( 1, n );
  110. CPPUNIT_ASSERT_EQUAL( 2, m );
  111. }
  112. void ScopeGuardTestCase::BlockExit()
  113. {
  114. int n = 1,
  115. m = 2;
  116. {
  117. gs_count = 1;
  118. wxON_BLOCK_EXIT0(IncGlobal);
  119. wxON_BLOCK_EXIT1(Inc, &n);
  120. wxON_BLOCK_EXIT2(IncBy, &m, 15);
  121. CPPUNIT_ASSERT_EQUAL( 1, gs_count );
  122. CPPUNIT_ASSERT_EQUAL( 1, n );
  123. CPPUNIT_ASSERT_EQUAL( 2, m );
  124. }
  125. CPPUNIT_ASSERT_EQUAL( 2, gs_count );
  126. CPPUNIT_ASSERT_EQUAL( 2, n );
  127. CPPUNIT_ASSERT_EQUAL( 17, m );
  128. }
  129. void ScopeGuardTestCase::BlockExitObj()
  130. {
  131. Counter count0(1),
  132. count1(2),
  133. count2(3);
  134. {
  135. wxON_BLOCK_EXIT_OBJ0(count0, Counter::Zero);
  136. wxON_BLOCK_EXIT_OBJ1(count1, Counter::Set, 17);
  137. wxON_BLOCK_EXIT_OBJ2(count2, Counter::Sum, 2, 3);
  138. CPPUNIT_ASSERT_EQUAL( 1, count0.GetCount() );
  139. CPPUNIT_ASSERT_EQUAL( 2, count1.GetCount() );
  140. CPPUNIT_ASSERT_EQUAL( 3, count2.GetCount() );
  141. }
  142. CPPUNIT_ASSERT_EQUAL( 0, count0.GetCount() );
  143. CPPUNIT_ASSERT_EQUAL( 17, count1.GetCount() );
  144. CPPUNIT_ASSERT_EQUAL( 5, count2.GetCount() );
  145. }
  146. void ScopeGuardTestCase::BlockExitThis()
  147. {
  148. m_count = 1;
  149. {
  150. wxON_BLOCK_EXIT_THIS0(ScopeGuardTestCase::Zero);
  151. CPPUNIT_ASSERT_EQUAL( 1, m_count );
  152. }
  153. CPPUNIT_ASSERT_EQUAL( 0, m_count );
  154. {
  155. wxON_BLOCK_EXIT_THIS1(ScopeGuardTestCase::Set, 17);
  156. CPPUNIT_ASSERT_EQUAL( 0, m_count );
  157. }
  158. CPPUNIT_ASSERT_EQUAL( 17, m_count );
  159. {
  160. wxON_BLOCK_EXIT_THIS2(ScopeGuardTestCase::Sum, 2, 3);
  161. CPPUNIT_ASSERT_EQUAL( 17, m_count );
  162. }
  163. CPPUNIT_ASSERT_EQUAL( 5, m_count );
  164. }
  165. void ScopeGuardTestCase::BlockExitSetVar()
  166. {
  167. m_count = 1;
  168. {
  169. wxON_BLOCK_EXIT_SET(m_count, 17);
  170. CPPUNIT_ASSERT_EQUAL( 1, m_count );
  171. }
  172. CPPUNIT_ASSERT_EQUAL( 17, m_count );
  173. int count = 1;
  174. {
  175. wxON_BLOCK_EXIT_SET(count, 17);
  176. CPPUNIT_ASSERT_EQUAL( 1, count );
  177. }
  178. CPPUNIT_ASSERT_EQUAL( 17, count );
  179. wxString s("hi");
  180. {
  181. wxON_BLOCK_EXIT_SET(s, "bye");
  182. CPPUNIT_ASSERT_EQUAL( "hi", s );
  183. }
  184. CPPUNIT_ASSERT_EQUAL( "bye", s );
  185. ScopeGuardTestCase *p = this;
  186. {
  187. wxON_BLOCK_EXIT_NULL(p);
  188. CPPUNIT_ASSERT( p );
  189. }
  190. CPPUNIT_ASSERT( !p );
  191. }