virtual_sparse.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 subcontracting with sparse and complex inheritance graph.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/public_function.hpp>
  8. #include <boost/contract/assert.hpp>
  9. #include <boost/contract/check.hpp>
  10. #include <boost/contract/override.hpp>
  11. #include <boost/contract/base_types.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <sstream>
  14. boost::contract::test::detail::oteststream out;
  15. struct j {
  16. static void static_invariant() { out << "j::static_inv" << std::endl; }
  17. void invariant() const { out << "j::inv" << std::endl; }
  18. virtual void f(char ch, boost::contract::virtual_* v = 0) {
  19. boost::contract::check c = boost::contract::public_function(v, this)
  20. .precondition([&] {
  21. out << "j::f::pre" << std::endl;
  22. BOOST_CONTRACT_ASSERT(ch == 'j');
  23. })
  24. .old([] { out << "j::f::old" << std::endl; })
  25. .postcondition([] { out << "j::f::post" << std::endl; })
  26. ;
  27. out << "j::f::body" << std::endl;
  28. }
  29. };
  30. struct i {
  31. static void static_invariant() { out << "i::static_inv" << std::endl; }
  32. void invariant() const { out << "i::inv" << std::endl; }
  33. virtual void f(char ch, boost::contract::virtual_* v = 0) {
  34. boost::contract::check c = boost::contract::public_function(v, this)
  35. .precondition([&] {
  36. out << "i::f::pre" << std::endl;
  37. BOOST_CONTRACT_ASSERT(ch == 'i');
  38. })
  39. .old([] { out << "i::f::old" << std::endl; })
  40. .postcondition([] { out << "i::f::post" << std::endl; })
  41. ;
  42. out << "i::f::body" << std::endl;
  43. }
  44. };
  45. struct k {};
  46. struct h
  47. #define BASES public j
  48. : BASES
  49. {
  50. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  51. #undef BASES
  52. static void static_invariant() { out << "h::static_inv" << std::endl; }
  53. void invariant() const { out << "h::inv" << std::endl; }
  54. virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
  55. boost::contract::check c = boost::contract::public_function<
  56. override_f>(v, &h::f, this, ch)
  57. .precondition([&] {
  58. out << "h::f::pre" << std::endl;
  59. BOOST_CONTRACT_ASSERT(ch == 'h');
  60. })
  61. .old([] { out << "h::f::old" << std::endl; })
  62. .postcondition([] { out << "h::f::post" << std::endl; })
  63. ;
  64. out << "h::f::body" << std::endl;
  65. }
  66. BOOST_CONTRACT_OVERRIDE(f)
  67. };
  68. struct e
  69. #define BASES public virtual i
  70. : BASES
  71. {
  72. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  73. #undef BASES
  74. static void static_invariant() { out << "e::static_inv" << std::endl; }
  75. void invariant() const { out << "e::inv" << std::endl; }
  76. virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
  77. boost::contract::check c = boost::contract::public_function<
  78. override_f>(v, &e::f, this, ch)
  79. .precondition([&] {
  80. out << "e::f::pre" << std::endl;
  81. BOOST_CONTRACT_ASSERT(ch == 'e');
  82. })
  83. .old([] { out << "e::f::old" << std::endl; })
  84. .postcondition([] { out << "e::f::post" << std::endl; })
  85. ;
  86. out << "e::f::body" << std::endl;
  87. }
  88. BOOST_CONTRACT_OVERRIDE(f)
  89. };
  90. struct d
  91. #define BASES public k, virtual public i
  92. : BASES
  93. {
  94. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  95. #undef BASES
  96. static void static_invariant() { out << "d::static_inv" << std::endl; }
  97. void invariant() const { out << "d::inv" << std::endl; }
  98. };
  99. struct c {
  100. static void static_invariant() { out << "c::static_inv" << std::endl; }
  101. void invariant() const { out << "c::inv" << std::endl; }
  102. virtual void f(char ch, boost::contract::virtual_* v = 0) {
  103. boost::contract::check c = boost::contract::public_function(v, this)
  104. .precondition([&] {
  105. out << "c::f::pre" << std::endl;
  106. BOOST_CONTRACT_ASSERT(ch == 'c');
  107. })
  108. .old([] { out << "c::f::old" << std::endl; })
  109. .postcondition([] { out << "c::f::post" << std::endl; })
  110. ;
  111. out << "c::f::body" << std::endl;
  112. }
  113. };
  114. struct b
  115. #define BASES public c, public d
  116. : BASES
  117. {
  118. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  119. #undef BASES
  120. static void static_invariant() { out << "b::static_inv" << std::endl; }
  121. void invariant() const { out << "b::inv" << std::endl; }
  122. };
  123. struct x {};
  124. struct y {};
  125. struct z {};
  126. struct a
  127. #define BASES public b, public x, public e, protected y, public h, \
  128. private z
  129. : BASES
  130. {
  131. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  132. #undef BASES
  133. static void static_invariant() { out << "a::static_inv" << std::endl; }
  134. void invariant() const { out << "a::inv" << std::endl; }
  135. virtual void f(char ch, boost::contract::virtual_* v = 0) /* override */ {
  136. boost::contract::check c = boost::contract::public_function<
  137. override_f>(v, &a::f, this, ch)
  138. .precondition([&] {
  139. out << "a::f::pre" << std::endl;
  140. BOOST_CONTRACT_ASSERT(ch == 'a');
  141. })
  142. .old([] { out << "a::f::old" << std::endl; })
  143. .postcondition([] { out << "a::f::post" << std::endl; })
  144. ;
  145. out << "a::f::body" << std::endl;
  146. }
  147. BOOST_CONTRACT_OVERRIDE(f)
  148. };
  149. int main() {
  150. std::ostringstream ok;
  151. a aa;
  152. out.str("");
  153. aa.f('a');
  154. ok.str(""); ok
  155. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  156. << "c::static_inv" << std::endl
  157. << "c::inv" << std::endl
  158. << "i::static_inv" << std::endl
  159. << "i::inv" << std::endl
  160. << "e::static_inv" << std::endl
  161. << "e::inv" << std::endl
  162. << "j::static_inv" << std::endl
  163. << "j::inv" << std::endl
  164. << "h::static_inv" << std::endl
  165. << "h::inv" << std::endl
  166. << "a::static_inv" << std::endl
  167. << "a::inv" << std::endl
  168. #endif
  169. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  170. << "c::f::pre" << std::endl
  171. << "i::f::pre" << std::endl
  172. << "e::f::pre" << std::endl
  173. << "j::f::pre" << std::endl
  174. << "h::f::pre" << std::endl
  175. << "a::f::pre" << std::endl
  176. #endif
  177. #ifndef BOOST_CONTRACT_NO_OLDS
  178. << "c::f::old" << std::endl
  179. << "i::f::old" << std::endl
  180. << "e::f::old" << std::endl
  181. << "j::f::old" << std::endl
  182. << "h::f::old" << std::endl
  183. << "a::f::old" << std::endl
  184. #endif
  185. << "a::f::body" << std::endl
  186. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  187. << "c::static_inv" << std::endl
  188. << "c::inv" << std::endl
  189. << "i::static_inv" << std::endl
  190. << "i::inv" << std::endl
  191. << "e::static_inv" << std::endl
  192. << "e::inv" << std::endl
  193. << "j::static_inv" << std::endl
  194. << "j::inv" << std::endl
  195. << "h::static_inv" << std::endl
  196. << "h::inv" << std::endl
  197. << "a::static_inv" << std::endl
  198. << "a::inv" << std::endl
  199. #endif
  200. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  201. << "c::f::old" << std::endl
  202. << "c::f::post" << std::endl
  203. << "i::f::old" << std::endl
  204. << "i::f::post" << std::endl
  205. << "e::f::old" << std::endl
  206. << "e::f::post" << std::endl
  207. << "j::f::old" << std::endl
  208. << "j::f::post" << std::endl
  209. << "h::f::old" << std::endl
  210. << "h::f::post" << std::endl
  211. // No old call here because not a base object.
  212. << "a::f::post" << std::endl
  213. #endif
  214. ;
  215. BOOST_TEST(out.eq(ok.str()));
  216. c cc;
  217. out.str("");
  218. cc.f('c');
  219. ok.str(""); ok
  220. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  221. << "c::static_inv" << std::endl
  222. << "c::inv" << std::endl
  223. #endif
  224. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  225. << "c::f::pre" << std::endl
  226. #endif
  227. #ifndef BOOST_CONTRACT_NO_OLDS
  228. << "c::f::old" << std::endl
  229. #endif
  230. << "c::f::body" << std::endl
  231. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  232. << "c::static_inv" << std::endl
  233. << "c::inv" << std::endl
  234. #endif
  235. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  236. // No old call here because not a base object.
  237. << "c::f::post" << std::endl
  238. #endif
  239. ;
  240. BOOST_TEST(out.eq(ok.str()));
  241. d dd;
  242. out.str("");
  243. dd.f('i'); // d's f inherited from i.
  244. ok.str(""); ok
  245. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  246. << "i::static_inv" << std::endl
  247. << "i::inv" << std::endl
  248. #endif
  249. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  250. << "i::f::pre" << std::endl
  251. #endif
  252. #ifndef BOOST_CONTRACT_NO_OLDS
  253. << "i::f::old" << std::endl
  254. #endif
  255. << "i::f::body" << std::endl
  256. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  257. << "i::static_inv" << std::endl
  258. << "i::inv" << std::endl
  259. #endif
  260. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  261. // No old call here because not a base object.
  262. << "i::f::post" << std::endl
  263. #endif
  264. ;
  265. BOOST_TEST(out.eq(ok.str()));
  266. e ee;
  267. out.str("");
  268. ee.f('e');
  269. ok.str(""); ok
  270. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  271. << "i::static_inv" << std::endl
  272. << "i::inv" << std::endl
  273. << "e::static_inv" << std::endl
  274. << "e::inv" << std::endl
  275. #endif
  276. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  277. << "i::f::pre" << std::endl
  278. << "e::f::pre" << std::endl
  279. #endif
  280. #ifndef BOOST_CONTRACT_NO_OLDS
  281. << "i::f::old" << std::endl
  282. << "e::f::old" << std::endl
  283. #endif
  284. << "e::f::body" << std::endl
  285. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  286. << "i::static_inv" << std::endl
  287. << "i::inv" << std::endl
  288. << "e::static_inv" << std::endl
  289. << "e::inv" << std::endl
  290. #endif
  291. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  292. << "i::f::old" << std::endl
  293. << "i::f::post" << std::endl
  294. // No old call here because not a base object.
  295. << "e::f::post" << std::endl
  296. #endif
  297. ;
  298. BOOST_TEST(out.eq(ok.str()));
  299. i ii;
  300. out.str("");
  301. ii.f('i');
  302. ok.str(""); ok
  303. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  304. << "i::static_inv" << std::endl
  305. << "i::inv" << std::endl
  306. #endif
  307. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  308. << "i::f::pre" << std::endl
  309. #endif
  310. #ifndef BOOST_CONTRACT_NO_OLDS
  311. << "i::f::old" << std::endl
  312. #endif
  313. << "i::f::body" << std::endl
  314. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  315. << "i::static_inv" << std::endl
  316. << "i::inv" << std::endl
  317. #endif
  318. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  319. // No old call here because not a base object.
  320. << "i::f::post" << std::endl
  321. #endif
  322. ;
  323. BOOST_TEST(out.eq(ok.str()));
  324. h hh;
  325. out.str("");
  326. hh.f('h');
  327. ok.str(""); ok
  328. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  329. << "j::static_inv" << std::endl
  330. << "j::inv" << std::endl
  331. << "h::static_inv" << std::endl
  332. << "h::inv" << std::endl
  333. #endif
  334. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  335. << "j::f::pre" << std::endl
  336. << "h::f::pre" << std::endl
  337. #endif
  338. #ifndef BOOST_CONTRACT_NO_OLDS
  339. << "j::f::old" << std::endl
  340. << "h::f::old" << std::endl
  341. #endif
  342. << "h::f::body" << std::endl
  343. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  344. << "j::static_inv" << std::endl
  345. << "j::inv" << std::endl
  346. << "h::static_inv" << std::endl
  347. << "h::inv" << std::endl
  348. #endif
  349. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  350. << "j::f::old" << std::endl
  351. << "j::f::post" << std::endl
  352. // No old call here because not a base object.
  353. << "h::f::post" << std::endl
  354. #endif
  355. ;
  356. BOOST_TEST(out.eq(ok.str()));
  357. j jj;
  358. out.str("");
  359. jj.f('j');
  360. ok.str(""); ok
  361. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  362. << "j::static_inv" << std::endl
  363. << "j::inv" << std::endl
  364. #endif
  365. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  366. << "j::f::pre" << std::endl
  367. #endif
  368. #ifndef BOOST_CONTRACT_NO_OLDS
  369. << "j::f::old" << std::endl
  370. #endif
  371. << "j::f::body" << std::endl
  372. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  373. << "j::static_inv" << std::endl
  374. << "j::inv" << std::endl
  375. #endif
  376. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  377. // No old call here because not a base object.
  378. << "j::f::post" << std::endl
  379. #endif
  380. ;
  381. BOOST_TEST(out.eq(ok.str()));
  382. return boost::report_errors();
  383. }