bind_function_object_tests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Copyright (c) 2015 John Fletcher
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <iostream>
  8. #include <cmath>
  9. #include <boost/detail/lightweight_test.hpp>
  10. #include <boost/phoenix/core.hpp>
  11. #include <boost/phoenix/operator.hpp>
  12. #include <boost/phoenix/bind.hpp>
  13. namespace phoenix = boost::phoenix;
  14. using std::cout;
  15. using std::pow;
  16. struct test
  17. {
  18. typedef void result_type;
  19. void
  20. operator()() const
  21. {
  22. cout << "Test lazy functions...\n";
  23. }
  24. };
  25. struct sqr
  26. {
  27. template <typename Sig>
  28. struct result;
  29. template <typename This, typename Arg>
  30. struct result<This(Arg&)>
  31. {
  32. typedef Arg type;
  33. };
  34. template <typename Arg>
  35. Arg
  36. operator()(Arg n) const
  37. {
  38. return n * n;
  39. }
  40. };
  41. struct fact
  42. {
  43. template <typename Sig>
  44. struct result;
  45. template <typename This, typename Arg>
  46. struct result<This(Arg&)>
  47. {
  48. typedef Arg type;
  49. };
  50. template <typename Arg>
  51. Arg
  52. operator()(Arg n) const
  53. {
  54. return (n <= 0) ? 1 : n * (*this)(n-1);
  55. }
  56. };
  57. struct power
  58. {
  59. template<typename Sig>
  60. struct result;
  61. template<typename This, typename Arg1, typename Arg2>
  62. struct result<This(Arg1&, Arg2&)>
  63. {
  64. typedef Arg1 type;
  65. };
  66. template <typename Arg1, typename Arg2>
  67. Arg1
  68. operator()(Arg1 a, Arg2 b) const
  69. {
  70. return pow(a, b);
  71. }
  72. };
  73. struct add
  74. {
  75. template <typename Sig>
  76. struct result;
  77. template <typename This, typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  78. struct result<This(Arg1&, Arg2&, Arg3&, Arg4&)>
  79. {
  80. typedef Arg1 type;
  81. };
  82. template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
  83. Arg1
  84. operator()(Arg1 a, Arg2 b, Arg3 c, Arg4 d) const
  85. {
  86. return a + b + c + d;
  87. }
  88. };
  89. int
  90. main()
  91. {
  92. using phoenix::bind;
  93. using phoenix::ref;
  94. using phoenix::arg_names::_1;
  95. using phoenix::arg_names::arg1;
  96. using phoenix::arg_names::arg2;
  97. int i5 = 5;
  98. double d5 = 5, d3 = 3;
  99. test()();
  100. BOOST_TEST(bind(sqr(), arg1)(i5) == (i5*i5));
  101. BOOST_TEST(bind(fact(), 4)() == 24);
  102. BOOST_TEST(bind(fact(), arg1)(i5) == 120);
  103. BOOST_TEST((int)bind(power(), arg1, arg2)(d5, d3) == (int)std::pow(d5, d3));
  104. BOOST_TEST((bind(sqr(), arg1) + 5)(i5) == ((i5*i5)+5));
  105. BOOST_TEST(bind(add(), arg1, arg1, arg1, arg1)(i5) == (5+5+5+5));
  106. int const ic5 = 5;
  107. // testing consts
  108. BOOST_TEST(bind(sqr(), arg1)(ic5) == (ic5*ic5));
  109. // From Steven Watanabe
  110. sqr s;
  111. int x = 2;
  112. int result = bind(ref(s), _1)(x);
  113. BOOST_TEST(result == 4);
  114. return boost::report_errors();
  115. }