decl_post_ends.cpp 4.5 KB

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