decl_entry_static_inv_all.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 all derived and base classes with entry static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #undef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  9. #include "decl.hpp"
  10. #include <boost/preprocessor/control/iif.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. #include <string>
  14. std::string ok_end() {
  15. std::ostringstream ok; ok
  16. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  17. << "c::f::pre" << std::endl
  18. #endif
  19. #ifndef BOOST_CONTRACT_NO_OLDS
  20. << "c::f::old" << std::endl
  21. << "b::f::old" << std::endl
  22. << "a::f::old" << std::endl
  23. #endif
  24. << "a::f::body" << std::endl
  25. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  26. << "c::static_inv" << std::endl
  27. << "c::inv" << std::endl
  28. << "b::static_inv" << std::endl
  29. << "b::inv" << std::endl
  30. << "a::static_inv" << std::endl
  31. << "a::inv" << std::endl
  32. #endif
  33. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  34. << "c::f::old" << std::endl
  35. << "c::f::post" << std::endl
  36. << "b::f::old" << std::endl
  37. << "b::f::post" << std::endl
  38. << "a::f::post" << std::endl
  39. #endif
  40. ;
  41. return ok.str();
  42. }
  43. struct err {}; // Global decl so visible in MSVC10 lambdas.
  44. int main() {
  45. std::ostringstream ok;
  46. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  47. #define BOOST_CONTRACT_TEST_entry_inv 0
  48. #else
  49. #define BOOST_CONTRACT_TEST_entry_inv 1
  50. #endif
  51. a aa;
  52. a_entry_static_inv = true;
  53. b_entry_static_inv = true;
  54. c_entry_static_inv = true;
  55. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  56. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  57. out.str("");
  58. aa.f();
  59. ok.str(""); ok // Test nothing failed.
  60. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  61. << "c::static_inv" << std::endl
  62. << "c::inv" << std::endl
  63. << "b::static_inv" << std::endl
  64. << "b::inv" << std::endl
  65. << "a::static_inv" << std::endl
  66. << "a::inv" << std::endl
  67. #endif
  68. << ok_end()
  69. ;
  70. BOOST_TEST(out.eq(ok.str()));
  71. boost::contract::set_entry_invariant_failure(
  72. [] (boost::contract::from) { throw err(); });
  73. a_entry_static_inv = false;
  74. b_entry_static_inv = true;
  75. c_entry_static_inv = true;
  76. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  77. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  78. out.str("");
  79. try {
  80. aa.f();
  81. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  82. BOOST_TEST(false);
  83. } catch(err const&) {
  84. #endif
  85. ok.str(""); ok
  86. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  87. << "c::static_inv" << std::endl
  88. << "c::inv" << std::endl
  89. << "b::static_inv" << std::endl
  90. << "b::inv" << std::endl
  91. << "a::static_inv" << std::endl // Test this failed.
  92. #else
  93. << ok_end()
  94. #endif
  95. ;
  96. BOOST_TEST(out.eq(ok.str()));
  97. } catch(...) { BOOST_TEST(false); }
  98. a_entry_static_inv = true;
  99. b_entry_static_inv = false;
  100. c_entry_static_inv = true;
  101. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  102. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  103. out.str("");
  104. try {
  105. aa.f();
  106. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  107. BOOST_TEST(false);
  108. } catch(err const&) {
  109. #endif
  110. ok.str(""); ok
  111. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  112. << "c::static_inv" << std::endl
  113. << "c::inv" << std::endl
  114. << "b::static_inv" << std::endl // Test this failed.
  115. #else
  116. << ok_end()
  117. #endif
  118. ;
  119. BOOST_TEST(out.eq(ok.str()));
  120. } catch(...) { BOOST_TEST(false); }
  121. a_entry_static_inv = true;
  122. b_entry_static_inv = true;
  123. c_entry_static_inv = false;
  124. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  125. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  126. out.str("");
  127. try {
  128. aa.f();
  129. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  130. BOOST_TEST(false);
  131. } catch(err const&) {
  132. #endif
  133. ok.str(""); ok
  134. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  135. << "c::static_inv" << std::endl // Test this failed.
  136. #else
  137. << ok_end()
  138. #endif
  139. ;
  140. BOOST_TEST(out.eq(ok.str()));
  141. } catch(...) { BOOST_TEST(false); }
  142. a_entry_static_inv = false;
  143. b_entry_static_inv = false;
  144. c_entry_static_inv = false;
  145. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  146. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  147. out.str("");
  148. try {
  149. aa.f();
  150. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  151. BOOST_TEST(false);
  152. } catch(err const&) {
  153. #endif
  154. ok.str(""); ok
  155. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  156. // Test this failed (as all did).
  157. << "c::static_inv" << std::endl
  158. #else
  159. << ok_end()
  160. #endif
  161. ;
  162. BOOST_TEST(out.eq(ok.str()));
  163. } catch(...) { BOOST_TEST(false); }
  164. #undef BOOST_CONTRACT_TEST_entry_inv
  165. return boost::report_errors();
  166. }