decl_exit_static_inv_mid.cpp 6.1 KB

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