bind_eq_test.cpp 10 KB

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