throwing_post.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 throw from destructor .post() (in middle branch of inheritance tree).
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/destructor.hpp>
  8. #include <boost/contract/base_types.hpp>
  9. #include <boost/contract/check.hpp>
  10. #include <boost/config.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. boost::contract::test::detail::oteststream out;
  14. struct c {
  15. static void static_invariant() { out << "c::static_inv" << std::endl; }
  16. void invariant() const { out << "c::inv" << std::endl; }
  17. ~c() BOOST_NOEXCEPT_IF(false) {
  18. boost::contract::check c = boost::contract::destructor(this)
  19. .old([] { out << "c::dtor::old" << std::endl; })
  20. .postcondition([] { out << "c::dtor::post" << std::endl; })
  21. .except([] { out << "c::dtor::except" << std::endl; })
  22. ;
  23. out << "c::dtor::body" << std::endl;
  24. // Do not throw (from inheritance root).
  25. }
  26. };
  27. struct b_err {}; // Global decl so visible in MSVC10 lambdas.
  28. struct b
  29. #define BASES public c
  30. : BASES
  31. {
  32. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  33. #undef BASES
  34. static void static_invariant() { out << "b::static_inv" << std::endl; }
  35. void invariant() const { out << "b::inv" << std::endl; }
  36. ~b() BOOST_NOEXCEPT_IF(false) {
  37. boost::contract::check c = boost::contract::destructor(this)
  38. .old([] { out << "b::dtor::old" << std::endl; })
  39. .postcondition([] {
  40. out << "b::dtor::post" << std::endl;
  41. throw b_err(); // Test this throws (from mid branch).
  42. })
  43. .except([] { out << "b::dtor::except" << std::endl; })
  44. ;
  45. out << "b::dtor::body" << std::endl;
  46. }
  47. };
  48. struct a
  49. #define BASES public b
  50. : BASES
  51. {
  52. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  53. #undef BASES
  54. static void static_invariant() { out << "a::static_inv" << std::endl; }
  55. void invariant() const { out << "a::inv" << std::endl; }
  56. ~a() BOOST_NOEXCEPT_IF(false) {
  57. boost::contract::check c = boost::contract::destructor(this)
  58. .old([] { out << "a::dtor::old" << std::endl; })
  59. .postcondition([] { out << "a::dtor::post" << std::endl; })
  60. .except([] { out << "a::dtor::except" << std::endl; })
  61. ;
  62. out << "a::dtor::body" << std::endl;
  63. // Do not throw (from inheritance leaf).
  64. }
  65. };
  66. int main() {
  67. std::ostringstream ok;
  68. boost::contract::set_postcondition_failure(
  69. [] (boost::contract::from) { throw; });
  70. try {
  71. {
  72. a aa;
  73. out.str("");
  74. }
  75. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  76. BOOST_TEST(false);
  77. } catch(b_err const&) {
  78. #endif
  79. ok.str(""); ok
  80. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  81. << "a::static_inv" << std::endl
  82. << "a::inv" << std::endl
  83. #endif
  84. #ifndef BOOST_CONTRACT_NO_OLDS
  85. << "a::dtor::old" << std::endl
  86. #endif
  87. << "a::dtor::body" << std::endl
  88. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  89. // Test a destructed (so only static_inv and post, but no inv).
  90. << "a::static_inv" << std::endl
  91. #endif
  92. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  93. << "a::dtor::post" << std::endl
  94. #endif
  95. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  96. << "b::static_inv" << std::endl
  97. << "b::inv" << std::endl
  98. #endif
  99. #ifndef BOOST_CONTRACT_NO_OLDS
  100. << "b::dtor::old" << std::endl
  101. #endif
  102. << "b::dtor::body" << std::endl
  103. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  104. << "b::static_inv" << std::endl
  105. #endif
  106. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  107. << "b::dtor::post" << std::endl // Test this threw.
  108. #endif
  109. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  110. << "c::static_inv" << std::endl
  111. << "c::inv" << std::endl
  112. #endif
  113. #ifndef BOOST_CONTRACT_NO_OLDS
  114. << "c::dtor::old" << std::endl
  115. #endif
  116. << "c::dtor::body" << std::endl
  117. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  118. << "c::static_inv" << std::endl
  119. #endif
  120. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  121. // Test c not destructed (so both inv and except).
  122. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  123. << "c::inv" << std::endl
  124. #endif
  125. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  126. << "c::dtor::except" << std::endl
  127. #endif
  128. #endif
  129. ;
  130. BOOST_TEST(out.eq(ok.str()));
  131. } catch(...) { BOOST_TEST(false); }
  132. return boost::report_errors();
  133. }