decl.hpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. // no #include guard
  2. // Copyright (C) 2008-2018 Lorenzo Caminiti
  3. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  4. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  5. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  6. // Test with and without all invariants (static/cv/const-only) declarations.
  7. #include "../detail/oteststream.hpp"
  8. #include <boost/contract/base_types.hpp>
  9. #include <boost/contract/constructor.hpp>
  10. #include <boost/contract/destructor.hpp>
  11. #include <boost/contract/public_function.hpp>
  12. #include <boost/contract/function.hpp>
  13. #include <boost/contract/override.hpp>
  14. #include <boost/contract/check.hpp>
  15. #include <boost/contract/assert.hpp>
  16. #include <boost/detail/lightweight_test.hpp>
  17. #include <sstream>
  18. boost::contract::test::detail::oteststream out;
  19. struct b : private boost::contract::constructor_precondition<b> {
  20. // Test also with no base_types.
  21. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  22. static void static_invariant() { out << "b::static_inv" << std::endl; }
  23. #endif
  24. #ifdef BOOST_CONTRACT_TEST_CV_INV
  25. void invariant() const volatile { out << "b::cv_inv" << std::endl; }
  26. #endif
  27. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  28. void invariant() const { out << "b::const_inv" << std::endl; }
  29. #endif
  30. b() : boost::contract::constructor_precondition<b>([] {
  31. out << "b::ctor::pre" << std::endl;
  32. }) {
  33. boost::contract::check c = boost::contract::constructor(this)
  34. .old([] { out << "b::ctor::old" << std::endl; })
  35. .postcondition([] { out << "b::ctor::post" << std::endl; })
  36. ;
  37. out << "b::ctor::body" << std::endl;
  38. }
  39. virtual ~b() {
  40. boost::contract::check c = boost::contract::destructor(this)
  41. .old([] { out << "b::dtor::old" << std::endl; })
  42. .postcondition([] { out << "b::dtor::post" << std::endl; })
  43. ;
  44. out << "b::dtor::body" << std::endl;
  45. }
  46. virtual void f(char x, boost::contract::virtual_* v = 0) volatile {
  47. boost::contract::check c = boost::contract::public_function(v, this)
  48. .precondition([&] {
  49. out << "b::f::volatile_pre" << std::endl;
  50. BOOST_CONTRACT_ASSERT(x == 'b');
  51. })
  52. .old([] { out << "b::f::volatile_old" << std::endl; })
  53. .postcondition([] { out << "b::f::volatile_post" << std::endl; })
  54. ;
  55. out << "b::f::volatile_body" << std::endl;
  56. }
  57. virtual void f(char x, boost::contract::virtual_* v = 0) {
  58. boost::contract::check c = boost::contract::public_function(v, this)
  59. .precondition([&] {
  60. out << "b::f::pre" << std::endl;
  61. BOOST_CONTRACT_ASSERT(x == 'b');
  62. })
  63. .old([] { out << "b::f::old" << std::endl; })
  64. .postcondition([] { out << "b::f::post" << std::endl; })
  65. ;
  66. out << "b::f::body" << std::endl;
  67. }
  68. };
  69. struct a
  70. #define BASES private boost::contract::constructor_precondition<a>, public b
  71. : BASES
  72. {
  73. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  74. #undef BASES
  75. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  76. static void static_invariant() { out << "a::static_inv" << std::endl; }
  77. #endif
  78. #ifdef BOOST_CONTRACT_TEST_CV_INV
  79. void invariant() const volatile { out << "a::cv_inv" << std::endl; }
  80. #endif
  81. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  82. void invariant() const { out << "a::const_inv" << std::endl; }
  83. #endif
  84. a() : boost::contract::constructor_precondition<a>([] {
  85. out << "a::ctor::pre" << std::endl;
  86. }) {
  87. boost::contract::check c = boost::contract::constructor(this)
  88. .old([] { out << "a::ctor::old" << std::endl; })
  89. .postcondition([] { out << "a::ctor::post" << std::endl; })
  90. ;
  91. out << "a::ctor::body" << std::endl;
  92. }
  93. virtual ~a() {
  94. boost::contract::check c = boost::contract::destructor(this)
  95. .old([] { out << "a::dtor::old" << std::endl; })
  96. .postcondition([] { out << "a::dtor::post" << std::endl; })
  97. ;
  98. out << "a::dtor::body" << std::endl;
  99. }
  100. virtual void f(char x, boost::contract::virtual_* v = 0)
  101. volatile /* override */ {
  102. boost::contract::check c = boost::contract::public_function<
  103. override_f>(
  104. v,
  105. static_cast<void (a::*)(char x, boost::contract::virtual_*)
  106. volatile>(&a::f),
  107. this, x
  108. )
  109. .precondition([&] {
  110. out << "a::f::volatile_pre" << std::endl;
  111. BOOST_CONTRACT_ASSERT(x == 'a');
  112. })
  113. .old([] { out << "a::f::volatile_old" << std::endl; })
  114. .postcondition([] { out << "a::f::volatile_post" << std::endl; })
  115. ;
  116. out << "a::f::volatile_body" << std::endl;
  117. }
  118. virtual void f(char x, boost::contract::virtual_* v = 0) /* override */ {
  119. boost::contract::check c = boost::contract::public_function<
  120. override_f>(
  121. v,
  122. static_cast<void (a::*)(char x, boost::contract::virtual_*)>(&a::f),
  123. this, x
  124. )
  125. .precondition([&] {
  126. out << "a::f::pre" << std::endl;
  127. BOOST_CONTRACT_ASSERT(x == 'a');
  128. })
  129. .old([] { out << "a::f::old" << std::endl; })
  130. .postcondition([] { out << "a::f::post" << std::endl; })
  131. ;
  132. out << "a::f::body" << std::endl;
  133. }
  134. static void s() {
  135. boost::contract::check c = boost::contract::public_function<a>()
  136. .precondition([] { out << "a::s::pre" << std::endl; })
  137. .old([] { out << "a::s::old" << std::endl; })
  138. .postcondition([] { out << "a::s::post" << std::endl; })
  139. ;
  140. out << "a::s::body" << std::endl;
  141. }
  142. protected:
  143. void p() volatile {
  144. boost::contract::check c = boost::contract::function()
  145. .precondition([] { out << "a::p::volatile_pre" << std::endl; })
  146. .old([] { out << "a::p::volatile_old" << std::endl; })
  147. .postcondition([] { out << "a::p::volatile_post" << std::endl; })
  148. ;
  149. out << "a::p::volatile_body" << std::endl;
  150. }
  151. void p() {
  152. boost::contract::check c = boost::contract::function()
  153. .precondition([] { out << "a::p::pre" << std::endl; })
  154. .old([] { out << "a::p::old" << std::endl; })
  155. .postcondition([] { out << "a::p::post" << std::endl; })
  156. ;
  157. out << "a::p::body" << std::endl;
  158. }
  159. public:
  160. void call_p() volatile { p(); }
  161. void call_p() { p(); }
  162. private:
  163. void q() volatile {
  164. boost::contract::check c = boost::contract::function()
  165. .precondition([] { out << "a::q::volatile_pre" << std::endl; })
  166. .old([] { out << "a::q::volatile_old" << std::endl; })
  167. .postcondition([] { out << "a::q::volatile_post" << std::endl; })
  168. ;
  169. out << "a::q::volatile_body" << std::endl;
  170. }
  171. void q() {
  172. boost::contract::check c = boost::contract::function()
  173. .precondition([] { out << "a::q::pre" << std::endl; })
  174. .old([] { out << "a::q::old" << std::endl; })
  175. .postcondition([] { out << "a::q::post" << std::endl; })
  176. ;
  177. out << "a::q::body" << std::endl;
  178. }
  179. public:
  180. void call_q() volatile { q(); }
  181. void call_q() { q(); }
  182. BOOST_CONTRACT_OVERRIDE(f)
  183. };
  184. int main() {
  185. std::ostringstream ok;
  186. { // Test volatile call with bases.
  187. out.str("");
  188. a volatile av;
  189. ok.str(""); ok // Ctors always check const_inv (even if volatile).
  190. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  191. << "a::ctor::pre" << std::endl
  192. << "b::ctor::pre" << std::endl
  193. #endif
  194. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  195. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  196. << "b::static_inv" << std::endl
  197. #endif
  198. #endif
  199. #ifndef BOOST_CONTRACT_NO_OLDS
  200. << "b::ctor::old" << std::endl
  201. #endif
  202. << "b::ctor::body" << std::endl
  203. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  204. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  205. << "b::static_inv" << std::endl
  206. #endif
  207. #ifdef BOOST_CONTRACT_TEST_CV_INV
  208. << "b::cv_inv" << std::endl
  209. #endif
  210. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  211. << "b::const_inv" << std::endl
  212. #endif
  213. #endif
  214. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  215. << "b::ctor::post" << std::endl
  216. #endif
  217. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  218. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  219. << "a::static_inv" << std::endl
  220. #endif
  221. #endif
  222. #ifndef BOOST_CONTRACT_NO_OLDS
  223. << "a::ctor::old" << std::endl
  224. #endif
  225. << "a::ctor::body" << std::endl
  226. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  227. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  228. << "a::static_inv" << std::endl
  229. #endif
  230. #ifdef BOOST_CONTRACT_TEST_CV_INV
  231. << "a::cv_inv" << std::endl
  232. #endif
  233. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  234. << "a::const_inv" << std::endl
  235. #endif
  236. #endif
  237. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  238. << "a::ctor::post" << std::endl
  239. #endif
  240. ;
  241. BOOST_TEST(out.eq(ok.str()));
  242. out.str("");
  243. av.f('a');
  244. ok.str(""); ok // Volatile checks static and cv (but not const) inv.
  245. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  246. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  247. << "b::static_inv" << std::endl
  248. #endif
  249. #ifdef BOOST_CONTRACT_TEST_CV_INV
  250. << "b::cv_inv" << std::endl
  251. #endif
  252. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  253. << "a::static_inv" << std::endl
  254. #endif
  255. #ifdef BOOST_CONTRACT_TEST_CV_INV
  256. << "a::cv_inv" << std::endl
  257. #endif
  258. #endif
  259. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  260. << "b::f::volatile_pre" << std::endl
  261. << "a::f::volatile_pre" << std::endl
  262. #endif
  263. #ifndef BOOST_CONTRACT_NO_OLDS
  264. << "b::f::volatile_old" << std::endl
  265. << "a::f::volatile_old" << std::endl
  266. #endif
  267. << "a::f::volatile_body" << std::endl
  268. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  269. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  270. << "b::static_inv" << std::endl
  271. #endif
  272. #ifdef BOOST_CONTRACT_TEST_CV_INV
  273. << "b::cv_inv" << std::endl
  274. #endif
  275. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  276. << "a::static_inv" << std::endl
  277. #endif
  278. #ifdef BOOST_CONTRACT_TEST_CV_INV
  279. << "a::cv_inv" << std::endl
  280. #endif
  281. #endif
  282. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  283. << "b::f::volatile_old" << std::endl
  284. << "b::f::volatile_post" << std::endl
  285. << "a::f::volatile_post" << std::endl
  286. #endif
  287. ;
  288. BOOST_TEST(out.eq(ok.str()));
  289. out.str("");
  290. av.s(); // Test static call.
  291. ok.str(""); ok
  292. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  293. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  294. << "a::static_inv" << std::endl
  295. #endif
  296. #endif
  297. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  298. << "a::s::pre" << std::endl
  299. #endif
  300. #ifndef BOOST_CONTRACT_NO_OLDS
  301. << "a::s::old" << std::endl
  302. #endif
  303. << "a::s::body" << std::endl
  304. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  305. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  306. << "a::static_inv" << std::endl
  307. #endif
  308. #endif
  309. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  310. << "a::s::post" << std::endl
  311. #endif
  312. ;
  313. BOOST_TEST(out.eq(ok.str()));
  314. out.str("");
  315. av.call_p(); // Test (indirect) protected call.
  316. ok.str(""); ok
  317. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  318. << "a::p::volatile_pre" << std::endl
  319. #endif
  320. #ifndef BOOST_CONTRACT_NO_OLDS
  321. << "a::p::volatile_old" << std::endl
  322. #endif
  323. << "a::p::volatile_body" << std::endl
  324. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  325. << "a::p::volatile_post" << std::endl
  326. #endif
  327. ;
  328. BOOST_TEST(out.eq(ok.str()));
  329. out.str("");
  330. av.call_q(); // Test (indirect) private call.
  331. ok.str(""); ok
  332. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  333. << "a::q::volatile_pre" << std::endl
  334. #endif
  335. #ifndef BOOST_CONTRACT_NO_OLDS
  336. << "a::q::volatile_old" << std::endl
  337. #endif
  338. << "a::q::volatile_body" << std::endl
  339. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  340. << "a::q::volatile_post" << std::endl
  341. #endif
  342. ;
  343. BOOST_TEST(out.eq(ok.str()));
  344. out.str("");
  345. } // Call a's destructor.
  346. ok.str(""); ok // Dtors always check const_inv (even if volatile).
  347. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  348. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  349. << "a::static_inv" << std::endl
  350. #endif
  351. #ifdef BOOST_CONTRACT_TEST_CV_INV
  352. << "a::cv_inv" << std::endl
  353. #endif
  354. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  355. << "a::const_inv" << std::endl
  356. #endif
  357. #endif
  358. #ifndef BOOST_CONTRACT_NO_OLDS
  359. << "a::dtor::old" << std::endl
  360. #endif
  361. << "a::dtor::body" << std::endl
  362. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  363. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  364. << "a::static_inv" << std::endl
  365. #endif
  366. #endif
  367. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  368. << "a::dtor::post" << std::endl
  369. #endif
  370. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  371. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  372. << "b::static_inv" << std::endl
  373. #endif
  374. #ifdef BOOST_CONTRACT_TEST_CV_INV
  375. << "b::cv_inv" << std::endl
  376. #endif
  377. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  378. << "b::const_inv" << std::endl
  379. #endif
  380. #endif
  381. #ifndef BOOST_CONTRACT_NO_OLDS
  382. << "b::dtor::old" << std::endl
  383. #endif
  384. << "b::dtor::body" << std::endl
  385. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  386. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  387. << "b::static_inv" << std::endl
  388. #endif
  389. #endif
  390. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  391. << "b::dtor::post" << std::endl
  392. #endif
  393. ;
  394. BOOST_TEST(out.eq(ok.str()));
  395. { // Test non-volatile call with bases.
  396. out.str("");
  397. a aa;
  398. ok.str(""); ok // Ctors always check cv_inv (even if not volatile).
  399. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  400. << "a::ctor::pre" << std::endl
  401. << "b::ctor::pre" << std::endl
  402. #endif
  403. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  404. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  405. << "b::static_inv" << std::endl
  406. #endif
  407. #endif
  408. #ifndef BOOST_CONTRACT_NO_OLDS
  409. << "b::ctor::old" << std::endl
  410. #endif
  411. << "b::ctor::body" << std::endl
  412. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  413. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  414. << "b::static_inv" << std::endl
  415. #endif
  416. #ifdef BOOST_CONTRACT_TEST_CV_INV
  417. << "b::cv_inv" << std::endl
  418. #endif
  419. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  420. << "b::const_inv" << std::endl
  421. #endif
  422. #endif
  423. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  424. << "b::ctor::post" << std::endl
  425. #endif
  426. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  427. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  428. << "a::static_inv" << std::endl
  429. #endif
  430. #endif
  431. #ifndef BOOST_CONTRACT_NO_OLDS
  432. << "a::ctor::old" << std::endl
  433. #endif
  434. << "a::ctor::body" << std::endl
  435. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  436. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  437. << "a::static_inv" << std::endl
  438. #endif
  439. #ifdef BOOST_CONTRACT_TEST_CV_INV
  440. << "a::cv_inv" << std::endl
  441. #endif
  442. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  443. << "a::const_inv" << std::endl
  444. #endif
  445. #endif
  446. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  447. << "a::ctor::post" << std::endl
  448. #endif
  449. ;
  450. BOOST_TEST(out.eq(ok.str()));
  451. out.str("");
  452. aa.f('a');
  453. ok.str(""); ok // Non-cv checks static and const (but not cv) inv.
  454. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  455. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  456. << "b::static_inv" << std::endl
  457. #endif
  458. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  459. << "b::const_inv" << std::endl
  460. #endif
  461. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  462. << "a::static_inv" << std::endl
  463. #endif
  464. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  465. << "a::const_inv" << std::endl
  466. #endif
  467. #endif
  468. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  469. << "b::f::pre" << std::endl
  470. << "a::f::pre" << std::endl
  471. #endif
  472. #ifndef BOOST_CONTRACT_NO_OLDS
  473. << "b::f::old" << std::endl
  474. << "a::f::old" << std::endl
  475. #endif
  476. << "a::f::body" << std::endl
  477. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  478. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  479. << "b::static_inv" << std::endl
  480. #endif
  481. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  482. << "b::const_inv" << std::endl
  483. #endif
  484. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  485. << "a::static_inv" << std::endl
  486. #endif
  487. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  488. << "a::const_inv" << std::endl
  489. #endif
  490. #endif
  491. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  492. << "b::f::old" << std::endl
  493. << "b::f::post" << std::endl
  494. << "a::f::post" << std::endl
  495. #endif
  496. ;
  497. BOOST_TEST(out.eq(ok.str()));
  498. out.str("");
  499. aa.s(); // Test static call.
  500. ok.str(""); ok
  501. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  502. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  503. << "a::static_inv" << std::endl
  504. #endif
  505. #endif
  506. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  507. << "a::s::pre" << std::endl
  508. #endif
  509. #ifndef BOOST_CONTRACT_NO_OLDS
  510. << "a::s::old" << std::endl
  511. #endif
  512. << "a::s::body" << std::endl
  513. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  514. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  515. << "a::static_inv" << std::endl
  516. #endif
  517. #endif
  518. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  519. << "a::s::post" << std::endl
  520. #endif
  521. ;
  522. BOOST_TEST(out.eq(ok.str()));
  523. out.str("");
  524. aa.call_p(); // Test (indirect) protected call.
  525. ok.str(""); ok
  526. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  527. << "a::p::pre" << std::endl
  528. #endif
  529. #ifndef BOOST_CONTRACT_NO_OLDS
  530. << "a::p::old" << std::endl
  531. #endif
  532. << "a::p::body" << std::endl
  533. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  534. << "a::p::post" << std::endl
  535. #endif
  536. ;
  537. BOOST_TEST(out.eq(ok.str()));
  538. out.str("");
  539. aa.call_q(); // Test (indirect) private call.
  540. ok.str(""); ok
  541. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  542. << "a::q::pre" << std::endl
  543. #endif
  544. #ifndef BOOST_CONTRACT_NO_OLDS
  545. << "a::q::old" << std::endl
  546. #endif
  547. << "a::q::body" << std::endl
  548. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  549. << "a::q::post" << std::endl
  550. #endif
  551. ;
  552. BOOST_TEST(out.eq(ok.str()));
  553. out.str("");
  554. } // Call a's destructor.
  555. ok.str(""); ok // Dtors always check cv_inv (even if not volatile).
  556. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  557. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  558. << "a::static_inv" << std::endl
  559. #endif
  560. #ifdef BOOST_CONTRACT_TEST_CV_INV
  561. << "a::cv_inv" << std::endl
  562. #endif
  563. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  564. << "a::const_inv" << std::endl
  565. #endif
  566. #endif
  567. #ifndef BOOST_CONTRACT_NO_OLDS
  568. << "a::dtor::old" << std::endl
  569. #endif
  570. << "a::dtor::body" << std::endl
  571. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  572. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  573. << "a::static_inv" << std::endl
  574. #endif
  575. #endif
  576. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  577. << "a::dtor::post" << std::endl
  578. #endif
  579. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  580. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  581. << "b::static_inv" << std::endl
  582. #endif
  583. #ifdef BOOST_CONTRACT_TEST_CV_INV
  584. << "b::cv_inv" << std::endl
  585. #endif
  586. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  587. << "b::const_inv" << std::endl
  588. #endif
  589. #endif
  590. #ifndef BOOST_CONTRACT_NO_OLDS
  591. << "b::dtor::old" << std::endl
  592. #endif
  593. << "b::dtor::body" << std::endl
  594. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  595. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  596. << "b::static_inv" << std::endl
  597. #endif
  598. #endif
  599. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  600. << "b::dtor::post" << std::endl
  601. #endif
  602. ;
  603. BOOST_TEST(out.eq(ok.str()));
  604. { // Test volatile call with no bases.
  605. out.str("");
  606. b volatile bv;
  607. ok.str(""); ok // Ctors always check const_inv (even if volatile).
  608. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  609. << "b::ctor::pre" << std::endl
  610. #endif
  611. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  612. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  613. << "b::static_inv" << std::endl
  614. #endif
  615. #endif
  616. #ifndef BOOST_CONTRACT_NO_OLDS
  617. << "b::ctor::old" << std::endl
  618. #endif
  619. << "b::ctor::body" << std::endl
  620. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  621. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  622. << "b::static_inv" << std::endl
  623. #endif
  624. #ifdef BOOST_CONTRACT_TEST_CV_INV
  625. << "b::cv_inv" << std::endl
  626. #endif
  627. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  628. << "b::const_inv" << std::endl
  629. #endif
  630. #endif
  631. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  632. << "b::ctor::post" << std::endl
  633. #endif
  634. ;
  635. BOOST_TEST(out.eq(ok.str()));
  636. out.str("");
  637. bv.f('b');
  638. ok.str(""); ok // Volatile checks static and cv (but not const) inv.
  639. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  640. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  641. << "b::static_inv" << std::endl
  642. #endif
  643. #ifdef BOOST_CONTRACT_TEST_CV_INV
  644. << "b::cv_inv" << std::endl
  645. #endif
  646. #endif
  647. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  648. << "b::f::volatile_pre" << std::endl
  649. #endif
  650. #ifndef BOOST_CONTRACT_NO_OLDS
  651. << "b::f::volatile_old" << std::endl
  652. #endif
  653. << "b::f::volatile_body" << std::endl
  654. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  655. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  656. << "b::static_inv" << std::endl
  657. #endif
  658. #ifdef BOOST_CONTRACT_TEST_CV_INV
  659. << "b::cv_inv" << std::endl
  660. #endif
  661. #endif
  662. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  663. << "b::f::volatile_post" << std::endl
  664. #endif
  665. ;
  666. BOOST_TEST(out.eq(ok.str()));
  667. out.str("");
  668. } // Call b's destructor.
  669. ok.str(""); ok // Dtors always check const_inv (even if volatile).
  670. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  671. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  672. << "b::static_inv" << std::endl
  673. #endif
  674. #ifdef BOOST_CONTRACT_TEST_CV_INV
  675. << "b::cv_inv" << std::endl
  676. #endif
  677. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  678. << "b::const_inv" << std::endl
  679. #endif
  680. #endif
  681. #ifndef BOOST_CONTRACT_NO_OLDS
  682. << "b::dtor::old" << std::endl
  683. #endif
  684. << "b::dtor::body" << std::endl
  685. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  686. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  687. << "b::static_inv" << std::endl
  688. #endif
  689. #endif
  690. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  691. << "b::dtor::post" << std::endl
  692. #endif
  693. ;
  694. BOOST_TEST(out.eq(ok.str()));
  695. { // Test non-volatile call with no bases.
  696. out.str("");
  697. b bb;
  698. ok.str(""); ok // Ctors always check cv_inv (even if not volatile).
  699. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  700. << "b::ctor::pre" << std::endl
  701. #endif
  702. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  703. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  704. << "b::static_inv" << std::endl
  705. #endif
  706. #endif
  707. #ifndef BOOST_CONTRACT_NO_OLDS
  708. << "b::ctor::old" << std::endl
  709. #endif
  710. << "b::ctor::body" << std::endl
  711. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  712. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  713. << "b::static_inv" << std::endl
  714. #endif
  715. #ifdef BOOST_CONTRACT_TEST_CV_INV
  716. << "b::cv_inv" << std::endl
  717. #endif
  718. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  719. << "b::const_inv" << std::endl
  720. #endif
  721. #endif
  722. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  723. << "b::ctor::post" << std::endl
  724. #endif
  725. ;
  726. BOOST_TEST(out.eq(ok.str()));
  727. out.str("");
  728. bb.f('b');
  729. ok.str(""); ok // Non-cv checks static and const (but not cv) inv.
  730. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  731. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  732. << "b::static_inv" << std::endl
  733. #endif
  734. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  735. << "b::const_inv" << std::endl
  736. #endif
  737. #endif
  738. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  739. << "b::f::pre" << std::endl
  740. #endif
  741. #ifndef BOOST_CONTRACT_NO_OLDS
  742. << "b::f::old" << std::endl
  743. #endif
  744. << "b::f::body" << std::endl
  745. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  746. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  747. << "b::static_inv" << std::endl
  748. #endif
  749. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  750. << "b::const_inv" << std::endl
  751. #endif
  752. #endif
  753. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  754. << "b::f::post" << std::endl
  755. #endif
  756. ;
  757. BOOST_TEST(out.eq(ok.str()));
  758. out.str("");
  759. } // Call b's destructor.
  760. ok.str(""); ok // Dtors always check cv_inv (even if not volatile).
  761. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  762. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  763. << "b::static_inv" << std::endl
  764. #endif
  765. #ifdef BOOST_CONTRACT_TEST_CV_INV
  766. << "b::cv_inv" << std::endl
  767. #endif
  768. #ifdef BOOST_CONTRACT_TEST_CONST_INV
  769. << "b::const_inv" << std::endl
  770. #endif
  771. #endif
  772. #ifndef BOOST_CONTRACT_NO_OLDS
  773. << "b::dtor::old" << std::endl
  774. #endif
  775. << "b::dtor::body" << std::endl
  776. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  777. #ifdef BOOST_CONTRACT_TEST_STATIC_INV
  778. << "b::static_inv" << std::endl
  779. #endif
  780. #endif
  781. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  782. << "b::dtor::post" << std::endl
  783. #endif
  784. ;
  785. BOOST_TEST(out.eq(ok.str()));
  786. return boost::report_errors();
  787. }