decl_entry_inv_all.cpp 6.7 KB

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