throwing_old.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 free function .old().
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/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 err {}; // Global decl so visible in MSVC10 lambdas.
  13. void f() {
  14. boost::contract::check c = boost::contract::function()
  15. .precondition([] { out << "f::pre" << std::endl; })
  16. .old([] {
  17. out << "f::old" << std::endl;
  18. throw err(); // Test this throws.
  19. })
  20. .postcondition([] { out << "f::post" << std::endl; })
  21. .except([] { out << "f::except" << std::endl; })
  22. ;
  23. out << "f::body" << std::endl;
  24. }
  25. int main() {
  26. std::ostringstream ok;
  27. boost::contract::set_old_failure([] (boost::contract::from) { throw; });
  28. try {
  29. out.str("");
  30. f();
  31. #ifndef BOOST_CONTRACT_NO_OLDS
  32. BOOST_TEST(false);
  33. } catch(err const&) {
  34. #endif
  35. ok.str(""); ok
  36. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  37. << "f::pre" << std::endl
  38. #endif
  39. #ifndef BOOST_CONTRACT_NO_OLDS
  40. << "f::old" << std::endl // Test this threw.
  41. #else
  42. << "f::body" << std::endl
  43. #endif
  44. ;
  45. BOOST_TEST(out.eq(ok.str()));
  46. } catch(...) { BOOST_TEST(false); }
  47. return boost::report_errors();
  48. }