decl_entry_static_inv_mid.cpp 5.8 KB

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