1// -- control_structures.cpp -- The Boost Lambda Library ------------------
2//
3// Copyright (C) 2000-2003 Jaakko Jarvi (jaakko.jarvi@cs.utu.fi)
4// Copyright (C) 2000-2003 Gary Powell (powellg@amazon.com)
5//
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)
9//
10// For more information, see www.boost.org
11
12// -----------------------------------------------------------------------
13
14#include <boost/test/minimal.hpp> // see "Header Implementation Option"
15
16#include "boost/lambda/lambda.hpp"
17#include "boost/lambda/if.hpp"
18#include "boost/lambda/loops.hpp"
19
20#include <iostream>
21#include <algorithm>
22#include <vector>
23
24using namespace boost;
25
26using boost::lambda::constant;
27using boost::lambda::_1;
28using boost::lambda::_2;
29using boost::lambda::_3;
30using boost::lambda::make_const;
31using boost::lambda::for_loop;
32using boost::lambda::while_loop;
33using boost::lambda::do_while_loop;
34using boost::lambda::if_then;
35using boost::lambda::if_then_else;
36using boost::lambda::if_then_else_return;
37
38// 2 container for_each
39template <class InputIter1, class InputIter2, class Function>
40Function for_each(InputIter1 first, InputIter1 last,
41 InputIter2 first2, Function f) {
42 for ( ; first != last; ++first, ++first2)
43 f(*first, *first2);
44 return f;
45}
46
47void simple_loops() {
48
49 // for loops ---------------------------------------------------------
50 int i;
51 int arithmetic_series = 0;
52 for_loop(a1: _1 = 0, a2: _1 < 10, a3: _1++, a4: arithmetic_series += _1)(i);
53 BOOST_CHECK(arithmetic_series == 45);
54
55 // no body case
56 for_loop(a1: boost::lambda::var(t&: i) = 0, a2: boost::lambda::var(t&: i) < 100, a3: ++boost::lambda::var(t&: i))();
57 BOOST_CHECK(i == 100);
58
59 // while loops -------------------------------------------------------
60 int a = 0, b = 0, c = 0;
61
62 while_loop(a1: (_1 + _2) >= (_1 * _2), a2: (++_1, ++_2, ++_3))(a, b, c);
63 BOOST_CHECK(c == 3);
64
65 int count;
66 count = 0; i = 0;
67 while_loop(a1: _1++ < 10, a2: ++boost::lambda::var(t&: count))(i);
68 BOOST_CHECK(count == 10);
69
70 // note that the first parameter of do_while_loop is the condition
71 count = 0; i = 0;
72 do_while_loop(a1: _1++ < 10, a2: ++boost::lambda::var(t&: count))(i);
73 BOOST_CHECK(count == 11);
74
75 a = 0;
76 do_while_loop(a1: constant(t: false), a2: _1++)(a);
77 BOOST_CHECK(a == 1);
78
79 // no body cases
80 a = 40; b = 30;
81 while_loop(a1: --_1 > _2)(a, b);
82 BOOST_CHECK(a == b);
83
84 // (the no body case for do_while_loop is pretty redundant)
85 a = 40; b = 30;
86 do_while_loop(a1: --_1 > _2)(a, b);
87 BOOST_CHECK(a == b);
88
89
90}
91
92void simple_ifs () {
93
94 int value = 42;
95 if_then(a1: _1 < 0, a2: _1 = 0)(value);
96 BOOST_CHECK(value == 42);
97
98 value = -42;
99 if_then(a1: _1 < 0, a2: _1 = -_1)(value);
100 BOOST_CHECK(value == 42);
101
102 int min;
103 if_then_else(a1: _1 < _2, a2: boost::lambda::var(t&: min) = _1, a3: boost::lambda::var(t&: min) = _2)
104 (make_const(t: 1), make_const(t: 2));
105 BOOST_CHECK(min == 1);
106
107 if_then_else(a1: _1 < _2, a2: boost::lambda::var(t&: min) = _1, a3: boost::lambda::var(t&: min) = _2)
108 (make_const(t: 5), make_const(t: 3));
109 BOOST_CHECK(min == 3);
110
111 int x, y;
112 x = -1; y = 1;
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));
115}
116
117
118int test_main(int, char *[])
119{
120 simple_loops();
121 simple_ifs();
122 return 0;
123}
124

source code of boost/libs/lambda/test/control_structures.cpp