1 #include <boost/config.hpp>
2
3 #if defined(BOOST_MSVC)
4 #pragma warning(disable: 4786) // identifier truncated in debug info
5 #pragma warning(disable: 4710) // function not inlined
6 #pragma warning(disable: 4711) // function selected for automatic inline expansion
7 #pragma warning(disable: 4514) // unreferenced inline removed
8 #endif
9
10 //
11 // bind_test.cpp - monolithic test for bind.hpp
12 //
13 // Copyright (c) 2001, 2002 Peter Dimov and Multi Media Ltd.
14 // Copyright (c) 2001 David Abrahams
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20
21 #include <boost/bind/bind.hpp>
22 #include <boost/ref.hpp>
23 #include <boost/core/lightweight_test.hpp>
24
25 using namespace boost::placeholders;
26
27 //
28
f_0()29 long f_0()
30 {
31 return 17041L;
32 }
33
f_1(long a)34 long f_1(long a)
35 {
36 return a;
37 }
38
f_2(long a,long b)39 long f_2(long a, long b)
40 {
41 return a + 10 * b;
42 }
43
f_3(long a,long b,long c)44 long f_3(long a, long b, long c)
45 {
46 return a + 10 * b + 100 * c;
47 }
48
f_4(long a,long b,long c,long d)49 long f_4(long a, long b, long c, long d)
50 {
51 return a + 10 * b + 100 * c + 1000 * d;
52 }
53
f_5(long a,long b,long c,long d,long e)54 long f_5(long a, long b, long c, long d, long e)
55 {
56 return a + 10 * b + 100 * c + 1000 * d + 10000 * e;
57 }
58
f_6(long a,long b,long c,long d,long e,long f)59 long f_6(long a, long b, long c, long d, long e, long f)
60 {
61 return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f;
62 }
63
f_7(long a,long b,long c,long d,long e,long f,long g)64 long f_7(long a, long b, long c, long d, long e, long f, long g)
65 {
66 return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g;
67 }
68
f_8(long a,long b,long c,long d,long e,long f,long g,long h)69 long f_8(long a, long b, long c, long d, long e, long f, long g, long h)
70 {
71 return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h;
72 }
73
f_9(long a,long b,long c,long d,long e,long f,long g,long h,long i)74 long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i)
75 {
76 return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i;
77 }
78
79 long global_result;
80
fv_0()81 void fv_0()
82 {
83 global_result = 17041L;
84 }
85
fv_1(long a)86 void fv_1(long a)
87 {
88 global_result = a;
89 }
90
fv_2(long a,long b)91 void fv_2(long a, long b)
92 {
93 global_result = a + 10 * b;
94 }
95
fv_3(long a,long b,long c)96 void fv_3(long a, long b, long c)
97 {
98 global_result = a + 10 * b + 100 * c;
99 }
100
fv_4(long a,long b,long c,long d)101 void fv_4(long a, long b, long c, long d)
102 {
103 global_result = a + 10 * b + 100 * c + 1000 * d;
104 }
105
fv_5(long a,long b,long c,long d,long e)106 void fv_5(long a, long b, long c, long d, long e)
107 {
108 global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e;
109 }
110
fv_6(long a,long b,long c,long d,long e,long f)111 void fv_6(long a, long b, long c, long d, long e, long f)
112 {
113 global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f;
114 }
115
fv_7(long a,long b,long c,long d,long e,long f,long g)116 void fv_7(long a, long b, long c, long d, long e, long f, long g)
117 {
118 global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g;
119 }
120
fv_8(long a,long b,long c,long d,long e,long f,long g,long h)121 void fv_8(long a, long b, long c, long d, long e, long f, long g, long h)
122 {
123 global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h;
124 }
125
fv_9(long a,long b,long c,long d,long e,long f,long g,long h,long i)126 void fv_9(long a, long b, long c, long d, long e, long f, long g, long h, long i)
127 {
128 global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i;
129 }
130
function_test()131 void function_test()
132 {
133 using namespace boost;
134
135 int const i = 1;
136
137 BOOST_TEST( bind(f_0)(i) == 17041L );
138 BOOST_TEST( bind(f_1, _1)(i) == 1L );
139 BOOST_TEST( bind(f_2, _1, 2)(i) == 21L );
140 BOOST_TEST( bind(f_3, _1, 2, 3)(i) == 321L );
141 BOOST_TEST( bind(f_4, _1, 2, 3, 4)(i) == 4321L );
142 BOOST_TEST( bind(f_5, _1, 2, 3, 4, 5)(i) == 54321L );
143 BOOST_TEST( bind(f_6, _1, 2, 3, 4, 5, 6)(i) == 654321L );
144 BOOST_TEST( bind(f_7, _1, 2, 3, 4, 5, 6, 7)(i) == 7654321L );
145 BOOST_TEST( bind(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i) == 87654321L );
146 BOOST_TEST( bind(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i) == 987654321L );
147
148 BOOST_TEST( (bind(fv_0)(i), (global_result == 17041L)) );
149 BOOST_TEST( (bind(fv_1, _1)(i), (global_result == 1L)) );
150 BOOST_TEST( (bind(fv_2, _1, 2)(i), (global_result == 21L)) );
151 BOOST_TEST( (bind(fv_3, _1, 2, 3)(i), (global_result == 321L)) );
152 BOOST_TEST( (bind(fv_4, _1, 2, 3, 4)(i), (global_result == 4321L)) );
153 BOOST_TEST( (bind(fv_5, _1, 2, 3, 4, 5)(i), (global_result == 54321L)) );
154 BOOST_TEST( (bind(fv_6, _1, 2, 3, 4, 5, 6)(i), (global_result == 654321L)) );
155 BOOST_TEST( (bind(fv_7, _1, 2, 3, 4, 5, 6, 7)(i), (global_result == 7654321L)) );
156 BOOST_TEST( (bind(fv_8, _1, 2, 3, 4, 5, 6, 7, 8)(i), (global_result == 87654321L)) );
157 BOOST_TEST( (bind(fv_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i), (global_result == 987654321L)) );
158 }
159
160 //
161
162 struct Y
163 {
operator ()Y164 short operator()(short & r) const { return ++r; }
operator ()Y165 int operator()(int a, int b) const { return a + 10 * b; }
operator ()Y166 long operator() (long a, long b, long c) const { return a + 10 * b + 100 * c; }
operator ()Y167 void operator() (long a, long b, long c, long d) const { global_result = a + 10 * b + 100 * c + 1000 * d; }
168 };
169
function_object_test()170 void function_object_test()
171 {
172 using namespace boost;
173
174 short i(6);
175
176 int const k = 3;
177
178 BOOST_TEST( bind<short>(Y(), ref(i))() == 7 );
179 BOOST_TEST( bind<short>(Y(), ref(i))() == 8 );
180 BOOST_TEST( bind<int>(Y(), i, _1)(k) == 38 );
181 BOOST_TEST( bind<long>(Y(), i, _1, 9)(k) == 938 );
182
183 #if !defined(__MWERKS__) || (__MWERKS__ > 0x2407) // Fails for this version of the compiler.
184
185 global_result = 0;
186 bind<void>(Y(), i, _1, 9, 4)(k);
187 BOOST_TEST( global_result == 4938 );
188
189 #endif
190 }
191
function_object_test2()192 void function_object_test2()
193 {
194 using namespace boost;
195
196 short i(6);
197
198 int const k = 3;
199
200 BOOST_TEST( bind(type<short>(), Y(), ref(i))() == 7 );
201 BOOST_TEST( bind(type<short>(), Y(), ref(i))() == 8 );
202 BOOST_TEST( bind(type<int>(), Y(), i, _1)(k) == 38 );
203 BOOST_TEST( bind(type<long>(), Y(), i, _1, 9)(k) == 938 );
204
205 global_result = 0;
206 bind(type<void>(), Y(), i, _1, 9, 4)(k);
207 BOOST_TEST( global_result == 4938 );
208 }
209
210 //
211
212 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
213
214 struct Z
215 {
216 typedef int result_type;
operator ()Z217 int operator()(int a, int b) const { return a + 10 * b; }
218 };
219
adaptable_function_object_test()220 void adaptable_function_object_test()
221 {
222 BOOST_TEST( boost::bind(Z(), 7, 4)() == 47 );
223 }
224
225 #endif
226
227 //
228
229 struct X
230 {
231 mutable unsigned int hash;
232
XX233 X(): hash(0) {}
234
f0X235 int f0() { f1(17); return 0; }
g0X236 int g0() const { g1(17); return 0; }
237
f1X238 int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; }
g1X239 int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; }
240
f2X241 int f2(int a1, int a2) { f1(a1); f1(a2); return 0; }
g2X242 int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; }
243
f3X244 int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; }
g3X245 int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; }
246
f4X247 int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; }
g4X248 int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; }
249
f5X250 int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; }
g5X251 int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; }
252
f6X253 int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; }
g6X254 int g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); return 0; }
255
f7X256 int f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); return 0; }
g7X257 int g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); return 0; }
258
f8X259 int f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); return 0; }
g8X260 int g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); return 0; }
261 };
262
263 struct V
264 {
265 mutable unsigned int hash;
266
VV267 V(): hash(0) {}
268
f0V269 void f0() { f1(17); }
g0V270 void g0() const { g1(17); }
271
f1V272 void f1(int a1) { hash = (hash * 17041 + a1) % 32768; }
g1V273 void g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; }
274
f2V275 void f2(int a1, int a2) { f1(a1); f1(a2); }
g2V276 void g2(int a1, int a2) const { g1(a1); g1(a2); }
277
f3V278 void f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); }
g3V279 void g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); }
280
f4V281 void f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); }
g4V282 void g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); }
283
f5V284 void f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); }
g5V285 void g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); }
286
f6V287 void f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); }
g6V288 void g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); }
289
f7V290 void f7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) { f6(a1, a2, a3, a4, a5, a6); f1(a7); }
g7V291 void g7(int a1, int a2, int a3, int a4, int a5, int a6, int a7) const { g6(a1, a2, a3, a4, a5, a6); g1(a7); }
292
f8V293 void f8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) { f7(a1, a2, a3, a4, a5, a6, a7); f1(a8); }
g8V294 void g8(int a1, int a2, int a3, int a4, int a5, int a6, int a7, int a8) const { g7(a1, a2, a3, a4, a5, a6, a7); g1(a8); }
295 };
296
member_function_test()297 void member_function_test()
298 {
299 using namespace boost;
300
301 X x;
302
303 // 0
304
305 bind(&X::f0, &x)();
306 bind(&X::f0, ref(x))();
307
308 bind(&X::g0, &x)();
309 bind(&X::g0, x)();
310 bind(&X::g0, ref(x))();
311
312 // 1
313
314 bind(&X::f1, &x, 1)();
315 bind(&X::f1, ref(x), 1)();
316
317 bind(&X::g1, &x, 1)();
318 bind(&X::g1, x, 1)();
319 bind(&X::g1, ref(x), 1)();
320
321 // 2
322
323 bind(&X::f2, &x, 1, 2)();
324 bind(&X::f2, ref(x), 1, 2)();
325
326 bind(&X::g2, &x, 1, 2)();
327 bind(&X::g2, x, 1, 2)();
328 bind(&X::g2, ref(x), 1, 2)();
329
330 // 3
331
332 bind(&X::f3, &x, 1, 2, 3)();
333 bind(&X::f3, ref(x), 1, 2, 3)();
334
335 bind(&X::g3, &x, 1, 2, 3)();
336 bind(&X::g3, x, 1, 2, 3)();
337 bind(&X::g3, ref(x), 1, 2, 3)();
338
339 // 4
340
341 bind(&X::f4, &x, 1, 2, 3, 4)();
342 bind(&X::f4, ref(x), 1, 2, 3, 4)();
343
344 bind(&X::g4, &x, 1, 2, 3, 4)();
345 bind(&X::g4, x, 1, 2, 3, 4)();
346 bind(&X::g4, ref(x), 1, 2, 3, 4)();
347
348 // 5
349
350 bind(&X::f5, &x, 1, 2, 3, 4, 5)();
351 bind(&X::f5, ref(x), 1, 2, 3, 4, 5)();
352
353 bind(&X::g5, &x, 1, 2, 3, 4, 5)();
354 bind(&X::g5, x, 1, 2, 3, 4, 5)();
355 bind(&X::g5, ref(x), 1, 2, 3, 4, 5)();
356
357 // 6
358
359 bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)();
360 bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)();
361
362 bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)();
363 bind(&X::g6, x, 1, 2, 3, 4, 5, 6)();
364 bind(&X::g6, ref(x), 1, 2, 3, 4, 5, 6)();
365
366 // 7
367
368 bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)();
369 bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
370
371 bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)();
372 bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)();
373 bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
374
375 // 8
376
377 bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
378 bind(&X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
379
380 bind(&X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
381 bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)();
382 bind(&X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
383
384 BOOST_TEST( x.hash == 23558 );
385 }
386
member_function_void_test()387 void member_function_void_test()
388 {
389 using namespace boost;
390
391 V v;
392
393 // 0
394
395 bind(&V::f0, &v)();
396 bind(&V::f0, ref(v))();
397
398 bind(&V::g0, &v)();
399 bind(&V::g0, v)();
400 bind(&V::g0, ref(v))();
401
402 // 1
403
404 bind(&V::f1, &v, 1)();
405 bind(&V::f1, ref(v), 1)();
406
407 bind(&V::g1, &v, 1)();
408 bind(&V::g1, v, 1)();
409 bind(&V::g1, ref(v), 1)();
410
411 // 2
412
413 bind(&V::f2, &v, 1, 2)();
414 bind(&V::f2, ref(v), 1, 2)();
415
416 bind(&V::g2, &v, 1, 2)();
417 bind(&V::g2, v, 1, 2)();
418 bind(&V::g2, ref(v), 1, 2)();
419
420 // 3
421
422 bind(&V::f3, &v, 1, 2, 3)();
423 bind(&V::f3, ref(v), 1, 2, 3)();
424
425 bind(&V::g3, &v, 1, 2, 3)();
426 bind(&V::g3, v, 1, 2, 3)();
427 bind(&V::g3, ref(v), 1, 2, 3)();
428
429 // 4
430
431 bind(&V::f4, &v, 1, 2, 3, 4)();
432 bind(&V::f4, ref(v), 1, 2, 3, 4)();
433
434 bind(&V::g4, &v, 1, 2, 3, 4)();
435 bind(&V::g4, v, 1, 2, 3, 4)();
436 bind(&V::g4, ref(v), 1, 2, 3, 4)();
437
438 // 5
439
440 bind(&V::f5, &v, 1, 2, 3, 4, 5)();
441 bind(&V::f5, ref(v), 1, 2, 3, 4, 5)();
442
443 bind(&V::g5, &v, 1, 2, 3, 4, 5)();
444 bind(&V::g5, v, 1, 2, 3, 4, 5)();
445 bind(&V::g5, ref(v), 1, 2, 3, 4, 5)();
446
447 // 6
448
449 bind(&V::f6, &v, 1, 2, 3, 4, 5, 6)();
450 bind(&V::f6, ref(v), 1, 2, 3, 4, 5, 6)();
451
452 bind(&V::g6, &v, 1, 2, 3, 4, 5, 6)();
453 bind(&V::g6, v, 1, 2, 3, 4, 5, 6)();
454 bind(&V::g6, ref(v), 1, 2, 3, 4, 5, 6)();
455
456 // 7
457
458 bind(&V::f7, &v, 1, 2, 3, 4, 5, 6, 7)();
459 bind(&V::f7, ref(v), 1, 2, 3, 4, 5, 6, 7)();
460
461 bind(&V::g7, &v, 1, 2, 3, 4, 5, 6, 7)();
462 bind(&V::g7, v, 1, 2, 3, 4, 5, 6, 7)();
463 bind(&V::g7, ref(v), 1, 2, 3, 4, 5, 6, 7)();
464
465 // 8
466
467 bind(&V::f8, &v, 1, 2, 3, 4, 5, 6, 7, 8)();
468 bind(&V::f8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)();
469
470 bind(&V::g8, &v, 1, 2, 3, 4, 5, 6, 7, 8)();
471 bind(&V::g8, v, 1, 2, 3, 4, 5, 6, 7, 8)();
472 bind(&V::g8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)();
473
474 BOOST_TEST( v.hash == 23558 );
475 }
476
nested_bind_test()477 void nested_bind_test()
478 {
479 using namespace boost;
480
481 int const x = 1;
482 int const y = 2;
483
484 BOOST_TEST( bind(f_1, bind(f_1, _1))(x) == 1L );
485 BOOST_TEST( bind(f_1, bind(f_2, _1, _2))(x, y) == 21L );
486 BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _1))(x) == 11L );
487 BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _2))(x, y) == 21L );
488 BOOST_TEST( bind(f_1, bind(f_0))() == 17041L );
489
490 BOOST_TEST( (bind(fv_1, bind(f_1, _1))(x), (global_result == 1L)) );
491 BOOST_TEST( (bind(fv_1, bind(f_2, _1, _2))(x, y), (global_result == 21L)) );
492 BOOST_TEST( (bind(fv_2, bind(f_1, _1), bind(f_1, _1))(x), (global_result == 11L)) );
493 BOOST_TEST( (bind(fv_2, bind(f_1, _1), bind(f_1, _2))(x, y), (global_result == 21L)) );
494 BOOST_TEST( (bind(fv_1, bind(f_0))(), (global_result == 17041L)) );
495 }
496
main()497 int main()
498 {
499 function_test();
500 function_object_test();
501 function_object_test2();
502
503 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
504 adaptable_function_object_test();
505 #endif
506
507 member_function_test();
508 member_function_void_test();
509 nested_bind_test();
510
511 return boost::report_errors();
512 }
513