smoke.hpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_CONTRACTS_HPP_
  2. #define BOOST_CONTRACT_TEST_PUBLIC_FUNCTION_CONTRACTS_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 public member function subcontracting (also with old and return values).
  8. #include "../detail/oteststream.hpp"
  9. #include "../detail/counter.hpp"
  10. #include <boost/contract/public_function.hpp>
  11. #include <boost/contract/base_types.hpp>
  12. #include <boost/contract/assert.hpp>
  13. #include <boost/contract/old.hpp>
  14. #include <boost/contract/check.hpp>
  15. #include <boost/contract/override.hpp>
  16. #include <boost/config.hpp>
  17. #include <string>
  18. boost::contract::test::detail::oteststream out;
  19. struct s_tag;
  20. typedef boost::contract::test::detail::counter<s_tag, std::string> s_type;
  21. struct except_error {};
  22. struct result_type {
  23. std::string value;
  24. explicit result_type(std::string const& s) : value(s) {}
  25. private: // Test non-copyable and non-default-constructible result.
  26. result_type();
  27. result_type(result_type const&);
  28. result_type& operator=(result_type const&);
  29. };
  30. // Test base without additional bases and pure virtual.
  31. template<char Id>
  32. struct t {
  33. static void static_invariant() { out << Id << "::static_inv" << std::endl; }
  34. void invariant() const {
  35. out << Id << "::inv" << std::endl;
  36. BOOST_CONTRACT_ASSERT(z.value != "");
  37. }
  38. struct z_tag;
  39. typedef boost::contract::test::detail::counter<z_tag, std::string> z_type;
  40. z_type z;
  41. t() { z.value.push_back(Id); }
  42. virtual result_type& f(s_type& s, boost::contract::virtual_* v = 0) = 0;
  43. };
  44. template<char Id> // Requires: Only pass lower case Id so it'll never be 'X'.
  45. result_type& t<Id>::f(s_type& s, boost::contract::virtual_* v) {
  46. std::ostringstream r; r << "none-" << Id;
  47. static result_type result(r.str());
  48. boost::contract::old_ptr<z_type> old_z =
  49. BOOST_CONTRACT_OLDOF(v, z_type::eval(z));
  50. boost::contract::old_ptr<s_type> old_s;
  51. boost::contract::check c = boost::contract::public_function(v, result, this)
  52. .precondition([&] {
  53. out << Id << "::f::pre" << std::endl;
  54. BOOST_CONTRACT_ASSERT(s.value[0] == Id || s.value[0] == 'X');
  55. })
  56. .old([&] {
  57. out << Id << "::f::old" << std::endl;
  58. old_s = BOOST_CONTRACT_OLDOF(v, s_type::eval(s));
  59. })
  60. .postcondition([&] (result_type const& result) {
  61. out << Id << "::f::post" << std::endl;
  62. BOOST_CONTRACT_ASSERT(z.value == old_z->value + old_s->value);
  63. BOOST_CONTRACT_ASSERT(s.value.find(old_z->value) !=
  64. std::string::npos);
  65. BOOST_CONTRACT_ASSERT(result.value == old_s->value);
  66. })
  67. .except([&] {
  68. out << Id << "::f::except" << std::endl;
  69. BOOST_CONTRACT_ASSERT(z.value == old_z->value);
  70. BOOST_CONTRACT_ASSERT(s.value == old_s->value);
  71. })
  72. ;
  73. out << "t<" << Id << ">::f::body" << std::endl;
  74. if(s.value == "X") throw except_error();
  75. return result;
  76. }
  77. // Test base with other bases, multiple inheritance, and no subcontracting from
  78. // protected and private bases (even if fully contracted).
  79. struct c
  80. #define BASES public t<'d'>, protected t<'p'>, private t<'q'>, public t<'e'>
  81. : BASES
  82. {
  83. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  84. #undef BASES
  85. static void static_invariant() { out << "c::static_inv" << std::endl; }
  86. void invariant() const {
  87. out << "c::inv" << std::endl;
  88. BOOST_CONTRACT_ASSERT(y.value != "");
  89. }
  90. struct y_tag;
  91. typedef boost::contract::test::detail::counter<y_tag, std::string> y_type;
  92. y_type y;
  93. c() { y.value = "c"; }
  94. virtual result_type& f(s_type& s, boost::contract::virtual_* v = 0)
  95. /* override */ {
  96. static result_type result("none-c");
  97. boost::contract::old_ptr<y_type> old_y =
  98. BOOST_CONTRACT_OLDOF(v, y_type::eval(y));
  99. boost::contract::old_ptr<s_type> old_s;
  100. boost::contract::check c = boost::contract::public_function<
  101. override_f>(v, result, &c::f, this, s)
  102. .precondition([&] {
  103. out << "c::f::pre" << std::endl;
  104. BOOST_CONTRACT_ASSERT(s.value == "C" || s.value == "X");
  105. })
  106. .old([&] {
  107. out << "c::f::old" << std::endl;
  108. old_s = BOOST_CONTRACT_OLDOF(v, s_type::eval(s));
  109. })
  110. .postcondition([&] (result_type const& result) {
  111. out << "c::f::post" << std::endl;
  112. BOOST_CONTRACT_ASSERT(y.value == old_y->value + old_s->value);
  113. BOOST_CONTRACT_ASSERT(s.value.find(old_y->value) !=
  114. std::string::npos);
  115. BOOST_CONTRACT_ASSERT(result.value == old_s->value);
  116. })
  117. .except([&] {
  118. out << "c::f::except" << std::endl;
  119. BOOST_CONTRACT_ASSERT(y.value == old_y->value);
  120. BOOST_CONTRACT_ASSERT(s.value == old_s->value);
  121. })
  122. ;
  123. out << "c::f::body" << std::endl;
  124. if(s.value == "X") throw except_error();
  125. std::string save_s = s.value;
  126. std::string save = y.value;
  127. y.value += save_s;
  128. s.value = save;
  129. save = t<'d'>::z.value;
  130. t<'d'>::z.value += save_s;
  131. s.value += save;
  132. save = t<'e'>::z.value;
  133. t<'e'>::z.value += save_s;
  134. s.value += save;
  135. result.value = save_s;
  136. return result;
  137. }
  138. BOOST_CONTRACT_OVERRIDE(f)
  139. };
  140. // Test no subcontracting from not (fully) contracted base.
  141. struct b {
  142. static void static_invariant() { out << "b::static_inv" << std::endl; }
  143. void invariant() const { out << "b::inv" << std::endl; }
  144. virtual ~b() {}
  145. // No contract (no virtual_ so this is not actually overridden by a::f).
  146. virtual result_type& f(s_type& s) {
  147. static result_type result("none-b");
  148. out << "b::f::body" << std::endl;
  149. result.value = s.value;
  150. return result;
  151. }
  152. };
  153. // Test public function with both non-contracted and contracted bases.
  154. struct a
  155. #define BASES public b, public c
  156. : BASES
  157. {
  158. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  159. #undef BASES
  160. static void static_invariant() { out << "a::static_inv" << std::endl; }
  161. void invariant() const {
  162. out << "a::inv" << std::endl;
  163. BOOST_CONTRACT_ASSERT(x.value != "");
  164. }
  165. struct x_tag;
  166. typedef boost::contract::test::detail::counter<x_tag, std::string> x_type;
  167. x_type x;
  168. a() { x.value = "a"; }
  169. #if defined(BOOST_GCC)
  170. #pragma GCC diagnostic push
  171. #pragma GCC diagnostic ignored "-Woverloaded-virtual" // For a::f.
  172. #elif defined(BOOST_CLANG)
  173. #pragma clang diagnostic push
  174. #pragma clang diagnostic ignored "-Woverloaded-virtual" // For a::f.
  175. #endif
  176. // Must use virtual_ even if no longer decl virtual for correct overloading.
  177. // NOTE: This intentionally hides but does not override `b::f` (it overrides
  178. // `c::f` instead). This generates warnings on some compilers (Clang, etc.).
  179. result_type& f(s_type& s, boost::contract::virtual_* v = 0)
  180. /* override */ {
  181. static result_type result("none-a");
  182. boost::contract::old_ptr<x_type> old_x =
  183. BOOST_CONTRACT_OLDOF(v, x_type::eval(x));
  184. boost::contract::old_ptr<s_type> old_s;
  185. boost::contract::check c = boost::contract::public_function<
  186. override_f>(v, result, &a::f, this, s)
  187. .precondition([&] {
  188. out << "a::f::pre" << std::endl;
  189. BOOST_CONTRACT_ASSERT(s.value == "A" || s.value == "X");
  190. })
  191. .old([&] {
  192. out << "a::f::old" << std::endl;
  193. old_s = BOOST_CONTRACT_OLDOF(v, s_type::eval(s));
  194. })
  195. .postcondition([&] (result_type const& result) {
  196. out << "a::f::post" << std::endl;
  197. BOOST_CONTRACT_ASSERT(x.value == old_x->value + old_s->value);
  198. BOOST_CONTRACT_ASSERT(s.value.find(old_x->value) !=
  199. std::string::npos);
  200. BOOST_CONTRACT_ASSERT(result.value == old_s->value);
  201. })
  202. .except([&] {
  203. out << "a::f::except" << std::endl;
  204. BOOST_CONTRACT_ASSERT(x.value == old_x->value);
  205. BOOST_CONTRACT_ASSERT(s.value == old_s->value);
  206. })
  207. ;
  208. out << "a::f::body" << std::endl;
  209. if(s.value == "X") throw except_error();
  210. std::string save_s = s.value;
  211. std::string save = x.value;
  212. x.value += save_s;
  213. s.value = save;
  214. save = y.value;
  215. y.value += save_s;
  216. s.value += save;
  217. save = t<'d'>::z.value;
  218. t<'d'>::z.value += save_s;
  219. s.value += save;
  220. save = t<'e'>::z.value;
  221. t<'e'>::z.value += save_s;
  222. s.value += save;
  223. result.value = save_s;
  224. return result;
  225. }
  226. BOOST_CONTRACT_OVERRIDE(f)
  227. #if defined(BOOST_GCC)
  228. #pragma GCC diagnostic pop
  229. #elif defined(BOOST_CLANG)
  230. #pragma clang diagnostic pop
  231. #endif
  232. };
  233. #endif // #include guard