throwing_body_virtual.cpp 4.2 KB

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