ifdef_macro.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 invariant compilation on/off (using macro interface).
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/unprotected_commas.hpp"
  8. #include <boost/contract_macro.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. boost::contract::test::detail::oteststream out;
  12. class a {
  13. public:
  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_VOLATILE({
  20. boost::contract::test::detail::unprotected_commas<void, void, void>::
  21. call();
  22. out << "a::cv_inv" << std::endl;
  23. })
  24. BOOST_CONTRACT_INVARIANT({
  25. boost::contract::test::detail::unprotected_commas<void, void, void>::
  26. call();
  27. out << "a::const_inv" << std::endl;
  28. })
  29. a() { // Test check both cv and const invariant (at exit if no throw).
  30. BOOST_CONTRACT_CONSTRUCTOR(boost::contract::test::detail::
  31. unprotected_commas<void, void, void>::same(this));
  32. out << "a::ctor::body" << std::endl;
  33. }
  34. ~a() { // Test check both cv and const invariant (at entry).
  35. BOOST_CONTRACT_DESTRUCTOR(boost::contract::test::detail::
  36. unprotected_commas<void, void, void>::same(this));
  37. out << "a::dtor::body" << std::endl;
  38. }
  39. void m() { // Test check const invariant (at entry and exit).
  40. BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
  41. unprotected_commas<void, void, void>::same(this));
  42. out << "a::m::body" << std::endl;
  43. }
  44. void c() const { // Test check const invariant (at entry and exit).
  45. BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
  46. unprotected_commas<void, void, void>::same(this));
  47. out << "a::c::body" << std::endl;
  48. }
  49. void v() volatile { // Test check cv invariant (at entry and exit).
  50. BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
  51. unprotected_commas<void, void, void>::same(this));
  52. out << "a::v::body" << std::endl;
  53. }
  54. void cv() const volatile { // Test check cv invariant (at entry and exit).
  55. BOOST_CONTRACT_PUBLIC_FUNCTION(boost::contract::test::detail::
  56. unprotected_commas<void, void, void>::same(this));
  57. out << "a::cv::body" << std::endl;
  58. }
  59. };
  60. int main() {
  61. std::ostringstream ok;
  62. {
  63. out.str("");
  64. a aa;
  65. ok.str(""); ok
  66. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  67. << "a::static_inv" << std::endl
  68. #endif
  69. << "a::ctor::body" << std::endl
  70. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  71. << "a::static_inv" << std::endl
  72. << "a::cv_inv" << std::endl
  73. << "a::const_inv" << std::endl
  74. #endif
  75. ;
  76. BOOST_TEST(out.eq(ok.str()));
  77. out.str("");
  78. aa.m();
  79. ok.str(""); ok
  80. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  81. << "a::static_inv" << std::endl
  82. << "a::const_inv" << std::endl
  83. #endif
  84. << "a::m::body" << std::endl
  85. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  86. << "a::static_inv" << std::endl
  87. << "a::const_inv" << std::endl
  88. #endif
  89. ;
  90. BOOST_TEST(out.eq(ok.str()));
  91. out.str("");
  92. aa.c();
  93. ok.str(""); ok
  94. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  95. << "a::static_inv" << std::endl
  96. << "a::const_inv" << std::endl
  97. #endif
  98. << "a::c::body" << std::endl
  99. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  100. << "a::static_inv" << std::endl
  101. << "a::const_inv" << std::endl
  102. #endif
  103. ;
  104. BOOST_TEST(out.eq(ok.str()));
  105. out.str("");
  106. aa.v();
  107. ok.str(""); ok
  108. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  109. << "a::static_inv" << std::endl
  110. << "a::cv_inv" << std::endl
  111. #endif
  112. << "a::v::body" << std::endl
  113. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  114. << "a::static_inv" << std::endl
  115. << "a::cv_inv" << std::endl
  116. #endif
  117. ;
  118. BOOST_TEST(out.eq(ok.str()));
  119. out.str("");
  120. aa.cv();
  121. ok.str(""); ok
  122. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  123. << "a::static_inv" << std::endl
  124. << "a::cv_inv" << std::endl
  125. #endif
  126. << "a::cv::body" << std::endl
  127. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  128. << "a::static_inv" << std::endl
  129. << "a::cv_inv" << std::endl
  130. #endif
  131. ;
  132. BOOST_TEST(out.eq(ok.str()));
  133. out.str("");
  134. } // Call dtor.
  135. ok.str(""); ok
  136. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  137. << "a::static_inv" << std::endl
  138. << "a::cv_inv" << std::endl
  139. << "a::const_inv" << std::endl
  140. #endif
  141. << "a::dtor::body" << std::endl
  142. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  143. << "a::static_inv" << std::endl
  144. #endif
  145. ;
  146. BOOST_TEST(out.eq(ok.str()));
  147. return boost::report_errors();
  148. }