throwing_body.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 public member function body throws with subcontracting.
  6. #include "smoke.hpp"
  7. #include <boost/optional.hpp>
  8. #include <boost/preprocessor/control/iif.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. int main() {
  12. std::ostringstream ok;
  13. a aa; // Test call to derived out-most leaf.
  14. s_type s; s.value = "X"; // So body will throw.
  15. out.str("");
  16. boost::optional<result_type&> r;
  17. try {
  18. r = aa.f(s);
  19. BOOST_TEST(false);
  20. } catch(except_error const&) {
  21. ok.str(""); ok
  22. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  23. << "d::static_inv" << std::endl
  24. << "d::inv" << std::endl
  25. << "e::static_inv" << std::endl
  26. << "e::inv" << std::endl
  27. << "c::static_inv" << std::endl
  28. << "c::inv" << std::endl
  29. << "a::static_inv" << std::endl
  30. << "a::inv" << std::endl
  31. #endif
  32. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  33. << "d::f::pre" << std::endl
  34. #endif
  35. #ifndef BOOST_CONTRACT_NO_OLDS
  36. << "d::f::old" << std::endl
  37. << "e::f::old" << std::endl
  38. << "c::f::old" << std::endl
  39. << "a::f::old" << std::endl
  40. #endif
  41. << "a::f::body" << std::endl
  42. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  43. << "d::static_inv" << std::endl
  44. << "d::inv" << std::endl
  45. << "e::static_inv" << std::endl
  46. << "e::inv" << std::endl
  47. << "c::static_inv" << std::endl
  48. << "c::inv" << std::endl
  49. << "a::static_inv" << std::endl
  50. << "a::inv" << std::endl
  51. #endif
  52. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  53. << "d::f::old" << std::endl
  54. << "d::f::except" << std::endl
  55. << "e::f::old" << std::endl
  56. << "e::f::except" << std::endl
  57. << "c::f::old" << std::endl
  58. << "c::f::except" << std::endl
  59. // No old call here because not a base object.
  60. << "a::f::except" << std::endl
  61. #endif
  62. ;
  63. BOOST_TEST(out.eq(ok.str()));
  64. #ifndef BOOST_CONTRACT_NO_OLDS
  65. #define BOOST_CONTRACT_TEST_old 1u
  66. #else
  67. #define BOOST_CONTRACT_TEST_old 0u
  68. #endif
  69. BOOST_TEST(!r); // Boost.Optional result not init (as body threw).
  70. BOOST_TEST_EQ(s.value, "X");
  71. BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 4);
  72. BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 4);
  73. BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 for local var.
  74. BOOST_TEST_EQ(aa.x.value, "a");
  75. BOOST_TEST_EQ(aa.x.copies(), BOOST_CONTRACT_TEST_old);
  76. BOOST_TEST_EQ(aa.x.evals(), BOOST_CONTRACT_TEST_old);
  77. BOOST_TEST_EQ(aa.x.ctors(), aa.x.dtors() + 1); // 1 for member var.
  78. BOOST_TEST_EQ(aa.y.value, "c");
  79. BOOST_TEST_EQ(aa.y.copies(), BOOST_CONTRACT_TEST_old);
  80. BOOST_TEST_EQ(aa.y.evals(), BOOST_CONTRACT_TEST_old);
  81. BOOST_TEST_EQ(aa.y.ctors(), aa.y.dtors() + 1); // 1 for member var.
  82. BOOST_TEST_EQ(aa.t<'d'>::z.value, "d");
  83. BOOST_TEST_EQ(aa.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
  84. BOOST_TEST_EQ(aa.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
  85. BOOST_TEST_EQ(aa.t<'d'>::z.ctors(), aa.t<'d'>::z.dtors() + 1); // 1 mem.
  86. BOOST_TEST_EQ(aa.t<'e'>::z.value, "e");
  87. BOOST_TEST_EQ(aa.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
  88. BOOST_TEST_EQ(aa.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
  89. BOOST_TEST_EQ(aa.t<'e'>::z.ctors(), aa.t<'e'>::z.dtors() + 1); // 1 mem.
  90. #undef BOOST_CONTRACT_TEST_old
  91. }
  92. return boost::report_errors();
  93. }