throwing_body.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 body (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. })
  48. {
  49. boost::contract::check c = boost::contract::constructor(this)
  50. .old([] { out << "b::ctor::old" << std::endl; })
  51. .postcondition([] { out << "b::ctor::post" << std::endl; })
  52. .except([] { out << "b::ctor::except" << std::endl; })
  53. ;
  54. out << "b::ctor::body" << std::endl;
  55. throw b_err(); // Test body throws (from inheritance mid branch).
  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. try {
  83. out.str("");
  84. a aa;
  85. BOOST_TEST(false);
  86. } catch(b_err const&) {
  87. ok.str(""); ok
  88. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  89. << "a::ctor::pre" << std::endl
  90. << "b::ctor::pre" << std::endl
  91. << "c::ctor::pre" << std::endl
  92. #endif
  93. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  94. << "c::static_inv" << std::endl
  95. #endif
  96. #ifndef BOOST_CONTRACT_NO_OLDS
  97. << "c::ctor::old" << std::endl
  98. #endif
  99. << "c::ctor::body" << std::endl
  100. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  101. << "c::static_inv" << std::endl
  102. << "c::inv" << std::endl
  103. #endif
  104. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  105. << "c::ctor::post" << std::endl
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  108. << "b::static_inv" << std::endl
  109. #endif
  110. #ifndef BOOST_CONTRACT_NO_OLDS
  111. << "b::ctor::old" << std::endl
  112. #endif
  113. << "b::ctor::body" << std::endl // Test this threw...
  114. // ... so check only following after (no post, no a ctor, etc.).
  115. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  116. << "b::static_inv" << std::endl
  117. #endif
  118. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  119. << "b::ctor::except" << std::endl
  120. #endif
  121. ;
  122. BOOST_TEST(out.eq(ok.str()));
  123. } catch(...) { BOOST_TEST(false); }
  124. return boost::report_errors();
  125. }