bind_stateful_test.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*==============================================================================
  2. Copyright (c) 2004 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. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  18. #pragma warning(push, 3)
  19. #endif
  20. #include <iostream>
  21. #if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
  22. #pragma warning(pop)
  23. #endif
  24. #include <boost/detail/lightweight_test.hpp>
  25. class X
  26. {
  27. private:
  28. mutable int state_;
  29. public:
  30. X(): state_(0)
  31. {
  32. }
  33. int state() const
  34. {
  35. return state_;
  36. }
  37. typedef int result_type;
  38. int operator()() const
  39. {
  40. return state_ += 17041;
  41. }
  42. int operator()(int x1) const
  43. {
  44. return state_ += x1;
  45. }
  46. int operator()(int x1, int x2) const
  47. {
  48. return state_ += x1+x2;
  49. }
  50. int operator()(int x1, int x2, int x3) const
  51. {
  52. return state_ += x1+x2+x3;
  53. }
  54. int operator()(int x1, int x2, int x3, int x4) const
  55. {
  56. return state_ += x1+x2+x3+x4;
  57. }
  58. int operator()(int x1, int x2, int x3, int x4, int x5) const
  59. {
  60. return state_ += x1+x2+x3+x4+x5;
  61. }
  62. int operator()(int x1, int x2, int x3, int x4, int x5, int x6) const
  63. {
  64. return state_ += x1+x2+x3+x4+x5+x6;
  65. }
  66. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7) const
  67. {
  68. return state_ += x1+x2+x3+x4+x5+x6+x7;
  69. }
  70. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8) const
  71. {
  72. return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
  73. }
  74. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9) const
  75. {
  76. return state_ += x1+x2+x3+x4+x5+x6+x7+x8+x9;
  77. }
  78. };
  79. class Y
  80. {
  81. private:
  82. int state_;
  83. public:
  84. Y(): state_(0)
  85. {
  86. }
  87. int state() const
  88. {
  89. return state_;
  90. }
  91. typedef int result_type;
  92. int operator()()
  93. {
  94. return state_ += 17041;
  95. }
  96. int operator()(int x1)
  97. {
  98. return state_ += x1;
  99. }
  100. int operator()(int x1, int x2)
  101. {
  102. return state_ += x1+x2;
  103. }
  104. int operator()(int x1, int x2, int x3)
  105. {
  106. return state_ += x1+x2+x3;
  107. }
  108. int operator()(int x1, int x2, int x3, int x4)
  109. {
  110. return state_ += x1+x2+x3+x4;
  111. }
  112. int operator()(int x1, int x2, int x3, int x4, int x5)
  113. {
  114. return state_ += x1+x2+x3+x4+x5;
  115. }
  116. int operator()(int x1, int x2, int x3, int x4, int x5, int x6)
  117. {
  118. return state_ += x1+x2+x3+x4+x5+x6;
  119. }
  120. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7)
  121. {
  122. return state_ += x1+x2+x3+x4+x5+x6+x7;
  123. }
  124. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
  125. {
  126. return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
  127. }
  128. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
  129. {
  130. return state_ += x1+x2+x3+x4+x5+x6+x7+x8+x9;
  131. }
  132. };
  133. int f0(int & state_)
  134. {
  135. return state_ += 17041;
  136. }
  137. int f1(int & state_, int x1)
  138. {
  139. return state_ += x1;
  140. }
  141. int f2(int & state_, int x1, int x2)
  142. {
  143. return state_ += x1+x2;
  144. }
  145. int f3(int & state_, int x1, int x2, int x3)
  146. {
  147. return state_ += x1+x2+x3;
  148. }
  149. int f4(int & state_, int x1, int x2, int x3, int x4)
  150. {
  151. return state_ += x1+x2+x3+x4;
  152. }
  153. int f5(int & state_, int x1, int x2, int x3, int x4, int x5)
  154. {
  155. return state_ += x1+x2+x3+x4+x5;
  156. }
  157. int f6(int & state_, int x1, int x2, int x3, int x4, int x5, int x6)
  158. {
  159. return state_ += x1+x2+x3+x4+x5+x6;
  160. }
  161. int f7(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7)
  162. {
  163. return state_ += x1+x2+x3+x4+x5+x6+x7;
  164. }
  165. int f8(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
  166. {
  167. return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
  168. }
  169. template <typename> struct wrap {};
  170. template<class F> void test(F f, int a, int b)
  171. {
  172. BOOST_TEST( f() == a + b );
  173. BOOST_TEST( f() == a + 2*b );
  174. BOOST_TEST( f() == a + 3*b );
  175. }
  176. using boost::phoenix::bind;
  177. using boost::phoenix::ref;
  178. void stateful_function_object_test()
  179. {
  180. ::test( bind( X() ), 0, 17041 );
  181. ::test( bind( X(), 1 ), 0, 1 );
  182. ::test( bind( X(), 1, 2 ), 0, 1+2 );
  183. ::test( bind( X(), 1, 2, 3 ), 0, 1+2+3 );
  184. ::test( bind( X(), 1, 2, 3, 4 ), 0, 1+2+3+4 );
  185. ::test( bind( X(), 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
  186. ::test( bind( X(), 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
  187. ::test( bind( X(), 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
  188. ::test( bind( X(), 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
  189. ::test( bind( X(), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 0, 1+2+3+4+5+6+7+8+9 );
  190. Y y;
  191. int n = y.state();
  192. ::test( bind( ref(y) ), n, 17041 );
  193. n += 3 * 17041;
  194. ::test( bind( ref(y), 1 ), n, 1 );
  195. n += 3*1;
  196. ::test( bind( ref(y), 1, 2 ), n, 1+2 );
  197. n += 3*(1+2);
  198. ::test( bind( ref(y), 1, 2, 3 ), n, 1+2+3 );
  199. n += 3*(1+2+3);
  200. ::test( bind( ref(y), 1, 2, 3, 4 ), n, 1+2+3+4 );
  201. n += 3*(1+2+3+4);
  202. ::test( bind( ref(y), 1, 2, 3, 4, 5 ), n, 1+2+3+4+5 );
  203. n += 3*(1+2+3+4+5);
  204. ::test( bind( ref(y), 1, 2, 3, 4, 5, 6 ), n, 1+2+3+4+5+6 );
  205. n += 3*(1+2+3+4+5+6);
  206. ::test( bind( ref(y), 1, 2, 3, 4, 5, 6, 7 ), n, 1+2+3+4+5+6+7 );
  207. n += 3*(1+2+3+4+5+6+7);
  208. ::test( bind( ref(y), 1, 2, 3, 4, 5, 6, 7, 8 ), n, 1+2+3+4+5+6+7+8 );
  209. n += 3*(1+2+3+4+5+6+7+8);
  210. ::test( bind( ref(y), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), n, 1+2+3+4+5+6+7+8+9 );
  211. n += 3*(1+2+3+4+5+6+7+8+9);
  212. BOOST_TEST( y.state() == n );
  213. }
  214. void stateful_function_test()
  215. {
  216. using boost::phoenix::ref;
  217. ::test( bind( f0, 0), 0, 17041 );
  218. ::test( bind( f1, 0, 1 ), 0, 1 );
  219. ::test( bind( f2, 0, 1, 2 ), 0, 1+2 );
  220. ::test( bind( f3, 0, 1, 2, 3 ), 0, 1+2+3 );
  221. ::test( bind( f4, 0, 1, 2, 3, 4 ), 0, 1+2+3+4 );
  222. ::test( bind( f5, 0, 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
  223. ::test( bind( f6, 0, 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
  224. ::test( bind( f7, 0, 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
  225. ::test( bind( f8, 0, 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
  226. }
  227. int main()
  228. {
  229. stateful_function_object_test();
  230. stateful_function_test();
  231. return boost::report_errors();
  232. }