ifdef.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef BOOST_CONTRACT_NO_DESTRUCTORS
  9. #include <boost/contract/destructor.hpp>
  10. #include <boost/contract/check.hpp>
  11. #include <boost/contract/old.hpp>
  12. #endif
  13. #include <boost/detail/lightweight_test.hpp>
  14. #include <sstream>
  15. boost::contract::test::detail::oteststream out;
  16. struct b {
  17. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  18. static void static_invariant() { out << "b::static_inv" << std::endl; }
  19. void invariant() const { out << "b::inv" << std::endl; }
  20. #endif
  21. virtual ~b() {
  22. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  23. boost::contract::old_ptr<int> old_y = BOOST_CONTRACT_OLDOF(y);
  24. #endif
  25. #ifndef BOOST_CONTRACT_NO_DESTRUCTORS
  26. boost::contract::check c = boost::contract::destructor(this)
  27. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  28. .old([] { out << "b::dtor::old" << std::endl; })
  29. .postcondition([] { out << "b::dtor::post" << std::endl; })
  30. #endif
  31. ;
  32. #endif
  33. out << "b::dtor::body" << std::endl;
  34. }
  35. static int y;
  36. };
  37. int b::y = 0;
  38. struct a : public b {
  39. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  40. static void static_invariant() { out << "a::static_inv" << std::endl; }
  41. void invariant() const { out << "a::inv" << std::endl; }
  42. #endif
  43. virtual ~a() {
  44. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  45. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  46. #endif
  47. #ifndef BOOST_CONTRACT_NO_DESTRUCTORS
  48. boost::contract::check c = boost::contract::destructor(this)
  49. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  50. .old([] { out << "a::dtor::old" << std::endl; })
  51. .postcondition([] { out << "a::dtor::post" << std::endl; })
  52. #endif
  53. ;
  54. #endif
  55. out << "a::dtor::body" << std::endl;
  56. }
  57. static int x;
  58. };
  59. int a::x = 0;
  60. int main() {
  61. std::ostringstream ok;
  62. {
  63. a aa;
  64. out.str("");
  65. }
  66. ok.str(""); ok
  67. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  68. << "a::static_inv" << std::endl
  69. << "a::inv" << std::endl
  70. #endif
  71. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  72. << "a::dtor::old" << std::endl
  73. #endif
  74. << "a::dtor::body" << std::endl
  75. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  76. << "a::static_inv" << std::endl
  77. #endif
  78. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  79. << "a::dtor::post" << std::endl
  80. #endif
  81. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  82. << "b::static_inv" << std::endl
  83. << "b::inv" << std::endl
  84. #endif
  85. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  86. << "b::dtor::old" << std::endl
  87. #endif
  88. << "b::dtor::body" << std::endl
  89. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  90. << "b::static_inv" << std::endl
  91. #endif
  92. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  93. << "b::dtor::post" << std::endl
  94. #endif
  95. ;
  96. BOOST_TEST(out.eq(ok.str()));
  97. return boost::report_errors();
  98. }