false_void.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 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<x> >(
  29. boost::bind(eq(), x1, x2) // Compiler-error... but not called.
  30. ); // Test no else.
  31. ok.str("");
  32. BOOST_TEST(out.eq(ok.str()));
  33. out.str("");
  34. // Test "..._c".
  35. boost::contract::call_if_c<boost::has_equal_to<x>::value>(
  36. boost::bind(eq(), x1, x2) // Compiler-error...but not called.
  37. ).else_(
  38. [] { out << true << std::endl; } // Test else.
  39. );
  40. ok.str(""); ok << true << std::endl;
  41. BOOST_TEST(out.eq(ok.str()));
  42. return boost::report_errors();
  43. }