bind_stateful_test.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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_stateful_test.cpp
  10. //
  11. // Copyright (c) 2004 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. #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. class X
  27. {
  28. private:
  29. int state_;
  30. public:
  31. X(): state_(0)
  32. {
  33. }
  34. // SGI-related compilers have odd compiler-synthesized ctors and dtors
  35. #ifdef __PATHSCALE__
  36. ~X() {}
  37. #endif
  38. int state() const
  39. {
  40. return state_;
  41. }
  42. int operator()()
  43. {
  44. return state_ += 17041;
  45. }
  46. int operator()(int x1)
  47. {
  48. return state_ += x1;
  49. }
  50. int operator()(int x1, int x2)
  51. {
  52. return state_ += x1+x2;
  53. }
  54. int operator()(int x1, int x2, int x3)
  55. {
  56. return state_ += x1+x2+x3;
  57. }
  58. int operator()(int x1, int x2, int x3, int x4)
  59. {
  60. return state_ += x1+x2+x3+x4;
  61. }
  62. int operator()(int x1, int x2, int x3, int x4, int x5)
  63. {
  64. return state_ += x1+x2+x3+x4+x5;
  65. }
  66. int operator()(int x1, int x2, int x3, int x4, int x5, int x6)
  67. {
  68. return state_ += x1+x2+x3+x4+x5+x6;
  69. }
  70. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7)
  71. {
  72. return state_ += x1+x2+x3+x4+x5+x6+x7;
  73. }
  74. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
  75. {
  76. return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
  77. }
  78. int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
  79. {
  80. return state_ += x1+x2+x3+x4+x5+x6+x7+x8+x9;
  81. }
  82. };
  83. int f0(int & state_)
  84. {
  85. return state_ += 17041;
  86. }
  87. int f1(int & state_, int x1)
  88. {
  89. return state_ += x1;
  90. }
  91. int f2(int & state_, int x1, int x2)
  92. {
  93. return state_ += x1+x2;
  94. }
  95. int f3(int & state_, int x1, int x2, int x3)
  96. {
  97. return state_ += x1+x2+x3;
  98. }
  99. int f4(int & state_, int x1, int x2, int x3, int x4)
  100. {
  101. return state_ += x1+x2+x3+x4;
  102. }
  103. int f5(int & state_, int x1, int x2, int x3, int x4, int x5)
  104. {
  105. return state_ += x1+x2+x3+x4+x5;
  106. }
  107. int f6(int & state_, int x1, int x2, int x3, int x4, int x5, int x6)
  108. {
  109. return state_ += x1+x2+x3+x4+x5+x6;
  110. }
  111. int f7(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7)
  112. {
  113. return state_ += x1+x2+x3+x4+x5+x6+x7;
  114. }
  115. int f8(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
  116. {
  117. return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
  118. }
  119. template<class F> void test(F f, int a, int b)
  120. {
  121. BOOST_TEST( f() == a + b );
  122. BOOST_TEST( f() == a + 2*b );
  123. BOOST_TEST( f() == a + 3*b );
  124. }
  125. void stateful_function_object_test()
  126. {
  127. test( boost::bind<int>( X() ), 0, 17041 );
  128. test( boost::bind<int>( X(), 1 ), 0, 1 );
  129. test( boost::bind<int>( X(), 1, 2 ), 0, 1+2 );
  130. test( boost::bind<int>( X(), 1, 2, 3 ), 0, 1+2+3 );
  131. test( boost::bind<int>( X(), 1, 2, 3, 4 ), 0, 1+2+3+4 );
  132. test( boost::bind<int>( X(), 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
  133. test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
  134. test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
  135. test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
  136. test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 0, 1+2+3+4+5+6+7+8+9 );
  137. X x;
  138. int n = x.state();
  139. test( boost::bind<int>( boost::ref(x) ), n, 17041 );
  140. n += 3*17041;
  141. test( boost::bind<int>( boost::ref(x), 1 ), n, 1 );
  142. n += 3*1;
  143. test( boost::bind<int>( boost::ref(x), 1, 2 ), n, 1+2 );
  144. n += 3*(1+2);
  145. test( boost::bind<int>( boost::ref(x), 1, 2, 3 ), n, 1+2+3 );
  146. n += 3*(1+2+3);
  147. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4 ), n, 1+2+3+4 );
  148. n += 3*(1+2+3+4);
  149. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5 ), n, 1+2+3+4+5 );
  150. n += 3*(1+2+3+4+5);
  151. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6 ), n, 1+2+3+4+5+6 );
  152. n += 3*(1+2+3+4+5+6);
  153. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7 ), n, 1+2+3+4+5+6+7 );
  154. n += 3*(1+2+3+4+5+6+7);
  155. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ), n, 1+2+3+4+5+6+7+8 );
  156. n += 3*(1+2+3+4+5+6+7+8);
  157. test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), n, 1+2+3+4+5+6+7+8+9 );
  158. n += 3*(1+2+3+4+5+6+7+8+9);
  159. BOOST_TEST( x.state() == n );
  160. }
  161. void stateful_function_test()
  162. {
  163. test( boost::bind( f0, 0 ), 0, 17041 );
  164. test( boost::bind( f1, 0, 1 ), 0, 1 );
  165. test( boost::bind( f2, 0, 1, 2 ), 0, 1+2 );
  166. test( boost::bind( f3, 0, 1, 2, 3 ), 0, 1+2+3 );
  167. test( boost::bind( f4, 0, 1, 2, 3, 4 ), 0, 1+2+3+4 );
  168. test( boost::bind( f5, 0, 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
  169. test( boost::bind( f6, 0, 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
  170. test( boost::bind( f7, 0, 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
  171. test( boost::bind( f8, 0, 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
  172. }
  173. int main()
  174. {
  175. stateful_function_object_test();
  176. stateful_function_test();
  177. return boost::report_errors();
  178. }