static_throwing_old.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 static member function .old().
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/public_function.hpp>
  8. #include <boost/contract/check.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. boost::contract::test::detail::oteststream out;
  12. struct a_err {}; // Global decl so visible in MSVC10 lambdas.
  13. struct a {
  14. static void static_invariant() { out << "a::static_inv" << std::endl; }
  15. void invariant() const { out << "a::inv" << std::endl; }
  16. static void f() {
  17. boost::contract::check c = boost::contract::public_function<a>()
  18. .precondition([] { out << "a::f::pre" << std::endl; })
  19. .old([] {
  20. out << "a::f::old" << std::endl;
  21. throw a_err(); // Test this throws.
  22. })
  23. .postcondition([] { out << "a::f::post" << std::endl; })
  24. .except([] { out << "a::f::except" << std::endl; })
  25. ;
  26. out << "a::f::body" << std::endl;
  27. }
  28. };
  29. int main() {
  30. std::ostringstream ok;
  31. boost::contract::set_old_failure([] (boost::contract::from) { throw; });
  32. try {
  33. out.str("");
  34. a::f();
  35. #ifndef BOOST_CONTRACT_NO_OLDS
  36. BOOST_TEST(false);
  37. } catch(a_err const&) {
  38. #endif
  39. ok.str(""); ok
  40. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  41. << "a::static_inv" << std::endl
  42. #endif
  43. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  44. << "a::f::pre" << std::endl
  45. #endif
  46. #ifndef BOOST_CONTRACT_NO_OLDS
  47. << "a::f::old" << std::endl // Test this threw.
  48. #else
  49. << "a::f::body" << std::endl
  50. // Test no post (but still static inv) because .old() threw.
  51. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  52. << "a::static_inv" << std::endl
  53. #endif
  54. #endif
  55. ;
  56. BOOST_TEST(out.eq(ok.str()));
  57. } catch(...) { BOOST_TEST(false); }
  58. return boost::report_errors();
  59. }