false_.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 call_if with false condition and non-void result type.
  6. #include "../detail/oteststream.hpp"
  7. #include <boost/contract/call_if.hpp>
  8. #include <boost/bind.hpp>
  9. #include <boost/type_traits/has_equal_to.hpp>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #include <sstream>
  12. #include <ios>
  13. boost::contract::test::detail::oteststream out;
  14. struct eq {
  15. typedef bool result_type; // Test non-void result type.
  16. template<typename L, typename R>
  17. result_type operator()(L left, R right) const {
  18. return left == right; // Requires operator==.
  19. }
  20. };
  21. struct x {}; // Doest not have operator==.
  22. int main() {
  23. std::ostringstream ok;
  24. ok << std::boolalpha;
  25. out << std::boolalpha;
  26. x x1, x2;;
  27. out.str("");
  28. out <<
  29. boost::contract::call_if<boost::has_equal_to<x> >(
  30. boost::bind(eq(), x1, x1) // Compiler-error... but not called.
  31. ).else_([] { return false; }) // Test else.
  32. << std::endl;
  33. ok.str(""); ok << false << std::endl;
  34. BOOST_TEST(out.eq(ok.str()));
  35. out.str("");
  36. out <<
  37. boost::contract::call_if<boost::has_equal_to<x> >(
  38. boost::bind(eq(), x1, x2) // Compiler-error... but not called.
  39. ).else_([] { return true; })
  40. << std::endl;
  41. ok.str(""); ok << true << std::endl;
  42. BOOST_TEST(out.eq(ok.str()));
  43. out.str("");
  44. out << // Test "..._c".
  45. boost::contract::call_if_c<boost::has_equal_to<x>::value>(
  46. boost::bind(eq(), x1, x2) // Compiler-error...but not called.
  47. ).else_([] { return true; })
  48. << std::endl;
  49. ok.str(""); ok << true << std::endl;
  50. BOOST_TEST(out.eq(ok.str()));
  51. return boost::report_errors();
  52. }