ifdef_macro.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 (including EXCEPT, using macro interface).
  6. #include "../detail/oteststream.hpp"
  7. #include "../detail/unprotected_commas.hpp"
  8. #include <boost/contract_macro.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. boost::contract::test::detail::oteststream out;
  12. struct except_error {};
  13. void f(int x) {
  14. BOOST_CONTRACT_OLD_PTR(
  15. boost::contract::test::detail::unprotected_commas<int, void, void>::
  16. type1
  17. )(
  18. old_x,
  19. (boost::contract::test::detail::unprotected_commas<void, void, void>::
  20. same(x))
  21. );
  22. BOOST_CONTRACT_FUNCTION()
  23. BOOST_CONTRACT_PRECONDITION([] {
  24. boost::contract::test::detail::unprotected_commas<void, void, void>
  25. ::call();
  26. out << "f::pre" << std::endl;
  27. })
  28. BOOST_CONTRACT_OLD([] {
  29. boost::contract::test::detail::unprotected_commas<void, void, void>
  30. ::call();
  31. out << "f::old" << std::endl;
  32. })
  33. BOOST_CONTRACT_POSTCONDITION([] {
  34. boost::contract::test::detail::unprotected_commas<void, void, void>
  35. ::call();
  36. out << "f::post" << std::endl;
  37. })
  38. BOOST_CONTRACT_EXCEPT([] { // Test EXCEPT macro (at least 1 time here).
  39. boost::contract::test::detail::unprotected_commas<void, void, void>
  40. ::call();
  41. out << "f::except" << std::endl;
  42. })
  43. ;
  44. out << "f::body" << std::endl;
  45. if(x == -1) throw except_error();
  46. }
  47. int main() {
  48. std::ostringstream ok;
  49. out.str("");
  50. f(123);
  51. ok.str(""); ok
  52. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  53. << "f::pre" << std::endl
  54. #endif
  55. #ifndef BOOST_CONTRACT_NO_OLDS
  56. << "f::old" << std::endl
  57. #endif
  58. << "f::body" << std::endl
  59. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  60. << "f::post" << std::endl
  61. #endif
  62. ;
  63. BOOST_TEST(out.eq(ok.str()));
  64. boost::contract::set_except_failure([] (boost::contract::from) {});
  65. out.str("");
  66. try {
  67. f(-1); // Test throw and EXCEPT macro (test only here... that's fine).
  68. BOOST_TEST(false);
  69. } catch(except_error const&) {} // OK.
  70. ok.str(""); ok
  71. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  72. << "f::pre" << std::endl
  73. #endif
  74. #ifndef BOOST_CONTRACT_NO_OLDS
  75. << "f::old" << std::endl
  76. #endif
  77. << "f::body" << std::endl
  78. #ifndef BOOST_CONTRACT_NO_EXCEPTS
  79. << "f::except" << std::endl
  80. #endif
  81. ;
  82. BOOST_TEST(out.eq(ok.str()));
  83. return boost::report_errors();
  84. }