static_ifdef_macro.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 (w/ macros).
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/unprotected_commas.hpp"
  8. #include <boost/contract/core/config.hpp>
  9. #include <boost/contract_macro.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. boost::contract::test::detail::oteststream out;
  13. struct a {
  14. BOOST_CONTRACT_STATIC_INVARIANT({
  15. boost::contract::test::detail::unprotected_commas<void, void, void>::
  16. call();
  17. out << "a::static_inv" << std::endl;
  18. })
  19. BOOST_CONTRACT_INVARIANT({
  20. boost::contract::test::detail::unprotected_commas<void, void, void>::
  21. call();
  22. out << "a::inv" << std::endl;
  23. })
  24. static void f(int x) {
  25. BOOST_CONTRACT_OLD_PTR(
  26. boost::contract::test::detail::unprotected_commas<int, void, void>::
  27. type1
  28. )(
  29. old_x,
  30. (boost::contract::test::detail::unprotected_commas<void, void,
  31. void>::same(x))
  32. );
  33. BOOST_CONTRACT_STATIC_PUBLIC_FUNCTION(boost::contract::test::detail::
  34. unprotected_commas<a, void, void>::type1)
  35. BOOST_CONTRACT_PRECONDITION([] {
  36. boost::contract::test::detail::unprotected_commas<
  37. void, void, void>::call();
  38. out << "a::f::pre" << std::endl;
  39. })
  40. BOOST_CONTRACT_OLD([] {
  41. boost::contract::test::detail::unprotected_commas<
  42. void, void, void>::call();
  43. out << "a::f::old" << std::endl;
  44. })
  45. BOOST_CONTRACT_POSTCONDITION([] {
  46. boost::contract::test::detail::unprotected_commas<
  47. void, void, void>::call();
  48. out << "a::f::post" << std::endl;
  49. })
  50. ;
  51. out << "a::f::body" << std::endl;
  52. }
  53. };
  54. int main() {
  55. std::ostringstream ok;
  56. out.str("");
  57. a::f(123);
  58. ok.str(""); ok
  59. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  60. << "a::static_inv" << std::endl
  61. #endif
  62. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  63. << "a::f::pre" << std::endl
  64. #endif
  65. #ifndef BOOST_CONTRACT_NO_OLDS
  66. << "a::f::old" << std::endl
  67. #endif
  68. << "a::f::body" << std::endl
  69. // Test no post (but still static inv) because body threw.
  70. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  71. << "a::static_inv" << std::endl
  72. #endif
  73. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  74. << "a::f::post" << std::endl
  75. #endif
  76. ;
  77. BOOST_TEST(out.eq(ok.str()));
  78. return boost::report_errors();
  79. }