true_.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 true 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<int> >(
  30. boost::bind(eq(), 123, 456) // False.
  31. ) // Test no else (not called).
  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<int> >(
  38. boost::bind(eq(), 123, 123) // True.
  39. ).else_([] { return false; }) // Test else not called.
  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<int>::value>(
  46. boost::bind(eq(), 123, 123) // True.
  47. ).else_( // Test else not called.
  48. boost::bind(eq(), x1, x2) // Compiler-error... but not called.
  49. )
  50. << std::endl;
  51. ok.str(""); ok << true << std::endl;
  52. BOOST_TEST(out.eq(ok.str()));
  53. return boost::report_errors();
  54. }