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