]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/phoenix/test/bll_compatibility/control_structures.cpp
1 // -- control_structures.cpp -- The Boost Lambda Library ------------------
3 // Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4 // Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
10 // For more information, see www.boost.org
12 // -----------------------------------------------------------------------
14 #include <boost/test/minimal.hpp> // see "Header Implementation Option"
16 #include "boost/lambda/lambda.hpp"
17 #include "boost/lambda/if.hpp"
18 #include "boost/lambda/loops.hpp"
24 using namespace boost
;
26 using boost::lambda::constant
;
27 using boost::lambda::_1
;
28 using boost::lambda::_2
;
29 using boost::lambda::_3
;
30 using boost::lambda::make_const
;
31 using boost::lambda::for_loop
;
32 using boost::lambda::while_loop
;
33 using boost::lambda::do_while_loop
;
34 using boost::lambda::if_then
;
35 using boost::lambda::if_then_else
;
36 using boost::lambda::if_then_else_return
;
38 // 2 container for_each
39 template <class InputIter1
, class InputIter2
, class Function
>
40 Function
for_each(InputIter1 first
, InputIter1 last
,
41 InputIter2 first2
, Function f
) {
42 for ( ; first
!= last
; ++first
, ++first2
)
49 // for loops ---------------------------------------------------------
51 int arithmetic_series
= 0;
52 for_loop(_1
= 0, _1
< 10, _1
++, arithmetic_series
+= _1
)(i
);
53 BOOST_CHECK(arithmetic_series
== 45);
56 for_loop(boost::lambda::var(i
) = 0, boost::lambda::var(i
) < 100, ++boost::lambda::var(i
))();
57 BOOST_CHECK(i
== 100);
59 // while loops -------------------------------------------------------
60 int a
= 0, b
= 0, c
= 0;
62 while_loop((_1
+ _2
) >= (_1
* _2
), (++_1
, ++_2
, ++_3
))(a
, b
, c
);
67 while_loop(_1
++ < 10, ++boost::lambda::var(count
))(i
);
68 BOOST_CHECK(count
== 10);
70 // note that the first parameter of do_while_loop is the condition
72 do_while_loop(_1
++ < 10, ++boost::lambda::var(count
))(i
);
73 BOOST_CHECK(count
== 11);
76 do_while_loop(constant(false), _1
++)(a
);
81 while_loop(--_1
> _2
)(a
, b
);
84 // (the no body case for do_while_loop is pretty redundant)
86 do_while_loop(--_1
> _2
)(a
, b
);
95 if_then(_1
< 0, _1
= 0)(value
);
96 BOOST_CHECK(value
== 42);
99 if_then(_1
< 0, _1
= -_1
)(value
);
100 BOOST_CHECK(value
== 42);
103 if_then_else(_1
< _2
, boost::lambda::var(min
) = _1
, boost::lambda::var(min
) = _2
)
104 (make_const(1), make_const(2));
105 BOOST_CHECK(min
== 1);
107 if_then_else(_1
< _2
, boost::lambda::var(min
) = _1
, boost::lambda::var(min
) = _2
)
108 (make_const(5), make_const(3));
109 BOOST_CHECK(min
== 3);
113 BOOST_CHECK(if_then_else_return(_1
< _2
, _2
, _1
)(x
, y
) == (std::max
)(x
,y
));
114 BOOST_CHECK(if_then_else_return(_1
< _2
, _2
, _1
)(y
, x
) == (std::max
)(x
,y
));
118 int test_main(int, char *[])