ifdef_macro.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/constructor.hpp> // Outside #if below for ctor pre.
  10. #include <boost/contract_macro.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. boost::contract::test::detail::oteststream out;
  14. struct b :
  15. private boost::contract::constructor_precondition<b> // OK, always in code.
  16. {
  17. BOOST_CONTRACT_STATIC_INVARIANT({
  18. boost::contract::test::detail::unprotected_commas<void, void, void>::
  19. call();
  20. out << "b::static_inv" << std::endl;
  21. })
  22. BOOST_CONTRACT_INVARIANT({
  23. boost::contract::test::detail::unprotected_commas<void, void, void>::
  24. call();
  25. out << "b::inv" << std::endl;
  26. })
  27. explicit b(int x) :
  28. BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(b)([] {
  29. boost::contract::test::detail::unprotected_commas<void, void, void>
  30. ::call();
  31. out << "b::ctor::pre" << std::endl;
  32. })
  33. {
  34. BOOST_CONTRACT_OLD_PTR(
  35. boost::contract::test::detail::unprotected_commas<int, void,
  36. void>::type1
  37. )(
  38. old_x,
  39. (boost::contract::test::detail::unprotected_commas<void, void,
  40. void>::same(x))
  41. );
  42. BOOST_CONTRACT_CONSTRUCTOR(this)
  43. BOOST_CONTRACT_OLD([] {
  44. boost::contract::test::detail::unprotected_commas<
  45. void, void, void>::call();
  46. out << "b::f::old" << std::endl;
  47. })
  48. BOOST_CONTRACT_POSTCONDITION([] {
  49. boost::contract::test::detail::unprotected_commas<
  50. void, void, void>::call();
  51. out << "b::ctor::post" << std::endl;
  52. })
  53. ;
  54. out << "b::ctor::body" << std::endl;
  55. }
  56. };
  57. struct a:
  58. private boost::contract::constructor_precondition<a>, // OK, always in code.
  59. public b
  60. {
  61. BOOST_CONTRACT_STATIC_INVARIANT({
  62. boost::contract::test::detail::unprotected_commas<void, void, void>::
  63. call();
  64. out << "a::static_inv" << std::endl;
  65. })
  66. BOOST_CONTRACT_INVARIANT({
  67. boost::contract::test::detail::unprotected_commas<void, void, void>::
  68. call();
  69. out << "a::inv" << std::endl;
  70. })
  71. explicit a(int x) :
  72. BOOST_CONTRACT_CONSTRUCTOR_PRECONDITION(a)([] {
  73. boost::contract::test::detail::unprotected_commas<void, void, void>
  74. ::call();
  75. out << "a::ctor::pre" << std::endl; }
  76. ),
  77. b(x)
  78. {
  79. BOOST_CONTRACT_OLD_PTR(
  80. boost::contract::test::detail::unprotected_commas<int, void,
  81. void>::type1
  82. )(
  83. old_x,
  84. (boost::contract::test::detail::unprotected_commas<void, void,
  85. void>::same(x))
  86. );
  87. BOOST_CONTRACT_CONSTRUCTOR(boost::contract::test::detail::
  88. unprotected_commas<void, void, void>::same(this))
  89. BOOST_CONTRACT_OLD([] {
  90. boost::contract::test::detail::unprotected_commas<
  91. void, void, void>::call();
  92. out << "a::f::old" << std::endl;
  93. })
  94. BOOST_CONTRACT_POSTCONDITION([] {
  95. boost::contract::test::detail::unprotected_commas<
  96. void, void, void>::call();
  97. out << "a::ctor::post" << std::endl;
  98. })
  99. ;
  100. out << "a::ctor::body" << std::endl;
  101. }
  102. };
  103. int main() {
  104. std::ostringstream ok;
  105. out.str("");
  106. a aa(123);
  107. ok.str(""); ok
  108. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  109. << "a::ctor::pre" << std::endl
  110. << "b::ctor::pre" << std::endl
  111. #endif
  112. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  113. << "b::static_inv" << std::endl
  114. #endif
  115. #ifndef BOOST_CONTRACT_NO_OLDS
  116. << "b::f::old" << std::endl
  117. #endif
  118. << "b::ctor::body" << std::endl
  119. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  120. << "b::static_inv" << std::endl
  121. << "b::inv" << std::endl
  122. #endif
  123. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  124. << "b::ctor::post" << std::endl
  125. #endif
  126. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  127. << "a::static_inv" << std::endl
  128. #endif
  129. #ifndef BOOST_CONTRACT_NO_OLDS
  130. << "a::f::old" << std::endl
  131. #endif
  132. << "a::ctor::body" << std::endl
  133. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  134. << "a::static_inv" << std::endl
  135. << "a::inv" << std::endl
  136. #endif
  137. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  138. << "a::ctor::post" << std::endl
  139. #endif
  140. ;
  141. BOOST_TEST(out.eq(ok.str()));
  142. return boost::report_errors();
  143. }