throwing_old.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 .old() (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([] {
  39. out << "b::dtor::old" << std::endl;
  40. throw b_err(); // Test this throws (from mid branch).
  41. })
  42. .postcondition([] { out << "b::dtor::post" << std::endl; })
  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_old_failure([] (boost::contract::from) { throw; });
  69. try {
  70. {
  71. a aa;
  72. out.str("");
  73. }
  74. #ifndef BOOST_CONTRACT_NO_OLDS
  75. BOOST_TEST(false);
  76. } catch(b_err const&) {
  77. #endif
  78. ok.str(""); ok
  79. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  80. << "a::static_inv" << std::endl
  81. << "a::inv" << std::endl
  82. #endif
  83. #ifndef BOOST_CONTRACT_NO_OLDS
  84. << "a::dtor::old" << std::endl
  85. #endif
  86. << "a::dtor::body" << std::endl
  87. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  88. // Test a destructed (so only static_inv and post, but no inv).
  89. << "a::static_inv" << std::endl
  90. #endif
  91. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  92. << "a::dtor::post" << std::endl
  93. #endif
  94. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  95. << "b::static_inv" << std::endl
  96. << "b::inv" << std::endl
  97. #endif
  98. #ifndef BOOST_CONTRACT_NO_OLDS
  99. << "b::dtor::old" << std::endl // Test this threw.
  100. #else
  101. << "b::dtor::body" << std::endl
  102. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  103. << "b::static_inv" << std::endl
  104. #endif
  105. #endif
  106. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  107. << "c::static_inv" << std::endl
  108. << "c::inv" << std::endl
  109. #endif
  110. #ifndef BOOST_CONTRACT_NO_OLDS
  111. << "c::dtor::old" << std::endl
  112. #endif
  113. << "c::dtor::body" << std::endl
  114. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  115. << "c::static_inv" << std::endl
  116. #endif
  117. #ifndef BOOST_CONTRACT_NO_OLDS
  118. // Test c not destructed (so both inv and except).
  119. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  120. << "c::inv" << std::endl
  121. #endif
  122. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  123. << "c::dtor::except" << std::endl
  124. #endif
  125. #endif
  126. ;
  127. BOOST_TEST(out.eq(ok.str()));
  128. } catch(...) { BOOST_TEST(false); }
  129. return boost::report_errors();
  130. }