ifdef.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 contract compilation on/off.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/core/config.hpp>
  8. #include <boost/contract/core/virtual.hpp>
  9. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  10. #include <boost/contract/public_function.hpp>
  11. #include <boost/contract/base_types.hpp>
  12. #include <boost/contract/override.hpp>
  13. #include <boost/contract/check.hpp>
  14. #include <boost/contract/old.hpp>
  15. #endif
  16. #include <boost/detail/lightweight_test.hpp>
  17. #include <sstream>
  18. boost::contract::test::detail::oteststream out;
  19. struct b {
  20. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  21. static void static_invariant() { out << "b::static_inv" << std::endl; }
  22. void invariant() const { out << "b::inv" << std::endl; }
  23. #endif
  24. virtual void f(int x, boost::contract::virtual_* v = 0) = 0;
  25. };
  26. void b::f(int x, boost::contract::virtual_* v) {
  27. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  28. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
  29. #endif
  30. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  31. boost::contract::check c = boost::contract::public_function(v, this)
  32. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  33. .precondition([] { out << "b::f::pre" << std::endl; })
  34. #endif
  35. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  36. .old([] { out << "b::f::old" << std::endl; })
  37. .postcondition([] { out << "b::f::post" << std::endl; })
  38. #endif
  39. ;
  40. #endif
  41. out << "b::f::body" << std::endl;
  42. }
  43. struct a
  44. #define BASES public b
  45. : BASES
  46. {
  47. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  48. typedef BOOST_CONTRACT_BASE_TYPES(BASES) base_types;
  49. BOOST_CONTRACT_OVERRIDE(f)
  50. #endif
  51. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  52. static void static_invariant() { out << "a::static_inv" << std::endl; }
  53. void invariant() const { out << "a::inv" << std::endl; }
  54. #endif
  55. virtual void f(int x, boost::contract::virtual_* v = 0) {
  56. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  57. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(v, x);
  58. #endif
  59. #ifndef BOOST_CONTRACT_NO_PUBLIC_FUNCTIONS
  60. boost::contract::check c = boost::contract::public_function<
  61. override_f>(v, &a::f, this, x)
  62. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  63. .precondition([] { out << "a::f::pre" << std::endl; })
  64. #endif
  65. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  66. .old([] { out << "a::f::old" << std::endl; })
  67. .postcondition([] { out << "a::f::post" << std::endl; })
  68. #endif
  69. ;
  70. #endif
  71. out << "a::f::body" << std::endl;
  72. }
  73. };
  74. int main() {
  75. std::ostringstream ok;
  76. a aa;
  77. out.str("");
  78. aa.f(123);
  79. ok.str(""); ok
  80. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  81. << "b::static_inv" << std::endl
  82. << "b::inv" << std::endl
  83. << "a::static_inv" << std::endl
  84. << "a::inv" << std::endl
  85. #endif
  86. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  87. << "b::f::pre" << std::endl
  88. #endif
  89. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  90. << "b::f::old" << std::endl
  91. << "a::f::old" << std::endl
  92. #endif
  93. << "a::f::body" << std::endl
  94. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  95. << "b::static_inv" << std::endl
  96. << "b::inv" << std::endl
  97. << "a::static_inv" << std::endl
  98. << "a::inv" << std::endl
  99. #endif
  100. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  101. << "b::f::old" << std::endl
  102. << "b::f::post" << std::endl
  103. << "a::f::post" << std::endl
  104. #endif
  105. ;
  106. BOOST_TEST(out.eq(ok.str()));
  107. return boost::report_errors();
  108. }