decl_exit_inv_ends.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 exit invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_INV
  7. #define 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. // No failure here.
  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 no failure.
  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. ok.str(""); ok
  126. << ok_c()
  127. << ok_b() // Test no b::inv so no failure.
  128. << ok_a()
  129. << ok_end()
  130. ;
  131. BOOST_TEST(out.eq(ok.str()));
  132. } catch(...) { BOOST_TEST(false); }
  133. a_exit_inv = true;
  134. b_exit_inv = true;
  135. c_exit_inv = false;
  136. try {
  137. out.str("");
  138. a aa;
  139. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  140. BOOST_TEST(false);
  141. } catch(err const&) {
  142. #endif
  143. ok.str(""); ok
  144. << ok_c() // Test c::inv failed.
  145. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  146. << ok_b()
  147. << ok_a()
  148. << ok_end()
  149. #endif
  150. ;
  151. BOOST_TEST(out.eq(ok.str()));
  152. } catch(...) { BOOST_TEST(false); }
  153. a_exit_inv = false;
  154. b_exit_inv = false;
  155. c_exit_inv = false;
  156. try {
  157. out.str("");
  158. a aa;
  159. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  160. BOOST_TEST(false);
  161. } catch(err const&) {
  162. #endif
  163. ok.str(""); ok
  164. << ok_c() // Test c::inv failed.
  165. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  166. << ok_b()
  167. << ok_a()
  168. << ok_end()
  169. #endif
  170. ;
  171. BOOST_TEST(out.eq(ok.str()));
  172. } catch(...) { BOOST_TEST(false); }
  173. return boost::report_errors();
  174. }