bind_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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. Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. ==============================================================================*/
  9. #include <boost/config.hpp>
  10. #if defined(BOOST_MSVC)
  11. #pragma warning(disable: 4786) // identifier truncated in debug info
  12. #pragma warning(disable: 4710) // function not inlined
  13. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  14. #pragma warning(disable: 4514) // unreferenced inline removed
  15. #endif
  16. #include <boost/phoenix/core.hpp>
  17. #include <boost/phoenix/bind.hpp>
  18. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  19. #pragma warning(push, 3)
  20. #endif
  21. #include <iostream>
  22. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  23. #pragma warning(pop)
  24. #endif
  25. #include <boost/detail/lightweight_test.hpp>
  26. //
  27. long f_0()
  28. {
  29. return 17041L;
  30. }
  31. long f_1(long a)
  32. {
  33. return a;
  34. }
  35. long f_2(long a, long b)
  36. {
  37. return a + 10 * b;
  38. }
  39. long f_3(long a, long b, long c)
  40. {
  41. return a + 10 * b + 100 * c;
  42. }
  43. long f_4(long a, long b, long c, long d)
  44. {
  45. return a + 10 * b + 100 * c + 1000 * d;
  46. }
  47. long f_5(long a, long b, long c, long d, long e)
  48. {
  49. return a + 10 * b + 100 * c + 1000 * d + 10000 * e;
  50. }
  51. long f_6(long a, long b, long c, long d, long e, long f)
  52. {
  53. return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f;
  54. }
  55. long f_7(long a, long b, long c, long d, long e, long f, long g)
  56. {
  57. return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g;
  58. }
  59. long f_8(long a, long b, long c, long d, long e, long f, long g, long h)
  60. {
  61. return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h;
  62. }
  63. long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i)
  64. {
  65. return a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i;
  66. }
  67. long global_result;
  68. void fv_0()
  69. {
  70. global_result = 17041L;
  71. }
  72. void fv_1(long a)
  73. {
  74. global_result = a;
  75. }
  76. void fv_2(long a, long b)
  77. {
  78. global_result = a + 10 * b;
  79. }
  80. void fv_3(long a, long b, long c)
  81. {
  82. global_result = a + 10 * b + 100 * c;
  83. }
  84. void fv_4(long a, long b, long c, long d)
  85. {
  86. global_result = a + 10 * b + 100 * c + 1000 * d;
  87. }
  88. void fv_5(long a, long b, long c, long d, long e)
  89. {
  90. global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e;
  91. }
  92. void fv_6(long a, long b, long c, long d, long e, long f)
  93. {
  94. global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f;
  95. }
  96. void fv_7(long a, long b, long c, long d, long e, long f, long g)
  97. {
  98. global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g;
  99. }
  100. void fv_8(long a, long b, long c, long d, long e, long f, long g, long h)
  101. {
  102. global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h;
  103. }
  104. void fv_9(long a, long b, long c, long d, long e, long f, long g, long h, long i)
  105. {
  106. global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i;
  107. }
  108. void function_test()
  109. {
  110. using boost::phoenix::bind;
  111. using boost::phoenix::placeholders::_1;
  112. int const i = 1;
  113. BOOST_TEST( bind(f_0)(i) == 17041L );
  114. BOOST_TEST( bind(f_1, _1)(i) == 1L );
  115. BOOST_TEST( bind(f_2, _1, 2)(i) == 21L );
  116. BOOST_TEST( bind(f_3, _1, 2, 3)(i) == 321L );
  117. BOOST_TEST( bind(f_4, _1, 2, 3, 4)(i) == 4321L );
  118. BOOST_TEST( bind(f_5, _1, 2, 3, 4, 5)(i) == 54321L );
  119. BOOST_TEST( bind(f_6, _1, 2, 3, 4, 5, 6)(i) == 654321L );
  120. BOOST_TEST( bind(f_7, _1, 2, 3, 4, 5, 6, 7)(i) == 7654321L );
  121. BOOST_TEST( bind(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i) == 87654321L );
  122. BOOST_TEST( bind(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i) == 987654321L );
  123. BOOST_TEST( (bind(fv_0)(i), (global_result == 17041L)) );
  124. BOOST_TEST( (bind(fv_1, _1)(i), (global_result == 1L)) );
  125. BOOST_TEST( (bind(fv_2, _1, 2)(i), (global_result == 21L)) );
  126. BOOST_TEST( (bind(fv_3, _1, 2, 3)(i), (global_result == 321L)) );
  127. BOOST_TEST( (bind(fv_4, _1, 2, 3, 4)(i), (global_result == 4321L)) );
  128. BOOST_TEST( (bind(fv_5, _1, 2, 3, 4, 5)(i), (global_result == 54321L)) );
  129. BOOST_TEST( (bind(fv_6, _1, 2, 3, 4, 5, 6)(i), (global_result == 654321L)) );
  130. BOOST_TEST( (bind(fv_7, _1, 2, 3, 4, 5, 6, 7)(i), (global_result == 7654321L)) );
  131. BOOST_TEST( (bind(fv_8, _1, 2, 3, 4, 5, 6, 7, 8)(i), (global_result == 87654321L)) );
  132. BOOST_TEST( (bind(fv_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i), (global_result == 987654321L)) );
  133. }
  134. //
  135. struct Y
  136. {
  137. template <typename Sig>
  138. struct result;
  139. template <typename This, typename A0>
  140. struct result<This(A0 &)> { typedef short type; };
  141. template <typename This, typename A0, typename A1>
  142. struct result<This(A0, A1)> { typedef int type; };
  143. template <typename This, typename A0, typename A1, typename A2>
  144. struct result<This(A0, A1, A2)> { typedef long type; };
  145. template <typename This, typename A0, typename A1, typename A2, typename A3>
  146. struct result<This(A0, A1, A2, A3)> { typedef void type; };
  147. short operator()(short & r) const { return ++r; }
  148. int operator()(int a, int b) const { return a + 10 * b; }
  149. long operator() (long a, long b, long c) const { return a + 10 * b + 100 * c; }
  150. void operator() (long a, long b, long c, long d) const { global_result = a + 10 * b + 100 * c + 1000 * d; }
  151. };
  152. void function_object_test()
  153. {
  154. using boost::phoenix::bind;
  155. using boost::phoenix::ref;
  156. using boost::phoenix::placeholders::_1;
  157. short i(6);
  158. int const k = 3;
  159. BOOST_TEST( bind(Y(), ref(i))() == 7 );
  160. BOOST_TEST( bind(Y(), ref(i))() == 8 );
  161. BOOST_TEST( bind(Y(), i, _1)(k) == 38 );
  162. BOOST_TEST( bind(Y(), i, _1, 9)(k) == 938 );
  163. #if !defined(__MWERKS__) || (__MWERKS__ > 0x2407) // Fails for this version of the compiler.
  164. global_result = 0;
  165. bind(Y(), i, _1, 9, 4)(k);
  166. BOOST_TEST( global_result == 4938 );
  167. #endif
  168. }
  169. void function_object_test2()
  170. {
  171. using boost::phoenix::bind;
  172. using boost::phoenix::ref;
  173. using boost::phoenix::placeholders::_1;
  174. short i(6);
  175. int const k = 3;
  176. BOOST_TEST( bind(Y(), ref(i))() == 7 );
  177. BOOST_TEST( bind(Y(), ref(i))() == 8 );
  178. BOOST_TEST( bind(Y(), i, _1)(k) == 38 );
  179. BOOST_TEST( bind(Y(), i, _1, 9)(k) == 938 );
  180. global_result = 0;
  181. bind(Y(), i, _1, 9, 4)(k);
  182. BOOST_TEST( global_result == 4938 );
  183. }
  184. //
  185. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  186. struct Z
  187. {
  188. typedef int result_type;
  189. int operator()(int a, int b) const { return a + 10 * b; }
  190. };
  191. void adaptable_function_object_test()
  192. {
  193. using boost::phoenix::bind;
  194. BOOST_TEST( bind(Z(), 7, 4)() == 47 );
  195. }
  196. #endif
  197. //
  198. struct X
  199. {
  200. mutable unsigned int hash;
  201. X(): hash(0) {}
  202. int f0() { f1(17); return 0; }
  203. int g0() const { g1(17); return 0; }
  204. int f1(int a1) { hash = (hash * 17041 + a1) % 32768; return 0; }
  205. int g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; return 0; }
  206. int f2(int a1, int a2) { f1(a1); f1(a2); return 0; }
  207. int g2(int a1, int a2) const { g1(a1); g1(a2); return 0; }
  208. int f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); return 0; }
  209. int g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); return 0; }
  210. int f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); return 0; }
  211. int g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); return 0; }
  212. int f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); return 0; }
  213. int g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); return 0; }
  214. int f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); return 0; }
  215. 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; }
  216. 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; }
  217. 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; }
  218. 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; }
  219. 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; }
  220. };
  221. struct V
  222. {
  223. mutable unsigned int hash;
  224. V(): hash(0) {}
  225. void f0() { f1(17); }
  226. void g0() const { g1(17); }
  227. void f1(int a1) { hash = (hash * 17041 + a1) % 32768; }
  228. void g1(int a1) const { hash = (hash * 17041 + a1 * 2) % 32768; }
  229. void f2(int a1, int a2) { f1(a1); f1(a2); }
  230. void g2(int a1, int a2) const { g1(a1); g1(a2); }
  231. void f3(int a1, int a2, int a3) { f2(a1, a2); f1(a3); }
  232. void g3(int a1, int a2, int a3) const { g2(a1, a2); g1(a3); }
  233. void f4(int a1, int a2, int a3, int a4) { f3(a1, a2, a3); f1(a4); }
  234. void g4(int a1, int a2, int a3, int a4) const { g3(a1, a2, a3); g1(a4); }
  235. void f5(int a1, int a2, int a3, int a4, int a5) { f4(a1, a2, a3, a4); f1(a5); }
  236. void g5(int a1, int a2, int a3, int a4, int a5) const { g4(a1, a2, a3, a4); g1(a5); }
  237. void f6(int a1, int a2, int a3, int a4, int a5, int a6) { f5(a1, a2, a3, a4, a5); f1(a6); }
  238. void g6(int a1, int a2, int a3, int a4, int a5, int a6) const { g5(a1, a2, a3, a4, a5); g1(a6); }
  239. 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); }
  240. 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); }
  241. 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); }
  242. 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); }
  243. };
  244. void member_function_test()
  245. {
  246. using boost::phoenix::bind;
  247. using boost::phoenix::ref;
  248. X x;
  249. // 0
  250. bind(&X::f0, &x)();
  251. bind(&X::f0, ref(x))();
  252. bind(&X::g0, &x)();
  253. bind(&X::g0, x)();
  254. bind(&X::g0, ref(x))();
  255. // 1
  256. bind(&X::f1, &x, 1)();
  257. bind(&X::f1, ref(x), 1)();
  258. bind(&X::g1, &x, 1)();
  259. bind(&X::g1, x, 1)();
  260. bind(&X::g1, ref(x), 1)();
  261. // 2
  262. bind(&X::f2, &x, 1, 2)();
  263. bind(&X::f2, ref(x), 1, 2)();
  264. bind(&X::g2, &x, 1, 2)();
  265. bind(&X::g2, x, 1, 2)();
  266. bind(&X::g2, ref(x), 1, 2)();
  267. // 3
  268. bind(&X::f3, &x, 1, 2, 3)();
  269. bind(&X::f3, ref(x), 1, 2, 3)();
  270. bind(&X::g3, &x, 1, 2, 3)();
  271. bind(&X::g3, x, 1, 2, 3)();
  272. bind(&X::g3, ref(x), 1, 2, 3)();
  273. // 4
  274. bind(&X::f4, &x, 1, 2, 3, 4)();
  275. bind(&X::f4, ref(x), 1, 2, 3, 4)();
  276. bind(&X::g4, &x, 1, 2, 3, 4)();
  277. bind(&X::g4, x, 1, 2, 3, 4)();
  278. bind(&X::g4, ref(x), 1, 2, 3, 4)();
  279. // 5
  280. bind(&X::f5, &x, 1, 2, 3, 4, 5)();
  281. bind(&X::f5, ref(x), 1, 2, 3, 4, 5)();
  282. bind(&X::g5, &x, 1, 2, 3, 4, 5)();
  283. bind(&X::g5, x, 1, 2, 3, 4, 5)();
  284. bind(&X::g5, ref(x), 1, 2, 3, 4, 5)();
  285. // 6
  286. bind(&X::f6, &x, 1, 2, 3, 4, 5, 6)();
  287. bind(&X::f6, ref(x), 1, 2, 3, 4, 5, 6)();
  288. bind(&X::g6, &x, 1, 2, 3, 4, 5, 6)();
  289. bind(&X::g6, x, 1, 2, 3, 4, 5, 6)();
  290. bind(&X::g6, ref(x), 1, 2, 3, 4, 5, 6)();
  291. // 7
  292. bind(&X::f7, &x, 1, 2, 3, 4, 5, 6, 7)();
  293. bind(&X::f7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
  294. bind(&X::g7, &x, 1, 2, 3, 4, 5, 6, 7)();
  295. bind(&X::g7, x, 1, 2, 3, 4, 5, 6, 7)();
  296. bind(&X::g7, ref(x), 1, 2, 3, 4, 5, 6, 7)();
  297. // 8
  298. bind(&X::f8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
  299. bind(&X::f8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
  300. bind(&X::g8, &x, 1, 2, 3, 4, 5, 6, 7, 8)();
  301. bind(&X::g8, x, 1, 2, 3, 4, 5, 6, 7, 8)();
  302. bind(&X::g8, ref(x), 1, 2, 3, 4, 5, 6, 7, 8)();
  303. BOOST_TEST( x.hash == 23558 );
  304. }
  305. void member_function_void_test()
  306. {
  307. using boost::phoenix::bind;
  308. using boost::phoenix::ref;
  309. using boost::phoenix::placeholders::_1;
  310. V v;
  311. // 0
  312. bind(&V::f0, &v)();
  313. bind(&V::f0, ref(v))();
  314. bind(&V::g0, &v)();
  315. bind(&V::g0, v)();
  316. bind(&V::g0, ref(v))();
  317. // 1
  318. bind(&V::f1, &v, 1)();
  319. bind(&V::f1, ref(v), 1)();
  320. bind(&V::g1, &v, 1)();
  321. bind(&V::g1, v, 1)();
  322. bind(&V::g1, ref(v), 1)();
  323. // 2
  324. bind(&V::f2, &v, 1, 2)();
  325. bind(&V::f2, ref(v), 1, 2)();
  326. bind(&V::g2, &v, 1, 2)();
  327. bind(&V::g2, v, 1, 2)();
  328. bind(&V::g2, ref(v), 1, 2)();
  329. // 3
  330. bind(&V::f3, &v, 1, 2, 3)();
  331. bind(&V::f3, ref(v), 1, 2, 3)();
  332. bind(&V::g3, &v, 1, 2, 3)();
  333. bind(&V::g3, v, 1, 2, 3)();
  334. bind(&V::g3, ref(v), 1, 2, 3)();
  335. // 4
  336. bind(&V::f4, &v, 1, 2, 3, 4)();
  337. bind(&V::f4, ref(v), 1, 2, 3, 4)();
  338. bind(&V::g4, &v, 1, 2, 3, 4)();
  339. bind(&V::g4, v, 1, 2, 3, 4)();
  340. bind(&V::g4, ref(v), 1, 2, 3, 4)();
  341. // 5
  342. bind(&V::f5, &v, 1, 2, 3, 4, 5)();
  343. bind(&V::f5, ref(v), 1, 2, 3, 4, 5)();
  344. bind(&V::g5, &v, 1, 2, 3, 4, 5)();
  345. bind(&V::g5, v, 1, 2, 3, 4, 5)();
  346. bind(&V::g5, ref(v), 1, 2, 3, 4, 5)();
  347. // 6
  348. bind(&V::f6, &v, 1, 2, 3, 4, 5, 6)();
  349. bind(&V::f6, ref(v), 1, 2, 3, 4, 5, 6)();
  350. bind(&V::g6, &v, 1, 2, 3, 4, 5, 6)();
  351. bind(&V::g6, v, 1, 2, 3, 4, 5, 6)();
  352. bind(&V::g6, ref(v), 1, 2, 3, 4, 5, 6)();
  353. // 7
  354. bind(&V::f7, &v, 1, 2, 3, 4, 5, 6, 7)();
  355. bind(&V::f7, ref(v), 1, 2, 3, 4, 5, 6, 7)();
  356. bind(&V::g7, &v, 1, 2, 3, 4, 5, 6, 7)();
  357. bind(&V::g7, v, 1, 2, 3, 4, 5, 6, 7)();
  358. bind(&V::g7, ref(v), 1, 2, 3, 4, 5, 6, 7)();
  359. // 8
  360. bind(&V::f8, &v, 1, 2, 3, 4, 5, 6, 7, 8)();
  361. bind(&V::f8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)();
  362. bind(&V::g8, &v, 1, 2, 3, 4, 5, 6, 7, 8)();
  363. bind(&V::g8, v, 1, 2, 3, 4, 5, 6, 7, 8)();
  364. bind(&V::g8, ref(v), 1, 2, 3, 4, 5, 6, 7, 8)();
  365. BOOST_TEST( v.hash == 23558 );
  366. }
  367. void nested_bind_test()
  368. {
  369. using boost::phoenix::bind;
  370. using boost::phoenix::placeholders::_1;
  371. using boost::phoenix::placeholders::_2;
  372. int const x = 1;
  373. int const y = 2;
  374. BOOST_TEST( bind(f_1, bind(f_1, _1))(x) == 1L );
  375. BOOST_TEST( bind(f_1, bind(f_2, _1, _2))(x, y) == 21L );
  376. BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _1))(x) == 11L );
  377. BOOST_TEST( bind(f_2, bind(f_1, _1), bind(f_1, _2))(x, y) == 21L );
  378. BOOST_TEST( bind(f_1, bind(f_0))() == 17041L );
  379. BOOST_TEST( (bind(fv_1, bind(f_1, _1))(x), (global_result == 1L)) );
  380. BOOST_TEST( (bind(fv_1, bind(f_2, _1, _2))(x, y), (global_result == 21L)) );
  381. BOOST_TEST( (bind(fv_2, bind(f_1, _1), bind(f_1, _1))(x), (global_result == 11L)) );
  382. BOOST_TEST( (bind(fv_2, bind(f_1, _1), bind(f_1, _2))(x, y), (global_result == 21L)) );
  383. BOOST_TEST( (bind(fv_1, bind(f_0))(), (global_result == 17041L)) );
  384. }
  385. int main()
  386. {
  387. function_test();
  388. function_object_test();
  389. function_object_test2();
  390. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
  391. adaptable_function_object_test();
  392. #endif
  393. member_function_test();
  394. member_function_void_test();
  395. nested_bind_test();
  396. return boost::report_errors();
  397. }