decl_exit_inv_mid.cpp 5.7 KB

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