decl_post_mid.cpp 4.2 KB

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