decl_entry_inv_ends.cpp 5.5 KB

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