decl.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 check (class and macro).
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/check.hpp>
  8. #include <boost/contract/assert.hpp>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <sstream>
  11. boost::contract::test::detail::oteststream out;
  12. struct err {}; // Global decl so visible in MSVC10 lambdas.
  13. void f(bool check) {
  14. #ifdef BOOST_CONTRACT_TEST_CHECK_MACRO
  15. BOOST_CONTRACT_CHECK([&] () -> bool {
  16. out << "f::check" << std::endl;
  17. return check;
  18. }());
  19. #else
  20. boost::contract::check c = [&] {
  21. out << "f::check" << std::endl;
  22. BOOST_CONTRACT_ASSERT(check);
  23. };
  24. #endif
  25. out << "f::body" << std::endl;
  26. }
  27. int main() {
  28. std::ostringstream ok;
  29. out.str("");
  30. f(true);
  31. ok.str(""); ok
  32. #ifndef BOOST_CONTRACT_NO_CHECKS
  33. << "f::check" << std::endl
  34. #endif
  35. << "f::body" << std::endl
  36. ;
  37. BOOST_TEST(out.eq(ok.str()));
  38. boost::contract::set_check_failure([] { throw err(); });
  39. out.str("");
  40. try {
  41. f(false);
  42. #ifndef BOOST_CONTRACT_NO_CHECKS
  43. BOOST_TEST(false);
  44. } catch(err const&) {
  45. ok.str("");
  46. ok << "f::check" << std::endl;
  47. #else
  48. ok.str("");
  49. ok << "f::body" << std::endl;
  50. #endif
  51. BOOST_TEST(out.eq(ok.str()));
  52. } catch(...) { BOOST_TEST(false); }
  53. return boost::report_errors();
  54. }