decl_entry_static_inv_ends.cpp 6.4 KB

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