decl_exit_inv_all.cpp 5.4 KB

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