throwing_old.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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) .old().
  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([] {
  26. out << "c::f::old" << std::endl;
  27. throw c_err(); // Test this throws.
  28. })
  29. .postcondition([] { out << "c::f::post" << std::endl; })
  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([] {
  52. out << "b::f::old" << std::endl;
  53. throw b_err(); // Test this throws.
  54. })
  55. .postcondition([] { out << "b::f::post" << std::endl; })
  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([] {
  76. out << "a::f::old" << std::endl;
  77. throw a_err(); // Test this throws.
  78. })
  79. .postcondition([] { out << "a::f::post" << std::endl; })
  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_old_failure([] (boost::contract::from) { throw; });
  89. a aa;
  90. b& ba = aa; // Test as virtual call via polymorphism.
  91. try {
  92. out.str("");
  93. ba.f();
  94. #ifndef BOOST_CONTRACT_NO_OLDS
  95. BOOST_TEST(false);
  96. } catch(c_err const&) {
  97. #endif
  98. ok.str(""); ok
  99. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  100. << "c::static_inv" << std::endl
  101. << "c::inv" << std::endl
  102. << "b::static_inv" << std::endl
  103. << "b::inv" << std::endl
  104. << "a::static_inv" << std::endl
  105. << "a::inv" << std::endl
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  108. << "c::f::pre" << std::endl
  109. << "b::f::pre" << std::endl
  110. << "a::f::pre" << std::endl
  111. #endif
  112. #ifndef BOOST_CONTRACT_NO_OLDS
  113. << "c::f::old" << std::endl // Test this threw.
  114. #else
  115. << "a::f::body" << std::endl
  116. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  117. << "c::static_inv" << std::endl
  118. << "c::inv" << std::endl
  119. << "b::static_inv" << std::endl
  120. << "b::inv" << std::endl
  121. << "a::static_inv" << std::endl
  122. << "a::inv" << std::endl
  123. #endif
  124. #endif
  125. ;
  126. BOOST_TEST(out.eq(ok.str()));
  127. } catch(...) { BOOST_TEST(false); }
  128. return boost::report_errors();
  129. }