true_void.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 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 void result_type; // Test void result type.
  16. template<typename L, typename R>
  17. result_type operator()(L left, R right) const {
  18. out << (left == right) << std::endl; // 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. boost::contract::call_if<boost::has_equal_to<int> >(
  29. boost::bind(eq(), 123, 456) // False.
  30. ); // Test no else (not called).
  31. ok.str(""); ok << false << std::endl;
  32. BOOST_TEST(out.eq(ok.str()));
  33. out.str("");
  34. boost::contract::call_if<boost::has_equal_to<int> >(
  35. boost::bind(eq(), 123, 123) // True.
  36. ).else_(
  37. [] { return false; }
  38. ); // Test else (not called).
  39. ok.str(""); ok << true << std::endl;
  40. BOOST_TEST(out.eq(ok.str()));
  41. out.str("");
  42. // Test "..._c".
  43. boost::contract::call_if_c<boost::has_equal_to<int>::value>(
  44. boost::bind(eq(), 123, 123) // True.
  45. ).else_( // Test else (not called).
  46. boost::bind(eq(), x1, x2) // Compiler-error... but not called.
  47. );
  48. ok.str(""); ok << true << std::endl;
  49. BOOST_TEST(out.eq(ok.str()));
  50. return boost::report_errors();
  51. }