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