ifdef_macro.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 (using macro interface only).
  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. void f(bool check) {
  13. BOOST_CONTRACT_CHECK((
  14. [&] () -> bool {
  15. boost::contract::test::detail::unprotected_commas<void, void, void>
  16. ::call();
  17. out << "f::check" << std::endl;
  18. return check;
  19. }()
  20. ));
  21. out << "f::body" << std::endl;
  22. }
  23. struct err {}; // Global decl so visible in MSVC10 lambdas.
  24. int main() {
  25. std::ostringstream ok;
  26. out.str("");
  27. f(true);
  28. ok.str(""); ok
  29. #ifndef BOOST_CONTRACT_NO_CHECKS
  30. << "f::check" << std::endl
  31. #endif
  32. << "f::body" << std::endl
  33. ;
  34. BOOST_TEST(out.eq(ok.str()));
  35. boost::contract::set_check_failure([] { throw err(); });
  36. out.str("");
  37. try {
  38. f(false);
  39. #ifndef BOOST_CONTRACT_NO_CHECKS
  40. BOOST_TEST(false);
  41. } catch(err const&) {
  42. ok.str("");
  43. ok << "f::check" << std::endl;
  44. #else
  45. ok.str("");
  46. ok << "f::body" << std::endl;
  47. #endif
  48. BOOST_TEST(out.eq(ok.str()));
  49. } catch(...) { BOOST_TEST(false); }
  50. return boost::report_errors();
  51. }