functors_tests.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*=============================================================================
  2. Phoenix V1.2.1
  3. Copyright (c) 2001-2003 Joel de Guzman
  4. Use, modification and distribution is subject to the Boost Software
  5. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <iostream>
  9. #include <cmath>
  10. #include <boost/detail/lightweight_test.hpp>
  11. #define PHOENIX_LIMIT 15
  12. #include <boost/spirit/include/phoenix1_primitives.hpp>
  13. #include <boost/spirit/include/phoenix1_composite.hpp>
  14. #include <boost/spirit/include/phoenix1_functions.hpp>
  15. #include <boost/spirit/include/phoenix1_operators.hpp>
  16. using namespace phoenix;
  17. using namespace std;
  18. ///////////////////////////////////////////////////////////////////////////////
  19. struct test_ {
  20. typedef void result_type;
  21. void operator()() const { cout << "TEST LAZY FUNCTION\n"; }
  22. };
  23. function<test_> test;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. struct sqr_ {
  26. template <typename ArgT>
  27. struct result { typedef ArgT type; };
  28. template <typename ArgT>
  29. ArgT operator()(ArgT n) const { return n * n; }
  30. };
  31. function<sqr_> sqr;
  32. ///////////////////////////////////////////////////////////////////////////////
  33. struct fact_ {
  34. template <typename ArgT>
  35. struct result { typedef ArgT type; };
  36. template <typename ArgT>
  37. ArgT operator()(ArgT n) const
  38. { return (n <= 0) ? 1 : n * this->operator()(n-1); }
  39. };
  40. function<fact_> fact;
  41. ///////////////////////////////////////////////////////////////////////////////
  42. struct pow_ {
  43. template <typename Arg1T, typename Arg2T>
  44. struct result { typedef Arg1T type; };
  45. template <typename Arg1T, typename Arg2T>
  46. Arg1T operator()(Arg1T a, Arg2T b) const { return pow(a, b); }
  47. };
  48. function<pow_> power;
  49. ///////////////////////////////////////////////////////////////////////////////
  50. int
  51. main()
  52. {
  53. int i5 = 5;
  54. double d5 = 5, d3 = 3;
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //
  57. // Lazy functors
  58. //
  59. ///////////////////////////////////////////////////////////////////////////////
  60. test()();
  61. BOOST_TEST(sqr(arg1)(i5) == (i5*i5));
  62. BOOST_TEST(fact(4)() == 24);
  63. BOOST_TEST(fact(arg1)(i5) == 120);
  64. BOOST_TEST((int)power(arg1, arg2)(d5, d3) == (int)pow(d5, d3));
  65. BOOST_TEST((sqr(arg1) + 5)(i5) == ((i5*i5)+5));
  66. ///////////////////////////////////////////////////////////////////////////////
  67. //
  68. // End asserts
  69. //
  70. ///////////////////////////////////////////////////////////////////////////////
  71. return boost::report_errors();
  72. }