decl_entry_inv_ends.cpp 6.0 KB

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