decl_pre_all.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 preconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_A_PRE
  7. #undef 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
  62. << ok_begin()
  63. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  64. << "c::f::pre" << std::endl // Test only c::f::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. << "b::f::pre" << std::endl
  79. << "a::f::pre" << std::endl // Test all pre checked.
  80. #endif
  81. << ok_end()
  82. ;
  83. BOOST_TEST(out.eq(ok.str()));
  84. a_pre = false;
  85. b_pre = true;
  86. c_pre = false;
  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
  93. << "b::f::pre" << std::endl
  94. // Test only a::f::pre not checked.
  95. #endif
  96. << ok_end()
  97. ;
  98. BOOST_TEST(out.eq(ok.str()));
  99. a_pre = false;
  100. b_pre = false;
  101. c_pre = true;
  102. out.str("");
  103. aa.f();
  104. ok.str(""); ok
  105. << ok_begin()
  106. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  107. << "c::f::pre" << std::endl // Test only c::f::pre checked.
  108. #endif
  109. << ok_end()
  110. ;
  111. BOOST_TEST(out.eq(ok.str()));
  112. boost::contract::set_precondition_failure(
  113. [] (boost::contract::from) { throw err(); });
  114. a_pre = false;
  115. b_pre = false;
  116. c_pre = false;
  117. out.str("");
  118. try {
  119. aa.f();
  120. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  121. BOOST_TEST(false);
  122. } catch(err const&) {
  123. #endif
  124. ok.str(""); ok
  125. << ok_begin()
  126. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  127. << "c::f::pre" << std::endl
  128. << "b::f::pre" << std::endl
  129. << "a::f::pre" << std::endl // Test all pre checked and failed.
  130. #else
  131. << ok_end()
  132. #endif
  133. ;
  134. BOOST_TEST(out.eq(ok.str()));
  135. } catch(...) { BOOST_TEST(false); }
  136. return boost::report_errors();
  137. }