decl_exit_static_inv_mid.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 static invariants.
  6. #define BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #define 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::inv" << std::endl
  18. << "b::static_inv" << std::endl
  19. << "b::inv" << std::endl
  20. << "a::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_static_inv = true;
  56. b_exit_static_inv = true;
  57. c_exit_static_inv = true;
  58. a_entering_static_inv = b_entering_static_inv = c_entering_static_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::inv" << std::endl
  66. << "b::static_inv" << std::endl
  67. << "b::inv" << std::endl
  68. << "a::inv" << std::endl
  69. #endif
  70. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  71. << ok_end()
  72. #endif
  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. ok.str(""); ok
  86. << ok_begin()
  87. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  88. << "c::inv" << std::endl
  89. << "b::static_inv" << std::endl
  90. << "b::inv" << std::endl
  91. // Test no failure here.
  92. << "a::inv" << std::endl
  93. #endif
  94. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  95. << ok_end()
  96. #endif
  97. ;
  98. BOOST_TEST(out.eq(ok.str()));
  99. } catch(...) { BOOST_TEST(false); }
  100. a_exit_static_inv = true;
  101. b_exit_static_inv = false;
  102. c_exit_static_inv = true;
  103. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  104. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  105. out.str("");
  106. try {
  107. aa.f();
  108. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  109. BOOST_TEST(false);
  110. } catch(err const&) {
  111. #endif
  112. ok.str(""); ok
  113. << ok_begin()
  114. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  115. << "c::inv" << std::endl
  116. << "b::static_inv" << std::endl // Test this failed.
  117. #elif !defined(BOOST_CONTRACT_NO_POSTCONDITIONS)
  118. << ok_end()
  119. #endif
  120. ;
  121. BOOST_TEST(out.eq(ok.str()));
  122. } catch(...) { BOOST_TEST(false); }
  123. a_exit_static_inv = true;
  124. b_exit_static_inv = true;
  125. c_exit_static_inv = false;
  126. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  127. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  128. out.str("");
  129. try {
  130. aa.f();
  131. ok.str(""); ok
  132. << ok_begin()
  133. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  134. << "c::inv" << std::endl
  135. // Test no failure here.
  136. << "b::static_inv" << std::endl
  137. << "b::inv" << std::endl
  138. << "a::inv" << std::endl
  139. #endif
  140. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  141. << ok_end()
  142. #endif
  143. ;
  144. BOOST_TEST(out.eq(ok.str()));
  145. } catch(...) { BOOST_TEST(false); }
  146. a_exit_static_inv = false;
  147. b_exit_static_inv = false;
  148. c_exit_static_inv = false;
  149. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  150. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  151. out.str("");
  152. try {
  153. aa.f();
  154. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  155. BOOST_TEST(false);
  156. } catch(err const&) {
  157. #endif
  158. ok.str(""); ok
  159. << ok_begin()
  160. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  161. << "c::inv" << std::endl
  162. // Test this failed (as all did).
  163. << "b::static_inv" << std::endl
  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. #undef BOOST_CONTRACT_TEST_entry_inv
  171. return boost::report_errors();
  172. }