decl_entry_static_inv_all.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 all derived and base classes with entry static invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  8. #undef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  9. #include "decl.hpp"
  10. #include <boost/preprocessor/control/iif.hpp>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <sstream>
  13. #include <string>
  14. std::string ok_c() {
  15. std::ostringstream ok; ok << "" // Suppress a warning.
  16. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  17. << "a::ctor::pre" << std::endl
  18. << "b::ctor::pre" << std::endl
  19. << "c::ctor::pre" << std::endl
  20. #endif
  21. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  22. << "c::static_inv" << std::endl // Might fail.
  23. #endif
  24. ;
  25. return ok.str();
  26. }
  27. std::string ok_b() {
  28. std::ostringstream ok; ok
  29. #ifndef BOOST_CONTRACT_NO_OLDS
  30. << "c::ctor::old" << std::endl
  31. #endif
  32. << "c::ctor::body" << std::endl
  33. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  34. << "c::static_inv" << std::endl
  35. << "c::inv" << std::endl
  36. #endif
  37. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  38. << "c::ctor::post" << std::endl
  39. #endif
  40. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  41. << "b::static_inv" << std::endl // Might fail.
  42. #endif
  43. ;
  44. return ok.str();
  45. }
  46. std::string ok_a() {
  47. std::ostringstream ok; ok
  48. #ifndef BOOST_CONTRACT_NO_OLDS
  49. << "b::ctor::old" << std::endl
  50. #endif
  51. << "b::ctor::body" << std::endl
  52. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  53. << "b::static_inv" << std::endl
  54. << "b::inv" << std::endl
  55. #endif
  56. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  57. << "b::ctor::post" << std::endl
  58. #endif
  59. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  60. << "a::static_inv" << std::endl // Might fail.
  61. #endif
  62. ;
  63. return ok.str();
  64. }
  65. std::string ok_end() {
  66. std::ostringstream ok; ok
  67. #ifndef BOOST_CONTRACT_NO_OLDS
  68. << "a::ctor::old" << std::endl
  69. #endif
  70. << "a::ctor::body" << std::endl
  71. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  72. << "a::static_inv" << std::endl
  73. << "a::inv" << std::endl
  74. #endif
  75. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  76. << "a::ctor::post" << std::endl
  77. #endif
  78. ;
  79. return ok.str();
  80. }
  81. struct err {}; // Global decl so visible in MSVC10 lambdas.
  82. int main() {
  83. std::ostringstream ok;
  84. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  85. #define BOOST_CONTRACT_TEST_entry_inv 0
  86. #else
  87. #define BOOST_CONTRACT_TEST_entry_inv 1
  88. #endif
  89. a_entry_static_inv = true;
  90. b_entry_static_inv = true;
  91. c_entry_static_inv = true;
  92. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  93. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  94. {
  95. out.str("");
  96. a aa;
  97. ok.str(""); ok // Test nothing failed.
  98. << ok_c()
  99. << ok_b()
  100. << ok_a()
  101. << ok_end()
  102. ;
  103. BOOST_TEST(out.eq(ok.str()));
  104. }
  105. boost::contract::set_entry_invariant_failure(
  106. [] (boost::contract::from) { throw err(); });
  107. a_entry_static_inv = false;
  108. b_entry_static_inv = true;
  109. c_entry_static_inv = true;
  110. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  111. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  112. try {
  113. out.str("");
  114. a aa;
  115. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  116. BOOST_TEST(false);
  117. } catch(err const&) {
  118. #endif
  119. ok.str(""); ok
  120. << ok_c()
  121. << ok_b()
  122. << ok_a() // Test a::static_inv failed.
  123. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  124. << ok_end()
  125. #endif
  126. ;
  127. BOOST_TEST(out.eq(ok.str()));
  128. } catch(...) { BOOST_TEST(false); }
  129. a_entry_static_inv = true;
  130. b_entry_static_inv = false;
  131. c_entry_static_inv = true;
  132. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  133. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  134. try {
  135. out.str("");
  136. a aa;
  137. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  138. BOOST_TEST(false);
  139. } catch(err const&) {
  140. #endif
  141. ok.str(""); ok
  142. << ok_c()
  143. << ok_b() // Test b::static_inv failed.
  144. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  145. << ok_a()
  146. << ok_end()
  147. #endif
  148. ;
  149. BOOST_TEST(out.eq(ok.str()));
  150. } catch(...) { BOOST_TEST(false); }
  151. a_entry_static_inv = true;
  152. b_entry_static_inv = true;
  153. c_entry_static_inv = false;
  154. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  155. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  156. try {
  157. out.str("");
  158. a aa;
  159. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  160. BOOST_TEST(false);
  161. } catch(err const&) {
  162. #endif
  163. ok.str(""); ok
  164. << ok_c() // Test c::static_inv failed.
  165. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  166. << ok_b()
  167. << ok_a()
  168. << ok_end()
  169. #endif
  170. ;
  171. BOOST_TEST(out.eq(ok.str()));
  172. } catch(...) { BOOST_TEST(false); }
  173. a_entry_static_inv = false;
  174. b_entry_static_inv = false;
  175. c_entry_static_inv = false;
  176. a_entering_static_inv = b_entering_static_inv = c_entering_static_inv =
  177. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  178. try {
  179. out.str("");
  180. a aa;
  181. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  182. BOOST_TEST(false);
  183. } catch(err const&) {
  184. #endif
  185. ok.str(""); ok
  186. << ok_c() // Test c::static_inv failed (as all did).
  187. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  188. << ok_b()
  189. << ok_a()
  190. << ok_end()
  191. #endif
  192. ;
  193. BOOST_TEST(out.eq(ok.str()));
  194. } catch(...) { BOOST_TEST(false); }
  195. #undef BOOST_CONTRACT_TEST_entry_inv
  196. return boost::report_errors();
  197. }