throwing_post.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 public function (derived and bases) .post().
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/public_function.hpp>
  8. #include <boost/contract/assert.hpp>
  9. #include <boost/contract/base_types.hpp>
  10. #include <boost/contract/override.hpp>
  11. #include <boost/contract/check.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <sstream>
  14. boost::contract::test::detail::oteststream out;
  15. struct c_err {}; // Global decl so visible in MSVC10 lambdas.
  16. struct c {
  17. static void static_invariant() { out << "c::static_inv" << std::endl; }
  18. void invariant() const { out << "c::inv" << std::endl; }
  19. virtual void f(boost::contract::virtual_* v = 0) {
  20. boost::contract::check c = boost::contract::public_function(v, this)
  21. .precondition([] {
  22. out << "c::f::pre" << std::endl;
  23. BOOST_CONTRACT_ASSERT(false); // To check derived pre.
  24. })
  25. .old([] { out << "c::f::old" << std::endl; })
  26. .postcondition([] {
  27. out << "c::f::post" << std::endl;
  28. // Test derived will throw.
  29. })
  30. .except([] { out << "c::f::except" << std::endl; })
  31. ;
  32. out << "c::f::body" << std::endl;
  33. }
  34. };
  35. struct b_err {}; // Global decl so visible in MSVC10 lambdas.
  36. struct b
  37. #define BASES 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. virtual void f(boost::contract::virtual_* v = 0) /* override */ {
  45. boost::contract::check c = boost::contract::public_function<override_f>(
  46. v, &b::f, this)
  47. .precondition([] {
  48. out << "b::f::pre" << std::endl;
  49. BOOST_CONTRACT_ASSERT(false); // To check derived pre.
  50. })
  51. .old([] { out << "b::f::old" << std::endl; })
  52. .postcondition([] {
  53. out << "b::f::post" << std::endl;
  54. throw b_err(); // Test this (both derived and base) throws.
  55. })
  56. .except([] { out << "b::f::except" << std::endl; })
  57. ;
  58. out << "b::f::body" << std::endl;
  59. }
  60. BOOST_CONTRACT_OVERRIDE(f)
  61. };
  62. struct a_err {}; // Global decl so visible in MSVC10 lambdas.
  63. struct a
  64. #define BASES public b
  65. : BASES
  66. {
  67. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  68. #undef BASES
  69. static void static_invariant() { out << "a::static_inv" << std::endl; }
  70. void invariant() const { out << "a::inv" << std::endl; }
  71. void f(boost::contract::virtual_* v = 0) /* override */ {
  72. boost::contract::check c = boost::contract::public_function<override_f>(
  73. v, &a::f, this)
  74. .precondition([] { out << "a::f::pre" << std::endl; })
  75. .old([] { out << "a::f::old" << std::endl; })
  76. .postcondition([] {
  77. out << "a::f::post" << std::endl;
  78. throw a_err(); // Test base already threw.
  79. })
  80. .except([] { out << "a::f::except" << std::endl; })
  81. ;
  82. out << "a::f::body" << std::endl;
  83. }
  84. BOOST_CONTRACT_OVERRIDE(f)
  85. };
  86. int main() {
  87. std::ostringstream ok;
  88. boost::contract::set_postcondition_failure(
  89. [] (boost::contract::from) { throw; });
  90. a aa;
  91. b& ba = aa; // Test as virtual call via polymorphism.
  92. try {
  93. out.str("");
  94. ba.f();
  95. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  96. BOOST_TEST(false);
  97. } catch(b_err const&) {
  98. #endif
  99. ok.str(""); ok
  100. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  101. << "c::static_inv" << std::endl
  102. << "c::inv" << std::endl
  103. << "b::static_inv" << std::endl
  104. << "b::inv" << std::endl
  105. << "a::static_inv" << std::endl
  106. << "a::inv" << std::endl
  107. #endif
  108. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  109. << "c::f::pre" << std::endl
  110. << "b::f::pre" << std::endl
  111. << "a::f::pre" << std::endl
  112. #endif
  113. #ifndef BOOST_CONTRACT_NO_OLDS
  114. << "c::f::old" << std::endl
  115. << "b::f::old" << std::endl
  116. << "a::f::old" << std::endl
  117. #endif
  118. << "a::f::body" << std::endl
  119. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  120. << "c::static_inv" << std::endl
  121. << "c::inv" << std::endl
  122. << "b::static_inv" << std::endl
  123. << "b::inv" << std::endl
  124. << "a::static_inv" << std::endl
  125. << "a::inv" << std::endl
  126. #endif
  127. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  128. << "c::f::old" << std::endl
  129. << "c::f::post" << std::endl
  130. << "b::f::old" << std::endl
  131. << "b::f::post" << std::endl // Test this threw.
  132. #endif
  133. ;
  134. BOOST_TEST(out.eq(ok.str()));
  135. } catch(...) { BOOST_TEST(false); }
  136. return boost::report_errors();
  137. }