smoke.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 destructor subcontracting.
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/counter.hpp"
  8. #include <boost/contract/destructor.hpp>
  9. #include <boost/contract/base_types.hpp>
  10. #include <boost/contract/assert.hpp>
  11. #include <boost/contract/old.hpp>
  12. #include <boost/contract/check.hpp>
  13. #include <boost/preprocessor/control/iif.hpp>
  14. #include <boost/detail/lightweight_test.hpp>
  15. #include <sstream>
  16. boost::contract::test::detail::oteststream out;
  17. template<char Id>
  18. struct t {
  19. static void static_invariant() {
  20. out << Id << "::static_inv" << std::endl;
  21. BOOST_CONTRACT_ASSERT(l.value >= 0);
  22. }
  23. void invariant() const {
  24. out << Id << "::inv" << std::endl;
  25. BOOST_CONTRACT_ASSERT(k_ < 0);
  26. }
  27. struct l_tag;
  28. typedef boost::contract::test::detail::counter<l_tag, int> l_type;
  29. static l_type l;
  30. explicit t() : k_(-1) { ++l.value; }
  31. virtual ~t() {
  32. boost::contract::old_ptr<l_type> old_l;
  33. boost::contract::check c = boost::contract::destructor(this)
  34. .old([&] {
  35. out << Id << "::dtor::old" << std::endl;
  36. old_l = BOOST_CONTRACT_OLDOF(l_type::eval(l));
  37. })
  38. .postcondition([&old_l] {
  39. out << Id << "::dtor::post" << std::endl;
  40. BOOST_CONTRACT_ASSERT(t<Id>::l.value == old_l->value - 1);
  41. })
  42. ;
  43. out << Id << "::dtor::body" << std::endl;
  44. --l.value;
  45. }
  46. private:
  47. int k_;
  48. };
  49. template<char Id> typename t<Id>::l_type t<Id>::l;
  50. // Test deep inheritance (2 vertical levels), multiple inheritance (4
  51. // horizontal levels), and that all public/protected/private part of
  52. // subcontracting for destructors (not just public, because all access levels
  53. // are part of C++ object destruction mechanism).
  54. struct c
  55. #define BASES public t<'d'>, protected t<'p'>, private t<'q'>, public t<'e'>
  56. : BASES
  57. {
  58. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  59. #undef BASES
  60. static void static_invariant() {
  61. out << "c::static_inv" << std::endl;
  62. BOOST_CONTRACT_ASSERT(m.value >= 0);
  63. }
  64. void invariant() const {
  65. out << "c::inv" << std::endl;
  66. BOOST_CONTRACT_ASSERT(j_ < 0);
  67. }
  68. struct m_tag;
  69. typedef boost::contract::test::detail::counter<m_tag, int> m_type;
  70. static m_type m;
  71. explicit c() : j_(-1) { ++m.value; }
  72. virtual ~c() {
  73. boost::contract::old_ptr<m_type> old_m =
  74. BOOST_CONTRACT_OLDOF(m_type::eval(m));
  75. boost::contract::check c = boost::contract::destructor(this)
  76. .old([] {
  77. out << "c::dtor::old" << std::endl;
  78. // Test old-of assignment above instead.
  79. })
  80. .postcondition([&old_m] {
  81. out << "c::dtor::post" << std::endl;
  82. BOOST_CONTRACT_ASSERT(c::m.value == old_m->value - 1);
  83. })
  84. ;
  85. out << "c::dtor::body" << std::endl;
  86. --m.value;
  87. }
  88. private:
  89. int j_;
  90. };
  91. c::m_type c::m;
  92. // Test not (fully) contracted base is not part of destructor subcontracting.
  93. struct b {
  94. static void static_invariant() { out << "b::static_inv" << std::endl; }
  95. void invariant() const { out << "b::inv" << std::endl; }
  96. explicit b() {}
  97. virtual ~b() {} // No contract.
  98. };
  99. struct a_n_tag; // Global decl so visible in MSVC10 lambdas.
  100. typedef boost::contract::test::detail::counter<a_n_tag, int> a_n_type;
  101. // Test destructor with both non-contracted and contracted bases.
  102. struct a
  103. #define BASES public b, public c
  104. : BASES
  105. {
  106. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  107. #undef BASES
  108. static void static_invariant() {
  109. out << "a::static_inv" << std::endl;
  110. BOOST_CONTRACT_ASSERT(n.value >= 0);
  111. }
  112. void invariant() const {
  113. out << "a::inv" << std::endl;
  114. BOOST_CONTRACT_ASSERT(i_ < 0);
  115. }
  116. static a_n_type n;
  117. explicit a() : i_(-1) {
  118. ++i_; --i_; // To avoid a warning when all contracts off.
  119. ++n.value;
  120. }
  121. virtual ~a() {
  122. boost::contract::old_ptr<a_n_type> old_n;
  123. boost::contract::check c = boost::contract::destructor(this)
  124. .old([&] {
  125. out << "a::dtor::old" << std::endl;
  126. old_n = BOOST_CONTRACT_OLDOF(a_n_type::eval(n));
  127. })
  128. .postcondition([&old_n] {
  129. out << "a::dtor::post" << std::endl;
  130. BOOST_CONTRACT_ASSERT(a::n.value == old_n->value - 1);
  131. })
  132. ;
  133. out << "a::dtor::body" << std::endl;
  134. --n.value;
  135. }
  136. private:
  137. int i_;
  138. };
  139. a_n_type a::n;
  140. int main() {
  141. std::ostringstream ok;
  142. {
  143. a aa;
  144. out.str("");
  145. } // Call aa's destructor.
  146. ok.str(""); ok
  147. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  148. << "a::static_inv" << std::endl
  149. << "a::inv" << std::endl
  150. #endif
  151. #ifndef BOOST_CONTRACT_NO_OLDS
  152. << "a::dtor::old" << std::endl
  153. #endif
  154. << "a::dtor::body" << std::endl
  155. // Test static inv, but not const inv, checked after destructor body.
  156. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  157. << "a::static_inv" << std::endl
  158. #endif
  159. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  160. << "a::dtor::post" << std::endl
  161. #endif
  162. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  163. << "c::static_inv" << std::endl
  164. << "c::inv" << std::endl
  165. #endif
  166. #ifndef BOOST_CONTRACT_NO_OLDS
  167. << "c::dtor::old" << std::endl
  168. #endif
  169. << "c::dtor::body" << std::endl
  170. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  171. << "c::static_inv" << std::endl
  172. #endif
  173. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  174. << "c::dtor::post" << std::endl
  175. #endif
  176. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  177. << "e::static_inv" << std::endl
  178. << "e::inv" << std::endl
  179. #endif
  180. #ifndef BOOST_CONTRACT_NO_OLDS
  181. << "e::dtor::old" << std::endl
  182. #endif
  183. << "e::dtor::body" << std::endl
  184. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  185. << "e::static_inv" << std::endl
  186. #endif
  187. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  188. << "e::dtor::post" << std::endl
  189. #endif
  190. // Test check also private bases (because part of C++ destruction).
  191. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  192. << "q::static_inv" << std::endl
  193. << "q::inv" << std::endl
  194. #endif
  195. #ifndef BOOST_CONTRACT_NO_OLDS
  196. << "q::dtor::old" << std::endl
  197. #endif
  198. << "q::dtor::body" << std::endl
  199. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  200. << "q::static_inv" << std::endl
  201. #endif
  202. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  203. << "q::dtor::post" << std::endl
  204. #endif
  205. // Test check also protected bases (because part of C++ destruction).
  206. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  207. << "p::static_inv" << std::endl
  208. << "p::inv" << std::endl
  209. #endif
  210. #ifndef BOOST_CONTRACT_NO_OLDS
  211. << "p::dtor::old" << std::endl
  212. #endif
  213. << "p::dtor::body" << std::endl
  214. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  215. << "p::static_inv" << std::endl
  216. #endif
  217. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  218. << "p::dtor::post" << std::endl
  219. #endif
  220. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  221. << "d::static_inv" << std::endl
  222. << "d::inv" << std::endl
  223. #endif
  224. #ifndef BOOST_CONTRACT_NO_OLDS
  225. << "d::dtor::old" << std::endl
  226. #endif
  227. << "d::dtor::body" << std::endl
  228. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  229. << "d::static_inv" << std::endl
  230. #endif
  231. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  232. << "d::dtor::post" << std::endl
  233. #endif
  234. ;
  235. BOOST_TEST(out.eq(ok.str()));
  236. #ifndef BOOST_CONTRACT_NO_OLDS
  237. #define BOOST_CONTRACT_TEST_old 1u
  238. #else
  239. #define BOOST_CONTRACT_TEST_old 0u
  240. #endif
  241. // Followings destroy only copies (actual objects are static data members).
  242. BOOST_TEST_EQ(a_n_type::copies(), BOOST_CONTRACT_TEST_old);
  243. BOOST_TEST_EQ(a_n_type::evals(), BOOST_CONTRACT_TEST_old);
  244. BOOST_TEST_EQ(a_n_type::copies(), a_n_type::dtors()); // No leak.
  245. BOOST_TEST_EQ(c::m_type::copies(), BOOST_CONTRACT_TEST_old);
  246. BOOST_TEST_EQ(c::m_type::evals(), BOOST_CONTRACT_TEST_old);
  247. BOOST_TEST_EQ(c::m_type::copies(), c::m_type::dtors()); // No leak.
  248. BOOST_TEST_EQ(t<'d'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  249. BOOST_TEST_EQ(t<'d'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  250. BOOST_TEST_EQ(t<'d'>::l_type::copies(), t<'d'>::l_type::dtors()); // No leak
  251. BOOST_TEST_EQ(t<'p'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  252. BOOST_TEST_EQ(t<'p'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  253. BOOST_TEST_EQ(t<'p'>::l_type::copies(), t<'p'>::l_type::dtors()); // No leak
  254. BOOST_TEST_EQ(t<'q'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  255. BOOST_TEST_EQ(t<'q'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  256. BOOST_TEST_EQ(t<'q'>::l_type::copies(), t<'q'>::l_type::dtors()); // No leak
  257. BOOST_TEST_EQ(t<'e'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  258. BOOST_TEST_EQ(t<'e'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  259. BOOST_TEST_EQ(t<'e'>::l_type::copies(), t<'e'>::l_type::dtors()); // No leak
  260. #undef BOOST_CONTRACT_TEST_old
  261. return boost::report_errors();
  262. }