• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2007 Joel de Guzman
3     Copyright (c) 2015 John Fletcher
4 
5     Distributed under the Boost Software License, Version 1.0. (See accompanying
6     file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 ==============================================================================*/
8 #include <iostream>
9 #include <cmath>
10 #include <algorithm>
11 #include <vector>
12 
13 #include <boost/phoenix/core/limits.hpp>
14 
15 #include <boost/detail/lightweight_test.hpp>
16 #include <boost/fusion/tuple.hpp>
17 #include <boost/phoenix/core.hpp>
18 #include <boost/phoenix/operator.hpp>
19 #include <boost/phoenix/function.hpp>
20 #include <boost/phoenix/fusion.hpp>
21 #include <boost/phoenix/scope.hpp>
22 
23 #include <typeinfo>
24 
25 namespace fusion = boost::fusion;
26 namespace mpl = boost::mpl;
27 
28 int
main()29 main()
30 {
31     using boost::phoenix::let;
32     using boost::phoenix::val;
33     using boost::phoenix::arg_names::_1;
34     using boost::phoenix::arg_names::_2;
35     using boost::phoenix::arg_names::_3;
36     using boost::phoenix::local_names::_a;
37     using boost::phoenix::local_names::_b;
38     using boost::phoenix::local_names::_c;
39     using boost::phoenix::local_names::_d;
40     using boost::phoenix::local_names::_e;
41     using boost::phoenix::local_names::_x;
42     using boost::phoenix::local_names::_y;
43     using boost::phoenix::local_names::_z;
44     using boost::phoenix::placeholders::arg1;
45 
46     {
47         int x = 1;
48         BOOST_TEST(
49             let(_a = _1)
50             [
51                 _a
52             ]
53             (x) == x
54         )
55         ;
56     }
57 
58     {
59         int x = 1, y = 10;
60         BOOST_TEST(
61             let(_a = _1, _b = _2)
62             [
63                 _a + _b
64             ]
65             (x, y) == x + y
66         );
67     }
68 
69     {
70         int x = 1, y = 10, z = 13;
71         BOOST_TEST(
72             let(_x = _1, _y = _2)
73             [
74                 let(_z = _3)
75                 [
76                     _x + _y + _z
77                 ]
78             ]
79             (x, y, z) == x + y + z
80         );
81     }
82 
83     {
84         int x = 1, y = 10;
85         BOOST_TEST(
86             let(_x = _1)
87             [
88                 _x +
89                     let(_x = _2)
90                     [
91                         -_x
92                     ]
93             ]
94             (x, y) == x + -y
95         );
96     }
97 
98     {
99         int x = 999;
100         BOOST_TEST(
101             let(_x = _1) // _x is a reference to x
102             [
103                 _x += 888
104             ]
105             (x) == 999 + 888
106         );
107 
108         BOOST_TEST(x == 888 + 999);
109     }
110     /*
111     {
112         int x = 999;
113 
114         BOOST_TEST(
115             let(_x = val(_1)) // _x holds x by value
116             [
117                 _x += 888
118             ]
119             (x) == x + 888
120         );
121 
122         BOOST_TEST(x == 999);
123 
124         BOOST_TEST(
125             let(_x = val(_1)) // _x holds x by value
126             [
127                 val(_x += 888)
128             ]
129             (x) == x + 888
130         );
131 
132         BOOST_TEST(x == 999);
133     }
134     */
135     {
136         BOOST_TEST(
137             let(_a = 1, _b = 2, _c = 3, _d = 4, _e = 5)
138             [
139                 _a + _b + _c + _d + _e
140             ]
141             () == 1 + 2 + 3 + 4 + 5
142         );
143     }
144 
145 #ifdef PHOENIX_SHOULD_NOT_COMPILE_TEST
146     {
147         // disallow this:
148         int i;
149         (_a + _b)(i);
150     }
151 #endif
152     /*
153     {
154         // show that we can return a local from an outer scope
155         int y = 0;
156         int x = (let(_a = 1)[let(_b = _1)[ _a ]])(y);
157 
158         BOOST_TEST(x == 1);
159     }
160     */
161     {
162         // show that this code returns an lvalue
163         int i = 1;
164         let(_a = arg1)[ _a ](i)++;
165         BOOST_TEST(i == 2);
166     }
167 
168     {
169         // show that what you put in is what you get out
170         int i = 1;
171         int& j = let(_a = arg1)[_a](i);
172         BOOST_TEST(&i == &j);
173     }
174 
175     {
176         using boost::phoenix::at_c;
177 
178         boost::fusion::tuple<int, int> t = boost::fusion::make_tuple(0, 1);
179         int i = let(_a = at_c<0>(_1))[_a](t);
180 
181         BOOST_TEST( i == 0 );
182     }
183 
184     {
185         int i = 0;
186         let(_a = _1)[_a = _2](i, 2);
187         BOOST_TEST(i == 2);
188     }
189 
190     return boost::report_errors();
191 }
192 
193