equal_to.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 STL equal_to with call_if.
  6. // C++17 warning from Boost.Bind.
  7. #define _SILENCE_CXX17_ADAPTOR_TYPEDEFS_DEPRECATION_WARNING
  8. #include "../detail/oteststream.hpp"
  9. #include <boost/contract/call_if.hpp>
  10. #include <boost/bind.hpp>
  11. #include <boost/type_traits/has_equal_to.hpp>
  12. #include <boost/detail/lightweight_test.hpp>
  13. #include <functional>
  14. #include <sstream>
  15. #include <ios>
  16. boost::contract::test::detail::oteststream out;
  17. template<typename T>
  18. struct void_equal_to {
  19. typedef void result_type; // Test void result type.
  20. void operator()(T const& left, T const& right) const {
  21. out << (left == right) << std::endl;
  22. }
  23. };
  24. struct x {}; // Doest not have operator==.
  25. int main() {
  26. std::ostringstream ok;
  27. ok << std::boolalpha;
  28. out << std::boolalpha;
  29. x x1, x2;;
  30. out.str("");
  31. out << // Test on true condition with non-void result type.
  32. boost::contract::call_if<boost::has_equal_to<int> >(
  33. boost::bind(std::equal_to<int>(), 123, 123) // True.
  34. ).else_(
  35. // Compiler-error... but not called.
  36. boost::bind(std::equal_to<x>(), x1, x2)
  37. )
  38. << std::endl;
  39. ok.str(""); ok << true << std::endl;
  40. BOOST_TEST(out.eq(ok.str()));
  41. out.str("");
  42. out << // Test on false condition with non-void result type.
  43. boost::contract::call_if<boost::has_equal_to<x> >(
  44. // Compiler-error... but not called.
  45. boost::bind(std::equal_to<x>(), x1, x2)
  46. ).else_([] { return true; })
  47. << std::endl;
  48. ok.str(""); ok << true << std::endl;
  49. BOOST_TEST(out.eq(ok.str()));
  50. out.str("");
  51. // Test on true condition void result type.
  52. boost::contract::call_if<boost::has_equal_to<int> >(
  53. boost::bind(void_equal_to<int>(), 123, 456) // False.
  54. ).else_(
  55. // Compiler-error... but not called.
  56. boost::bind(void_equal_to<x>(), x1, x1)
  57. );
  58. ok.str(""); ok << false << std::endl;
  59. BOOST_TEST(out.eq(ok.str()));
  60. out.str("");
  61. // Test on false condition with void result type.
  62. boost::contract::call_if<boost::has_equal_to<x> >(
  63. // Compiler-error... but not called.
  64. boost::bind(void_equal_to<x>(), x1, x1)
  65. ).else_(
  66. boost::bind(void_equal_to<int>(), 123, 456) // False.
  67. );
  68. ok.str(""); ok << false << std::endl;
  69. BOOST_TEST(out.eq(ok.str()));
  70. return boost::report_errors();
  71. }