decl_exit_static_inv_mid.cpp 5.8 KB

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