decl_pre_mid.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 preconditions.
  6. #define BOOST_CONTRACT_TEST_NO_A_PRE
  7. #undef BOOST_CONTRACT_TEST_NO_B_PRE
  8. #define BOOST_CONTRACT_TEST_NO_C_PRE
  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 << "" // Suppress a warning.
  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. ;
  24. return ok.str();
  25. }
  26. std::string ok_end() {
  27. std::ostringstream ok; ok
  28. #ifndef BOOST_CONTRACT_NO_OLDS
  29. << "c::f::old" << std::endl
  30. << "b::f::old" << std::endl
  31. << "a::f::old" << std::endl
  32. #endif
  33. << "a::f::body" << std::endl
  34. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  35. << "c::static_inv" << std::endl
  36. << "c::inv" << std::endl
  37. << "b::static_inv" << std::endl
  38. << "b::inv" << std::endl
  39. << "a::static_inv" << std::endl
  40. << "a::inv" << std::endl
  41. #endif
  42. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  43. << "c::f::old" << std::endl // Old only if post (or except) run.
  44. << "c::f::post" << std::endl
  45. << "b::f::old" << std::endl
  46. << "b::f::post" << std::endl
  47. << "a::f::post" << std::endl
  48. #endif
  49. ;
  50. return ok.str();
  51. }
  52. struct err {}; // Global decl so visible in MSVC10 lambdas.
  53. int main() {
  54. std::ostringstream ok;
  55. a aa;
  56. a_pre = true;
  57. b_pre = true;
  58. c_pre = true;
  59. out.str("");
  60. aa.f();
  61. ok.str(""); ok // Test nothing failed.
  62. << ok_begin()
  63. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  64. << "b::f::pre" << std::endl
  65. #endif
  66. << ok_end()
  67. ;
  68. BOOST_TEST(out.eq(ok.str()));
  69. a_pre = false;
  70. b_pre = true;
  71. c_pre = false;
  72. out.str("");
  73. aa.f();
  74. ok.str(""); ok
  75. << ok_begin()
  76. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  77. // Test only middle pre checked and no fail.
  78. << "b::f::pre" << std::endl
  79. #endif
  80. << ok_end()
  81. ;
  82. BOOST_TEST(out.eq(ok.str()));
  83. boost::contract::set_precondition_failure(
  84. [] (boost::contract::from) { throw err(); });
  85. a_pre = true;
  86. b_pre = false;
  87. c_pre = false;
  88. out.str("");
  89. try {
  90. aa.f();
  91. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  92. BOOST_TEST(false);
  93. } catch(err const&) {
  94. #endif
  95. ok.str(""); ok
  96. << ok_begin()
  97. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  98. // Test only middle pre checked and failed.
  99. << "b::f::pre" << std::endl
  100. #else
  101. << ok_end()
  102. #endif
  103. ;
  104. BOOST_TEST(out.eq(ok.str()));
  105. } catch(...) { BOOST_TEST(false); }
  106. a_pre = false;
  107. b_pre = false;
  108. c_pre = true;
  109. out.str("");
  110. try {
  111. aa.f();
  112. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  113. BOOST_TEST(false);
  114. } catch(err const&) {
  115. #endif
  116. ok.str(""); ok
  117. << ok_begin()
  118. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  119. // Test only middle pre checked and failed.
  120. << "b::f::pre" << std::endl
  121. #else
  122. << ok_end()
  123. #endif
  124. ;
  125. BOOST_TEST(out.eq(ok.str()));
  126. } catch(...) { BOOST_TEST(false); }
  127. a_pre = false;
  128. b_pre = false;
  129. c_pre = false;
  130. out.str("");
  131. try {
  132. aa.f();
  133. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  134. BOOST_TEST(false);
  135. } catch(err const&) {
  136. #endif
  137. ok.str(""); ok
  138. << ok_begin()
  139. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  140. // Test only middle pre checked and failed.
  141. << "b::f::pre" << std::endl
  142. #else
  143. << ok_end()
  144. #endif
  145. ;
  146. BOOST_TEST(out.eq(ok.str()));
  147. } catch(...) { BOOST_TEST(false); }
  148. return boost::report_errors();
  149. }