decl_exit_static_inv_ends.cpp 6.1 KB

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