decl_exit_static_inv_all.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (C) 2008-2018 Lorenzo Caminiti
  2. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  3. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  4. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  5. // Test all derived and base classes with exit static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #undef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  9. #include "decl.hpp"
  10. #include <boost/preprocessor/control/iif.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. #include <string>
  14. std::string ok_a(bool failed = false) {
  15. std::ostringstream ok; ok
  16. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  17. << "a::static_inv" << std::endl
  18. << "a::inv" << std::endl
  19. #endif
  20. #ifndef BOOST_CONTRACT_NO_OLDS
  21. << "a::dtor::old" << std::endl
  22. #endif
  23. << "a::dtor::body" << std::endl
  24. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  25. << "a::static_inv" << std::endl // This can fail.
  26. #endif
  27. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  28. << (!failed ? "a::dtor::post\n" : "")
  29. #endif
  30. ;
  31. return ok.str();
  32. }
  33. enum checked { passed, failed, threw };
  34. std::string ok_b(checked check = passed) {
  35. std::ostringstream ok; ok
  36. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  37. << "b::static_inv" << std::endl
  38. << "b::inv" << std::endl
  39. #endif
  40. #ifndef BOOST_CONTRACT_NO_OLDS
  41. << "b::dtor::old" << std::endl
  42. #endif
  43. << "b::dtor::body" << std::endl
  44. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  45. << "b::static_inv" << std::endl // This can fail.
  46. << (check == threw ? "b::inv\n" : "")
  47. #endif
  48. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  49. << (check == passed ? "b::dtor::post\n" : "")
  50. #endif
  51. ;
  52. return ok.str();
  53. }
  54. std::string ok_c(checked check = passed) {
  55. std::ostringstream ok; ok
  56. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  57. << "c::static_inv" << std::endl
  58. << "c::inv" << std::endl
  59. #endif
  60. #ifndef BOOST_CONTRACT_NO_OLDS
  61. << "c::dtor::old" << std::endl
  62. #endif
  63. << "c::dtor::body" << std::endl
  64. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  65. << "c::static_inv" << std::endl // This can fail.
  66. << (check == threw ? "c::inv\n" : "")
  67. #endif
  68. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  69. << (check == passed ? "c::dtor::post\n" : "")
  70. #endif
  71. ;
  72. return ok.str();
  73. }
  74. struct err {}; // Global decl so visible in MSVC10 lambdas.
  75. int main() {
  76. std::ostringstream ok;
  77. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  78. #define BOOST_CONTRACT_TEST_entry_inv 0
  79. #else
  80. #define BOOST_CONTRACT_TEST_entry_inv 1
  81. #endif
  82. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  83. #define BOOST_CONTRACT_TEST_exit_inv 0
  84. #else
  85. #define BOOST_CONTRACT_TEST_exit_inv 1
  86. #endif
  87. a_exit_static_inv = true;
  88. b_exit_static_inv = true;
  89. c_exit_static_inv = true;
  90. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  91. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  92. {
  93. a aa;
  94. out.str("");
  95. }
  96. ok.str(""); ok // Test nothing failed.
  97. << ok_a()
  98. << ok_b()
  99. << ok_c()
  100. ;
  101. BOOST_TEST(out.eq(ok.str()));
  102. boost::contract::set_exit_invariant_failure([&ok] (boost::contract::from) {
  103. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws.
  104. throw err(); // For testing only (as dtors should not throw otherwise).
  105. });
  106. a_exit_static_inv = false;
  107. b_exit_static_inv = true;
  108. c_exit_static_inv = true;
  109. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  110. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  111. try {
  112. {
  113. a aa;
  114. ok.str(""); ok
  115. // Test a::static_inv failed...
  116. << ok_a(BOOST_CONTRACT_TEST_exit_inv)
  117. ;
  118. out.str("");
  119. }
  120. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  121. BOOST_TEST(false);
  122. } catch(err const&) {
  123. #endif
  124. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  125. << ok_b(BOOST_CONTRACT_TEST_exit_inv ? threw : passed)
  126. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? threw : passed)
  127. ;
  128. BOOST_TEST(out.eq(ok.str()));
  129. } catch(...) { BOOST_TEST(false); }
  130. a_exit_static_inv = true;
  131. b_exit_static_inv = false;
  132. c_exit_static_inv = true;
  133. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  134. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  135. try {
  136. {
  137. a aa;
  138. ok.str(""); ok
  139. << ok_a()
  140. // Test b::static_inv failed...
  141. << ok_b(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  142. ;
  143. out.str("");
  144. }
  145. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  146. BOOST_TEST(false);
  147. } catch(err const&) {
  148. #endif
  149. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  150. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? threw : passed)
  151. ;
  152. BOOST_TEST(out.eq(ok.str()));
  153. } catch(...) { BOOST_TEST(false); }
  154. a_exit_static_inv = true;
  155. b_exit_static_inv = true;
  156. c_exit_static_inv = false;
  157. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  158. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  159. try {
  160. {
  161. a aa;
  162. ok.str(""); ok
  163. << ok_a()
  164. << ok_b()
  165. // Test exit c::static_inv failed.
  166. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  167. ;
  168. out.str("");
  169. }
  170. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  171. BOOST_TEST(false);
  172. } catch(err const&) {
  173. #endif
  174. // ... then exec other dtors and check inv on throw (as dtor threw).
  175. BOOST_TEST(out.eq(ok.str()));
  176. } catch(...) { BOOST_TEST(false); }
  177. boost::contract::set_exit_invariant_failure([] (boost::contract::from) {
  178. // Testing multiple failures but dtors must not throw multiple except,
  179. // just ignore failure and continue test program (for testing only).
  180. });
  181. a_exit_static_inv = false;
  182. b_exit_static_inv = false;
  183. c_exit_static_inv = false;
  184. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  185. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  186. {
  187. a aa;
  188. out.str("");
  189. }
  190. ok.str(""); ok
  191. // Test exit a::static_inv failed (as all did).
  192. << ok_a(BOOST_CONTRACT_TEST_exit_inv)
  193. // Test exit b::static_inv failed (as all did).
  194. << ok_b(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  195. // Test exit c::static_inv failed (as all did).
  196. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  197. ;
  198. BOOST_TEST(out.eq(ok.str()));
  199. #undef BOOST_CONTRACT_TEST_entry_inv
  200. #undef BOOST_CONTRACT_TEST_exit_inv
  201. return boost::report_errors();
  202. }