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