decl_entry_inv_mid.cpp 5.2 KB

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