decl_exit_static_inv_all.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #undef 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_ENTRY_INVARIANTS
  43. << "b::static_inv" << std::endl
  44. #endif
  45. #ifndef BOOST_CONTRACT_NO_OLDS
  46. << "b::ctor::old" << std::endl
  47. #endif
  48. << "b::ctor::body" << std::endl
  49. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  50. << "b::static_inv" << std::endl // This can fail.
  51. #endif
  52. ;
  53. return ok.str();
  54. }
  55. std::string ok_a() {
  56. std::ostringstream ok; ok
  57. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  58. << "b::inv" << std::endl
  59. #endif
  60. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  61. << "b::ctor::post" << std::endl
  62. #endif
  63. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  64. << "a::static_inv" << std::endl
  65. #endif
  66. #ifndef BOOST_CONTRACT_NO_OLDS
  67. << "a::ctor::old" << std::endl
  68. #endif
  69. << "a::ctor::body" << std::endl
  70. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  71. << "a::static_inv" << std::endl // This can fail.
  72. #endif
  73. ;
  74. return ok.str();
  75. }
  76. std::string ok_end() {
  77. std::ostringstream ok; ok << "" // Suppress a warning.
  78. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  79. << "a::inv" << std::endl
  80. #endif
  81. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  82. << "a::ctor::post" << std::endl
  83. #endif
  84. ;
  85. return ok.str();
  86. }
  87. struct err {}; // Global decl so visible in MSVC10 lambdas.
  88. int main() {
  89. std::ostringstream ok;
  90. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  91. #define BOOST_CONTRACT_TEST_entry_inv 0
  92. #else
  93. #define BOOST_CONTRACT_TEST_entry_inv 1
  94. #endif
  95. a_exit_static_inv = true;
  96. b_exit_static_inv = true;
  97. c_exit_static_inv = true;
  98. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  99. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  100. {
  101. out.str("");
  102. a aa;
  103. ok.str(""); ok // Test nothing failed.
  104. << ok_c()
  105. << ok_b()
  106. << ok_a()
  107. << ok_end()
  108. ;
  109. BOOST_TEST(out.eq(ok.str()));
  110. }
  111. boost::contract::set_exit_invariant_failure(
  112. [] (boost::contract::from) { throw err(); });
  113. a_exit_static_inv = false;
  114. b_exit_static_inv = true;
  115. c_exit_static_inv = true;
  116. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  117. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  118. try {
  119. out.str("");
  120. a aa;
  121. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  122. BOOST_TEST(false);
  123. } catch(err const&) {
  124. #endif
  125. ok.str(""); ok
  126. << ok_c()
  127. << ok_b()
  128. << ok_a() // Test exit a::static_inv failed.
  129. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  130. << ok_end()
  131. #endif
  132. ;
  133. BOOST_TEST(out.eq(ok.str()));
  134. } catch(...) { BOOST_TEST(false); }
  135. a_exit_static_inv = true;
  136. b_exit_static_inv = false;
  137. c_exit_static_inv = true;
  138. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  139. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  140. try {
  141. out.str("");
  142. a aa;
  143. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  144. BOOST_TEST(false);
  145. } catch(err const&) {
  146. #endif
  147. ok.str(""); ok
  148. << ok_c()
  149. << ok_b() // Test exit b::static_inv failed.
  150. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  151. << ok_a()
  152. << ok_end()
  153. #endif
  154. ;
  155. BOOST_TEST(out.eq(ok.str()));
  156. } catch(...) { BOOST_TEST(false); }
  157. a_exit_static_inv = true;
  158. b_exit_static_inv = true;
  159. c_exit_static_inv = false;
  160. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  161. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, 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 exit c::static_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. a_exit_static_inv = false;
  180. b_exit_static_inv = false;
  181. c_exit_static_inv = false;
  182. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  183. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  184. try {
  185. out.str("");
  186. a aa;
  187. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  188. BOOST_TEST(false);
  189. } catch(err const&) {
  190. #endif
  191. ok.str(""); ok
  192. << ok_c() // Test exit c::static_inv failed (as all did).
  193. #ifdef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  194. << ok_b()
  195. << ok_a()
  196. << ok_end()
  197. #endif
  198. ;
  199. BOOST_TEST(out.eq(ok.str()));
  200. } catch(...) { BOOST_TEST(false); }
  201. return boost::report_errors();
  202. }