ifdef.cpp 5.4 KB

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