throwing_post.cpp 5.4 KB

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