decl_entry_static_inv_ends.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 entry static inv.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #define BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #undef 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_a() {
  15. std::ostringstream ok; ok
  16. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  17. << "a::static_inv" << std::endl
  18. << "a::inv" << std::endl
  19. #endif
  20. #ifndef BOOST_CONTRACT_NO_OLDS
  21. << "a::dtor::old" << std::endl
  22. #endif
  23. << "a::dtor::body" << std::endl
  24. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  25. << "a::static_inv" << std::endl
  26. #endif
  27. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  28. << "a::dtor::post" << std::endl
  29. #endif
  30. ;
  31. return ok.str();
  32. }
  33. std::string ok_b(bool threw = false) {
  34. std::ostringstream ok; ok
  35. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  36. << "b::inv" << std::endl
  37. #endif
  38. #ifndef BOOST_CONTRACT_NO_OLDS
  39. << "b::dtor::old" << std::endl
  40. #endif
  41. << "b::dtor::body" << std::endl
  42. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  43. << (threw ? "b::inv\n" : "")
  44. #endif
  45. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  46. << (!threw ? "b::dtor::post\n" : "")
  47. #endif
  48. ;
  49. return ok.str();
  50. }
  51. std::string ok_c(bool threw = false) {
  52. std::ostringstream ok; ok
  53. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  54. << "c::static_inv" << std::endl
  55. << "c::inv" << std::endl
  56. #endif
  57. #ifndef BOOST_CONTRACT_NO_OLDS
  58. << "c::dtor::old" << std::endl
  59. #endif
  60. << "c::dtor::body" << std::endl
  61. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  62. << "c::static_inv" << std::endl
  63. << (threw ? "c::inv\n" : "")
  64. #endif
  65. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  66. << (!threw ? "c::dtor::post\n" : "")
  67. #endif
  68. ;
  69. return ok.str();
  70. }
  71. struct err {}; // Global decl so visible in MSVC10 lambdas.
  72. int main() {
  73. std::ostringstream ok;
  74. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  75. #define BOOST_CONTRACT_TEST_entry_inv 0
  76. #else
  77. #define BOOST_CONTRACT_TEST_entry_inv 1
  78. #endif
  79. a_entry_static_inv = true;
  80. b_entry_static_inv = true;
  81. c_entry_static_inv = true;
  82. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  83. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  84. {
  85. a aa;
  86. out.str("");
  87. }
  88. ok.str(""); ok // Test nothing failed.
  89. << ok_a()
  90. << ok_b()
  91. << ok_c()
  92. ;
  93. BOOST_TEST(out.eq(ok.str()));
  94. boost::contract::set_entry_invariant_failure([&ok] (boost::contract::from) {
  95. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws.
  96. throw err(); // For testing only (dtors should never throw otherwise).
  97. });
  98. a_entry_static_inv = false;
  99. b_entry_static_inv = true;
  100. c_entry_static_inv = true;
  101. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  102. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  103. try {
  104. {
  105. a aa;
  106. ok.str(""); ok
  107. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  108. << "a::static_inv" << std::endl // Test this failed...
  109. #else
  110. << ok_a()
  111. #endif
  112. ;
  113. out.str("");
  114. }
  115. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  116. BOOST_TEST(false);
  117. } catch(err const&) {
  118. #endif
  119. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  120. << ok_b(BOOST_CONTRACT_TEST_entry_inv)
  121. << ok_c(BOOST_CONTRACT_TEST_entry_inv)
  122. ;
  123. BOOST_TEST(out.eq(ok.str()));
  124. } catch(...) { BOOST_TEST(false); }
  125. a_entry_static_inv = true;
  126. b_entry_static_inv = false;
  127. c_entry_static_inv = true;
  128. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  129. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  130. {
  131. a aa;
  132. out.str("");
  133. }
  134. ok.str(""); ok
  135. << ok_a()
  136. << ok_b() // Test no b::static_inv so no failure here.
  137. << ok_c()
  138. ;
  139. BOOST_TEST(out.eq(ok.str()));
  140. a_entry_static_inv = true;
  141. b_entry_static_inv = true;
  142. c_entry_static_inv = false;
  143. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  144. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  145. try {
  146. {
  147. a aa;
  148. ok.str(""); ok
  149. << ok_a()
  150. << ok_b()
  151. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  152. << "c::static_inv" << std::endl // Test this failed...
  153. #else
  154. << ok_c()
  155. #endif
  156. ;
  157. out.str("");
  158. }
  159. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  160. BOOST_TEST(false);
  161. } catch(err const&) {
  162. #endif
  163. // ...then exec other dtors and check inv on throw (as dtor threw).
  164. BOOST_TEST(out.eq(ok.str()));
  165. } catch(...) { BOOST_TEST(false); }
  166. boost::contract::set_entry_invariant_failure([] (boost::contract::from) {
  167. // Testing multiple failures so dtors must not throw multiple
  168. // exceptions, just ignore failure and continue test program.
  169. });
  170. a_entry_static_inv = false;
  171. b_entry_static_inv = false;
  172. c_entry_static_inv = false;
  173. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  174. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  175. {
  176. a aa;
  177. out.str("");
  178. }
  179. ok.str(""); ok
  180. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  181. << "a::static_inv" << std::endl // Test this failed (as all did).
  182. << "a::dtor::body" << std::endl
  183. << ok_b() // Test no b::static_inv so no failure here.
  184. << "c::static_inv" << std::endl // Test this failed (as all did).
  185. << "c::dtor::body" << std::endl
  186. #else
  187. << ok_a()
  188. << ok_b()
  189. << ok_c()
  190. #endif
  191. ;
  192. BOOST_TEST(out.eq(ok.str()));
  193. #undef BOOST_CONTRACT_TEST_entry_inv
  194. return boost::report_errors();
  195. }