bind_test.cpp 14 KB

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