pre_post_except.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 specify pre, post, and except, no old (same if not free func).
  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. void f() {
  13. boost::contract::check c = boost::contract::function()
  14. .precondition([] { out << "f::pre" << std::endl; })
  15. .postcondition([] { out << "f::post" << std::endl; })
  16. .except([] { out << "f::except" << std::endl; })
  17. ;
  18. out << "f::body" << std::endl;
  19. }
  20. int main() {
  21. std::ostringstream ok;
  22. out.str("");
  23. f();
  24. ok.str("");
  25. ok
  26. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  27. << "f::pre" << std::endl
  28. #endif
  29. << "f::body" << std::endl
  30. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  31. << "f::post" << std::endl
  32. #endif
  33. ;
  34. BOOST_TEST(out.eq(ok.str()));
  35. return boost::report_errors();
  36. }