• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <boost/config.hpp>
2 
3 #if defined(BOOST_MSVC)
4 #pragma warning(disable: 4786)  // identifier truncated in debug info
5 #pragma warning(disable: 4710)  // function not inlined
6 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
7 #pragma warning(disable: 4514)  // unreferenced inline removed
8 #endif
9 
10 //
11 //  bind_stateful_test.cpp
12 //
13 //  Copyright (c) 2004 Peter Dimov
14 //
15 // Distributed under the Boost Software License, Version 1.0. (See
16 // accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
18 //
19 
20 #include <boost/bind/bind.hpp>
21 #include <boost/core/lightweight_test.hpp>
22 
23 //
24 
25 class X
26 {
27 private:
28 
29     int state_;
30 
31 public:
32 
X()33     X(): state_(0)
34     {
35     }
36 
37     // SGI-related compilers have odd compiler-synthesized ctors and dtors
38     #ifdef __PATHSCALE__
~X()39     ~X() {}
40     #endif
41 
state() const42     int state() const
43     {
44         return state_;
45     }
46 
operator ()()47     int operator()()
48     {
49         return state_ += 17041;
50     }
51 
operator ()(int x1)52     int operator()(int x1)
53     {
54         return state_ += x1;
55     }
56 
operator ()(int x1,int x2)57     int operator()(int x1, int x2)
58     {
59         return state_ += x1+x2;
60     }
61 
operator ()(int x1,int x2,int x3)62     int operator()(int x1, int x2, int x3)
63     {
64         return state_ += x1+x2+x3;
65     }
66 
operator ()(int x1,int x2,int x3,int x4)67     int operator()(int x1, int x2, int x3, int x4)
68     {
69         return state_ += x1+x2+x3+x4;
70     }
71 
operator ()(int x1,int x2,int x3,int x4,int x5)72     int operator()(int x1, int x2, int x3, int x4, int x5)
73     {
74         return state_ += x1+x2+x3+x4+x5;
75     }
76 
operator ()(int x1,int x2,int x3,int x4,int x5,int x6)77     int operator()(int x1, int x2, int x3, int x4, int x5, int x6)
78     {
79         return state_ += x1+x2+x3+x4+x5+x6;
80     }
81 
operator ()(int x1,int x2,int x3,int x4,int x5,int x6,int x7)82     int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7)
83     {
84         return state_ += x1+x2+x3+x4+x5+x6+x7;
85     }
86 
operator ()(int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8)87     int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
88     {
89         return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
90     }
91 
operator ()(int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8,int x9)92     int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
93     {
94         return state_ += x1+x2+x3+x4+x5+x6+x7+x8+x9;
95     }
96 };
97 
f0(int & state_)98 int f0(int & state_)
99 {
100     return state_ += 17041;
101 }
102 
f1(int & state_,int x1)103 int f1(int & state_, int x1)
104 {
105     return state_ += x1;
106 }
107 
f2(int & state_,int x1,int x2)108 int f2(int & state_, int x1, int x2)
109 {
110     return state_ += x1+x2;
111 }
112 
f3(int & state_,int x1,int x2,int x3)113 int f3(int & state_, int x1, int x2, int x3)
114 {
115     return state_ += x1+x2+x3;
116 }
117 
f4(int & state_,int x1,int x2,int x3,int x4)118 int f4(int & state_, int x1, int x2, int x3, int x4)
119 {
120     return state_ += x1+x2+x3+x4;
121 }
122 
f5(int & state_,int x1,int x2,int x3,int x4,int x5)123 int f5(int & state_, int x1, int x2, int x3, int x4, int x5)
124 {
125     return state_ += x1+x2+x3+x4+x5;
126 }
127 
f6(int & state_,int x1,int x2,int x3,int x4,int x5,int x6)128 int f6(int & state_, int x1, int x2, int x3, int x4, int x5, int x6)
129 {
130     return state_ += x1+x2+x3+x4+x5+x6;
131 }
132 
f7(int & state_,int x1,int x2,int x3,int x4,int x5,int x6,int x7)133 int f7(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7)
134 {
135     return state_ += x1+x2+x3+x4+x5+x6+x7;
136 }
137 
f8(int & state_,int x1,int x2,int x3,int x4,int x5,int x6,int x7,int x8)138 int f8(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
139 {
140     return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
141 }
142 
test(F f,int a,int b)143 template<class F> void test(F f, int a, int b)
144 {
145     BOOST_TEST( f() == a +   b );
146     BOOST_TEST( f() == a + 2*b );
147     BOOST_TEST( f() == a + 3*b );
148 }
149 
stateful_function_object_test()150 void stateful_function_object_test()
151 {
152     test( boost::bind<int>( X() ), 0, 17041 );
153     test( boost::bind<int>( X(), 1 ), 0, 1 );
154     test( boost::bind<int>( X(), 1, 2 ), 0, 1+2 );
155     test( boost::bind<int>( X(), 1, 2, 3 ), 0, 1+2+3 );
156     test( boost::bind<int>( X(), 1, 2, 3, 4 ), 0, 1+2+3+4 );
157     test( boost::bind<int>( X(), 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
158     test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
159     test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
160     test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
161     test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 0, 1+2+3+4+5+6+7+8+9 );
162 
163     X x;
164 
165     int n = x.state();
166 
167     test( boost::bind<int>( boost::ref(x) ), n, 17041 );
168     n += 3*17041;
169 
170     test( boost::bind<int>( boost::ref(x), 1 ), n, 1 );
171     n += 3*1;
172 
173     test( boost::bind<int>( boost::ref(x), 1, 2 ), n, 1+2 );
174     n += 3*(1+2);
175 
176     test( boost::bind<int>( boost::ref(x), 1, 2, 3 ), n, 1+2+3 );
177     n += 3*(1+2+3);
178 
179     test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4 ), n, 1+2+3+4 );
180     n += 3*(1+2+3+4);
181 
182     test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5 ), n, 1+2+3+4+5 );
183     n += 3*(1+2+3+4+5);
184 
185     test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6 ), n, 1+2+3+4+5+6 );
186     n += 3*(1+2+3+4+5+6);
187 
188     test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7 ), n, 1+2+3+4+5+6+7 );
189     n += 3*(1+2+3+4+5+6+7);
190 
191     test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ), n, 1+2+3+4+5+6+7+8 );
192     n += 3*(1+2+3+4+5+6+7+8);
193 
194     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 );
195     n += 3*(1+2+3+4+5+6+7+8+9);
196 
197     BOOST_TEST( x.state() == n );
198 }
199 
stateful_function_test()200 void stateful_function_test()
201 {
202     test( boost::bind( f0, 0 ), 0, 17041 );
203     test( boost::bind( f1, 0, 1 ), 0, 1 );
204     test( boost::bind( f2, 0, 1, 2 ), 0, 1+2 );
205     test( boost::bind( f3, 0, 1, 2, 3 ), 0, 1+2+3 );
206     test( boost::bind( f4, 0, 1, 2, 3, 4 ), 0, 1+2+3+4 );
207     test( boost::bind( f5, 0, 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
208     test( boost::bind( f6, 0, 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
209     test( boost::bind( f7, 0, 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
210     test( boost::bind( f8, 0, 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
211 }
212 
main()213 int main()
214 {
215     stateful_function_object_test();
216     stateful_function_test();
217     return boost::report_errors();
218 }
219