bind_eq_test.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*==============================================================================
  2. Copyright (c) 2004, 2005, 2009 Peter Dimov
  3. Copyright (c) 2005-2010 Joel de Guzman
  4. Copyright (c) 2010 Thomas Heller
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #include <boost/config.hpp>
  9. #if defined(BOOST_MSVC)
  10. #pragma warning(disable: 4786) // identifier truncated in debug info
  11. #pragma warning(disable: 4710) // function not inlined
  12. #pragma warning(disable: 4711) // function selected for automatic inline expansion
  13. #pragma warning(disable: 4514) // unreferenced inline removed
  14. #endif
  15. #include <boost/phoenix/core.hpp>
  16. #include <boost/phoenix/bind.hpp>
  17. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  18. # include <boost/function_equal.hpp>
  19. #endif
  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. struct X
  29. {
  30. int i_;
  31. explicit X(int i): i_(i)
  32. {
  33. }
  34. bool operator==(X const & rhs) const
  35. {
  36. return i_ == rhs.i_;
  37. }
  38. };
  39. // f_*
  40. int f_0()
  41. {
  42. return 0;
  43. }
  44. int f_1(X)
  45. {
  46. return 0;
  47. }
  48. int f_2(X, X)
  49. {
  50. return 0;
  51. }
  52. int f_3(X, X, X)
  53. {
  54. return 0;
  55. }
  56. int f_4(X, X, X, X)
  57. {
  58. return 0;
  59. }
  60. int f_5(X, X, X, X, X)
  61. {
  62. return 0;
  63. }
  64. int f_6(X, X, X, X, X, X)
  65. {
  66. return 0;
  67. }
  68. int f_7(X, X, X, X, X, X, X)
  69. {
  70. return 0;
  71. }
  72. int f_8(X, X, X, X, X, X, X, X)
  73. {
  74. return 0;
  75. }
  76. int f_9(X, X, X, X, X, X, X, X, X)
  77. {
  78. return 0;
  79. }
  80. // fv_*
  81. void fv_0()
  82. {
  83. }
  84. void fv_1(X)
  85. {
  86. }
  87. void fv_2(X, X)
  88. {
  89. }
  90. void fv_3(X, X, X)
  91. {
  92. }
  93. void fv_4(X, X, X, X)
  94. {
  95. }
  96. void fv_5(X, X, X, X, X)
  97. {
  98. }
  99. void fv_6(X, X, X, X, X, X)
  100. {
  101. }
  102. void fv_7(X, X, X, X, X, X, X)
  103. {
  104. }
  105. void fv_8(X, X, X, X, X, X, X, X)
  106. {
  107. }
  108. void fv_9(X, X, X, X, X, X, X, X, X)
  109. {
  110. }
  111. template<class F> void test_eq(F f1, F f2)
  112. {
  113. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  114. using boost::function_equal;
  115. #endif
  116. BOOST_TEST( function_equal( f1, f2 ) );
  117. }
  118. template<class F> void test_ne(F f1, F f2)
  119. {
  120. #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
  121. using boost::function_equal;
  122. #endif
  123. BOOST_TEST( !function_equal( f1, f2 ) );
  124. }
  125. using boost::phoenix::bind;
  126. using boost::phoenix::ref;
  127. using boost::phoenix::placeholders::_1;
  128. using boost::phoenix::placeholders::_2;
  129. using boost::phoenix::placeholders::_3;
  130. using boost::phoenix::placeholders::_4;
  131. using boost::phoenix::placeholders::_5;
  132. using boost::phoenix::placeholders::_6;
  133. using boost::phoenix::placeholders::_7;
  134. using boost::phoenix::placeholders::_8;
  135. using boost::phoenix::placeholders::_9;
  136. // 0
  137. template<class F> void test_0(F f)
  138. {
  139. test_eq( bind(f), bind(f) );
  140. }
  141. // 1
  142. template<class F, class V> void test_1_(F f, V v1, V v2)
  143. {
  144. test_eq( bind(f, v1), bind(f, v1) );
  145. test_ne( bind(f, v1), bind(f, v2) );
  146. }
  147. template<class F> void test_1(F f)
  148. {
  149. test_eq( bind(f, _1), bind(f, _1) );
  150. test_1_( f, X(1), X(2) );
  151. X a(0), b(0);
  152. test_1_( f, ref(a), ref(b) );
  153. }
  154. // 2
  155. template<class F, class V> void test_2_(F f, V v1, V v2)
  156. {
  157. test_eq( bind(f, v1, v1), bind(f, v1, v1) );
  158. test_ne( bind(f, v1, v1), bind(f, v1, v2) );
  159. test_ne( bind(f, v1, v1), bind(f, v2, v1) );
  160. }
  161. template<class F> void test_2(F f)
  162. {
  163. test_eq( bind(f, _1, _2), bind(f, _1, _2) );
  164. test_2_( f, X(1), X(2) );
  165. X a(0), b(0);
  166. test_2_( f, ref(a), ref(b) );
  167. }
  168. // 3
  169. template<class F, class V> void test_3_(F f, V v1, V v2)
  170. {
  171. test_eq( bind(f, v1, v1, v1), bind(f, v1, v1, v1) );
  172. test_ne( bind(f, v1, v1, v1), bind(f, v1, v1, v2) );
  173. test_ne( bind(f, v1, v1, v1), bind(f, v1, v2, v1) );
  174. test_ne( bind(f, v1, v1, v1), bind(f, v2, v1, v1) );
  175. }
  176. template<class F> void test_3(F f)
  177. {
  178. test_eq( bind(f, _1, _2, _3), bind(f, _1, _2, _3) );
  179. test_3_( f, X(1), X(2) );
  180. X a(0), b(0);
  181. test_3_( f, ref(a), ref(b) );
  182. }
  183. // 4
  184. template<class F, class V> void test_4_(F f, V v1, V v2)
  185. {
  186. test_eq( bind(f, v1, v1, v1, v1), bind(f, v1, v1, v1, v1) );
  187. test_ne( bind(f, v1, v1, v1, v1), bind(f, v1, v1, v1, v2) );
  188. test_ne( bind(f, v1, v1, v1, v1), bind(f, v1, v1, v2, v1) );
  189. test_ne( bind(f, v1, v1, v1, v1), bind(f, v1, v2, v1, v1) );
  190. test_ne( bind(f, v1, v1, v1, v1), bind(f, v2, v1, v1, v1) );
  191. }
  192. template<class F> void test_4(F f)
  193. {
  194. test_eq( bind(f, _1, _2, _3, _4), bind(f, _1, _2, _3, _4) );
  195. test_4_( f, X(1), X(2) );
  196. X a(0), b(0);
  197. test_4_( f, ref(a), ref(b) );
  198. }
  199. // 5
  200. template<class F, class V> void test_5_(F f, V v1, V v2)
  201. {
  202. test_eq( bind(f, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1) );
  203. test_ne( bind(f, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v2) );
  204. test_ne( bind(f, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v2, v1) );
  205. test_ne( bind(f, v1, v1, v1, v1, v1), bind(f, v1, v1, v2, v1, v1) );
  206. test_ne( bind(f, v1, v1, v1, v1, v1), bind(f, v1, v2, v1, v1, v1) );
  207. test_ne( bind(f, v1, v1, v1, v1, v1), bind(f, v2, v1, v1, v1, v1) );
  208. }
  209. template<class F> void test_5(F f)
  210. {
  211. test_eq( bind(f, _1, _2, _3, _4, _5), bind(f, _1, _2, _3, _4, _5) );
  212. test_5_( f, X(1), X(2) );
  213. X a(0), b(0);
  214. test_5_( f, ref(a), ref(b) );
  215. }
  216. // 6
  217. template<class F, class V> void test_6_(F f, V v1, V v2)
  218. {
  219. test_eq( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1) );
  220. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v2) );
  221. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v2, v1) );
  222. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v2, v1, v1) );
  223. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v2, v1, v1, v1) );
  224. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v1, v2, v1, v1, v1, v1) );
  225. test_ne( bind(f, v1, v1, v1, v1, v1, v1), bind(f, v2, v1, v1, v1, v1, v1) );
  226. }
  227. template<class F> void test_6(F f)
  228. {
  229. test_eq( bind(f, _1, _2, _3, _4, _5, _6), bind(f, _1, _2, _3, _4, _5, _6) );
  230. test_6_( f, X(1), X(2) );
  231. X a(0), b(0);
  232. test_6_( f, ref(a), ref(b) );
  233. }
  234. // 7
  235. template<class F, class V> void test_7_(F f, V v1, V v2)
  236. {
  237. test_eq( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1) );
  238. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v2) );
  239. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v2, v1) );
  240. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v2, v1, v1) );
  241. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v2, v1, v1, v1) );
  242. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v2, v1, v1, v1, v1) );
  243. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v2, v1, v1, v1, v1, v1) );
  244. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1), bind(f, v2, v1, v1, v1, v1, v1, v1) );
  245. }
  246. template<class F> void test_7(F f)
  247. {
  248. test_eq( bind(f, _1, _2, _3, _4, _5, _6, _7), bind(f, _1, _2, _3, _4, _5, _6, _7) );
  249. test_7_( f, X(1), X(2) );
  250. X a(0), b(0);
  251. test_7_( f, ref(a), ref(b) );
  252. }
  253. // 8
  254. template<class F, class V> void test_8_(F f, V v1, V v2)
  255. {
  256. test_eq( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1, v1) );
  257. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1, v2) );
  258. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v2, v1) );
  259. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v2, v1, v1) );
  260. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v2, v1, v1, v1) );
  261. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v2, v1, v1, v1, v1) );
  262. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v2, v1, v1, v1, v1, v1) );
  263. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v2, v1, v1, v1, v1, v1, v1) );
  264. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v2, v1, v1, v1, v1, v1, v1, v1) );
  265. }
  266. template<class F> void test_8(F f)
  267. {
  268. test_eq( bind(f, _1, _2, _3, _4, _5, _6, _7, _8), bind(f, _1, _2, _3, _4, _5, _6, _7, _8) );
  269. test_8_( f, X(1), X(2) );
  270. X a(0), b(0);
  271. test_8_( f, ref(a), ref(b) );
  272. }
  273. // 9
  274. template<class F, class V> void test_9_(F f, V v1, V v2)
  275. {
  276. test_eq( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1) );
  277. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v2) );
  278. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v1, v2, v1) );
  279. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v1, v2, v1, v1) );
  280. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v1, v2, v1, v1, v1) );
  281. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v1, v2, v1, v1, v1, v1) );
  282. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v1, v2, v1, v1, v1, v1, v1) );
  283. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v1, v2, v1, v1, v1, v1, v1, v1) );
  284. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v1, v2, v1, v1, v1, v1, v1, v1, v1) );
  285. test_ne( bind(f, v1, v1, v1, v1, v1, v1, v1, v1, v1), bind(f, v2, v1, v1, v1, v1, v1, v1, v1, v1) );
  286. }
  287. template<class F> void test_9(F f)
  288. {
  289. test_eq( bind(f, _1, _2, _3, _4, _5, _6, _7, _8, _9), bind(f, _1, _2, _3, _4, _5, _6, _7, _8, _9) );
  290. test_9_( f, X(1), X(2) );
  291. X a(0), b(0);
  292. test_9_( f, ref(a), ref(b) );
  293. }
  294. int main()
  295. {
  296. // 0
  297. test_0( f_0 );
  298. test_0( fv_0 );
  299. // 1
  300. test_1( f_1 );
  301. test_1( fv_1 );
  302. // 2
  303. test_2( f_2 );
  304. test_2( fv_2 );
  305. // 3
  306. test_3( f_3 );
  307. test_3( fv_3 );
  308. // 4
  309. test_4( f_4 );
  310. test_4( fv_4 );
  311. // 5
  312. test_5( f_5 );
  313. test_5( fv_5 );
  314. // 6
  315. test_6( f_6 );
  316. test_6( fv_6 );
  317. // 7
  318. test_7( f_7 );
  319. test_7( fv_7 );
  320. // 8
  321. test_8( f_8 );
  322. test_8( fv_8 );
  323. // 9
  324. test_9( f_9 );
  325. test_9( fv_9 );
  326. return boost::report_errors();
  327. }