decl_exit_static_inv_ends.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 grandparent classes (ends) with exit static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #define BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #undef 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_c() {
  15. std::ostringstream ok; ok << "" // Suppress a warning.
  16. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  17. << "a::ctor::pre" << std::endl
  18. << "b::ctor::pre" << std::endl
  19. << "c::ctor::pre" << std::endl
  20. #endif
  21. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  22. << "c::static_inv" << std::endl
  23. #endif
  24. #ifndef BOOST_CONTRACT_NO_OLDS
  25. << "c::ctor::old" << std::endl
  26. #endif
  27. << "c::ctor::body" << std::endl
  28. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  29. << "c::static_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_EXIT_INVARIANTS
  37. << "c::inv" << std::endl
  38. #endif
  39. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  40. << "c::ctor::post" << 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. ;
  47. return ok.str();
  48. }
  49. std::string ok_a() {
  50. std::ostringstream ok; ok
  51. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  52. << "b::inv" << std::endl
  53. #endif
  54. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  55. << "b::ctor::post" << std::endl
  56. #endif
  57. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  58. << "a::static_inv" << std::endl
  59. #endif
  60. #ifndef BOOST_CONTRACT_NO_OLDS
  61. << "a::ctor::old" << std::endl
  62. #endif
  63. << "a::ctor::body" << std::endl
  64. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  65. << "a::static_inv" << std::endl
  66. #endif
  67. ;
  68. return ok.str();
  69. }
  70. std::string ok_end() {
  71. std::ostringstream ok; ok << "" // Suppress a warning.
  72. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  73. << "a::inv" << std::endl
  74. #endif
  75. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  76. << "a::ctor::post" << std::endl
  77. #endif
  78. ;
  79. return ok.str();
  80. }
  81. struct err {}; // Global decl so visible in MSVC10 lambdas.
  82. int main() {
  83. std::ostringstream ok;
  84. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  85. #define BOOST_CONTRACT_TEST_entry_inv 0
  86. #else
  87. #define BOOST_CONTRACT_TEST_entry_inv 1
  88. #endif
  89. a_exit_static_inv = true;
  90. b_exit_static_inv = true;
  91. c_exit_static_inv = true;
  92. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  93. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  94. {
  95. out.str("");
  96. a aa;
  97. ok.str(""); ok // Test nothing failed.
  98. << ok_c()
  99. << ok_b()
  100. << ok_a()
  101. << ok_end()
  102. ;
  103. BOOST_TEST(out.eq(ok.str()));
  104. }
  105. boost::contract::set_exit_invariant_failure(
  106. [] (boost::contract::from) { throw err(); });
  107. a_exit_static_inv = false;
  108. b_exit_static_inv = true;
  109. c_exit_static_inv = true;
  110. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  111. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  112. try {
  113. out.str("");
  114. a aa;
  115. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  116. BOOST_TEST(false);
  117. } catch(err const&) {
  118. #endif
  119. ok.str(""); ok
  120. << ok_c()
  121. << ok_b()
  122. << ok_a() // Test a::static_inv failed.
  123. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  124. << ok_end()
  125. #endif
  126. ;
  127. BOOST_TEST(out.eq(ok.str()));
  128. } catch(...) { BOOST_TEST(false); }
  129. a_exit_static_inv = true;
  130. b_exit_static_inv = false;
  131. c_exit_static_inv = true;
  132. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  133. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  134. try {
  135. out.str("");
  136. a aa;
  137. ok.str(""); ok
  138. << ok_c()
  139. << ok_b()
  140. << ok_a()
  141. << ok_end()
  142. ;
  143. BOOST_TEST(out.eq(ok.str()));
  144. } catch(...) { BOOST_TEST(false); }
  145. a_exit_static_inv = true;
  146. b_exit_static_inv = true;
  147. c_exit_static_inv = false;
  148. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  149. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  150. try {
  151. out.str("");
  152. a aa;
  153. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  154. BOOST_TEST(false);
  155. } catch(err const&) {
  156. #endif
  157. ok.str(""); ok
  158. << ok_c() // Test c::static_inv failed (as all did).
  159. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  160. << ok_b()
  161. << ok_a()
  162. << ok_end()
  163. #endif
  164. ;
  165. BOOST_TEST(out.eq(ok.str()));
  166. } catch(...) { BOOST_TEST(false); }
  167. a_exit_static_inv = false;
  168. b_exit_static_inv = false;
  169. c_exit_static_inv = false;
  170. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  171. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  172. try {
  173. out.str("");
  174. a aa;
  175. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  176. BOOST_TEST(false);
  177. } catch(err const&) {
  178. #endif
  179. ok.str(""); ok
  180. << ok_c() // Test c::static_inv failed (as all did).
  181. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  182. << ok_b()
  183. << ok_a()
  184. << ok_end()
  185. #endif
  186. ;
  187. BOOST_TEST(out.eq(ok.str()));
  188. } catch(...) { BOOST_TEST(false); }
  189. #undef BOOST_CONTRACT_TEST_entry_inv
  190. return boost::report_errors();
  191. }