virtual_branch.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 function subcontracting from middle branch of inheritance tree.
  6. #include "smoke.hpp"
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <sstream>
  9. int main() {
  10. std::ostringstream ok;
  11. c cc; // Test call to class at mid- inheritance tree (a base with bases).
  12. s_type s; s.value = "C";
  13. out.str("");
  14. result_type& r = cc.f(s);
  15. ok.str(""); ok
  16. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  17. << "d::static_inv" << std::endl
  18. << "d::inv" << std::endl
  19. << "e::static_inv" << std::endl
  20. << "e::inv" << std::endl
  21. << "c::static_inv" << std::endl
  22. << "c::inv" << std::endl
  23. #endif
  24. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  25. << "d::f::pre" << std::endl
  26. << "e::f::pre" << std::endl
  27. << "c::f::pre" << std::endl
  28. #endif
  29. #ifndef BOOST_CONTRACT_NO_OLDS
  30. << "d::f::old" << std::endl
  31. << "e::f::old" << std::endl
  32. << "c::f::old" << std::endl
  33. #endif
  34. << "c::f::body" << std::endl
  35. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  36. << "d::static_inv" << std::endl
  37. << "d::inv" << std::endl
  38. << "e::static_inv" << std::endl
  39. << "e::inv" << std::endl
  40. << "c::static_inv" << std::endl
  41. << "c::inv" << std::endl
  42. #endif
  43. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  44. << "d::f::old" << std::endl
  45. << "d::f::post" << std::endl
  46. << "e::f::old" << std::endl
  47. << "e::f::post" << std::endl
  48. // No old call here because not a base object.
  49. << "c::f::post" << std::endl
  50. #endif
  51. ;
  52. BOOST_TEST(out.eq(ok.str()));
  53. #ifndef BOOST_CONTRACT_NO_OLDS
  54. #define BOOST_CONTRACT_TEST_old 1u
  55. #else
  56. #define BOOST_CONTRACT_TEST_old 0u
  57. #endif
  58. BOOST_TEST_EQ(r.value, "C");
  59. BOOST_TEST_EQ(s.value, "cde");
  60. BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 3);
  61. BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 3);
  62. BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 local var.
  63. BOOST_TEST_EQ(cc.y.value, "cC");
  64. BOOST_TEST_EQ(cc.y.copies(), BOOST_CONTRACT_TEST_old);
  65. BOOST_TEST_EQ(cc.y.evals(), BOOST_CONTRACT_TEST_old);
  66. BOOST_TEST_EQ(cc.y.ctors(), cc.y.dtors() + 1); // 1 data member.
  67. BOOST_TEST_EQ(cc.t<'d'>::z.value, "dC");
  68. BOOST_TEST_EQ(cc.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
  69. BOOST_TEST_EQ(cc.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
  70. BOOST_TEST_EQ(cc.t<'d'>::z.ctors(), cc.t<'d'>::z.dtors() + 1); // 1 member.
  71. BOOST_TEST_EQ(cc.t<'e'>::z.value, "eC");
  72. BOOST_TEST_EQ(cc.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
  73. BOOST_TEST_EQ(cc.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
  74. BOOST_TEST_EQ(cc.t<'e'>::z.ctors(), cc.t<'e'>::z.dtors() + 1); // 1 member.
  75. #undef BOOST_CONTRACT_TEST_old
  76. return boost::report_errors();
  77. }