decl_pre_all.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 with preconditions.
  6. #undef BOOST_CONTRACT_TEST_NO_F_PRE
  7. #include "decl.hpp"
  8. #include <boost/detail/lightweight_test.hpp>
  9. #include <sstream>
  10. #include <string>
  11. std::string ok_f(bool failed = false) {
  12. std::ostringstream ok; ok << "" // Suppress a warning.
  13. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  14. << "f::pre" << std::endl // Test no failure here.
  15. #endif
  16. ;
  17. if(!failed) ok
  18. #ifndef BOOST_CONTRACT_NO_OLDS
  19. << "f::old" << std::endl
  20. #endif
  21. << "f::body" << std::endl
  22. #ifndef BOOST_CONTRACT_NO_POSTCONDITIONS
  23. << "f::post" << std::endl
  24. #endif
  25. ;
  26. return ok.str();
  27. }
  28. struct err {}; // Global decl so visible in MSVC10 lambdas.
  29. int main() {
  30. std::ostringstream ok;
  31. f_pre = true;
  32. out.str("");
  33. f();
  34. ok.str(""); ok
  35. << ok_f()
  36. ;
  37. BOOST_TEST(out.eq(ok.str()));
  38. #ifdef BOOST_CONTRACT_NO_PRECONDITIONS
  39. #define BOOST_CONTRACT_TEST_pre 0
  40. #else
  41. #define BOOST_CONTRACT_TEST_pre 1
  42. #endif
  43. boost::contract::set_precondition_failure(
  44. [] (boost::contract::from) { throw err(); });
  45. f_pre = false;
  46. out.str("");
  47. try {
  48. f();
  49. #ifndef BOOST_CONTRACT_NO_PRECONDITIONS
  50. BOOST_TEST(false);
  51. } catch(err const&) {
  52. #endif
  53. ok.str(""); ok
  54. << ok_f(BOOST_CONTRACT_TEST_pre)
  55. ;
  56. BOOST_TEST(out.eq(ok.str()));
  57. } catch(...) { BOOST_TEST(false); }
  58. #undef BOOST_CONTRACT_TEST_pre
  59. return boost::report_errors();
  60. }