lib_xy.hpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 contracts in .cpp compiled to never check post/except (but not in .hpp).
  6. #include "lib_x.hpp"
  7. #include "lib_y.hpp"
  8. #include "../detail/oteststream.hpp"
  9. #include <boost/contract/function.hpp>
  10. #include <boost/contract/check.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. void f() {
  14. using boost::contract::test::detail::out;
  15. boost::contract::check c = boost::contract::function()
  16. // Capturing [&] so out() visible in MSVC10 lambdas.
  17. .precondition([&] { out("f::pre\n"); })
  18. .old([&] { out("f::old\n"); })
  19. .postcondition([&] { out("f::post\n"); })
  20. ;
  21. out("f::body\n");
  22. }
  23. int main() {
  24. using boost::contract::test::detail::out;
  25. std::ostringstream ok;
  26. out("");
  27. f();
  28. ok.str(""); ok // Test normal (no lib) case.
  29. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  30. << "f::pre" << std::endl
  31. #endif
  32. #ifndef BOOST_CONTRACT_NO_OLDS
  33. << "f::old" << std::endl
  34. #endif
  35. << "f::body" << std::endl
  36. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  37. << "f::post" << std::endl
  38. #endif
  39. ;
  40. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
  41. out("");
  42. x();
  43. ok.str(""); ok // Test contracts in .cpp so no post (NO_POST in build file).
  44. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  45. << "x::pre" << std::endl
  46. #endif
  47. << "x::body" << std::endl
  48. ;
  49. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
  50. out("");
  51. y();
  52. ok.str(""); ok // Test contracts in .hpp so post (NO_POST in build file).
  53. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  54. << "y::pre" << std::endl
  55. #endif
  56. #ifndef BOOST_CONTRACT_NO_OLDS
  57. << "y::old" << std::endl
  58. #endif
  59. << "y::body" << std::endl
  60. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  61. << "y::post" << std::endl
  62. #endif
  63. ;
  64. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
  65. return boost::report_errors();
  66. }