• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2007 Joel de Guzman
3 
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/function.hpp>
13 
14 using namespace boost::phoenix;
15 using namespace boost::phoenix::arg_names;
16 using namespace std;
17 
18     struct test_impl
19     {
20         typedef void result_type;
operator ()test_impl21         void operator()() const
22         {
23             cout << "Test lazy functions...\n";
24         }
25     };
26 
27     function<test_impl> test;
28 
29     struct sqr_impl
30     {
31         template <typename Arg>
32         struct result
33         {
34             typedef Arg type;
35         };
36 
37         template <typename Arg>
operator ()sqr_impl38         Arg operator()(Arg n) const
39         {
40             return n * n;
41         }
42     };
43 
44     function<sqr_impl> sqr;
45 
46     struct fact_impl
47     {
48         template <typename Arg>
49         struct result
50         {
51             typedef Arg type;
52         };
53 
54         template <typename Arg>
operator ()fact_impl55         Arg operator()(Arg n) const
56         {
57             return (n <= 0) ? 1 : n * (*this)(n-1);
58         }
59     };
60 
61     function<fact_impl> fact;
62 
63     struct pow_impl
64     {
65         template <typename Arg1, typename Arg2>
66         struct result
67         {
68             typedef Arg1 type;
69         };
70 
71         template <typename Arg1, typename Arg2>
operator ()pow_impl72         Arg1 operator()(Arg1 a, Arg2 b) const
73         {
74             return pow(a, b);
75         }
76     };
77 
78     function<pow_impl> power;
79 
80     struct add_impl
81     {
82         template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
83         struct result
84         {
85             typedef Arg1 type;
86         };
87 
88         template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
operator ()add_impl89         Arg1 operator()(Arg1 a, Arg2 b, Arg3 c, Arg4 d) const
90         {
91             return a + b + c + d;
92         }
93     };
94 
95     function<add_impl> add;
96 
97 int
main()98 main()
99 {
100     int i5 = 5;
101     double d5 = 5, d3 = 3;
102 
103     test()();
104     BOOST_TEST(sqr(arg1)(i5) == (i5*i5));
105     BOOST_TEST(fact(4)() == 24);
106     BOOST_TEST(fact(arg1)(i5) == 120);
107     BOOST_TEST((int)power(arg1, arg2)(d5, d3) == (int)pow(d5, d3));
108     BOOST_TEST((sqr(arg1) + 5)(i5) == ((i5*i5)+5));
109     BOOST_TEST(add(arg1, arg1, arg1, arg1)(i5) == (5+5+5+5));
110 
111     int const ic5 = 5;
112     // testing consts
113     BOOST_TEST(sqr(arg1)(ic5) == (ic5*ic5));
114 
115     return boost::report_errors();
116 }
117