decl_entry_static_inv_ends.cpp 6.1 KB

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