ifdef.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 contract compilation on/off.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/core/config.hpp>
  8. #ifndef BOOST_CONTRACT_NO_FUNCTIONS
  9. #include <boost/contract/function.hpp>
  10. #include <boost/contract/check.hpp>
  11. #include <boost/contract/old.hpp>
  12. #endif
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <sstream>
  15. boost::contract::test::detail::oteststream out;
  16. void f(int x) {
  17. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  18. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  19. #endif
  20. #ifndef BOOST_CONTRACT_NO_FUNCTIONS
  21. boost::contract::check c = boost::contract::function()
  22. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  23. .precondition([] { out << "f::pre" << std::endl; })
  24. #endif
  25. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  26. .old([] { out << "f::old" << std::endl; })
  27. .postcondition([] { out << "f::post" << std::endl; })
  28. #endif
  29. ;
  30. #endif
  31. out << "f::body" << std::endl;
  32. }
  33. int main() {
  34. std::ostringstream ok;
  35. out.str("");
  36. f(123);
  37. ok.str(""); ok
  38. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  39. << "f::pre" << std::endl
  40. #endif
  41. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  42. << "f::old" << std::endl
  43. #endif
  44. << "f::body" << std::endl
  45. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  46. << "f::post" << std::endl
  47. #endif
  48. ;
  49. BOOST_TEST(out.eq(ok.str()));
  50. return boost::report_errors();
  51. }