decl_exit_inv_all.cpp 6.1 KB

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