decl_pre_mid.cpp 4.4 KB

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