static.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 public static member function contracts.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/base_types.hpp>
  8. #include <boost/contract/public_function.hpp>
  9. #include <boost/contract/check.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. boost::contract::test::detail::oteststream out;
  13. struct b {
  14. static void static_invariant() { out << "b::static_inv" << std::endl; }
  15. void invariant() const { out << "b::inv" << std::endl; }
  16. static void f() {
  17. boost::contract::check c = boost::contract::public_function<b>()
  18. .precondition([] { out << "b::f::pre" << std::endl; })
  19. .old([] { out << "b::f::old" << std::endl; })
  20. .postcondition([] { out << "b::f::post" << std::endl; })
  21. ;
  22. out << "b::f::body" << std::endl;
  23. }
  24. };
  25. struct a
  26. #define BASES public b
  27. : BASES
  28. {
  29. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  30. #undef BASES
  31. static void static_invariant() { out << "a::static_inv" << std::endl; }
  32. void invariant() const { out << "a::inv" << std::endl; }
  33. static void f() {
  34. boost::contract::check c = boost::contract::public_function<a>()
  35. .precondition([] { out << "a::f::pre" << std::endl; })
  36. .old([] { out << "a::f::old" << std::endl; })
  37. .postcondition([] { out << "a::f::post" << std::endl; })
  38. ;
  39. out << "a::f::body" << std::endl;
  40. }
  41. };
  42. int main() {
  43. std::ostringstream ok;
  44. out.str("");
  45. a::f();
  46. ok.str(""); ok
  47. // Static so no object thus only static inv, plus never virtual so subst
  48. // principle does not apply and no subcontracting.
  49. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  50. << "a::static_inv" << std::endl
  51. #endif
  52. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  53. << "a::f::pre" << std::endl
  54. #endif
  55. #ifndef BOOST_CONTRACT_NO_OLDS
  56. << "a::f::old" << std::endl
  57. #endif
  58. << "a::f::body" << std::endl
  59. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  60. << "a::static_inv" << std::endl
  61. #endif
  62. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  63. // No old call here because not base object.
  64. << "a::f::post" << std::endl
  65. #endif
  66. ;
  67. BOOST_TEST(out.eq(ok.str()));
  68. return boost::report_errors();
  69. }