]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | ||
1e59de90 TL |
14 | #include <boost/core/lightweight_test.hpp> |
15 | #define BOOST_CHECK BOOST_TEST | |
7c673cae FG |
16 | |
17 | #include "boost/lambda/lambda.hpp" | |
18 | #include "boost/lambda/if.hpp" | |
19 | #include "boost/lambda/loops.hpp" | |
20 | ||
21 | #include <iostream> | |
22 | #include <algorithm> | |
23 | #include <vector> | |
24 | ||
25 | using namespace boost; | |
26 | ||
27 | using boost::lambda::constant; | |
28 | using boost::lambda::_1; | |
29 | using boost::lambda::_2; | |
30 | using boost::lambda::_3; | |
31 | using boost::lambda::make_const; | |
32 | using boost::lambda::for_loop; | |
33 | using boost::lambda::while_loop; | |
34 | using boost::lambda::do_while_loop; | |
35 | using boost::lambda::if_then; | |
36 | using boost::lambda::if_then_else; | |
37 | using boost::lambda::if_then_else_return; | |
38 | ||
39 | // 2 container for_each | |
40 | template <class InputIter1, class InputIter2, class Function> | |
41 | Function for_each(InputIter1 first, InputIter1 last, | |
42 | InputIter2 first2, Function f) { | |
43 | for ( ; first != last; ++first, ++first2) | |
44 | f(*first, *first2); | |
45 | return f; | |
46 | } | |
47 | ||
48 | void simple_loops() { | |
49 | ||
50 | // for loops --------------------------------------------------------- | |
51 | int i; | |
52 | int arithmetic_series = 0; | |
53 | for_loop(_1 = 0, _1 < 10, _1++, arithmetic_series += _1)(i); | |
54 | BOOST_CHECK(arithmetic_series == 45); | |
55 | ||
56 | // no body case | |
57 | for_loop(boost::lambda::var(i) = 0, boost::lambda::var(i) < 100, ++boost::lambda::var(i))(); | |
58 | BOOST_CHECK(i == 100); | |
59 | ||
60 | // while loops ------------------------------------------------------- | |
61 | int a = 0, b = 0, c = 0; | |
62 | ||
63 | while_loop((_1 + _2) >= (_1 * _2), (++_1, ++_2, ++_3))(a, b, c); | |
64 | BOOST_CHECK(c == 3); | |
65 | ||
66 | int count; | |
67 | count = 0; i = 0; | |
68 | while_loop(_1++ < 10, ++boost::lambda::var(count))(i); | |
69 | BOOST_CHECK(count == 10); | |
70 | ||
71 | // note that the first parameter of do_while_loop is the condition | |
72 | count = 0; i = 0; | |
73 | do_while_loop(_1++ < 10, ++boost::lambda::var(count))(i); | |
74 | BOOST_CHECK(count == 11); | |
75 | ||
76 | a = 0; | |
77 | do_while_loop(constant(false), _1++)(a); | |
78 | BOOST_CHECK(a == 1); | |
79 | ||
80 | // no body cases | |
81 | a = 40; b = 30; | |
82 | while_loop(--_1 > _2)(a, b); | |
83 | BOOST_CHECK(a == b); | |
84 | ||
85 | // (the no body case for do_while_loop is pretty redundant) | |
86 | a = 40; b = 30; | |
87 | do_while_loop(--_1 > _2)(a, b); | |
88 | BOOST_CHECK(a == b); | |
89 | ||
90 | ||
91 | } | |
92 | ||
93 | void simple_ifs () { | |
94 | ||
95 | int value = 42; | |
96 | if_then(_1 < 0, _1 = 0)(value); | |
97 | BOOST_CHECK(value == 42); | |
98 | ||
99 | value = -42; | |
100 | if_then(_1 < 0, _1 = -_1)(value); | |
101 | BOOST_CHECK(value == 42); | |
102 | ||
103 | int min; | |
104 | if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2) | |
105 | (make_const(1), make_const(2)); | |
106 | BOOST_CHECK(min == 1); | |
107 | ||
108 | if_then_else(_1 < _2, boost::lambda::var(min) = _1, boost::lambda::var(min) = _2) | |
109 | (make_const(5), make_const(3)); | |
110 | BOOST_CHECK(min == 3); | |
111 | ||
112 | int x, y; | |
113 | x = -1; y = 1; | |
114 | BOOST_CHECK(if_then_else_return(_1 < _2, _2, _1)(x, y) == (std::max)(x ,y)); | |
115 | BOOST_CHECK(if_then_else_return(_1 < _2, _2, _1)(y, x) == (std::max)(x ,y)); | |
116 | } | |
117 | ||
118 | ||
1e59de90 | 119 | int main() |
7c673cae FG |
120 | { |
121 | simple_loops(); | |
122 | simple_ifs(); | |
1e59de90 | 123 | return boost::report_errors(); |
7c673cae | 124 | } |