smoke.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 subcontracting.
  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; // Test call to derived out-most leaf.
  13. s_type s; s.value = "A";
  14. out.str("");
  15. result_type& r = aa.f(s);
  16. ok.str(""); ok
  17. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  18. << "d::static_inv" << std::endl
  19. << "d::inv" << std::endl
  20. << "e::static_inv" << std::endl
  21. << "e::inv" << std::endl
  22. << "c::static_inv" << std::endl
  23. << "c::inv" << std::endl
  24. << "a::static_inv" << std::endl
  25. << "a::inv" << std::endl
  26. #endif
  27. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  28. << "d::f::pre" << std::endl
  29. << "e::f::pre" << std::endl
  30. << "c::f::pre" << std::endl
  31. << "a::f::pre" << std::endl
  32. #endif
  33. #ifndef BOOST_CONTRACT_NO_OLDS
  34. << "d::f::old" << std::endl
  35. << "e::f::old" << std::endl
  36. << "c::f::old" << std::endl
  37. << "a::f::old" << std::endl
  38. #endif
  39. << "a::f::body" << std::endl
  40. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  41. << "d::static_inv" << std::endl
  42. << "d::inv" << std::endl
  43. << "e::static_inv" << std::endl
  44. << "e::inv" << std::endl
  45. << "c::static_inv" << std::endl
  46. << "c::inv" << std::endl
  47. << "a::static_inv" << std::endl
  48. << "a::inv" << std::endl
  49. #endif
  50. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  51. << "d::f::old" << std::endl
  52. << "d::f::post" << std::endl
  53. << "e::f::old" << std::endl
  54. << "e::f::post" << std::endl
  55. << "c::f::old" << std::endl
  56. << "c::f::post" << std::endl
  57. // No old call here because not a base object.
  58. << "a::f::post" << std::endl
  59. #endif
  60. ;
  61. BOOST_TEST(out.eq(ok.str()));
  62. #ifndef BOOST_CONTRACT_NO_OLDS
  63. #define BOOST_CONTRACT_TEST_old 1u
  64. #else
  65. #define BOOST_CONTRACT_TEST_old 0u
  66. #endif
  67. BOOST_TEST_EQ(r.value, "A");
  68. BOOST_TEST_EQ(s.value, "acde");
  69. BOOST_TEST_EQ(s.copies(), BOOST_CONTRACT_TEST_old * 4);
  70. BOOST_TEST_EQ(s.evals(), BOOST_CONTRACT_TEST_old * 4);
  71. BOOST_TEST_EQ(s.ctors(), s.dtors() + 1); // 1 for local var.
  72. BOOST_TEST_EQ(aa.x.value, "aA");
  73. BOOST_TEST_EQ(aa.x.copies(), BOOST_CONTRACT_TEST_old);
  74. BOOST_TEST_EQ(aa.x.evals(), BOOST_CONTRACT_TEST_old);
  75. BOOST_TEST_EQ(aa.x.ctors(), aa.x.dtors() + 1); // 1 for member var.
  76. BOOST_TEST_EQ(aa.y.value, "cA");
  77. BOOST_TEST_EQ(aa.y.copies(), BOOST_CONTRACT_TEST_old);
  78. BOOST_TEST_EQ(aa.y.evals(), BOOST_CONTRACT_TEST_old);
  79. BOOST_TEST_EQ(aa.y.ctors(), aa.y.dtors() + 1); // 1 for member var.
  80. BOOST_TEST_EQ(aa.t<'d'>::z.value, "dA");
  81. BOOST_TEST_EQ(aa.t<'d'>::z.copies(), BOOST_CONTRACT_TEST_old);
  82. BOOST_TEST_EQ(aa.t<'d'>::z.evals(), BOOST_CONTRACT_TEST_old);
  83. BOOST_TEST_EQ(aa.t<'d'>::z.ctors(), aa.t<'d'>::z.dtors() + 1); // 1 member.
  84. BOOST_TEST_EQ(aa.t<'e'>::z.value, "eA");
  85. BOOST_TEST_EQ(aa.t<'e'>::z.copies(), BOOST_CONTRACT_TEST_old);
  86. BOOST_TEST_EQ(aa.t<'e'>::z.evals(), BOOST_CONTRACT_TEST_old);
  87. BOOST_TEST_EQ(aa.t<'e'>::z.ctors(), aa.t<'e'>::z.dtors() + 1); // 1 member.
  88. #undef BOOST_CONTRACT_TEST_old
  89. return boost::report_errors();
  90. }