throwing_pre.cpp 5.6 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 throw form constructor .pre() (in middle branch of inheritance tree).
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/constructor.hpp>
  8. #include <boost/contract/base_types.hpp>
  9. #include <boost/contract/check.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. boost::contract::test::detail::oteststream out;
  13. struct c
  14. #define BASES private boost::contract::constructor_precondition<c>
  15. : BASES
  16. {
  17. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  18. #undef BASES
  19. static void static_invariant() { out << "c::static_inv" << std::endl; }
  20. void invariant() const { out << "c::inv" << std::endl; }
  21. c() :
  22. boost::contract::constructor_precondition<c>([] {
  23. out << "c::ctor::pre" << std::endl;
  24. })
  25. {
  26. boost::contract::check c = boost::contract::constructor(this)
  27. .old([] { out << "c::ctor::old" << std::endl; })
  28. .postcondition([] { out << "c::ctor::post" << std::endl; })
  29. .except([] { out << "c::ctor::except" << std::endl; })
  30. ;
  31. out << "c::ctor::body" << std::endl;
  32. // Do not throw (from inheritance root).
  33. }
  34. };
  35. struct b_err {}; // Global decl so visible in MSVC10 lambdas.
  36. struct b
  37. #define BASES private boost::contract::constructor_precondition<b>, public c
  38. : BASES
  39. {
  40. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  41. #undef BASES
  42. static void static_invariant() { out << "b::static_inv" << std::endl; }
  43. void invariant() const { out << "b::inv" << std::endl; }
  44. b() :
  45. boost::contract::constructor_precondition<b>([] {
  46. out << "b::ctor::pre" << std::endl;
  47. throw b_err(); // Test this throws (from mid branch).
  48. })
  49. {
  50. boost::contract::check c = boost::contract::constructor(this)
  51. .old([] { out << "b::ctor::old" << std::endl; })
  52. .postcondition([] { out << "b::ctor::post" << std::endl; })
  53. .except([] { out << "b::ctor::except" << std::endl; })
  54. ;
  55. out << "b::ctor::body" << std::endl;
  56. }
  57. };
  58. struct a
  59. #define BASES private boost::contract::constructor_precondition<a>, public b
  60. : BASES
  61. {
  62. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  63. #undef BASES
  64. static void static_invariant() { out << "a::static_inv" << std::endl; }
  65. void invariant() const { out << "a::inv" << std::endl; }
  66. a() :
  67. boost::contract::constructor_precondition<a>([] {
  68. out << "a::ctor::pre" << std::endl;
  69. })
  70. {
  71. boost::contract::check c = boost::contract::constructor(this)
  72. .old([] { out << "a::ctor::old" << std::endl; })
  73. .postcondition([] { out << "a::ctor::post" << std::endl; })
  74. .except([] { out << "a::ctor::except" << std::endl; })
  75. ;
  76. out << "a::ctor::body" << std::endl;
  77. // Do not throw (from inheritance leaf).
  78. }
  79. };
  80. int main() {
  81. std::ostringstream ok;
  82. boost::contract::set_precondition_failure(
  83. [] (boost::contract::from) { throw; });
  84. try {
  85. out.str("");
  86. a aa;
  87. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  88. BOOST_TEST(false);
  89. } catch(b_err const&) {
  90. #endif
  91. ok.str(""); ok
  92. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  93. << "a::ctor::pre" << std::endl
  94. << "b::ctor::pre" << std::endl // Test this threw.
  95. #else
  96. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  97. << "c::static_inv" << std::endl
  98. #endif
  99. #ifndef BOOST_CONTRACT_NO_OLDS
  100. << "c::ctor::old" << std::endl
  101. #endif
  102. << "c::ctor::body" << std::endl
  103. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  104. << "c::static_inv" << std::endl
  105. << "c::inv" << std::endl
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  108. << "c::ctor::post" << std::endl
  109. #endif
  110. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  111. << "b::static_inv" << std::endl
  112. #endif
  113. #ifndef BOOST_CONTRACT_NO_OLDS
  114. << "b::ctor::old" << std::endl
  115. #endif
  116. << "b::ctor::body" << std::endl
  117. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  118. << "b::static_inv" << std::endl
  119. << "b::inv" << std::endl
  120. #endif
  121. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  122. << "b::ctor::post" << std::endl
  123. #endif
  124. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  125. << "a::static_inv" << std::endl
  126. #endif
  127. #ifndef BOOST_CONTRACT_NO_OLDS
  128. << "a::ctor::old" << std::endl
  129. #endif
  130. << "a::ctor::body" << std::endl
  131. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  132. << "a::static_inv" << std::endl
  133. << "a::inv" << std::endl
  134. #endif
  135. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  136. << "a::ctor::post" << std::endl
  137. #endif
  138. #endif
  139. ;
  140. BOOST_TEST(out.eq(ok.str()));
  141. } catch(...) { BOOST_TEST(false); }
  142. return boost::report_errors();
  143. }