decl_exit_inv_ends.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_INV
  7. #define BOOST_CONTRACT_TEST_NO_B_INV
  8. #undef BOOST_CONTRACT_TEST_NO_C_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::static_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_inv = true;
  57. b_exit_inv = true;
  58. c_exit_inv = true;
  59. a_entering_inv = b_entering_inv = c_entering_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::static_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_inv = false;
  78. b_exit_inv = true;
  79. c_exit_inv = true;
  80. a_entering_inv = b_entering_inv = c_entering_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::static_inv" << std::endl
  95. << "a::static_inv" << std::endl
  96. << "a::inv" << std::endl // Test this failed.
  97. #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
  98. << ok_end()
  99. #endif
  100. ;
  101. BOOST_TEST(out.eq(ok.str()));
  102. } catch(...) { BOOST_TEST(false); }
  103. a_exit_inv = true;
  104. b_exit_inv = false;
  105. c_exit_inv = true;
  106. a_entering_inv = b_entering_inv = c_entering_inv =
  107. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  108. out.str("");
  109. try {
  110. aa.f();
  111. ok.str(""); ok
  112. << ok_begin()
  113. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  114. << "c::static_inv" << std::endl
  115. << "c::inv" << std::endl
  116. << "b::static_inv" << std::endl
  117. << "a::static_inv" << std::endl
  118. << "a::inv" << std::endl
  119. #endif
  120. << ok_end()
  121. ;
  122. BOOST_TEST(out.eq(ok.str()));
  123. } catch(...) { BOOST_TEST(false); }
  124. a_exit_inv = true;
  125. b_exit_inv = true;
  126. c_exit_inv = false;
  127. a_entering_inv = b_entering_inv = c_entering_inv =
  128. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  129. out.str("");
  130. try {
  131. aa.f();
  132. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  133. BOOST_TEST(false);
  134. } catch(err const&) {
  135. #endif
  136. ok.str(""); ok
  137. << ok_begin()
  138. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  139. << "c::static_inv" << std::endl
  140. << "c::inv" << std::endl // Test this failed.
  141. #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
  142. << ok_end()
  143. #endif
  144. ;
  145. BOOST_TEST(out.eq(ok.str()));
  146. } catch(...) { BOOST_TEST(false); }
  147. a_exit_inv = false;
  148. b_exit_inv = false;
  149. c_exit_inv = false;
  150. a_entering_inv = b_entering_inv = c_entering_inv =
  151. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  152. out.str("");
  153. try {
  154. aa.f();
  155. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  156. BOOST_TEST(false);
  157. } catch(err const&) {
  158. #endif
  159. ok.str(""); ok
  160. << ok_begin()
  161. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  162. << "c::static_inv" << std::endl
  163. << "c::inv" << std::endl // Test this failed (as all did).
  164. #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
  165. << ok_end()
  166. #endif
  167. ;
  168. BOOST_TEST(out.eq(ok.str()));
  169. } catch(...) { BOOST_TEST(false); }
  170. return boost::report_errors();
  171. }