decl_entry_inv_all.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 invariants.
  6. #undef BOOST_CONTRACT_TEST_NO_A_INV
  7. #undef BOOST_CONTRACT_TEST_NO_B_INV
  8. #undef BOOST_CONTRACT_TEST_NO_C_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_end() {
  15. std::ostringstream ok; ok
  16. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  17. << "c::f::pre" << std::endl
  18. #endif
  19. #ifndef BOOST_CONTRACT_NO_OLDS
  20. << "c::f::old" << std::endl
  21. << "b::f::old" << std::endl
  22. << "a::f::old" << std::endl
  23. #endif
  24. << "a::f::body" << std::endl
  25. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  26. << "c::static_inv" << std::endl
  27. << "c::inv" << std::endl
  28. << "b::static_inv" << std::endl
  29. << "b::inv" << std::endl
  30. << "a::static_inv" << std::endl
  31. << "a::inv" << std::endl
  32. #endif
  33. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  34. << "c::f::old" << std::endl
  35. << "c::f::post" << std::endl
  36. << "b::f::old" << std::endl
  37. << "b::f::post" << std::endl
  38. << "a::f::post" << std::endl
  39. #endif
  40. ;
  41. return ok.str();
  42. }
  43. struct err {}; // Global decl so visible in MSVC10 lambdas.
  44. int main() {
  45. std::ostringstream ok;
  46. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  47. #define BOOST_CONTRACT_TEST_entry_inv 0
  48. #else
  49. #define BOOST_CONTRACT_TEST_entry_inv 1
  50. #endif
  51. a aa;
  52. a_entry_inv = true;
  53. b_entry_inv = true;
  54. c_entry_inv = true;
  55. a_entering_inv = b_entering_inv = c_entering_inv =
  56. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  57. out.str("");
  58. aa.f();
  59. ok.str(""); ok // Test nothing failed.
  60. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  61. << "c::static_inv" << std::endl
  62. << "c::inv" << std::endl
  63. << "b::static_inv" << std::endl
  64. << "b::inv" << std::endl
  65. << "a::static_inv" << std::endl
  66. << "a::inv" << std::endl
  67. #endif
  68. << ok_end()
  69. ;
  70. BOOST_TEST(out.eq(ok.str()));
  71. boost::contract::set_entry_invariant_failure(
  72. [] (boost::contract::from) { throw err(); });
  73. a_entry_inv = false;
  74. b_entry_inv = true;
  75. c_entry_inv = true;
  76. a_entering_inv = b_entering_inv = c_entering_inv =
  77. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  78. out.str("");
  79. try {
  80. aa.f();
  81. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  82. BOOST_TEST(false);
  83. } catch(err const&) {
  84. #endif
  85. ok.str(""); ok
  86. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  87. << "c::static_inv" << std::endl
  88. << "c::inv" << std::endl
  89. << "b::static_inv" << std::endl
  90. << "b::inv" << std::endl
  91. << "a::static_inv" << std::endl
  92. << "a::inv" << std::endl // Test this failed.
  93. #else
  94. << ok_end()
  95. #endif
  96. ;
  97. BOOST_TEST(out.eq(ok.str()));
  98. } catch(...) { BOOST_TEST(false); }
  99. a_entry_inv = true;
  100. b_entry_inv = false;
  101. c_entry_inv = true;
  102. a_entering_inv = b_entering_inv = c_entering_inv =
  103. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  104. out.str("");
  105. try {
  106. aa.f();
  107. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  108. BOOST_TEST(false);
  109. } catch(err const&) {
  110. #endif
  111. ok.str(""); ok
  112. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  113. << "c::static_inv" << std::endl
  114. << "c::inv" << std::endl
  115. << "b::static_inv" << std::endl
  116. << "b::inv" << std::endl // Test this failed.
  117. #else
  118. << ok_end()
  119. #endif
  120. ;
  121. BOOST_TEST(out.eq(ok.str()));
  122. } catch(...) { BOOST_TEST(false); }
  123. a_entry_inv = true;
  124. b_entry_inv = true;
  125. c_entry_inv = false;
  126. a_entering_inv = b_entering_inv = c_entering_inv =
  127. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  128. out.str("");
  129. try {
  130. aa.f();
  131. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  132. BOOST_TEST(false);
  133. } catch(err const&) {
  134. #endif
  135. ok.str(""); ok
  136. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  137. << "c::static_inv" << std::endl
  138. << "c::inv" << std::endl // Test this failed.
  139. #else
  140. << ok_end()
  141. #endif
  142. ;
  143. BOOST_TEST(out.eq(ok.str()));
  144. } catch(...) { BOOST_TEST(false); }
  145. a_entry_inv = false;
  146. b_entry_inv = false;
  147. c_entry_inv = false;
  148. a_entering_inv = b_entering_inv = c_entering_inv =
  149. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  150. out.str("");
  151. try {
  152. aa.f();
  153. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  154. BOOST_TEST(false);
  155. } catch(err const&) {
  156. #endif
  157. ok.str(""); ok
  158. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  159. << "c::static_inv" << std::endl
  160. << "c::inv" << std::endl // Test this failed (as all did).
  161. #else
  162. << ok_end()
  163. #endif
  164. ;
  165. BOOST_TEST(out.eq(ok.str()));
  166. } catch(...) { BOOST_TEST(false); }
  167. return boost::report_errors();
  168. }