throwing_old.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 .old() (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([] {
  51. out << "b::ctor::old" << std::endl;
  52. throw b_err(); // Test this throws (from mid branch).
  53. })
  54. .postcondition([] { out << "b::ctor::post" << std::endl; })
  55. .except([] { out << "b::ctor::except" << std::endl; })
  56. ;
  57. out << "b::ctor::body" << std::endl;
  58. }
  59. };
  60. struct a
  61. #define BASES private boost::contract::constructor_precondition<a>, public b
  62. : BASES
  63. {
  64. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  65. #undef BASES
  66. static void static_invariant() { out << "a::static_inv" << std::endl; }
  67. void invariant() const { out << "a::inv" << std::endl; }
  68. a() :
  69. boost::contract::constructor_precondition<a>([] {
  70. out << "a::ctor::pre" << std::endl;
  71. })
  72. {
  73. boost::contract::check c = boost::contract::constructor(this)
  74. .old([] { out << "a::ctor::old" << std::endl; })
  75. .postcondition([] { out << "a::ctor::post" << std::endl; })
  76. .except([] { out << "a::ctor::except" << std::endl; })
  77. ;
  78. out << "a::ctor::body" << std::endl;
  79. // Do not throw (from inheritance leaf).
  80. }
  81. };
  82. int main() {
  83. std::ostringstream ok;
  84. boost::contract::set_old_failure([] (boost::contract::from) { throw; });
  85. try {
  86. out.str("");
  87. a aa;
  88. #ifndef BOOST_CONTRACT_NO_OLDS
  89. BOOST_TEST(false);
  90. } catch(b_err const&) {
  91. #endif
  92. ok.str(""); ok
  93. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  94. << "a::ctor::pre" << std::endl
  95. << "b::ctor::pre" << std::endl
  96. << "c::ctor::pre" << std::endl
  97. #endif
  98. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  99. << "c::static_inv" << std::endl
  100. #endif
  101. #ifndef BOOST_CONTRACT_NO_OLDS
  102. << "c::ctor::old" << std::endl
  103. #endif
  104. << "c::ctor::body" << std::endl
  105. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  106. << "c::static_inv" << std::endl
  107. << "c::inv" << std::endl
  108. #endif
  109. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  110. << "c::ctor::post" << std::endl
  111. #endif
  112. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  113. << "b::static_inv" << std::endl
  114. #endif
  115. #ifndef BOOST_CONTRACT_NO_OLDS
  116. << "b::ctor::old" << std::endl // Test this threw.
  117. #else
  118. << "b::ctor::body" << std::endl
  119. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  120. << "b::static_inv" << std::endl
  121. << "b::inv" << std::endl
  122. #endif
  123. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  124. << "a::static_inv" << std::endl
  125. #endif
  126. << "a::ctor::body" << std::endl
  127. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  128. << "a::static_inv" << std::endl
  129. << "a::inv" << std::endl
  130. #endif
  131. #endif
  132. ;
  133. BOOST_TEST(out.eq(ok.str()));
  134. } catch(...) { BOOST_TEST(false); }
  135. return boost::report_errors();
  136. }