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/bind.hpp>
13
14 using namespace boost::phoenix;
15 using namespace boost::phoenix::arg_names;
16 using namespace std;
17
18 struct test
19 {
20 typedef void result_type;
operator ()test21 void operator()() const
22 {
23 cout << "Test lazy functions...\n";
24 }
25 };
26
27 struct sqr
28 {
29 template <typename Arg>
30 struct result
31 {
32 typedef Arg type;
33 };
34
35 template <typename Arg>
operator ()sqr36 Arg operator()(Arg n) const
37 {
38 return n * n;
39 }
40 };
41
42 struct fact
43 {
44 template <typename Arg>
45 struct result
46 {
47 typedef Arg type;
48 };
49
50 template <typename Arg>
operator ()fact51 Arg operator()(Arg n) const
52 {
53 return (n <= 0) ? 1 : n * (*this)(n-1);
54 }
55 };
56
57 struct power
58 {
59 template <typename Arg1, typename Arg2>
60 struct result
61 {
62 typedef Arg1 type;
63 };
64
65 template <typename Arg1, typename Arg2>
operator ()power66 Arg1 operator()(Arg1 a, Arg2 b) const
67 {
68 return pow(a, b);
69 }
70 };
71
72 struct add
73 {
74 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
75 struct result
76 {
77 typedef Arg1 type;
78 };
79
80 template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
operator ()add81 Arg1 operator()(Arg1 a, Arg2 b, Arg3 c, Arg4 d) const
82 {
83 return a + b + c + d;
84 }
85 };
86
87 int
main()88 main()
89 {
90 int i5 = 5;
91 double d5 = 5, d3 = 3;
92
93 test()();
94 BOOST_TEST(bind(sqr(), arg1)(i5) == (i5*i5));
95 BOOST_TEST(bind(fact(), 4)() == 24);
96 BOOST_TEST(bind(fact(), arg1)(i5) == 120);
97 BOOST_TEST((int)bind(power(), arg1, arg2)(d5, d3) == (int)pow(d5, d3));
98 BOOST_TEST((bind(sqr(), arg1) + 5)(i5) == ((i5*i5)+5));
99 BOOST_TEST(bind(add(), arg1, arg1, arg1, arg1)(i5) == (5+5+5+5));
100
101 int const ic5 = 5;
102 // testing consts
103 BOOST_TEST(bind(sqr(), arg1)(ic5) == (ic5*ic5));
104
105 // From Steven Watanabe
106 sqr s;
107 int x = 2;
108 int result = bind(ref(s), _1)(x);
109 BOOST_TEST(result == 4);
110
111 return boost::report_errors();
112 }
113