decl_post_all.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 with postconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_F_POST
  7. #include "decl.hpp"
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <sstream>
  10. #include <string>
  11. std::string ok_f() {
  12. std::ostringstream ok; ok
  13. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  14. << "f::pre" << std::endl
  15. #endif
  16. #ifndef BOOST_CONTRACT_NO_OLDS
  17. << "f::old" << std::endl
  18. #endif
  19. << "f::body" << std::endl
  20. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  21. << "f::post" << std::endl // This can fail.
  22. #endif
  23. ;
  24. return ok.str();
  25. }
  26. struct err {}; // Global decl so visible in MSVC10 lambdas.
  27. int main() {
  28. std::ostringstream ok;
  29. f_post = true;
  30. out.str("");
  31. f();
  32. ok.str(""); ok // Test nothing failed.
  33. << ok_f()
  34. ;
  35. BOOST_TEST(out.eq(ok.str()));
  36. boost::contract::set_postcondition_failure(
  37. [] (boost::contract::from) { throw err(); });
  38. f_post = false;
  39. out.str("");
  40. try {
  41. f();
  42. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  43. BOOST_TEST(false);
  44. } catch(err const&) {
  45. #endif
  46. ok.str(""); ok
  47. << ok_f() // Test f::post failed.
  48. ;
  49. BOOST_TEST(out.eq(ok.str()));
  50. } catch(...) { BOOST_TEST(false); }
  51. return boost::report_errors();
  52. }