lib_ab.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #include "lib_a.hpp"
  6. #include "lib_b.hpp"
  7. #include "../detail/oteststream.hpp"
  8. #include <boost/contract/core/exception.hpp>
  9. #include <boost/contract/core/config.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. #include <string>
  13. std::string ok_f() {
  14. std::ostringstream ok; ok
  15. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  16. << "a::static_inv" << std::endl
  17. << "a::inv" << std::endl
  18. #endif
  19. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  20. << "a::f::pre" << std::endl
  21. #endif
  22. #ifndef BOOST_CONTRACT_NO_OLDS
  23. << "a::f::old" << std::endl
  24. #endif
  25. << "a::f::body" << std::endl
  26. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  27. << "a::static_inv" << std::endl
  28. << "a::inv" << std::endl
  29. #endif
  30. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  31. << "a::f::post" << std::endl
  32. #endif
  33. ;
  34. return ok.str();
  35. }
  36. int main() {
  37. using boost::contract::test::detail::out;
  38. std::ostringstream ok;
  39. b bb;
  40. out("");
  41. bb.g();
  42. ok.str(""); ok
  43. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  44. << "b::static_inv" << std::endl
  45. << "b::inv" << std::endl
  46. #endif
  47. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  48. << "b::g::pre" << std::endl
  49. #ifdef BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION
  50. // Test preconditions have disabled no contract.
  51. << ok_f()
  52. #else
  53. // Test call while checking executes body (but no contracts).
  54. << "a::f::body" << std::endl
  55. #endif
  56. #endif
  57. #ifndef BOOST_CONTRACT_NO_OLDS
  58. << "b::g::old" << std::endl
  59. #endif
  60. << "b::g::body" << std::endl
  61. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  62. << "b::static_inv" << std::endl
  63. << "b::inv" << std::endl
  64. #endif
  65. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  66. << "b::g::post" << std::endl
  67. // Test call while checking executes body (but no contracts).
  68. << "a::f::body" << std::endl
  69. #endif
  70. ;
  71. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok.str()));
  72. // Test old values not copied for disabled contracts.
  73. #if defined(BOOST_CONTRACT_PRECONDITIONS_DISABLE_NO_ASSERTION) && \
  74. !defined(BOOST_CONTRACT_NO_PRECONDITIONS) && \
  75. !defined(BOOST_CONTRACT_NO_OLDS)
  76. #define BOOST_CONTRACT_TEST_old 1u
  77. #else
  78. #define BOOST_CONTRACT_TEST_old 0u
  79. #endif
  80. BOOST_TEST_EQ(a::x_type::copies(), BOOST_CONTRACT_TEST_old);
  81. BOOST_TEST_EQ(a::x_type::evals(), BOOST_CONTRACT_TEST_old);
  82. BOOST_TEST_EQ(a::x_type::ctors(), a::x_type::dtors());
  83. // Double check a call to f outside another contract checks f's contracts.
  84. out("");
  85. call_f();
  86. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(), ok_f()));
  87. // Test setting failure handlers (from this program using a lib).
  88. a::disable_pre_failure();
  89. out("");
  90. boost::contract::precondition_failure(boost::contract::from());
  91. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  92. "a::pre_failure\n"));
  93. a::disable_post_failure();
  94. out("");
  95. boost::contract::postcondition_failure(boost::contract::from());
  96. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  97. "a::post_failure\n"));
  98. a::disable_entry_inv_failure();
  99. out("");
  100. boost::contract::entry_invariant_failure(boost::contract::from());
  101. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  102. "a::entry_inv_failure\n"));
  103. a::disable_exit_inv_failure();
  104. out("");
  105. boost::contract::exit_invariant_failure(boost::contract::from());
  106. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  107. "a::exit_inv_failure\n"));
  108. a::disable_inv_failure();
  109. out("");
  110. boost::contract::entry_invariant_failure(boost::contract::from());
  111. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  112. "a::inv_failure\n"));
  113. out("");
  114. boost::contract::exit_invariant_failure(boost::contract::from());
  115. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  116. "a::inv_failure\n"));
  117. a::disable_failure();
  118. out("");
  119. boost::contract::precondition_failure(boost::contract::from());
  120. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  121. "a::failure\n"));
  122. out("");
  123. boost::contract::postcondition_failure(boost::contract::from());
  124. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  125. "a::failure\n"));
  126. out("");
  127. boost::contract::entry_invariant_failure(boost::contract::from());
  128. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  129. "a::failure\n"));
  130. out("");
  131. boost::contract::exit_invariant_failure(boost::contract::from());
  132. BOOST_TEST(boost::contract::test::detail::oteststream::eq(out(),
  133. "a::failure\n"));
  134. // Test setting failure handlers (from a lib using another lib).
  135. BOOST_TEST(b::test_disable_pre_failure());
  136. BOOST_TEST(b::test_disable_post_failure());
  137. BOOST_TEST(b::test_disable_entry_inv_failure());
  138. BOOST_TEST(b::test_disable_exit_inv_failure());
  139. BOOST_TEST(b::test_disable_inv_failure());
  140. BOOST_TEST(b::test_disable_failure());
  141. #undef BOOST_CONTRACT_TEST_old
  142. return boost::report_errors();
  143. }