]>
git.proxmox.com Git - ceph.git/blob - 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
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 ==============================================================================*/
11 #include <boost/config.hpp>
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
20 #include <boost/phoenix/core.hpp>
21 #include <boost/phoenix/bind.hpp>
23 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
24 #pragma warning(push, 3)
29 #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
33 #include <boost/detail/lightweight_test.hpp>
47 long f_2(long a
, long b
)
52 long f_3(long a
, long b
, long c
)
54 return a
+ 10 * b
+ 100 * c
;
57 long f_4(long a
, long b
, long c
, long d
)
59 return a
+ 10 * b
+ 100 * c
+ 1000 * d
;
62 long f_5(long a
, long b
, long c
, long d
, long e
)
64 return a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
;
67 long f_6(long a
, long b
, long c
, long d
, long e
, long f
)
69 return a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
;
72 long f_7(long a
, long b
, long c
, long d
, long e
, long f
, long g
)
74 return a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
;
77 long f_8(long a
, long b
, long c
, long d
, long e
, long f
, long g
, long h
)
79 return a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
+ 10000000 * h
;
82 long f_9(long a
, long b
, long c
, long d
, long e
, long f
, long g
, long h
, long i
)
84 return a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
+ 10000000 * h
+ 100000000 * i
;
91 global_result
= 17041L;
99 void fv_2(long a
, long b
)
101 global_result
= a
+ 10 * b
;
104 void fv_3(long a
, long b
, long c
)
106 global_result
= a
+ 10 * b
+ 100 * c
;
109 void fv_4(long a
, long b
, long c
, long d
)
111 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
;
114 void fv_5(long a
, long b
, long c
, long d
, long e
)
116 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
;
119 void fv_6(long a
, long b
, long c
, long d
, long e
, long f
)
121 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
;
124 void fv_7(long a
, long b
, long c
, long d
, long e
, long f
, long g
)
126 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
;
129 void fv_8(long a
, long b
, long c
, long d
, long e
, long f
, long g
, long h
)
131 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
+ 10000000 * h
;
134 void fv_9(long a
, long b
, long c
, long d
, long e
, long f
, long g
, long h
, long i
)
136 global_result
= a
+ 10 * b
+ 100 * c
+ 1000 * d
+ 10000 * e
+ 100000 * f
+ 1000000 * g
+ 10000000 * h
+ 100000000 * i
;
141 using boost::phoenix::bind
;
142 using boost::phoenix::placeholders::_1
;
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 );
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)) );
173 template <typename Sig
>
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
; };
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
; }
190 void function_object_test()
192 using boost::phoenix::bind
;
193 using boost::phoenix::ref
;
194 using boost::phoenix::placeholders::_1
;
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 );
205 #if !defined(__MWERKS__) || (__MWERKS__ > 0x2407) // Fails for this version of the compiler.
208 bind(Y(), i
, _1
, 9, 4)(k
);
209 BOOST_TEST( global_result
== 4938 );
214 void function_object_test2()
216 using boost::phoenix::bind
;
217 using boost::phoenix::ref
;
218 using boost::phoenix::placeholders::_1
;
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 );
230 bind(Y(), i
, _1
, 9, 4)(k
);
231 BOOST_TEST( global_result
== 4938 );
236 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
240 typedef int result_type
;
241 int operator()(int a
, int b
) const { return a
+ 10 * b
; }
244 void adaptable_function_object_test()
246 using boost::phoenix::bind
;
247 BOOST_TEST( bind(Z(), 7, 4)() == 47 );
256 mutable unsigned int hash
;
260 int f0() { f1(17); return 0; }
261 int g0() const { g1(17); return 0; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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; }
290 mutable unsigned int hash
;
294 void f0() { f1(17); }
295 void g0() const { g1(17); }
297 void f1(int a1
) { hash
= (hash
* 17041 + a1
) % 32768; }
298 void g1(int a1
) const { hash
= (hash
* 17041 + a1
* 2) % 32768; }
300 void f2(int a1
, int a2
) { f1(a1
); f1(a2
); }
301 void g2(int a1
, int a2
) const { g1(a1
); g1(a2
); }
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
); }
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
); }
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
); }
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
); }
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
); }
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
); }
322 void member_function_test()
324 using boost::phoenix::bind
;
325 using boost::phoenix::ref
;
332 bind(&X::f0
, ref(x
))();
336 bind(&X::g0
, ref(x
))();
340 bind(&X::f1
, &x
, 1)();
341 bind(&X::f1
, ref(x
), 1)();
343 bind(&X::g1
, &x
, 1)();
344 bind(&X::g1
, x
, 1)();
345 bind(&X::g1
, ref(x
), 1)();
349 bind(&X::f2
, &x
, 1, 2)();
350 bind(&X::f2
, ref(x
), 1, 2)();
352 bind(&X::g2
, &x
, 1, 2)();
353 bind(&X::g2
, x
, 1, 2)();
354 bind(&X::g2
, ref(x
), 1, 2)();
358 bind(&X::f3
, &x
, 1, 2, 3)();
359 bind(&X::f3
, ref(x
), 1, 2, 3)();
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)();
367 bind(&X::f4
, &x
, 1, 2, 3, 4)();
368 bind(&X::f4
, ref(x
), 1, 2, 3, 4)();
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)();
376 bind(&X::f5
, &x
, 1, 2, 3, 4, 5)();
377 bind(&X::f5
, ref(x
), 1, 2, 3, 4, 5)();
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)();
385 bind(&X::f6
, &x
, 1, 2, 3, 4, 5, 6)();
386 bind(&X::f6
, ref(x
), 1, 2, 3, 4, 5, 6)();
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)();
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)();
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)();
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)();
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)();
410 BOOST_TEST( x
.hash
== 23558 );
413 void member_function_void_test()
415 using boost::phoenix::bind
;
416 using boost::phoenix::ref
;
417 using boost::phoenix::placeholders::_1
;
424 bind(&V::f0
, ref(v
))();
428 bind(&V::g0
, ref(v
))();
432 bind(&V::f1
, &v
, 1)();
433 bind(&V::f1
, ref(v
), 1)();
435 bind(&V::g1
, &v
, 1)();
436 bind(&V::g1
, v
, 1)();
437 bind(&V::g1
, ref(v
), 1)();
441 bind(&V::f2
, &v
, 1, 2)();
442 bind(&V::f2
, ref(v
), 1, 2)();
444 bind(&V::g2
, &v
, 1, 2)();
445 bind(&V::g2
, v
, 1, 2)();
446 bind(&V::g2
, ref(v
), 1, 2)();
450 bind(&V::f3
, &v
, 1, 2, 3)();
451 bind(&V::f3
, ref(v
), 1, 2, 3)();
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)();
459 bind(&V::f4
, &v
, 1, 2, 3, 4)();
460 bind(&V::f4
, ref(v
), 1, 2, 3, 4)();
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)();
468 bind(&V::f5
, &v
, 1, 2, 3, 4, 5)();
469 bind(&V::f5
, ref(v
), 1, 2, 3, 4, 5)();
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)();
477 bind(&V::f6
, &v
, 1, 2, 3, 4, 5, 6)();
478 bind(&V::f6
, ref(v
), 1, 2, 3, 4, 5, 6)();
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)();
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)();
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)();
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)();
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)();
502 BOOST_TEST( v
.hash
== 23558 );
505 void nested_bind_test()
507 using boost::phoenix::bind
;
508 using boost::phoenix::placeholders::_1
;
509 using boost::phoenix::placeholders::_2
;
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 );
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)) );
530 function_object_test();
531 function_object_test2();
533 #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
534 adaptable_function_object_test();
537 member_function_test();
538 member_function_void_test();
541 return boost::report_errors();