decl.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_DECL_HPP_
  2. #define BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_DECL_HPP_
  3. // Copyright (C) 2008-2018 Lorenzo Caminiti
  4. // Distributed under the Boost Software License, Version 1.0 (see accompanying
  5. // file LICENSE_1_0.txt or a copy at http://www.boost.org/LICENSE_1_0.txt).
  6. // See: http://www.boost.org/doc/libs/release/libs/contract/doc/html/index.html
  7. // Test with and without pre, post, and inv declarations.
  8. #include "../detail/oteststream.hpp"
  9. #include <boost/contract/public_function.hpp>
  10. #include <boost/contract/base_types.hpp>
  11. #include <boost/contract/override.hpp>
  12. #include <boost/contract/check.hpp>
  13. #include <boost/contract/assert.hpp>
  14. boost::contract::test::detail::oteststream out;
  15. bool c_pre = true, c_post = true;
  16. bool c_entering_static_inv = true, c_entry_static_inv = true,
  17. c_exit_static_inv = true;
  18. bool c_entering_inv = true, c_entry_inv = true, c_exit_inv = true;
  19. struct c {
  20. #ifndef BOOST_CONTRACT_TEST_NO_C_STATIC_INV
  21. static void static_invariant() {
  22. out << "c::static_inv" << std::endl;
  23. if(c_entering_static_inv) BOOST_CONTRACT_ASSERT(c_entry_static_inv);
  24. else BOOST_CONTRACT_ASSERT(c_exit_static_inv);
  25. c_entering_static_inv = false;
  26. }
  27. #endif
  28. #ifndef BOOST_CONTRACT_TEST_NO_C_INV
  29. void invariant() const {
  30. out << "c::inv" << std::endl;
  31. if(c_entering_inv) BOOST_CONTRACT_ASSERT(c_entry_inv);
  32. else BOOST_CONTRACT_ASSERT(c_exit_inv);
  33. c_entering_inv = false;
  34. }
  35. #endif
  36. virtual void f(boost::contract::virtual_* v = 0) {
  37. boost::contract::check c = boost::contract::public_function(v, this)
  38. #ifndef BOOST_CONTRACT_TEST_NO_C_PRE
  39. .precondition([] {
  40. out << "c::f::pre" << std::endl;
  41. BOOST_CONTRACT_ASSERT(c_pre);
  42. })
  43. #endif
  44. .old([] { out << "c::f::old" << std::endl; })
  45. #ifndef BOOST_CONTRACT_TEST_NO_C_POST
  46. .postcondition([] {
  47. out << "c::f::post" << std::endl;
  48. BOOST_CONTRACT_ASSERT(c_post);
  49. })
  50. #endif
  51. ;
  52. out << "c::f::body" << std::endl;
  53. }
  54. };
  55. bool b_pre = true, b_post = true;
  56. bool b_entering_static_inv = true, b_entry_static_inv = true,
  57. b_exit_static_inv = true;
  58. bool b_entering_inv = true, b_entry_inv = true, b_exit_inv = true;
  59. struct b
  60. #define BASES public c
  61. : BASES
  62. {
  63. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  64. #undef BASES
  65. #ifndef BOOST_CONTRACT_TEST_NO_B_STATIC_INV
  66. static void static_invariant() {
  67. out << "b::static_inv" << std::endl;
  68. if(b_entering_static_inv) BOOST_CONTRACT_ASSERT(b_entry_static_inv);
  69. else BOOST_CONTRACT_ASSERT(b_exit_static_inv);
  70. b_entering_static_inv = false;
  71. }
  72. #endif
  73. #ifndef BOOST_CONTRACT_TEST_NO_B_INV
  74. void invariant() const {
  75. out << "b::inv" << std::endl;
  76. if(b_entering_inv) BOOST_CONTRACT_ASSERT(b_entry_inv);
  77. else BOOST_CONTRACT_ASSERT(b_exit_inv);
  78. b_entering_inv = false;
  79. }
  80. #endif
  81. virtual void f(boost::contract::virtual_* v = 0) {
  82. boost::contract::check c = boost::contract::public_function(v, this)
  83. #ifndef BOOST_CONTRACT_TEST_NO_B_PRE
  84. .precondition([] {
  85. out << "b::f::pre" << std::endl;
  86. BOOST_CONTRACT_ASSERT(b_pre);
  87. })
  88. #endif
  89. .old([] { out << "b::f::old" << std::endl; })
  90. #ifndef BOOST_CONTRACT_TEST_NO_B_POST
  91. .postcondition([] {
  92. out << "b::f::post" << std::endl;
  93. BOOST_CONTRACT_ASSERT(b_post);
  94. })
  95. #endif
  96. ;
  97. out << "a::f::body" << std::endl;
  98. }
  99. };
  100. bool a_pre = true, a_post = true;
  101. bool a_entering_static_inv = true, a_entry_static_inv = true,
  102. a_exit_static_inv = true;
  103. bool a_entering_inv = true, a_entry_inv = true, a_exit_inv = true;
  104. struct a
  105. #define BASES public b
  106. : BASES
  107. {
  108. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  109. #undef BASES
  110. #ifndef BOOST_CONTRACT_TEST_NO_A_STATIC_INV
  111. static void static_invariant() {
  112. out << "a::static_inv" << std::endl;
  113. if(a_entering_static_inv) BOOST_CONTRACT_ASSERT(a_entry_static_inv);
  114. else BOOST_CONTRACT_ASSERT(a_exit_static_inv);
  115. a_entering_static_inv = false;
  116. }
  117. #endif
  118. #ifndef BOOST_CONTRACT_TEST_NO_A_INV
  119. void invariant() const {
  120. out << "a::inv" << std::endl;
  121. if(a_entering_inv) BOOST_CONTRACT_ASSERT(a_entry_inv);
  122. else BOOST_CONTRACT_ASSERT(a_exit_inv);
  123. a_entering_inv = false;
  124. }
  125. #endif
  126. virtual void f(boost::contract::virtual_* v = 0) /* override */ {
  127. boost::contract::check c = boost::contract::public_function<override_f>(
  128. v, &a::f, this)
  129. #ifndef BOOST_CONTRACT_TEST_NO_A_PRE
  130. .precondition([] {
  131. out << "a::f::pre" << std::endl;
  132. BOOST_CONTRACT_ASSERT(a_pre);
  133. })
  134. #endif
  135. .old([] { out << "a::f::old" << std::endl; })
  136. #ifndef BOOST_CONTRACT_TEST_NO_A_POST
  137. .postcondition([] {
  138. out << "a::f::post" << std::endl;
  139. BOOST_CONTRACT_ASSERT(a_post);
  140. })
  141. #endif
  142. ;
  143. out << "a::f::body" << std::endl;
  144. }
  145. BOOST_CONTRACT_OVERRIDE(f)
  146. };
  147. #endif // #include guard