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