ifdef.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #if !defined(BOOST_CONTRACT_NO_CONSTRUCTORS) || \
  9. !defined(BOOST_CONTRACT_NO_PRECONDITIONS)
  10. #include <boost/contract/constructor.hpp>
  11. #endif
  12. #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
  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_PRECONDITIONS
  21. : private boost::contract::constructor_precondition<b>
  22. #endif
  23. {
  24. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  25. static void static_invariant() { out << "b::static_inv" << std::endl; }
  26. void invariant() const { out << "b::inv" << std::endl; }
  27. #endif
  28. explicit b(int x)
  29. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  30. : boost::contract::constructor_precondition<b>([] {
  31. out << "b::ctor::pre" << std::endl;
  32. })
  33. #endif
  34. {
  35. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  36. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  37. #endif
  38. #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
  39. boost::contract::check c = boost::contract::constructor(this)
  40. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  41. .old([] { out << "b::f::old" << std::endl; })
  42. .postcondition([] { out << "b::ctor::post" << std::endl; })
  43. #endif
  44. ;
  45. #endif
  46. out << "b::ctor::body" << std::endl;
  47. }
  48. };
  49. struct a :
  50. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  51. private boost::contract::constructor_precondition<a>,
  52. #endif
  53. public b
  54. {
  55. #ifndef BOOST_CONTRACT_NO_INVARIANTS
  56. static void static_invariant() { out << "a::static_inv" << std::endl; }
  57. void invariant() const { out << "a::inv" << std::endl; }
  58. #endif
  59. explicit a(int x) :
  60. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  61. boost::contract::constructor_precondition<a>([] {
  62. out << "a::ctor::pre" << std::endl;
  63. }),
  64. #endif
  65. b(x)
  66. {
  67. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  68. boost::contract::old_ptr<int> old_x = BOOST_CONTRACT_OLDOF(x);
  69. #endif
  70. #ifndef BOOST_CONTRACT_NO_CONSTRUCTORS
  71. boost::contract::check c = boost::contract::constructor(this)
  72. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  73. .old([] { out << "a::f::old" << std::endl; })
  74. .postcondition([] { out << "a::ctor::post" << std::endl; })
  75. #endif
  76. ;
  77. #endif
  78. out << "a::ctor::body" << std::endl;
  79. }
  80. };
  81. int main() {
  82. std::ostringstream ok;
  83. out.str("");
  84. a aa(123);
  85. ok.str(""); ok
  86. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  87. << "a::ctor::pre" << std::endl
  88. << "b::ctor::pre" << std::endl
  89. #endif
  90. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  91. << "b::static_inv" << std::endl
  92. #endif
  93. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  94. << "b::f::old" << std::endl
  95. #endif
  96. << "b::ctor::body" << std::endl
  97. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  98. << "b::static_inv" << std::endl
  99. << "b::inv" << std::endl
  100. #endif
  101. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  102. << "b::ctor::post" << std::endl
  103. #endif
  104. #ifndef BOOST_CONTRACT_NO_ENTRY_INVARIANTS
  105. << "a::static_inv" << std::endl
  106. #endif
  107. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  108. << "a::f::old" << std::endl
  109. #endif
  110. << "a::ctor::body" << std::endl
  111. #ifndef BOOST_CONTRACT_NO_EXIT_INVARIANTS
  112. << "a::static_inv" << std::endl
  113. << "a::inv" << std::endl
  114. #endif
  115. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  116. << "a::ctor::post" << std::endl
  117. #endif
  118. ;
  119. BOOST_TEST(out.eq(ok.str()));
  120. return boost::report_errors();
  121. }