decl_post_all.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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_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_b() {
  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. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  51. << "b::ctor::post" << std::endl
  52. #endif
  53. ;
  54. return ok.str();
  55. }
  56. std::string ok_a() {
  57. std::ostringstream ok; ok
  58. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  59. << "a::static_inv" << std::endl
  60. #endif
  61. #ifndef BOOST_CONTRACT_NO_OLDS
  62. << "a::ctor::old" << std::endl
  63. #endif
  64. << "a::ctor::body" << std::endl
  65. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  66. << "a::static_inv" << std::endl
  67. << "a::inv" << std::endl
  68. #endif
  69. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  70. << "a::ctor::post" << std::endl
  71. #endif
  72. ;
  73. return ok.str();
  74. }
  75. struct err {}; // Global decl so visible in MSVC10 lambdas.
  76. int main() {
  77. std::ostringstream ok;
  78. a_post = true;
  79. b_post = true;
  80. c_post = true;
  81. out.str("");
  82. {
  83. a aa;
  84. ok.str(""); ok // Test nothing failed.
  85. << ok_c()
  86. << ok_b()
  87. << ok_a()
  88. ;
  89. BOOST_TEST(out.eq(ok.str()));
  90. }
  91. boost::contract::set_postcondition_failure(
  92. [] (boost::contract::from) { throw err(); });
  93. a_post = false;
  94. b_post = true;
  95. c_post = true;
  96. out.str("");
  97. try {
  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_c()
  105. << ok_b()
  106. << ok_a()
  107. ;
  108. BOOST_TEST(out.eq(ok.str()));
  109. } catch(...) { BOOST_TEST(false); }
  110. a_post = true;
  111. b_post = false;
  112. c_post = true;
  113. out.str("");
  114. try {
  115. a aa;
  116. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  117. BOOST_TEST(false);
  118. } catch(err const&) {
  119. #endif
  120. ok.str(""); ok
  121. << ok_c()
  122. << ok_b() // Test b::ctor::post failed.
  123. #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
  124. << ok_a()
  125. #endif
  126. ;
  127. BOOST_TEST(out.eq(ok.str()));
  128. } catch(...) { BOOST_TEST(false); }
  129. a_post = true;
  130. b_post = true;
  131. c_post = false;
  132. out.str("");
  133. try {
  134. a aa;
  135. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  136. BOOST_TEST(false);
  137. } catch(err const&) {
  138. #endif
  139. ok.str(""); ok
  140. << ok_c() // Test c::ctor::post failed.
  141. #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
  142. << ok_b()
  143. << ok_a()
  144. #endif
  145. ;
  146. BOOST_TEST(out.eq(ok.str()));
  147. } catch(...) { BOOST_TEST(false); }
  148. a_post = false;
  149. b_post = false;
  150. c_post = false;
  151. out.str("");
  152. try {
  153. a aa;
  154. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  155. BOOST_TEST(false);
  156. } catch(err const&) {
  157. #endif
  158. ok.str(""); ok
  159. << ok_c() // Test c::ctor::post failed (as all did)
  160. #ifdef BOOST_CONTRACT_NO_POSTCONDITIONS
  161. << ok_b()
  162. << ok_a()
  163. #endif
  164. ;
  165. BOOST_TEST(out.eq(ok.str()));
  166. } catch(...) { BOOST_TEST(false); }
  167. return boost::report_errors();
  168. }