decl_entry_inv_mid.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 invariants.
  6. #define BOOST_CONTRACT_TEST_NO_A_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_INV
  8. #define 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() {
  14. std::ostringstream ok; ok
  15. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  16. << "a::static_inv" << std::endl
  17. #endif
  18. #ifndef BOOST_CONTRACT_NO_OLDS
  19. << "a::dtor::old" << std::endl
  20. #endif
  21. << "a::dtor::body" << std::endl
  22. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  23. << "a::static_inv" << std::endl
  24. #endif
  25. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  26. << "a::dtor::post" << std::endl
  27. #endif
  28. ;
  29. return ok.str();
  30. }
  31. std::string ok_b(bool failed = false) {
  32. std::ostringstream ok; ok << "" // Suppress a warning.
  33. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  34. << "b::static_inv" << std::endl
  35. << "b::inv" << std::endl // This can fail.
  36. #endif
  37. ;
  38. if(!failed) ok
  39. #ifndef BOOST_CONTRACT_NO_OLDS
  40. << "b::dtor::old" << std::endl
  41. #endif
  42. << "b::dtor::body" << std::endl
  43. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  44. << "b::static_inv" << std::endl
  45. #endif
  46. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  47. << "b::dtor::post" << std::endl
  48. #endif
  49. ;
  50. return ok.str();
  51. }
  52. std::string ok_c(bool threw = false) {
  53. std::ostringstream ok; ok
  54. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  55. << "c::static_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. // No b::inv here (not even when threw).
  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. a_entry_inv = true;
  75. b_entry_inv = true;
  76. c_entry_inv = true;
  77. {
  78. a aa;
  79. out.str("");
  80. }
  81. ok.str(""); ok // Test nothing failed.
  82. << ok_a()
  83. << ok_b()
  84. << ok_c()
  85. ;
  86. BOOST_TEST(out.eq(ok.str()));
  87. boost::contract::set_entry_invariant_failure([&ok] (boost::contract::from) {
  88. BOOST_TEST(out.eq(ok.str())); // Must check before dtor throws...
  89. throw err(); // for testing (as dtors should never throw anyways).
  90. });
  91. a_entry_inv = false;
  92. b_entry_inv = true;
  93. c_entry_inv = true;
  94. try {
  95. {
  96. a aa;
  97. ok.str("");
  98. out.str("");
  99. }
  100. ok.str(""); ok
  101. << ok_a() // Test no entry a::inv so no failure here.
  102. << ok_b()
  103. << ok_c()
  104. ;
  105. BOOST_TEST(out.eq(ok.str()));
  106. } catch(...) { BOOST_TEST(false); }
  107. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  108. #define BOOST_CONTRACT_TEST_entry_inv 0
  109. #else
  110. #define BOOST_CONTRACT_TEST_entry_inv 2
  111. #endif
  112. a_entry_inv = true;
  113. b_entry_inv = false;
  114. c_entry_inv = true;
  115. try {
  116. {
  117. a aa;
  118. ok.str(""); ok
  119. << ok_a()
  120. // Test entry b::inv failed...
  121. << ok_b(bool(BOOST_CONTRACT_TEST_entry_inv))
  122. ;
  123. out.str("");
  124. }
  125. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  126. BOOST_TEST(false);
  127. } catch(err const&) {
  128. #endif
  129. ok // ...then exec other dtors and check inv on throw (as dtor threw).
  130. << ok_c(bool(BOOST_CONTRACT_TEST_entry_inv))
  131. ;
  132. BOOST_TEST(out.eq(ok.str()));
  133. } catch(...) { BOOST_TEST(false); }
  134. a_entry_inv = true;
  135. b_entry_inv = true;
  136. c_entry_inv = false;
  137. try {
  138. {
  139. a aa;
  140. ok.str("");
  141. out.str("");
  142. }
  143. ok.str(""); ok
  144. << ok_a()
  145. << ok_b()
  146. << ok_c() // Test no entry c::inv so no failure here.
  147. ;
  148. BOOST_TEST(out.eq(ok.str()));
  149. } catch(...) { BOOST_TEST(false); }
  150. boost::contract::set_entry_invariant_failure([] (boost::contract::from) {
  151. // Testing multiple failures so dtors must not throw multiple
  152. // exceptions, just ignore failure and continue test program...
  153. });
  154. a_entry_inv = false;
  155. b_entry_inv = false;
  156. c_entry_inv = false;
  157. {
  158. a aa;
  159. out.str("");
  160. }
  161. ok.str(""); ok
  162. << ok_a() // Test no entry a::inv so no failure here.
  163. // Test entry b::inv failed (as all did).
  164. << ok_b(bool(BOOST_CONTRACT_TEST_entry_inv))
  165. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  166. << "b::dtor::body" << std::endl
  167. #endif
  168. << ok_c() // Test no entry c::inv so no failure here.
  169. ;
  170. BOOST_TEST(out.eq(ok.str()));
  171. #undef BOOST_CONTRACT_TEST_entry_inv
  172. return boost::report_errors();
  173. }