bind_and_or_test.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include <boost/config.hpp>
  2. #if defined(BOOST_MSVC)
  3. #pragma warning(disable: 4786) // identifier truncated in debug info
  4. #pragma warning(disable: 4710) // function not inlined
  5. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  6. #pragma warning(disable: 4514) // unreferenced inline removed
  7. #endif
  8. //
  9. // bind_and_or_test.cpp - &&, || operators
  10. //
  11. // Copyright (c) 2008 Peter Dimov
  12. //
  13. // Distributed under the Boost Software License, Version 1.0. (See
  14. // accompanying file LICENSE_1_0.txt or copy at
  15. // http://www.boost.org/LICENSE_1_0.txt)
  16. //
  17. #include <boost/bind.hpp>
  18. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  19. #pragma warning(push, 3)
  20. #endif
  21. #include <iostream>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. #pragma warning(pop)
  24. #endif
  25. #include <boost/detail/lightweight_test.hpp>
  26. bool f( bool x )
  27. {
  28. return x;
  29. }
  30. bool g( bool x )
  31. {
  32. return !x;
  33. }
  34. bool h()
  35. {
  36. BOOST_ERROR( "Short-circuit evaluation failure" );
  37. return false;
  38. }
  39. template< class F, class A1, class A2, class R > void test( F f, A1 a1, A2 a2, R r )
  40. {
  41. BOOST_TEST( f( a1, a2 ) == r );
  42. }
  43. int main()
  44. {
  45. // &&
  46. test( boost::bind( f, true ) && boost::bind( g, true ), false, false, f( true ) && g( true ) );
  47. test( boost::bind( f, true ) && boost::bind( g, false ), false, false, f( true ) && g( false ) );
  48. test( boost::bind( f, false ) && boost::bind( h ), false, false, f( false ) && h() );
  49. test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, true, f( true ) && g( true ) );
  50. test( boost::bind( f, _1 ) && boost::bind( g, _2 ), true, false, f( true ) && g( false ) );
  51. test( boost::bind( f, _1 ) && boost::bind( h ), false, false, f( false ) && h() );
  52. // ||
  53. test( boost::bind( f, false ) || boost::bind( g, true ), false, false, f( false ) || g( true ) );
  54. test( boost::bind( f, false ) || boost::bind( g, false ), false, false, f( false ) || g( false ) );
  55. test( boost::bind( f, true ) || boost::bind( h ), false, false, f( true ) || h() );
  56. test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, true, f( false ) || g( true ) );
  57. test( boost::bind( f, _1 ) || boost::bind( g, _2 ), false, false, f( false ) || g( false ) );
  58. test( boost::bind( f, _1 ) || boost::bind( h ), true, false, f( true ) || h() );
  59. //
  60. return boost::report_errors();
  61. }