throwing_body.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 body (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([] { out << "b::dtor::post" << std::endl; })
  40. .except([] { out << "b::dtor::except" << std::endl; })
  41. ;
  42. out << "b::dtor::body" << std::endl;
  43. throw b_err(); // Test body throw (from inheritance mid branch).
  44. }
  45. };
  46. struct a
  47. #define BASES public b
  48. : BASES
  49. {
  50. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  51. #undef BASES
  52. static void static_invariant() { out << "a::static_inv" << std::endl; }
  53. void invariant() const { out << "a::inv" << std::endl; }
  54. ~a() BOOST_NOEXCEPT_IF(false) {
  55. boost::contract::check c = boost::contract::destructor(this)
  56. .old([] { out << "a::dtor::old" << std::endl; })
  57. .postcondition([] { out << "a::dtor::post" << std::endl; })
  58. .except([] { out << "a::dtor::except" << std::endl; })
  59. ;
  60. out << "a::dtor::body" << std::endl;
  61. // Do not throw (from inheritance leaf).
  62. }
  63. };
  64. int main() {
  65. std::ostringstream ok;
  66. try {
  67. {
  68. a aa;
  69. out.str("");
  70. }
  71. BOOST_TEST(false);
  72. } catch(b_err const&) {
  73. ok.str(""); ok
  74. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  75. << "a::static_inv" << std::endl
  76. << "a::inv" << std::endl
  77. #endif
  78. #ifndef BOOST_CONTRACT_NO_OLDS
  79. << "a::dtor::old" << std::endl
  80. #endif
  81. << "a::dtor::body" << std::endl
  82. // Test a destructed (so only static_inv and post, but no inv).
  83. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  84. << "a::static_inv" << std::endl
  85. #endif
  86. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  87. << "a::dtor::post" << std::endl
  88. #endif
  89. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  90. << "b::static_inv" << std::endl
  91. << "b::inv" << std::endl
  92. #endif
  93. #ifndef BOOST_CONTRACT_NO_OLDS
  94. << "b::dtor::old" << std::endl
  95. #endif
  96. << "b::dtor::body" << std::endl // Test this threw.
  97. // Test b not destructed (so static_inv, inv, and except, no post).
  98. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  99. << "b::static_inv" << std::endl
  100. << "b::inv" << std::endl
  101. #endif
  102. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  103. << "b::dtor::except" << std::endl
  104. #endif
  105. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  106. << "c::static_inv" << std::endl
  107. << "c::inv" << std::endl
  108. #endif
  109. #ifndef BOOST_CONTRACT_NO_OLDS
  110. << "c::dtor::old" << std::endl
  111. #endif
  112. << "c::dtor::body" << std::endl
  113. // Test c not destructed (so static_inv, inv, and except, no post).
  114. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  115. << "c::static_inv" << std::endl
  116. << "c::inv" << std::endl
  117. #endif
  118. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  119. << "c::dtor::except" << std::endl
  120. #endif
  121. ;
  122. BOOST_TEST(out.eq(ok.str()));
  123. } catch(...) { BOOST_TEST(false); }
  124. return boost::report_errors();
  125. }