decl_exit_inv_none.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 without exit invariants.
  6. #define BOOST_CONTRACT_TEST_NO_A_INV
  7. #define BOOST_CONTRACT_TEST_NO_B_INV
  8. #define 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. struct err {}; // Global decl so visible in MSVC10 lambdas.
  14. int main() {
  15. std::ostringstream ok;
  16. ok.str(""); ok // Test nothing fails.
  17. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  18. // No invariants.
  19. << "c::static_inv" << std::endl
  20. << "b::static_inv" << std::endl
  21. << "a::static_inv" << std::endl
  22. #endif
  23. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  24. << "c::f::pre" << std::endl
  25. #endif
  26. #ifndef BOOST_CONTRACT_NO_OLDS
  27. << "c::f::old" << std::endl
  28. << "b::f::old" << std::endl
  29. << "a::f::old" << std::endl
  30. #endif
  31. << "a::f::body" << std::endl
  32. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  33. << "c::static_inv" << std::endl
  34. << "b::static_inv" << std::endl
  35. << "a::static_inv" << std::endl
  36. #endif
  37. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  38. << "c::f::old" << std::endl
  39. << "c::f::post" << std::endl
  40. << "b::f::old" << std::endl
  41. << "b::f::post" << std::endl
  42. << "a::f::post" << std::endl
  43. #endif
  44. ;
  45. boost::contract::set_exit_invariant_failure(
  46. [] (boost::contract::from) { throw err(); });
  47. a aa;
  48. #ifdef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  49. #define BOOST_CONTRACT_TEST_entry_inv 0
  50. #else
  51. #define BOOST_CONTRACT_TEST_entry_inv 1
  52. #endif
  53. a_exit_inv = true;
  54. b_exit_inv = true;
  55. c_exit_inv = true;
  56. a_entering_inv = b_entering_inv = c_entering_inv =
  57. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  58. out.str("");
  59. aa.f();
  60. BOOST_TEST(out.eq(ok.str()));
  61. a_exit_inv = false;
  62. b_exit_inv = true;
  63. c_exit_inv = true;
  64. a_entering_inv = b_entering_inv = c_entering_inv =
  65. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  66. out.str("");
  67. aa.f();
  68. BOOST_TEST(out.eq(ok.str()));
  69. a_exit_inv = true;
  70. b_exit_inv = false;
  71. a_entering_inv = b_entering_inv = c_entering_inv =
  72. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  73. out.str("");
  74. aa.f();
  75. BOOST_TEST(out.eq(ok.str()));
  76. a_exit_inv = true;
  77. b_exit_inv = true;
  78. c_exit_inv = false;
  79. a_entering_inv = b_entering_inv = c_entering_inv =
  80. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  81. out.str("");
  82. aa.f();
  83. BOOST_TEST(out.eq(ok.str()));
  84. a_exit_inv = false;
  85. b_exit_inv = false;
  86. c_exit_inv = false;
  87. a_entering_inv = b_entering_inv = c_entering_inv =
  88. BOOST_PP_IIF(BOOST_CONTRACT_TEST_entry_inv, true, false);
  89. out.str("");
  90. aa.f();
  91. BOOST_TEST(out.eq(ok.str()));
  92. #undef BOOST_CONTRACT_TEST_entry_inv
  93. return boost::report_errors();
  94. }