throwing_pre.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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) .pre().
  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([] { out << "c::f::post" << std::endl; })
  27. .except([] { out << "c::f::except" << std::endl; })
  28. ;
  29. out << "c::f::body" << std::endl;
  30. }
  31. };
  32. struct b_err {}; // Global decl so visible in MSVC10 lambdas.
  33. struct b
  34. #define BASES public c
  35. : BASES
  36. {
  37. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  38. #undef BASES
  39. static void static_invariant() { out << "b::static_inv" << std::endl; }
  40. void invariant() const { out << "b::inv" << std::endl; }
  41. virtual void f(boost::contract::virtual_* v = 0) /* override */ {
  42. boost::contract::check c = boost::contract::public_function<override_f>(
  43. v, &b::f, this)
  44. .precondition([] {
  45. out << "b::f::pre" << std::endl;
  46. BOOST_CONTRACT_ASSERT(false); // To check derived pre.
  47. })
  48. .old([] { out << "b::f::old" << std::endl; })
  49. .postcondition([] { out << "b::f::post" << std::endl; })
  50. .except([] { out << "b::f::except" << std::endl; })
  51. ;
  52. out << "b::f::body" << std::endl;
  53. }
  54. BOOST_CONTRACT_OVERRIDE(f)
  55. };
  56. struct a_err {}; // Global decl so visible in MSVC10 lambdas.
  57. struct a
  58. #define BASES public b
  59. : BASES
  60. {
  61. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  62. #undef BASES
  63. static void static_invariant() { out << "a::static_inv" << std::endl; }
  64. void invariant() const { out << "a::inv" << std::endl; }
  65. void f(boost::contract::virtual_* v = 0) /* override */ {
  66. boost::contract::check c = boost::contract::public_function<override_f>(
  67. v, &a::f, this)
  68. .precondition([] {
  69. out << "a::f::pre" << std::endl;
  70. throw a_err(); // Test this throws.
  71. })
  72. .old([] { out << "a::f::old" << std::endl; })
  73. .postcondition([] { out << "a::f::post" << std::endl; })
  74. .except([] { out << "a::f::except" << std::endl; })
  75. ;
  76. out << "a::f::body" << std::endl;
  77. }
  78. BOOST_CONTRACT_OVERRIDE(f)
  79. };
  80. int main() {
  81. std::ostringstream ok;
  82. boost::contract::set_precondition_failure(
  83. [] (boost::contract::from) { throw; });
  84. a aa;
  85. b& ba = aa; // Test as virtual call via polymorphism.
  86. try {
  87. out.str("");
  88. ba.f();
  89. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  90. BOOST_TEST(false);
  91. } catch(a_err const&) {
  92. #endif
  93. ok.str(""); ok
  94. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  95. << "c::static_inv" << std::endl
  96. << "c::inv" << std::endl
  97. << "b::static_inv" << std::endl
  98. << "b::inv" << std::endl
  99. << "a::static_inv" << std::endl
  100. << "a::inv" << std::endl
  101. #endif
  102. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  103. << "c::f::pre" << std::endl
  104. << "b::f::pre" << std::endl
  105. << "a::f::pre" << std::endl // Test this threw.
  106. #else
  107. #ifndef BOOST_CONTRACT_NO_OLDS
  108. << "c::f::old" << std::endl
  109. << "b::f::old" << std::endl
  110. << "a::f::old" << std::endl
  111. #endif
  112. << "a::f::body" << std::endl
  113. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  114. << "c::static_inv" << std::endl
  115. << "c::inv" << std::endl
  116. << "b::static_inv" << std::endl
  117. << "b::inv" << std::endl
  118. << "a::static_inv" << std::endl
  119. << "a::inv" << std::endl
  120. #endif
  121. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  122. << "c::f::old" << std::endl
  123. << "c::f::post" << std::endl
  124. << "b::f::old" << std::endl
  125. << "b::f::post" << std::endl
  126. // No old call here because not a base object.
  127. << "a::f::post" << std::endl
  128. #endif
  129. #endif
  130. ;
  131. BOOST_TEST(out.eq(ok.str()));
  132. } catch(...) { BOOST_TEST(false); }
  133. return boost::report_errors();
  134. }