ifdef_macro.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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/core/virtual.hpp>
  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. BOOST_CONTRACT_STATIC_INVARIANT({
  16. boost::contract::test::detail::unprotected_commas<void, void, void>::
  17. call();
  18. out << "b::static_inv" << std::endl;
  19. })
  20. BOOST_CONTRACT_INVARIANT({
  21. boost::contract::test::detail::unprotected_commas<void, void, void>::
  22. call();
  23. out << "b::inv" << std::endl;
  24. })
  25. virtual void f(int x, boost::contract::virtual_* v = 0) = 0;
  26. };
  27. void b::f(int x, boost::contract::virtual_* v) {
  28. BOOST_CONTRACT_OLD_PTR(
  29. boost::contract::test::detail::unprotected_commas<int, void, void>::
  30. type1
  31. )(
  32. (boost::contract::test::detail::unprotected_commas<void, void, void>::
  33. same(v)),
  34. old_x,
  35. (boost::contract::test::detail::unprotected_commas<void, void, void>::
  36. same(x))
  37. );
  38. BOOST_CONTRACT_PUBLIC_FUNCTION(
  39. boost::contract::test::detail::unprotected_commas<void, void, void>::
  40. same(v),
  41. boost::contract::test::detail::unprotected_commas<void, void, void>::
  42. same(this)
  43. )
  44. BOOST_CONTRACT_PRECONDITION([] {
  45. boost::contract::test::detail::unprotected_commas<
  46. void, void, void>::call();
  47. out << "b::f::pre" << std::endl;
  48. })
  49. BOOST_CONTRACT_OLD([] {
  50. boost::contract::test::detail::unprotected_commas<
  51. void, void, void>::call();
  52. out << "b::f::old" << std::endl;
  53. })
  54. BOOST_CONTRACT_POSTCONDITION([] {
  55. boost::contract::test::detail::unprotected_commas<
  56. void, void, void>::call();
  57. out << "b::f::post" << std::endl;
  58. })
  59. ;
  60. out << "b::f::body" << std::endl;
  61. }
  62. struct a
  63. #define BASES public boost::contract::test::detail::unprotected_commas< \
  64. b, void, void>::type1
  65. : BASES
  66. {
  67. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  68. BOOST_CONTRACT_OVERRIDE(f)
  69. BOOST_CONTRACT_STATIC_INVARIANT({
  70. boost::contract::test::detail::unprotected_commas<void, void, void>::
  71. call();
  72. out << "a::static_inv" << std::endl;
  73. })
  74. BOOST_CONTRACT_INVARIANT({
  75. boost::contract::test::detail::unprotected_commas<void, void, void>::
  76. call();
  77. out << "a::inv" << std::endl;
  78. })
  79. virtual void f(int x, boost::contract::virtual_* v = 0) {
  80. BOOST_CONTRACT_OLD_PTR(
  81. boost::contract::test::detail::unprotected_commas<int, void, void>::
  82. type1
  83. )(
  84. (boost::contract::test::detail::unprotected_commas<void, void,
  85. void>::same(v)),
  86. old_x,
  87. (boost::contract::test::detail::unprotected_commas<void, void,
  88. void>::same(x))
  89. );
  90. BOOST_CONTRACT_PUBLIC_FUNCTION_OVERRIDE(
  91. boost::contract::test::detail::unprotected_commas<override_f, void,
  92. void>::type1
  93. )(
  94. boost::contract::test::detail::unprotected_commas<void, void, void>
  95. ::same(v),
  96. &a::f,
  97. boost::contract::test::detail::unprotected_commas<void, void, void>
  98. ::same(this),
  99. boost::contract::test::detail::unprotected_commas<void, void, void>
  100. ::same(x)
  101. )
  102. BOOST_CONTRACT_PRECONDITION([] {
  103. boost::contract::test::detail::unprotected_commas<
  104. void, void, void>::call();
  105. out << "a::f::pre" << std::endl;
  106. })
  107. BOOST_CONTRACT_OLD([] {
  108. boost::contract::test::detail::unprotected_commas<
  109. void, void, void>::call();
  110. out << "a::f::old" << std::endl;
  111. })
  112. BOOST_CONTRACT_POSTCONDITION([] {
  113. boost::contract::test::detail::unprotected_commas<
  114. void, void, void>::call();
  115. out << "a::f::post" << std::endl;
  116. })
  117. ;
  118. out << "a::f::body" << std::endl;
  119. }
  120. };
  121. int main() {
  122. std::ostringstream ok;
  123. a aa;
  124. out.str("");
  125. aa.f(123);
  126. ok.str(""); ok
  127. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  128. << "b::static_inv" << std::endl
  129. << "b::inv" << std::endl
  130. << "a::static_inv" << std::endl
  131. << "a::inv" << std::endl
  132. #endif
  133. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  134. << "b::f::pre" << std::endl
  135. #endif
  136. #ifndef BOOST_CONTRACT_NO_OLDS
  137. << "b::f::old" << std::endl
  138. << "a::f::old" << std::endl
  139. #endif
  140. << "a::f::body" << std::endl
  141. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  142. << "b::static_inv" << std::endl
  143. << "b::inv" << std::endl
  144. << "a::static_inv" << std::endl
  145. << "a::inv" << std::endl
  146. #endif
  147. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  148. << "b::f::old" << std::endl // Called by post (so under NO_POST).
  149. << "b::f::post" << std::endl
  150. << "a::f::post" << std::endl
  151. #endif
  152. ;
  153. BOOST_TEST(out.eq(ok.str()));
  154. return boost::report_errors();
  155. }