decl_pre_ends.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 preconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_A_PRE
  7. #define BOOST_CONTRACT_TEST_NO_B_PRE
  8. #undef 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. << "c::f::pre" << std::endl // Test only c pre checked.
  65. #endif
  66. << ok_end()
  67. ;
  68. BOOST_TEST(out.eq(ok.str()));
  69. a_pre = true;
  70. b_pre = false;
  71. c_pre = false;
  72. out.str("");
  73. aa.f();
  74. ok.str(""); ok
  75. << ok_begin()
  76. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  77. << "c::f::pre" << std::endl
  78. // Test b's pre not checked.
  79. << "a::f::pre" << std::endl
  80. #endif
  81. << ok_end()
  82. ;
  83. BOOST_TEST(out.eq(ok.str()));
  84. a_pre = false;
  85. b_pre = false;
  86. c_pre = true;
  87. out.str("");
  88. aa.f();
  89. ok.str(""); ok
  90. << ok_begin()
  91. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  92. << "c::f::pre" << std::endl // Test only c pre checked.
  93. #endif
  94. << ok_end()
  95. ;
  96. BOOST_TEST(out.eq(ok.str()));
  97. boost::contract::set_precondition_failure(
  98. [] (boost::contract::from) { throw err(); });
  99. a_pre = false;
  100. b_pre = true;
  101. c_pre = false;
  102. out.str("");
  103. try {
  104. aa.f();
  105. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  106. BOOST_TEST(false);
  107. } catch(err const&) {
  108. #endif
  109. ok.str(""); ok
  110. << ok_begin()
  111. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  112. << "c::f::pre" << std::endl
  113. << "a::f::pre" << std::endl // Only ends pre checked and failed.
  114. #else
  115. << ok_end()
  116. #endif
  117. ;
  118. BOOST_TEST(out.eq(ok.str()));
  119. } catch(...) { BOOST_TEST(false); }
  120. a_pre = false;
  121. b_pre = false;
  122. c_pre = false;
  123. out.str("");
  124. try {
  125. aa.f();
  126. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  127. BOOST_TEST(false);
  128. } catch(err const&) {
  129. #endif
  130. ok.str(""); ok
  131. << ok_begin()
  132. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  133. << "c::f::pre" << std::endl
  134. << "a::f::pre" << std::endl // Only ends pre checked and failed.
  135. #else
  136. << ok_end()
  137. #endif
  138. ;
  139. BOOST_TEST(out.eq(ok.str()));
  140. } catch(...) { BOOST_TEST(false); }
  141. return boost::report_errors();
  142. }