decl_post_ends.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 postconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_A_POST
  7. #define BOOST_CONTRACT_TEST_NO_B_POST
  8. #undef BOOST_CONTRACT_TEST_NO_C_POST
  9. #include "decl.hpp"
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. #include <string>
  13. std::string ok_a() {
  14. std::ostringstream ok; ok
  15. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  16. << "a::static_inv" << std::endl
  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_EXIT_INVARIANTS
  24. << "a::static_inv" << std::endl
  25. #endif
  26. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  27. << "a::dtor::post" << std::endl // This can fail.
  28. #endif
  29. ;
  30. return ok.str();
  31. }
  32. std::string ok_b(bool threw = false) {
  33. std::ostringstream ok; ok
  34. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  35. << "b::static_inv" << std::endl
  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. << "b::static_inv" << std::endl
  44. << (threw ? "b::inv\n" : "")
  45. #endif
  46. ;
  47. return ok.str();
  48. }
  49. std::string ok_c(bool threw = false) {
  50. std::ostringstream ok; ok
  51. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  52. << "c::static_inv" << std::endl
  53. << "c::inv" << std::endl
  54. #endif
  55. #ifndef BOOST_CONTRACT_NO_OLDS
  56. << "c::dtor::old" << std::endl
  57. #endif
  58. << "c::dtor::body" << std::endl
  59. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  60. << "c::static_inv" << std::endl
  61. << (threw ? "c::inv\n" : "")
  62. #endif
  63. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  64. << (!threw ? "c::dtor::post\n" : "") // This can fail.
  65. #endif
  66. ;
  67. return ok.str();
  68. }
  69. struct err {}; // Global decl so visible in MSVC10 lambdas.
  70. int main() {
  71. std::ostringstream ok;
  72. a_post = true;
  73. b_post = true;
  74. c_post = true;
  75. {
  76. a aa;
  77. out.str("");
  78. }
  79. ok.str(""); ok // Test nothing failed.
  80. << ok_a()
  81. << ok_b()
  82. << ok_c()
  83. ;
  84. BOOST_TEST(out.eq(ok.str()));
  85. boost::contract::set_postcondition_failure([&ok] (boost::contract::from) {
  86. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws...
  87. throw err(); // ... for testing (as dtors should never throw anyways).
  88. });
  89. #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
  90. #define BOOST_CONTRACT_TEST_post 0
  91. #else
  92. #define BOOST_CONTRACT_TEST_post 1
  93. #endif
  94. a_post = false;
  95. b_post = true;
  96. c_post = true;
  97. try {
  98. {
  99. a aa;
  100. ok.str(""); ok
  101. << ok_a() // Test a::dtor::post failed.
  102. ;
  103. out.str("");
  104. }
  105. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  106. BOOST_TEST(false);
  107. } catch(err const&) {
  108. #endif
  109. ok // ... then exec other dtors and check inv on throw (as dtor threw).
  110. << ok_b(BOOST_CONTRACT_TEST_post)
  111. << ok_c(BOOST_CONTRACT_TEST_post)
  112. ;
  113. BOOST_TEST(out.eq(ok.str()));
  114. } catch(...) { BOOST_TEST(false); }
  115. a_post = true;
  116. b_post = false;
  117. c_post = true;
  118. try {
  119. {
  120. a aa;
  121. out.str("");
  122. }
  123. ok.str(""); ok
  124. << ok_a()
  125. << ok_b() // Test no b::dtor::post so no failure here.
  126. << ok_c()
  127. ;
  128. BOOST_TEST(out.eq(ok.str()));
  129. } catch(...) { BOOST_TEST(false); }
  130. a_post = true;
  131. b_post = true;
  132. c_post = false;
  133. try {
  134. {
  135. a aa;
  136. ok.str(""); ok
  137. << ok_a()
  138. << ok_b()
  139. << ok_c() // Test c::dtor::post failed.
  140. ;
  141. out.str("");
  142. }
  143. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  144. BOOST_TEST(false);
  145. } catch(err const&) {
  146. #endif
  147. // ... then exec other dtors and check inv on throw (as dtor threw).
  148. BOOST_TEST(out.eq(ok.str()));
  149. } catch(...) { BOOST_TEST(false); }
  150. a_post = false;
  151. b_post = false;
  152. c_post = false;
  153. try {
  154. {
  155. a aa;
  156. ok.str(""); ok
  157. << ok_a() // Test a::dtor::post failed.
  158. ;
  159. out.str("");
  160. }
  161. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  162. BOOST_TEST(false);
  163. } catch(err const&) {
  164. #endif
  165. ok // ... then exec other dtors and check inv on throw (as dtor threw).
  166. << ok_b(BOOST_CONTRACT_TEST_post)
  167. << ok_c(BOOST_CONTRACT_TEST_post)
  168. ;
  169. BOOST_TEST(out.eq(ok.str()));
  170. } catch(...) { BOOST_TEST(false); }
  171. #undef BOOST_CONTRACT_TEST_post
  172. return boost::report_errors();
  173. }