static_ifdef.cpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 public static member function contract compilation on/off.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/core/config.hpp>
  8. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  9. #include <boost/contract/public_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. struct a {
  17. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  18. static void static_invariant() { out << "a::static_inv" << std::endl; }
  19. void invariant() const { out << "a::inv" << std::endl; }
  20. #endif
  21. static void f(int x) {
  22. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  23. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  24. #endif
  25. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  26. boost::contract::check c = boost::contract::public_function<a>()
  27. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  28. .precondition([] { out << "a::f::pre" << std::endl; })
  29. #endif
  30. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  31. .old([] { out << "a::f::old" << std::endl; })
  32. .postcondition([] { out << "a::f::post" << std::endl; })
  33. #endif
  34. ;
  35. #endif
  36. out << "a::f::body" << std::endl;
  37. }
  38. };
  39. int main() {
  40. std::ostringstream ok;
  41. out.str("");
  42. a::f(123);
  43. ok.str(""); ok
  44. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  45. << "a::static_inv" << std::endl
  46. #endif
  47. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  48. << "a::f::pre" << std::endl
  49. #endif
  50. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  51. << "a::f::old" << std::endl
  52. #endif
  53. << "a::f::body" << std::endl
  54. // Test no post (but still static inv) because body threw.
  55. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  56. << "a::static_inv" << std::endl
  57. #endif
  58. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  59. << "a::f::post" << std::endl
  60. #endif
  61. ;
  62. BOOST_TEST(out.eq(ok.str()));
  63. return boost::report_errors();
  64. }