• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <boost/detail/lightweight_test.hpp>
11 
12 #define PHOENIX_LIMIT 15
13 #include <boost/spirit/include/phoenix1_primitives.hpp>
14 #include <boost/spirit/include/phoenix1_composite.hpp>
15 #include <boost/spirit/include/phoenix1_functions.hpp>
16 #include <boost/spirit/include/phoenix1_operators.hpp>
17 
18 using namespace phoenix;
19 using namespace std;
20 
21     ///////////////////////////////////////////////////////////////////////////////
22     struct sqr_ {
23 
24         template <typename ArgT>
25         struct result { typedef ArgT type; };
26 
27         template <typename ArgT>
operator ()sqr_28         ArgT operator()(ArgT n) const { return n * n; }
29     };
30 
31     function<sqr_> sqr;
32 
33     ///////////////////////////////////////////////////////////////////////////////
34     struct adder_ {
35 
36         template <typename Arg1T, typename Arg2T, typename ArgT3>
37         struct result { typedef Arg1T type; };
38 
39         template <typename Arg1T, typename Arg2T, typename ArgT3>
operator ()adder_40         Arg1T operator()(Arg1T a, Arg2T b, ArgT3 c) const { return a + b + c; }
41     };
42 
43     function<adder_> adder;
44 
45 ///////////////////////////////////////////////////////////////////////////////
46 int
main()47 main()
48 {
49     int     i2 = 2, i = 4, i50 = 50, i10 = 10, i20 = 20, i100 = 100;
50     double  d5 = 5, d10 = 10;
51     string hello = "hello";
52 
53 ///////////////////////////////////////////////////////////////////////////////
54 //
55 //  More complex expressions
56 //
57 ///////////////////////////////////////////////////////////////////////////////
58     BOOST_TEST((10 - arg1)(i100) == (10 - i100));
59     BOOST_TEST((20 - arg1)(i100) == (20 - i100));
60     BOOST_TEST((arg1 - 10)(i100) == (i100 - 10));
61     BOOST_TEST((arg1 - 20)(i100) == (i100 - 20));
62     BOOST_TEST((arg1 - arg2)(i100, i50) == (i100 - i50));
63     BOOST_TEST((arg1 - var(i))(i10) == (i10 - i));
64     BOOST_TEST((arg1 + arg2 - arg3)(i100, i50, i20) == (i100 + i50 - i20));
65     BOOST_TEST((sqr(arg1) + arg2 - arg3)(i100, i50, i20) == ((i100*i100) + i50 - i20));
66 
67     int ii = i;
68     BOOST_TEST((var(i) += arg1)(i2) == (ii += i2));
69     BOOST_TEST((sqr(sqr(arg1)))(i100) == (i100*i100*i100*i100));
70 
71 
72 #if 0   /*** SHOULD NOT COMPILE ***/
73     (val(3) += arg1)(i100);
74     (val(3) = 3)();
75 #endif
76 
77     BOOST_TEST(((adder(arg1, arg2, arg3) + arg2 - arg3)(i100, i50, i20)) == (i100 + i50 + i20) + i50 - i20);
78     BOOST_TEST((adder(arg1, arg2, arg3)(i100, i50, i20)) == (i100 + i50 + i20));
79     BOOST_TEST((sqr(sqr(sqr(sqr(arg1)))))(d10) == 1e16);
80     BOOST_TEST((sqr(sqr(arg1)) / arg1 / arg1)(d5) == 25);
81 
82     for (int j = 0; j < 20; ++j)
83     {
84         cout << (10 < arg1)(j);
85         BOOST_TEST((10 < arg1)(j) == (10 < j));
86     }
87     cout << endl;
88 
89     for (int k = 0; k < 20; ++k)
90     {
91         bool r = ((arg1 % 2 == 0) && (arg1 < 15))(k);
92         cout << r;
93         BOOST_TEST(r == ((k % 2 == 0) && (k < 15)));
94     }
95     cout << endl;
96 
97 ///////////////////////////////////////////////////////////////////////////////
98 //
99 //  End asserts
100 //
101 ///////////////////////////////////////////////////////////////////////////////
102 
103     return boost::report_errors();
104 }
105