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