decl_post_mid.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 postconditions.
  6. #define BOOST_CONTRACT_TEST_NO_A_POST
  7. #undef BOOST_CONTRACT_TEST_NO_B_POST
  8. #define BOOST_CONTRACT_TEST_NO_C_POST
  9. #include "decl.hpp"
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. #include <string>
  13. std::string ok_begin() {
  14. std::ostringstream ok; ok
  15. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  16. << "c::static_inv" << std::endl
  17. << "c::inv" << std::endl
  18. << "b::static_inv" << std::endl
  19. << "b::inv" << std::endl
  20. << "a::static_inv" << std::endl
  21. << "a::inv" << std::endl
  22. #endif
  23. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  24. << "c::f::pre" << std::endl
  25. #endif
  26. #ifndef BOOST_CONTRACT_NO_OLDS
  27. << "c::f::old" << std::endl
  28. << "b::f::old" << std::endl
  29. << "a::f::old" << std::endl
  30. #endif
  31. << "a::f::body" << std::endl
  32. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  33. << "c::static_inv" << std::endl
  34. << "c::inv" << std::endl
  35. << "b::static_inv" << std::endl
  36. << "b::inv" << std::endl
  37. << "a::static_inv" << std::endl
  38. << "a::inv" << std::endl
  39. #endif
  40. ;
  41. return ok.str();
  42. }
  43. struct err {}; // Global decl so visible in MSVC10 lambdas.
  44. int main() {
  45. std::ostringstream ok;
  46. a aa;
  47. a_post = true;
  48. b_post = true;
  49. c_post = true;
  50. out.str("");
  51. aa.f();
  52. ok.str(""); ok // Test nothing failed.
  53. << ok_begin()
  54. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  55. << "c::f::old" << std::endl
  56. << "b::f::old" << std::endl
  57. << "b::f::post" << std::endl
  58. #endif
  59. ;
  60. BOOST_TEST(out.eq(ok.str()));
  61. boost::contract::set_postcondition_failure(
  62. [] (boost::contract::from) { throw err(); });
  63. a_post = false;
  64. b_post = true;
  65. c_post = true;
  66. out.str("");
  67. try {
  68. aa.f();
  69. ok.str(""); ok
  70. << ok_begin()
  71. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  72. << "c::f::old" << std::endl
  73. << "b::f::old" << std::endl
  74. << "b::f::post" << std::endl
  75. // Test no failure here.
  76. #endif
  77. ;
  78. BOOST_TEST(out.eq(ok.str()));
  79. } catch(...) { BOOST_TEST(false); }
  80. a_post = true;
  81. b_post = false;
  82. c_post = true;
  83. out.str("");
  84. try {
  85. aa.f();
  86. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  87. BOOST_TEST(false);
  88. } catch(err const&) {
  89. #endif
  90. ok.str(""); ok
  91. << ok_begin()
  92. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  93. << "c::f::old" << std::endl
  94. << "b::f::old" << std::endl
  95. << "b::f::post" << std::endl // Test this failed.
  96. #endif
  97. ;
  98. BOOST_TEST(out.eq(ok.str()));
  99. } catch(...) { BOOST_TEST(false); }
  100. a_post = true;
  101. b_post = true;
  102. c_post = false;
  103. out.str("");
  104. try {
  105. aa.f();
  106. ok.str(""); ok
  107. << ok_begin()
  108. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  109. << "c::f::old" << std::endl
  110. // Test no failure here.
  111. << "b::f::old" << std::endl
  112. << "b::f::post" << std::endl
  113. #endif
  114. ;
  115. BOOST_TEST(out.eq(ok.str()));
  116. } catch(...) { BOOST_TEST(false); }
  117. a_post = false;
  118. b_post = false;
  119. c_post = false;
  120. out.str("");
  121. try {
  122. aa.f();
  123. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  124. BOOST_TEST(false);
  125. } catch(err const&) {
  126. #endif
  127. ok.str(""); ok
  128. << ok_begin()
  129. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  130. << "c::f::old" << std::endl
  131. << "b::f::old" << std::endl
  132. << "b::f::post" << std::endl // Test this failed (as all did).
  133. #endif
  134. ;
  135. BOOST_TEST(out.eq(ok.str()));
  136. } catch(...) { BOOST_TEST(false); }
  137. return boost::report_errors();
  138. }