virtual.cpp 3.8 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 function subcontracting via virtual functions.
  6. #include "smoke.hpp"
  7. #include <boost/preprocessor/control/iif.hpp>
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <sstream>
  10. int main() {
  11. std::ostringstream ok;
  12. a aa;
  13. c& ca = aa; // Test polymorphic virtual call (via reference to base c).
  14. s_type s; s.value = "A";
  15. out.str("");
  16. result_type& r = ca.f(s);
  17. ok.str(""); ok
  18. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  19. << "d::static_inv" << std::endl
  20. << "d::inv" << std::endl
  21. << "e::static_inv" << std::endl
  22. << "e::inv" << std::endl
  23. << "c::static_inv" << std::endl
  24. << "c::inv" << std::endl
  25. << "a::static_inv" << std::endl
  26. << "a::inv" << std::endl
  27. #endif
  28. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  29. << "d::f::pre" << std::endl
  30. << "e::f::pre" << std::endl
  31. << "c::f::pre" << std::endl
  32. << "a::f::pre" << std::endl
  33. #endif
  34. #ifndef BOOST_CONTRACT_NO_OLDS
  35. << "d::f::old" << std::endl
  36. << "e::f::old" << std::endl
  37. << "c::f::old" << std::endl
  38. << "a::f::old" << std::endl
  39. #endif
  40. << "a::f::body" << std::endl
  41. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  42. << "d::static_inv" << std::endl
  43. << "d::inv" << std::endl
  44. << "e::static_inv" << std::endl
  45. << "e::inv" << std::endl
  46. << "c::static_inv" << std::endl
  47. << "c::inv" << std::endl
  48. << "a::static_inv" << std::endl
  49. << "a::inv" << std::endl
  50. #endif
  51. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  52. << "d::f::old" << std::endl
  53. << "d::f::post" << std::endl
  54. << "e::f::old" << std::endl
  55. << "e::f::post" << std::endl
  56. << "c::f::old" << std::endl
  57. << "c::f::post" << std::endl
  58. // No old call here because not a base object.
  59. << "a::f::post" << std::endl
  60. #endif
  61. ;
  62. BOOST_TEST(out.eq(ok.str()));
  63. #ifndef BOOST_CONTRACT_NO_OLDS
  64. #define BOOST_CONTRACT_TEST_old 1u
  65. #else
  66. #define BOOST_CONTRACT_TEST_old 0u
  67. #endif
  68. BOOST_TEST_EQ(r.value, "A");
  69. BOOST_TEST_EQ(s.value, "acde");
  70. BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 4);
  71. BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 4);
  72. BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 local var.
  73. // Cannot access x via ca, but only via aa.
  74. BOOST_TEST_EQ(aa.x.value, "aA");
  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 data member.
  78. BOOST_TEST_EQ(ca.y.value, "cA");
  79. BOOST_TEST_EQ(ca.y.copies(), BOOST_CONTRACT_TEST_old);
  80. BOOST_TEST_EQ(ca.y.evals(), BOOST_CONTRACT_TEST_old);
  81. BOOST_TEST_EQ(ca.y.ctors(), ca.y.dtors() + 1); // 1 data member.
  82. BOOST_TEST_EQ(ca.t<'d'>::z.value, "dA");
  83. BOOST_TEST_EQ(ca.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
  84. BOOST_TEST_EQ(ca.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
  85. BOOST_TEST_EQ(ca.t<'d'>::z.ctors(), ca.t<'d'>::z.dtors() + 1); // 1 member.
  86. BOOST_TEST_EQ(ca.t<'e'>::z.value, "eA");
  87. BOOST_TEST_EQ(ca.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
  88. BOOST_TEST_EQ(ca.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
  89. BOOST_TEST_EQ(ca.t<'e'>::z.ctors(), ca.t<'e'>::z.dtors() + 1); // 1 member.
  90. #undef BOOST_CONTRACT_TEST_old
  91. return boost::report_errors();
  92. }