bind_and_or_test.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*==============================================================================
  2. Copyright (c) 2008 Peter Dimov
  3. Copyright (c) 2005-2010 Joel de Guzman
  4. Copyright (c) 2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/phoenix/core.hpp>
  9. #include <boost/phoenix/bind.hpp>
  10. #include <boost/phoenix/operator.hpp>
  11. #include <iostream>
  12. #include <boost/detail/lightweight_test.hpp>
  13. bool
  14. f(bool x)
  15. {
  16. return x;
  17. }
  18. bool
  19. g(bool x)
  20. {
  21. return !x;
  22. }
  23. bool
  24. h()
  25. {
  26. BOOST_ERROR("Short-circuit evaluation failure");
  27. return false;
  28. }
  29. template <typename F, typename A1, typename A2, typename R>
  30. void
  31. tester(F f, A1 a1, A2 a2, R r)
  32. {
  33. BOOST_TEST(f(a1, a2) == r);
  34. }
  35. int main()
  36. {
  37. using boost::phoenix::bind;
  38. using boost::phoenix::placeholders::_1;
  39. using boost::phoenix::placeholders::_2;
  40. // &&
  41. tester(bind(f, true) && bind(g, true), false, false, f(true) && g(true));
  42. tester(bind(f, true) && bind(g, false), false, false, f(true) && g(false));
  43. tester(bind(f, false) && bind(h), false, false, f(false) && h());
  44. tester(bind(f, _1) && bind(g, _2), true, true, f(true) && g(true));
  45. tester(bind(f, _1) && bind(g, _2), true, false, f(true) && g(false));
  46. tester(bind(f, _1) && bind(h), false, false, f(false) && h());
  47. // ||
  48. tester(bind(f, false) || bind(g, true), false, false, f(false) || g(true));
  49. tester(bind(f, false) || bind(g, false), false, false, f(false) || g(false));
  50. tester(bind(f, true) || bind(h), false, false, f(true) || h());
  51. tester(bind(f, _1) || bind(g, _2), false, true, f(false) || g(true));
  52. tester(bind(f, _1) || bind(g, _2), false, false, f(false) || g(false));
  53. tester(bind(f, _1) || bind(h), true, false, f(true) || h());
  54. //
  55. return boost::report_errors();
  56. }