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