ifdef_macro.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 (using macro interface).
  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 b {
  14. BOOST_CONTRACT_STATIC_INVARIANT({
  15. boost::contract::test::detail::unprotected_commas<void, void, void>::
  16. call();
  17. out << "b::static_inv" << std::endl;
  18. })
  19. BOOST_CONTRACT_INVARIANT({
  20. boost::contract::test::detail::unprotected_commas<void, void, void>::
  21. call();
  22. out << "b::inv" << std::endl;
  23. })
  24. virtual ~b() {
  25. BOOST_CONTRACT_OLD_PTR(
  26. boost::contract::test::detail::unprotected_commas<int, void,
  27. void>::type1
  28. )(
  29. old_y,
  30. (boost::contract::test::detail::unprotected_commas<void, void,
  31. void>::same(y))
  32. );
  33. BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
  34. unprotected_commas<void, void, void>::same(this))
  35. BOOST_CONTRACT_OLD([] {
  36. boost::contract::test::detail::unprotected_commas<
  37. void, void, void>::call();
  38. out << "b::dtor::old" << std::endl;
  39. })
  40. BOOST_CONTRACT_POSTCONDITION([] {
  41. boost::contract::test::detail::unprotected_commas<
  42. void, void, void>::call();
  43. out << "b::dtor::post" << std::endl;
  44. })
  45. ;
  46. out << "b::dtor::body" << std::endl;
  47. }
  48. static int y;
  49. };
  50. int b::y = 0;
  51. struct a : public b {
  52. BOOST_CONTRACT_STATIC_INVARIANT({
  53. boost::contract::test::detail::unprotected_commas<void, void, void>::
  54. call();
  55. out << "a::static_inv" << std::endl;
  56. })
  57. BOOST_CONTRACT_INVARIANT({
  58. boost::contract::test::detail::unprotected_commas<void, void, void>::
  59. call();
  60. out << "a::inv" << std::endl;
  61. })
  62. virtual ~a() {
  63. BOOST_CONTRACT_OLD_PTR(
  64. boost::contract::test::detail::unprotected_commas<int, void, void>::
  65. type1
  66. )(
  67. old_x,
  68. (boost::contract::test::detail::unprotected_commas<void, void,
  69. void>::same(x))
  70. );
  71. BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
  72. unprotected_commas<void, void, void>::same(this))
  73. BOOST_CONTRACT_OLD([] {
  74. boost::contract::test::detail::unprotected_commas<
  75. void, void, void>::call();
  76. out << "a::dtor::old" << std::endl;
  77. })
  78. BOOST_CONTRACT_POSTCONDITION([] {
  79. boost::contract::test::detail::unprotected_commas<
  80. void, void, void>::call();
  81. out << "a::dtor::post" << std::endl;
  82. })
  83. ;
  84. out << "a::dtor::body" << std::endl;
  85. }
  86. static int x;
  87. };
  88. int a::x = 0;
  89. int main() {
  90. std::ostringstream ok;
  91. {
  92. a aa;
  93. out.str("");
  94. }
  95. ok.str(""); ok
  96. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  97. << "a::static_inv" << std::endl
  98. << "a::inv" << std::endl
  99. #endif
  100. #ifndef BOOST_CONTRACT_NO_OLDS
  101. << "a::dtor::old" << std::endl
  102. #endif
  103. << "a::dtor::body" << std::endl
  104. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  105. << "a::static_inv" << std::endl
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  108. << "a::dtor::post" << std::endl
  109. #endif
  110. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  111. << "b::static_inv" << std::endl
  112. << "b::inv" << std::endl
  113. #endif
  114. #ifndef BOOST_CONTRACT_NO_OLDS
  115. << "b::dtor::old" << std::endl
  116. #endif
  117. << "b::dtor::body" << std::endl
  118. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  119. << "b::static_inv" << std::endl
  120. #endif
  121. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  122. << "b::dtor::post" << std::endl
  123. #endif
  124. ;
  125. BOOST_TEST(out.eq(ok.str()));
  126. return boost::report_errors();
  127. }