decl_entry_static_inv_mid.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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_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::inv" << std::endl
  27. << "b::static_inv" << std::endl
  28. << "b::inv" << std::endl
  29. << "a::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_static_inv = true;
  51. b_entry_static_inv = true;
  52. c_entry_static_inv = true;
  53. a_entering_static_inv = b_entering_static_inv = c_entering_static_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::inv" << std::endl
  60. << "b::static_inv" << std::endl
  61. << "b::inv" << std::endl
  62. << "a::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_static_inv = false;
  70. b_entry_static_inv = true;
  71. c_entry_static_inv = true;
  72. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =true;
  73. out.str("");
  74. try {
  75. aa.f();
  76. ok.str(""); ok
  77. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  78. << "c::inv" << std::endl
  79. << "b::static_inv" << std::endl
  80. << "b::inv" << std::endl
  81. << "a::inv" << std::endl
  82. #endif
  83. << ok_end()
  84. ;
  85. BOOST_TEST(out.eq(ok.str()));
  86. } catch(...) { BOOST_TEST(false); }
  87. a_entry_static_inv = true;
  88. b_entry_static_inv = false;
  89. c_entry_static_inv = true;
  90. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  91. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  92. out.str("");
  93. try {
  94. aa.f();
  95. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  96. BOOST_TEST(false);
  97. } catch(err const&) {
  98. #endif
  99. ok.str(""); ok
  100. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  101. << "c::inv" << std::endl
  102. << "b::static_inv" << std::endl // Test this fail.
  103. #else
  104. << ok_end()
  105. #endif
  106. ;
  107. BOOST_TEST(out.eq(ok.str()));
  108. } catch(...) { BOOST_TEST(false); }
  109. a_entry_static_inv = true;
  110. b_entry_static_inv = true;
  111. c_entry_static_inv = false;
  112. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =true;
  113. out.str("");
  114. try {
  115. aa.f();
  116. ok.str(""); ok
  117. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  118. << "c::inv" << std::endl
  119. << "b::static_inv" << std::endl
  120. << "b::inv" << std::endl
  121. << "a::inv" << std::endl
  122. #endif
  123. << ok_end()
  124. ;
  125. BOOST_TEST(out.eq(ok.str()));
  126. } catch(...) { BOOST_TEST(false); }
  127. a_entry_static_inv = false;
  128. b_entry_static_inv = false;
  129. c_entry_static_inv = false;
  130. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  131. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  132. out.str("");
  133. try {
  134. aa.f();
  135. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  136. BOOST_TEST(false);
  137. } catch(err const&) {
  138. #endif
  139. ok.str(""); ok
  140. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  141. << "c::inv" << std::endl
  142. // Test this failed (as all did).
  143. << "b::static_inv" << std::endl
  144. #else
  145. << ok_end()
  146. #endif
  147. ;
  148. BOOST_TEST(out.eq(ok.str()));
  149. } catch(...) { BOOST_TEST(false); }
  150. #undef BOOST_CONTRACT_TEST_entry_inv
  151. return boost::report_errors();
  152. }