function_tests.cpp 3.2 KB

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