decl_post_mid.cpp 4.8 KB

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