smoke.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 constructor subcontracting.
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/counter.hpp"
  8. #include <boost/contract/constructor.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/bind.hpp>
  14. #include <boost/ref.hpp>
  15. #include <boost/detail/lightweight_test.hpp>
  16. #include <sstream>
  17. boost::contract::test::detail::oteststream out;
  18. template<char Id>
  19. struct t
  20. #define BASES private boost::contract::constructor_precondition<t<Id> >
  21. : BASES
  22. {
  23. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  24. #undef BASES
  25. static void static_invariant() {
  26. out << Id << "::static_inv" << std::endl;
  27. BOOST_CONTRACT_ASSERT(l.value >= 0);
  28. }
  29. void invariant() const {
  30. out << Id << "::inv" << std::endl;
  31. BOOST_CONTRACT_ASSERT(k_ < 0);
  32. }
  33. struct l_tag;
  34. typedef boost::contract::test::detail::counter<l_tag, int> l_type;
  35. static l_type l;
  36. struct z_tag;
  37. typedef boost::contract::test::detail::counter<z_tag, int> z_type;
  38. explicit t(z_type& z) :
  39. boost::contract::constructor_precondition<t<Id> >([&] {
  40. out << Id << "::ctor::pre" << std::endl;
  41. BOOST_CONTRACT_ASSERT(z.value < 0);
  42. })
  43. {
  44. boost::contract::old_ptr<z_type> old_z;
  45. boost::contract::old_ptr<l_type> old_l =
  46. BOOST_CONTRACT_OLDOF(l_type::eval(l));
  47. boost::contract::check c = boost::contract::constructor(this)
  48. .old([&] {
  49. out << Id << "::ctor::old" << std::endl;
  50. old_z = BOOST_CONTRACT_OLDOF(z_type::eval(z));
  51. })
  52. .postcondition([&] {
  53. out << Id << "::ctor::post" << std::endl;
  54. BOOST_CONTRACT_ASSERT(k_ == old_z->value);
  55. BOOST_CONTRACT_ASSERT(z.value == l.value);
  56. BOOST_CONTRACT_ASSERT(l.value == old_l->value + 1);
  57. })
  58. ;
  59. out << Id << "::ctor::body" << std::endl;
  60. k_ = z.value;
  61. z.value = ++l.value;
  62. }
  63. virtual ~t() { --l.value; }
  64. private:
  65. int k_;
  66. };
  67. template<char Id> typename t<Id>::l_type t<Id>::l;
  68. // Test deep inheritance (2 vertical levels), multiple inheritance (4
  69. // horizontal levels), and that all public/protected/private part of
  70. // subcontracting for constructors (not just public, because all access levels
  71. // are part of C++ object construction mechanism).
  72. struct c
  73. #define BASES private boost::contract::constructor_precondition<c>, \
  74. public t<'d'>, protected t<'p'>, private t<'q'>, public t<'e'>
  75. : BASES
  76. {
  77. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  78. #undef BASES
  79. static void static_invariant() {
  80. out << "c::static_inv" << std::endl;
  81. BOOST_CONTRACT_ASSERT(m.value >= 0);
  82. }
  83. void invariant() const {
  84. out << "c::inv" << std::endl;
  85. BOOST_CONTRACT_ASSERT(j_ < 0);
  86. }
  87. struct m_tag;
  88. typedef boost::contract::test::detail::counter<m_tag, int> m_type;
  89. static m_type m;
  90. struct y_tag;
  91. typedef boost::contract::test::detail::counter<y_tag, int> y_type;
  92. explicit c(y_type& y, t<'d'>::z_type& dz, t<'p'>::z_type& pz,
  93. t<'q'>::z_type& qz, t<'e'>::z_type& ez) :
  94. boost::contract::constructor_precondition<c>([&] {
  95. out << "c::ctor::pre" << std::endl;
  96. BOOST_CONTRACT_ASSERT(y.value < 0);
  97. }),
  98. t<'d'>(dz), t<'p'>(pz), t<'q'>(qz), t<'e'>(ez)
  99. {
  100. boost::contract::old_ptr<y_type> old_y =
  101. BOOST_CONTRACT_OLDOF(y_type::eval(y));
  102. boost::contract::old_ptr<m_type> old_m;
  103. boost::contract::check c = boost::contract::constructor(this)
  104. .old([&] {
  105. out << "c::ctor::old" << std::endl;
  106. old_m = BOOST_CONTRACT_OLDOF(m_type::eval(m));
  107. })
  108. .postcondition([&] {
  109. out << "c::ctor::post" << std::endl;
  110. BOOST_CONTRACT_ASSERT(j_ == old_y->value);
  111. BOOST_CONTRACT_ASSERT(y.value == m.value);
  112. BOOST_CONTRACT_ASSERT(m.value == old_m->value + 1);
  113. })
  114. ;
  115. out << "c::ctor::body" << std::endl;
  116. j_ = y.value;
  117. y.value = ++m.value;
  118. }
  119. virtual ~c() { --m.value; }
  120. private:
  121. int j_;
  122. };
  123. c::m_type c::m;
  124. // Test not (fully) contracted base is not part of constructor subcontracting.
  125. struct b
  126. #define BASES private boost::contract::constructor_precondition<b>
  127. : BASES
  128. {
  129. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  130. #undef BASES
  131. static void static_invariant() { out << "b::static_inv" << std::endl; }
  132. void invariant() const { out << "b::inv" << std::endl; }
  133. explicit b() {} // No contract.
  134. virtual ~b() {}
  135. };
  136. // Test constructor with both non-contracted and contracted bases.
  137. struct a
  138. #define BASES private boost::contract::constructor_precondition<a>, \
  139. public b, public c
  140. : BASES
  141. {
  142. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  143. #undef BASES
  144. static void static_invariant() {
  145. out << "a::static_inv" << std::endl;
  146. BOOST_CONTRACT_ASSERT(n.value >= 0);
  147. }
  148. void invariant() const {
  149. out << "a::inv" << std::endl;
  150. BOOST_CONTRACT_ASSERT(i_ < 0);
  151. }
  152. struct n_tag;
  153. typedef boost::contract::test::detail::counter<n_tag, int> n_type;
  154. static n_type n;
  155. struct x_tag;
  156. typedef boost::contract::test::detail::counter<x_tag, int> x_type;
  157. explicit a(x_type& x, c::y_type& y, t<'d'>::z_type& dz,
  158. t<'p'>::z_type& pz, t<'q'>::z_type& qz, t<'e'>::z_type& ez) :
  159. boost::contract::constructor_precondition<a>([&] {
  160. out << "a::ctor::pre" << std::endl;
  161. BOOST_CONTRACT_ASSERT(x.value < 0);
  162. }),
  163. b(), c(y, dz, pz, qz, ez)
  164. {
  165. boost::contract::old_ptr<x_type> old_x;
  166. boost::contract::old_ptr<n_type> old_n =
  167. BOOST_CONTRACT_OLDOF(n_type::eval(n));
  168. boost::contract::check c = boost::contract::constructor(this)
  169. .old([&] {
  170. out << "a::ctor::old" << std::endl;
  171. old_x = BOOST_CONTRACT_OLDOF(x_type::eval(x));
  172. })
  173. .postcondition([&] {
  174. out << "a::ctor::post" << std::endl;
  175. BOOST_CONTRACT_ASSERT(i_ == old_x->value);
  176. BOOST_CONTRACT_ASSERT(x.value == n.value);
  177. BOOST_CONTRACT_ASSERT(n.value == old_n->value + 1);
  178. })
  179. ;
  180. out << "a::ctor::body" << std::endl;
  181. i_ = x.value;
  182. x.value = ++n.value;
  183. }
  184. virtual ~a() { --n.value; }
  185. private:
  186. int i_;
  187. };
  188. a::n_type a::n;
  189. int main() {
  190. std::ostringstream ok;
  191. {
  192. t<'e'>::z_type ez; ez.value = -5;
  193. t<'q'>::z_type qz; qz.value = -5;
  194. t<'p'>::z_type pz; pz.value = -4;
  195. t<'d'>::z_type dz; dz.value = -3;
  196. c::y_type y; y.value = -2;
  197. a::x_type x; x.value = -1;
  198. out.str("");
  199. a aa(x, y, dz, pz, qz, ez);
  200. ok.str(""); ok
  201. // Test all constructor pre checked first.
  202. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  203. << "a::ctor::pre" << std::endl
  204. << "c::ctor::pre" << std::endl
  205. #endif
  206. // Test static inv, but not const inv, checked before ctor body.
  207. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  208. << "d::ctor::pre" << std::endl
  209. #endif
  210. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  211. << "d::static_inv" << std::endl
  212. #endif
  213. #ifndef BOOST_CONTRACT_NO_OLDS
  214. << "d::ctor::old" << std::endl
  215. #endif
  216. << "d::ctor::body" << std::endl
  217. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  218. << "d::static_inv" << std::endl
  219. << "d::inv" << std::endl
  220. #endif
  221. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  222. << "d::ctor::post" << std::endl
  223. #endif
  224. // Test check also protected bases (because part of C++ constr.).
  225. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  226. << "p::ctor::pre" << std::endl
  227. #endif
  228. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  229. << "p::static_inv" << std::endl
  230. #endif
  231. #ifndef BOOST_CONTRACT_NO_OLDS
  232. << "p::ctor::old" << std::endl
  233. #endif
  234. << "p::ctor::body" << std::endl
  235. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  236. << "p::static_inv" << std::endl
  237. << "p::inv" << std::endl
  238. #endif
  239. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  240. << "p::ctor::post" << std::endl
  241. #endif
  242. // Test check also private bases (because part of C++ constr.).
  243. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  244. << "q::ctor::pre" << std::endl
  245. #endif
  246. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  247. << "q::static_inv" << std::endl
  248. #endif
  249. #ifndef BOOST_CONTRACT_NO_OLDS
  250. << "q::ctor::old" << std::endl
  251. #endif
  252. << "q::ctor::body" << std::endl
  253. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  254. << "q::static_inv" << std::endl
  255. << "q::inv" << std::endl
  256. #endif
  257. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  258. << "q::ctor::post" << std::endl
  259. #endif
  260. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  261. << "e::ctor::pre" << std::endl
  262. #endif
  263. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  264. << "e::static_inv" << std::endl
  265. #endif
  266. #ifndef BOOST_CONTRACT_NO_OLDS
  267. << "e::ctor::old" << std::endl
  268. #endif
  269. << "e::ctor::body" << std::endl
  270. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  271. << "e::static_inv" << std::endl
  272. << "e::inv" << std::endl
  273. #endif
  274. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  275. << "e::ctor::post" << std::endl
  276. #endif
  277. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  278. << "c::static_inv" << std::endl
  279. #endif
  280. #ifndef BOOST_CONTRACT_NO_OLDS
  281. << "c::ctor::old" << std::endl
  282. #endif
  283. << "c::ctor::body" << std::endl
  284. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  285. << "c::static_inv" << std::endl
  286. << "c::inv" << std::endl
  287. #endif
  288. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  289. << "c::ctor::post" << std::endl
  290. #endif
  291. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  292. << "a::static_inv" << std::endl
  293. #endif
  294. #ifndef BOOST_CONTRACT_NO_OLDS
  295. << "a::ctor::old" << std::endl
  296. #endif
  297. << "a::ctor::body" << std::endl
  298. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  299. << "a::static_inv" << std::endl
  300. << "a::inv" << std::endl
  301. #endif
  302. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  303. << "a::ctor::post" << std::endl
  304. #endif
  305. ;
  306. BOOST_TEST(out.eq(ok.str()));
  307. }
  308. #ifndef BOOST_CONTRACT_NO_OLDS
  309. #define BOOST_CONTRACT_TEST_old 1u
  310. #else
  311. #define BOOST_CONTRACT_TEST_old 0u
  312. #endif
  313. std::clog << a::x_type::copies() << std::endl;
  314. std::clog << BOOST_CONTRACT_TEST_old << std::endl;
  315. BOOST_TEST_EQ(a::x_type::copies(), BOOST_CONTRACT_TEST_old);
  316. BOOST_TEST_EQ(a::x_type::evals(), BOOST_CONTRACT_TEST_old);
  317. BOOST_TEST_EQ(a::x_type::ctors(), a::x_type::dtors()); // No leak.
  318. BOOST_TEST_EQ(c::y_type::copies(), BOOST_CONTRACT_TEST_old);
  319. BOOST_TEST_EQ(c::y_type::evals(), BOOST_CONTRACT_TEST_old);
  320. BOOST_TEST_EQ(c::y_type::ctors(), c::y_type::dtors()); // No leak.
  321. BOOST_TEST_EQ(t<'d'>::z_type::copies(), BOOST_CONTRACT_TEST_old);
  322. BOOST_TEST_EQ(t<'d'>::z_type::evals(), BOOST_CONTRACT_TEST_old);
  323. BOOST_TEST_EQ(t<'d'>::z_type::ctors(), t<'d'>::z_type::dtors()); // No leak.
  324. BOOST_TEST_EQ(t<'p'>::z_type::copies(), BOOST_CONTRACT_TEST_old);
  325. BOOST_TEST_EQ(t<'p'>::z_type::evals(), BOOST_CONTRACT_TEST_old);
  326. BOOST_TEST_EQ(t<'p'>::z_type::ctors(), t<'p'>::z_type::dtors()); // No leak.
  327. BOOST_TEST_EQ(t<'q'>::z_type::copies(), BOOST_CONTRACT_TEST_old);
  328. BOOST_TEST_EQ(t<'q'>::z_type::evals(), BOOST_CONTRACT_TEST_old);
  329. BOOST_TEST_EQ(t<'q'>::z_type::ctors(), t<'q'>::z_type::dtors()); // No leak.
  330. BOOST_TEST_EQ(t<'e'>::z_type::copies(), BOOST_CONTRACT_TEST_old);
  331. BOOST_TEST_EQ(t<'e'>::z_type::evals(), BOOST_CONTRACT_TEST_old);
  332. BOOST_TEST_EQ(t<'e'>::z_type::ctors(), t<'e'>::z_type::dtors()); // No leak.
  333. // Following destroy only copies (actual objects are static data members).
  334. BOOST_TEST_EQ(a::n_type::copies(), BOOST_CONTRACT_TEST_old);
  335. BOOST_TEST_EQ(a::n_type::evals(), BOOST_CONTRACT_TEST_old);
  336. BOOST_TEST_EQ(a::n_type::copies(), a::n_type::dtors()); // No leak.
  337. BOOST_TEST_EQ(c::m_type::copies(), BOOST_CONTRACT_TEST_old);
  338. BOOST_TEST_EQ(c::m_type::evals(), BOOST_CONTRACT_TEST_old);
  339. BOOST_TEST_EQ(c::m_type::copies(), c::m_type::dtors()); // No leak.
  340. BOOST_TEST_EQ(t<'d'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  341. BOOST_TEST_EQ(t<'d'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  342. BOOST_TEST_EQ(t<'e'>::l_type::copies(), t<'e'>::l_type::dtors()); // No leak
  343. BOOST_TEST_EQ(t<'p'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  344. BOOST_TEST_EQ(t<'p'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  345. BOOST_TEST_EQ(t<'e'>::l_type::copies(), t<'e'>::l_type::dtors()); // No leak
  346. BOOST_TEST_EQ(t<'q'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  347. BOOST_TEST_EQ(t<'q'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  348. BOOST_TEST_EQ(t<'e'>::l_type::copies(), t<'e'>::l_type::dtors()); // No leak
  349. BOOST_TEST_EQ(t<'e'>::l_type::copies(), BOOST_CONTRACT_TEST_old);
  350. BOOST_TEST_EQ(t<'e'>::l_type::evals(), BOOST_CONTRACT_TEST_old);
  351. BOOST_TEST_EQ(t<'e'>::l_type::copies(), t<'e'>::l_type::dtors()); // No leak
  352. #undef BOOST_CONTRACT_TEST_old
  353. return boost::report_errors();
  354. }