decl_pre_all.cpp 5.0 KB

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