]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // bind_tests_simple.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 | ||
1e59de90 TL |
15 | #include <boost/core/lightweight_test.hpp> |
16 | #define BOOST_CHECK BOOST_TEST | |
7c673cae FG |
17 | |
18 | #include "boost/lambda/bind.hpp" | |
19 | ||
20 | #include <iostream> | |
21 | ||
22 | using namespace boost::lambda; | |
23 | ||
24 | ||
25 | int sum_of_args_0() { return 0; } | |
26 | int sum_of_args_1(int a) { return a; } | |
27 | int sum_of_args_2(int a, int b) { return a+b; } | |
28 | int sum_of_args_3(int a, int b, int c) { return a+b+c; } | |
29 | int sum_of_args_4(int a, int b, int c, int d) { return a+b+c+d; } | |
30 | int sum_of_args_5(int a, int b, int c, int d, int e) { return a+b+c+d+e; } | |
31 | int sum_of_args_6(int a, int b, int c, int d, int e, int f) { return a+b+c+d+e+f; } | |
32 | int sum_of_args_7(int a, int b, int c, int d, int e, int f, int g) { return a+b+c+d+e+f+g; } | |
33 | int sum_of_args_8(int a, int b, int c, int d, int e, int f, int g, int h) { return a+b+c+d+e+f+g+h; } | |
34 | int sum_of_args_9(int a, int b, int c, int d, int e, int f, int g, int h, int i) { return a+b+c+d+e+f+g+h+i; } | |
35 | ||
36 | ||
37 | // ---------------------------- | |
38 | ||
39 | class A { | |
40 | int i; | |
41 | public: | |
42 | A(int n) : i(n) {}; | |
43 | int add(const int& j) { return i + j; } | |
44 | int add2(int a1, int a2) { return i + a1 + a2; } | |
45 | int add3(int a1, int a2, int a3) { return i + a1 + a2 + a3; } | |
46 | int add4(int a1, int a2, int a3, int a4) { return i + a1 + a2 + a3 + a4; } | |
47 | int add5(int a1, int a2, int a3, int a4, int a5) | |
48 | { return i + a1 + a2 + a3 + a4 + a5; } | |
49 | int add6(int a1, int a2, int a3, int a4, int a5, int a6) | |
50 | { return i + a1 + a2 + a3 + a4 + a5 + a6; } | |
51 | int add7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) | |
52 | { return i + a1 + a2 + a3 + a4 + a5 + a6 + a7; } | |
53 | int add8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) | |
54 | { return i + a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8; } | |
55 | ||
56 | }; | |
57 | ||
58 | void test_member_functions() | |
59 | { | |
60 | using boost::ref; | |
61 | A a(10); | |
62 | int i = 1; | |
63 | ||
64 | ||
65 | ||
66 | ||
67 | BOOST_CHECK(bind(&A::add, ref(a), _1)(i) == 11); | |
68 | BOOST_CHECK(bind(&A::add, &a, _1)(i) == 11); | |
69 | BOOST_CHECK(bind(&A::add, _1, 1)(a) == 11); | |
70 | BOOST_CHECK(bind(&A::add, _1, 1)(make_const(&a)) == 11); | |
71 | ||
72 | BOOST_CHECK(bind(&A::add2, _1, 1, 1)(a) == 12); | |
73 | BOOST_CHECK(bind(&A::add3, _1, 1, 1, 1)(a) == 13); | |
74 | BOOST_CHECK(bind(&A::add4, _1, 1, 1, 1, 1)(a) == 14); | |
75 | BOOST_CHECK(bind(&A::add5, _1, 1, 1, 1, 1, 1)(a) == 15); | |
76 | BOOST_CHECK(bind(&A::add6, _1, 1, 1, 1, 1, 1, 1)(a) == 16); | |
77 | BOOST_CHECK(bind(&A::add7, _1, 1, 1, 1, 1, 1, 1, 1)(a) == 17); | |
78 | BOOST_CHECK(bind(&A::add8, _1, 1, 1, 1, 1, 1, 1, 1, 1)(a) == 18); | |
79 | ||
80 | // This should fail, as lambda functors store arguments as const | |
81 | // bind(&A::add, a, _1); | |
82 | } | |
83 | ||
84 | struct B { | |
85 | B(int n) : i(n) {}; | |
86 | int i; | |
87 | }; | |
88 | ||
89 | void test_data_members() | |
90 | { | |
91 | using boost::ref; | |
92 | B b(10); | |
93 | BOOST_CHECK(bind(&B::i, ref(b))() == 10); | |
94 | BOOST_CHECK(bind(&B::i, b)() == 10); | |
95 | BOOST_CHECK(bind(&B::i, _1)(b) == 10); | |
96 | BOOST_CHECK(bind(&B::i, _1)(B(11)) == 11); | |
97 | bind(&B::i, ref(b))() = 1; | |
98 | BOOST_CHECK(b.i == 1); | |
99 | } | |
100 | ||
1e59de90 | 101 | int main() { |
7c673cae FG |
102 | |
103 | int i = 1; int j = 2; int k = 3; | |
104 | int result; | |
105 | ||
106 | // bind all parameters | |
107 | BOOST_CHECK(bind(&sum_of_args_0)()==0); | |
108 | BOOST_CHECK(bind(&sum_of_args_1, 1)()==1); | |
109 | BOOST_CHECK(bind(&sum_of_args_2, 1, 2)()==3); | |
110 | BOOST_CHECK(bind(&sum_of_args_3, 1, 2, 3)()==6); | |
111 | BOOST_CHECK(bind(&sum_of_args_4, 1, 2, 3, 4)()==10); | |
112 | BOOST_CHECK(bind(&sum_of_args_5, 1, 2, 3, 4, 5)()==15); | |
113 | BOOST_CHECK(bind(&sum_of_args_6, 1, 2, 3, 4, 5, 6)()==21); | |
114 | BOOST_CHECK(bind(&sum_of_args_7, 1, 2, 3, 4, 5, 6, 7)()==28); | |
115 | BOOST_CHECK(bind(&sum_of_args_8, 1, 2, 3, 4, 5, 6, 7, 8)()==36); | |
116 | BOOST_CHECK(bind(&sum_of_args_9, 1, 2, 3, 4, 5, 6, 7, 8, 9)()==45); | |
117 | ||
118 | // first parameter open | |
119 | BOOST_CHECK(bind(&sum_of_args_0)()==0); | |
120 | BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1); | |
121 | BOOST_CHECK(bind(&sum_of_args_2, _1, 2)(i)==3); | |
122 | BOOST_CHECK(bind(&sum_of_args_3, _1, 2, 3)(i)==6); | |
123 | BOOST_CHECK(bind(&sum_of_args_4, _1, 2, 3, 4)(i)==10); | |
124 | BOOST_CHECK(bind(&sum_of_args_5, _1, 2, 3, 4, 5)(i)==15); | |
125 | BOOST_CHECK(bind(&sum_of_args_6, _1, 2, 3, 4, 5, 6)(i)==21); | |
126 | BOOST_CHECK(bind(&sum_of_args_7, _1, 2, 3, 4, 5, 6, 7)(i)==28); | |
127 | BOOST_CHECK(bind(&sum_of_args_8, _1, 2, 3, 4, 5, 6, 7, 8)(i)==36); | |
128 | BOOST_CHECK(bind(&sum_of_args_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i)==45); | |
129 | ||
130 | // two open arguments | |
131 | BOOST_CHECK(bind(&sum_of_args_0)()==0); | |
132 | BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1); | |
133 | BOOST_CHECK(bind(&sum_of_args_2, _1, _2)(i, j)==3); | |
134 | BOOST_CHECK(bind(&sum_of_args_3, _1, _2, 3)(i, j)==6); | |
135 | BOOST_CHECK(bind(&sum_of_args_4, _1, _2, 3, 4)(i, j)==10); | |
136 | BOOST_CHECK(bind(&sum_of_args_5, _1, _2, 3, 4, 5)(i, j)==15); | |
137 | BOOST_CHECK(bind(&sum_of_args_6, _1, _2, 3, 4, 5, 6)(i, j)==21); | |
138 | BOOST_CHECK(bind(&sum_of_args_7, _1, _2, 3, 4, 5, 6, 7)(i, j)==28); | |
139 | BOOST_CHECK(bind(&sum_of_args_8, _1, _2, 3, 4, 5, 6, 7, 8)(i, j)==36); | |
140 | BOOST_CHECK(bind(&sum_of_args_9, _1, _2, 3, 4, 5, 6, 7, 8, 9)(i, j)==45); | |
141 | ||
142 | // three open arguments | |
143 | BOOST_CHECK(bind(&sum_of_args_0)()==0); | |
144 | BOOST_CHECK(bind(&sum_of_args_1, _1)(i)==1); | |
145 | BOOST_CHECK(bind(&sum_of_args_2, _1, _2)(i, j)==3); | |
146 | BOOST_CHECK(bind(&sum_of_args_3, _1, _2, _3)(i, j, k)==6); | |
147 | BOOST_CHECK(bind(&sum_of_args_4, _1, _2, _3, 4)(i, j, k)==10); | |
148 | BOOST_CHECK(bind(&sum_of_args_5, _1, _2, _3, 4, 5)(i, j, k)==15); | |
149 | BOOST_CHECK(bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6)(i, j, k)==21); | |
150 | BOOST_CHECK(bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7)(i, j, k)==28); | |
151 | BOOST_CHECK(bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8)(i, j, k)==36); | |
152 | BOOST_CHECK(bind(&sum_of_args_9, _1, _2, _3, 4, 5, 6, 7, 8, 9)(i, j, k)==45); | |
153 | ||
154 | // function compositions with bind | |
155 | BOOST_CHECK(bind(&sum_of_args_3, bind(&sum_of_args_2, _1, 2), 2, 3)(i)==8); | |
156 | BOOST_CHECK( | |
157 | bind(&sum_of_args_9, | |
158 | bind(&sum_of_args_0), // 0 | |
159 | bind(&sum_of_args_1, _1), // 1 | |
160 | bind(&sum_of_args_2, _1, _2), // 3 | |
161 | bind(&sum_of_args_3, _1, _2, _3), // 6 | |
162 | bind(&sum_of_args_4, _1, _2, _3, 4), // 10 | |
163 | bind(&sum_of_args_5, _1, _2, _3, 4, 5), // 15 | |
164 | bind(&sum_of_args_6, _1, _2, _3, 4, 5, 6), // 21 | |
165 | bind(&sum_of_args_7, _1, _2, _3, 4, 5, 6, 7), // 28 | |
166 | bind(&sum_of_args_8, _1, _2, _3, 4, 5, 6, 7, 8) // 36 | |
167 | )(i, j, k) == 120); | |
168 | ||
169 | // deeper nesting | |
170 | result = | |
171 | bind(&sum_of_args_1, // 12 | |
172 | bind(&sum_of_args_4, // 12 | |
173 | bind(&sum_of_args_2, // 3 | |
174 | bind(&sum_of_args_1, // 1 | |
175 | bind(&sum_of_args_1, _1) // 1 | |
176 | ), | |
177 | _2), | |
178 | _2, | |
179 | _3, | |
180 | 4) | |
181 | )(i, j, k); | |
182 | BOOST_CHECK(result == 12); | |
183 | ||
184 | test_member_functions(); | |
185 | ||
186 | ||
1e59de90 | 187 | return boost::report_errors(); |
7c673cae | 188 | } |