decl_post_ends.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 postconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_A_POST
  7. #define BOOST_CONTRACT_TEST_NO_B_POST
  8. #undef 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. << "c::f::post" << std::endl
  57. << "b::f::old" << std::endl
  58. << "a::f::post" << std::endl
  59. #endif
  60. ;
  61. BOOST_TEST(out.eq(ok.str()));
  62. boost::contract::set_postcondition_failure(
  63. [] (boost::contract::from) { throw err(); });
  64. a_post = false;
  65. b_post = true;
  66. c_post = true;
  67. out.str("");
  68. try {
  69. aa.f();
  70. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  71. BOOST_TEST(false);
  72. } catch(err const&) {
  73. #endif
  74. ok.str(""); ok
  75. << ok_begin()
  76. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  77. << "c::f::old" << std::endl
  78. << "c::f::post" << std::endl
  79. << "b::f::old" << std::endl
  80. << "a::f::post" << std::endl // Test this failed.
  81. #endif
  82. ;
  83. BOOST_TEST(out.eq(ok.str()));
  84. } catch(...) { BOOST_TEST(false); }
  85. a_post = true;
  86. b_post = false;
  87. c_post = true;
  88. out.str("");
  89. try {
  90. aa.f();
  91. ok.str(""); ok
  92. << ok_begin()
  93. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  94. << "c::f::old" << std::endl
  95. << "c::f::post" << std::endl
  96. << "b::f::old" << std::endl
  97. // Test no failure here.
  98. << "a::f::post" << std::endl
  99. #endif
  100. ;
  101. BOOST_TEST(out.eq(ok.str()));
  102. } catch(...) { BOOST_TEST(false); }
  103. a_post = true;
  104. b_post = true;
  105. c_post = false;
  106. out.str("");
  107. try {
  108. aa.f();
  109. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  110. BOOST_TEST(false);
  111. } catch(err const&) {
  112. #endif
  113. ok.str(""); ok
  114. << ok_begin()
  115. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  116. << "c::f::old" << std::endl
  117. << "c::f::post" << std::endl // Test this failed.
  118. #endif
  119. ;
  120. BOOST_TEST(out.eq(ok.str()));
  121. } catch(...) { BOOST_TEST(false); }
  122. a_post = false;
  123. b_post = false;
  124. c_post = false;
  125. out.str("");
  126. try {
  127. aa.f();
  128. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  129. BOOST_TEST(false);
  130. } catch(err const&) {
  131. #endif
  132. ok.str(""); ok
  133. << ok_begin()
  134. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  135. << "c::f::old" << std::endl
  136. << "c::f::post" << std::endl // Test this failed (as all did).
  137. #endif
  138. ;
  139. BOOST_TEST(out.eq(ok.str()));
  140. } catch(...) { BOOST_TEST(false); }
  141. return boost::report_errors();
  142. }