decl_entry_static_inv_mid.cpp 6.1 KB

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