decl_post_all.cpp 4.7 KB

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