decl_pre_ends.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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_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. << "a::ctor::pre" << std::endl
  72. << "c::ctor::pre" << std::endl
  73. #endif
  74. << ok_after()
  75. ;
  76. BOOST_TEST(out.eq(ok.str()));
  77. }
  78. boost::contract::set_precondition_failure(
  79. [] (boost::contract::from) { throw err(); });
  80. a_pre = false;
  81. b_pre = true;
  82. c_pre = true;
  83. try {
  84. out.str("");
  85. a aa;
  86. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  87. BOOST_TEST(false);
  88. } catch(err const&) {
  89. #endif
  90. ok.str(""); ok
  91. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  92. << "a::ctor::pre" << std::endl // Test this failed.
  93. #else
  94. << ok_after()
  95. #endif
  96. ;
  97. BOOST_TEST(out.eq(ok.str()));
  98. } catch(...) { BOOST_TEST(false); }
  99. a_pre = true;
  100. b_pre = false;
  101. c_pre = true;
  102. {
  103. out.str("");
  104. a aa;
  105. ok.str(""); ok
  106. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  107. << "a::ctor::pre" << std::endl
  108. // Test no failure here.
  109. << "c::ctor::pre" << std::endl
  110. #endif
  111. << ok_after()
  112. ;
  113. BOOST_TEST(out.eq(ok.str()));
  114. }
  115. a_pre = true;
  116. b_pre = true;
  117. c_pre = false;
  118. try {
  119. out.str("");
  120. a aa;
  121. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  122. BOOST_TEST(false);
  123. } catch(err const&) {
  124. #endif
  125. ok.str(""); ok
  126. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  127. << "a::ctor::pre" << std::endl
  128. << "c::ctor::pre" << std::endl // Test this failed.
  129. #else
  130. << ok_after()
  131. #endif
  132. ;
  133. BOOST_TEST(out.eq(ok.str()));
  134. } catch(...) { BOOST_TEST(false); }
  135. a_pre = false;
  136. b_pre = false;
  137. c_pre = false;
  138. try {
  139. out.str("");
  140. a aa;
  141. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  142. BOOST_TEST(false);
  143. } catch(err const&) {
  144. #endif
  145. ok.str(""); ok
  146. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  147. << "a::ctor::pre" << std::endl // Test this failed (as all did).
  148. #else
  149. << ok_after()
  150. #endif
  151. ;
  152. BOOST_TEST(out.eq(ok.str()));
  153. } catch(...) { BOOST_TEST(false); }
  154. return boost::report_errors();
  155. }