decl_post_all.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 postconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_A_POST
  7. #undef 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. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  47. << (!threw ? "b::dtor::post\n" : "") // This can fail.
  48. #endif
  49. ;
  50. return ok.str();
  51. }
  52. std::string ok_c(bool threw = false) {
  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
  64. << (threw ? "c::inv\n" : "")
  65. #endif
  66. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  67. << (!threw ? "c::dtor::post\n" : "") // This can fail.
  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. a_post = true;
  76. b_post = true;
  77. c_post = true;
  78. {
  79. a aa;
  80. out.str("");
  81. }
  82. ok.str(""); ok // Test nothing failed.
  83. << ok_a()
  84. << ok_b()
  85. << ok_c()
  86. ;
  87. BOOST_TEST(out.eq(ok.str()));
  88. boost::contract::set_postcondition_failure([&ok] (boost::contract::from) {
  89. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws...
  90. throw err(); // for testing (as dtors should never throw anyways).
  91. });
  92. #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
  93. #define BOOST_CONTRACT_TEST_post 0
  94. #else
  95. #define BOOST_CONTRACT_TEST_post 1
  96. #endif
  97. a_post = false;
  98. b_post = true;
  99. c_post = true;
  100. try {
  101. {
  102. a aa;
  103. ok.str(""); ok
  104. << ok_a() // Test a::dtor::post failed...
  105. ;
  106. out.str("");
  107. }
  108. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  109. BOOST_TEST(false);
  110. } catch(err const&) {
  111. #endif
  112. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  113. << ok_b(BOOST_CONTRACT_TEST_post)
  114. << ok_c(BOOST_CONTRACT_TEST_post)
  115. ;
  116. BOOST_TEST(out.eq(ok.str()));
  117. } catch(...) { BOOST_TEST(false); }
  118. a_post = true;
  119. b_post = false;
  120. c_post = true;
  121. try {
  122. {
  123. a aa;
  124. ok.str(""); ok
  125. << ok_a()
  126. << ok_b() // Test b::dtor::post failed...
  127. ;
  128. out.str("");
  129. }
  130. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  131. BOOST_TEST(false);
  132. } catch(err const&) {
  133. #endif
  134. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  135. << ok_c(BOOST_CONTRACT_TEST_post)
  136. ;
  137. BOOST_TEST(out.eq(ok.str()));
  138. } catch(...) { BOOST_TEST(false); }
  139. a_post = true;
  140. b_post = true;
  141. c_post = false;
  142. try {
  143. {
  144. a aa;
  145. ok.str(""); ok
  146. << ok_a()
  147. << ok_b()
  148. << ok_c() // Test c::dtor::post failed...
  149. ;
  150. out.str("");
  151. }
  152. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  153. BOOST_TEST(false);
  154. } catch(err const&) {
  155. #endif
  156. // ...then exec other dtors and check inv on throw (as dtor threw).
  157. BOOST_TEST(out.eq(ok.str()));
  158. } catch(...) { BOOST_TEST(false); }
  159. a_post = false;
  160. b_post = false;
  161. c_post = false;
  162. try {
  163. {
  164. a aa;
  165. ok.str(""); ok
  166. << ok_a() // Test a::dtor::post failed...
  167. ;
  168. out.str("");
  169. }
  170. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  171. BOOST_TEST(false);
  172. } catch(err const&) {
  173. #endif
  174. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  175. << ok_b(BOOST_CONTRACT_TEST_post)
  176. << ok_c(BOOST_CONTRACT_TEST_post)
  177. ;
  178. BOOST_TEST(out.eq(ok.str()));
  179. } catch(...) { BOOST_TEST(false); }
  180. #undef BOOST_CONTRACT_TEST_post
  181. return boost::report_errors();
  182. }