decl_exit_static_inv_ends.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 only derived and grandparent classes (ends) with exit static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #define 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. std::string ok_b(bool threw = false) {
  34. std::ostringstream ok; ok
  35. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  36. << "b::inv" << std::endl
  37. #endif
  38. #ifndef BOOST_CONTRACT_NO_OLDS
  39. << "b::dtor::old" << std::endl
  40. #endif
  41. << "b::dtor::body" << std::endl
  42. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  43. << (threw ? "b::inv\n" : "")
  44. #endif
  45. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  46. << (!threw ? "b::dtor::post\n" : "")
  47. #endif
  48. ;
  49. return ok.str();
  50. }
  51. enum checked { passed, failed, threw };
  52. std::string ok_c(checked check = passed) {
  53. std::ostringstream ok; ok
  54. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  55. << "c::static_inv" << std::endl
  56. << "c::inv" << std::endl
  57. #endif
  58. #ifndef BOOST_CONTRACT_NO_OLDS
  59. << "c::dtor::old" << std::endl
  60. #endif
  61. << "c::dtor::body" << std::endl
  62. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  63. << "c::static_inv" << std::endl // This can fail.
  64. << (check == threw ? "c::inv\n" : "")
  65. #endif
  66. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  67. << (check == passed ? "c::dtor::post\n" : "")
  68. #endif
  69. ;
  70. return ok.str();
  71. }
  72. struct err {}; // Global decl so visible in MSVC10 lambdas.
  73. int main() {
  74. std::ostringstream ok;
  75. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  76. #define BOOST_CONTRACT_TEST_entry_inv 0
  77. #else
  78. #define BOOST_CONTRACT_TEST_entry_inv 1
  79. #endif
  80. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  81. #define BOOST_CONTRACT_TEST_exit_inv 0
  82. #else
  83. #define BOOST_CONTRACT_TEST_exit_inv 1
  84. #endif
  85. a_exit_static_inv = true;
  86. b_exit_static_inv = true;
  87. c_exit_static_inv = true;
  88. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  89. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  90. {
  91. a aa;
  92. out.str("");
  93. }
  94. ok.str(""); ok // Test nothing failed.
  95. << ok_a()
  96. << ok_b()
  97. << ok_c()
  98. ;
  99. BOOST_TEST(out.eq(ok.str()));
  100. boost::contract::set_exit_invariant_failure([&ok] (boost::contract::from) {
  101. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws.
  102. throw err(); // For testing only (as dtors should not throw otherwise).
  103. });
  104. a_exit_static_inv = false;
  105. b_exit_static_inv = true;
  106. c_exit_static_inv = true;
  107. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  108. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  109. try {
  110. {
  111. a aa;
  112. ok.str(""); ok
  113. // Test a::static_inv failed...
  114. << ok_a(BOOST_CONTRACT_TEST_exit_inv)
  115. ;
  116. out.str("");
  117. }
  118. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  119. BOOST_TEST(false);
  120. } catch(err const&) {
  121. #endif
  122. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  123. << ok_b(BOOST_CONTRACT_TEST_exit_inv)
  124. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? threw : passed)
  125. ;
  126. BOOST_TEST(out.eq(ok.str()));
  127. } catch(...) { BOOST_TEST(false); }
  128. a_exit_static_inv = true;
  129. b_exit_static_inv = false;
  130. c_exit_static_inv = true;
  131. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  132. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  133. {
  134. a aa;
  135. out.str("");
  136. }
  137. ok.str(""); ok
  138. << ok_a()
  139. << ok_b() // Test no exit b::static_inv so no failure here.
  140. << ok_c()
  141. ;
  142. BOOST_TEST(out.eq(ok.str()));
  143. a_exit_static_inv = true;
  144. b_exit_static_inv = true;
  145. c_exit_static_inv = false;
  146. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  147. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  148. try {
  149. {
  150. a aa;
  151. ok.str(""); ok
  152. << ok_a()
  153. << ok_b()
  154. // Test c::static_inv failed...
  155. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  156. ;
  157. out.str("");
  158. }
  159. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  160. BOOST_TEST(false);
  161. } catch(err const&) {
  162. #endif
  163. // ...then exec other dtors and check inv on throw (as dtor threw).
  164. BOOST_TEST(out.eq(ok.str()));
  165. } catch(...) { BOOST_TEST(false); }
  166. boost::contract::set_exit_invariant_failure([] (boost::contract::from) {
  167. // Testing multiple failures so dtors must not throw multiple
  168. // exceptions, just ignore failure and continue test program...
  169. });
  170. a_exit_static_inv = false;
  171. b_exit_static_inv = false;
  172. c_exit_static_inv = false;
  173. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  174. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  175. {
  176. a aa;
  177. out.str("");
  178. }
  179. ok.str(""); ok
  180. // Test a::static_inv failed (as all did).
  181. << ok_a(BOOST_CONTRACT_TEST_exit_inv)
  182. // Test no exit b::static_inv so no failure here.
  183. << ok_b()
  184. // Test c::static_inv failed (as all did).
  185. << ok_c(BOOST_CONTRACT_TEST_exit_inv ? failed : passed)
  186. ;
  187. BOOST_TEST(out.eq(ok.str()));
  188. #undef BOOST_CONTRACT_TEST_entry_inv
  189. #undef BOOST_CONTRACT_TEST_exit_inv
  190. return boost::report_errors();
  191. }