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
25class X
26{
27private:
28
29 int state_;
30
31public:
32
33 X(): state_(0)
34 {
35 }
36
37 // SGI-related compilers have odd compiler-synthesized ctors and dtors
38 #ifdef __PATHSCALE__
39 ~X() {}
40 #endif
41
42 int state() const
43 {
44 return state_;
45 }
46
47 int operator()()
48 {
49 return state_ += 17041;
50 }
51
52 int operator()(int x1)
53 {
54 return state_ += x1;
55 }
56
57 int operator()(int x1, int x2)
58 {
59 return state_ += x1+x2;
60 }
61
62 int operator()(int x1, int x2, int x3)
63 {
64 return state_ += x1+x2+x3;
65 }
66
67 int operator()(int x1, int x2, int x3, int x4)
68 {
69 return state_ += x1+x2+x3+x4;
70 }
71
72 int operator()(int x1, int x2, int x3, int x4, int x5)
73 {
74 return state_ += x1+x2+x3+x4+x5;
75 }
76
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
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
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
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
98int f0(int & state_)
99{
100 return state_ += 17041;
101}
102
103int f1(int & state_, int x1)
104{
105 return state_ += x1;
106}
107
108int f2(int & state_, int x1, int x2)
109{
110 return state_ += x1+x2;
111}
112
113int f3(int & state_, int x1, int x2, int x3)
114{
115 return state_ += x1+x2+x3;
116}
117
118int f4(int & state_, int x1, int x2, int x3, int x4)
119{
120 return state_ += x1+x2+x3+x4;
121}
122
123int f5(int & state_, int x1, int x2, int x3, int x4, int x5)
124{
125 return state_ += x1+x2+x3+x4+x5;
126}
127
128int 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
133int 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
138int 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
143template<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
150void stateful_function_object_test()
151{
152 test( f: boost::bind<int>( f: X() ), a: 0, b: 17041 );
153 test( f: boost::bind<int>( f: X(), a1: 1 ), a: 0, b: 1 );
154 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2 ), a: 0, b: 1+2 );
155 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3 ), a: 0, b: 1+2+3 );
156 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4 ), a: 0, b: 1+2+3+4 );
157 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5 ), a: 0, b: 1+2+3+4+5 );
158 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6 ), a: 0, b: 1+2+3+4+5+6 );
159 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7 ), a: 0, b: 1+2+3+4+5+6+7 );
160 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7, a8: 8 ), a: 0, b: 1+2+3+4+5+6+7+8 );
161 test( f: boost::bind<int>( f: X(), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7, a8: 8, a9: 9 ), a: 0, b: 1+2+3+4+5+6+7+8+9 );
162
163 X x;
164
165 int n = x.state();
166
167 test( f: boost::bind<int>( f: boost::ref(t&: x) ), a: n, b: 17041 );
168 n += 3*17041;
169
170 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1 ), a: n, b: 1 );
171 n += 3*1;
172
173 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2 ), a: n, b: 1+2 );
174 n += 3*(1+2);
175
176 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3 ), a: n, b: 1+2+3 );
177 n += 3*(1+2+3);
178
179 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4 ), a: n, b: 1+2+3+4 );
180 n += 3*(1+2+3+4);
181
182 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5 ), a: n, b: 1+2+3+4+5 );
183 n += 3*(1+2+3+4+5);
184
185 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6 ), a: n, b: 1+2+3+4+5+6 );
186 n += 3*(1+2+3+4+5+6);
187
188 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7 ), a: n, b: 1+2+3+4+5+6+7 );
189 n += 3*(1+2+3+4+5+6+7);
190
191 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7, a8: 8 ), a: n, b: 1+2+3+4+5+6+7+8 );
192 n += 3*(1+2+3+4+5+6+7+8);
193
194 test( f: boost::bind<int>( f: boost::ref(t&: x), a1: 1, a2: 2, a3: 3, a4: 4, a5: 5, a6: 6, a7: 7, a8: 8, a9: 9 ), a: n, b: 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
200void stateful_function_test()
201{
202 test( f: boost::bind( f: f0, a1: 0 ), a: 0, b: 17041 );
203 test( f: boost::bind( f: f1, a1: 0, a2: 1 ), a: 0, b: 1 );
204 test( f: boost::bind( f: f2, a1: 0, a2: 1, a3: 2 ), a: 0, b: 1+2 );
205 test( f: boost::bind( f: f3, a1: 0, a2: 1, a3: 2, a4: 3 ), a: 0, b: 1+2+3 );
206 test( f: boost::bind( f: f4, a1: 0, a2: 1, a3: 2, a4: 3, a5: 4 ), a: 0, b: 1+2+3+4 );
207 test( f: boost::bind( f: f5, a1: 0, a2: 1, a3: 2, a4: 3, a5: 4, a6: 5 ), a: 0, b: 1+2+3+4+5 );
208 test( f: boost::bind( f: f6, a1: 0, a2: 1, a3: 2, a4: 3, a5: 4, a6: 5, a7: 6 ), a: 0, b: 1+2+3+4+5+6 );
209 test( f: boost::bind( f: f7, a1: 0, a2: 1, a3: 2, a4: 3, a5: 4, a6: 5, a7: 6, a8: 7 ), a: 0, b: 1+2+3+4+5+6+7 );
210 test( f: boost::bind( f: f8, a1: 0, a2: 1, a3: 2, a4: 3, a5: 4, a6: 5, a7: 6, a8: 7, a9: 8 ), a: 0, b: 1+2+3+4+5+6+7+8 );
211}
212
213int main()
214{
215 stateful_function_object_test();
216 stateful_function_test();
217 return boost::report_errors();
218}
219

source code of boost/libs/bind/test/bind_stateful_test.cpp