File: bind_stateful_test.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (222 lines) | stat: -rw-r--r-- 5,440 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <boost/config.hpp>

#if defined(BOOST_MSVC)
#pragma warning(disable: 4786)  // identifier truncated in debug info
#pragma warning(disable: 4710)  // function not inlined
#pragma warning(disable: 4711)  // function selected for automatic inline expansion
#pragma warning(disable: 4514)  // unreferenced inline removed
#endif

//
//  bind_stateful_test.cpp
//
//  Copyright (c) 2004 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//

#include <boost/bind.hpp>

#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(push, 3)
#endif

#include <iostream>

#if defined(BOOST_MSVC) && (BOOST_MSVC < 1300)
#pragma warning(pop)
#endif

#include <boost/detail/lightweight_test.hpp>

class X
{
private:

    int state_;

public:

    X(): state_(0)
    {
    }

    int state() const
    {
        return state_;
    }

    int operator()()
    {
        return state_ += 17041;
    }

    int operator()(int x1)
    {
        return state_ += x1;
    }

    int operator()(int x1, int x2)
    {
        return state_ += x1+x2;
    }

    int operator()(int x1, int x2, int x3)
    {
        return state_ += x1+x2+x3;
    }

    int operator()(int x1, int x2, int x3, int x4)
    {
        return state_ += x1+x2+x3+x4;
    }

    int operator()(int x1, int x2, int x3, int x4, int x5)
    {
        return state_ += x1+x2+x3+x4+x5;
    }

    int operator()(int x1, int x2, int x3, int x4, int x5, int x6)
    {
        return state_ += x1+x2+x3+x4+x5+x6;
    }

    int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7)
    {
        return state_ += x1+x2+x3+x4+x5+x6+x7;
    }

    int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
    {
        return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
    }

    int operator()(int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8, int x9)
    {
        return state_ += x1+x2+x3+x4+x5+x6+x7+x8+x9;
    }
};

int f0(int & state_)
{
    return state_ += 17041;
}

int f1(int & state_, int x1)
{
    return state_ += x1;
}

int f2(int & state_, int x1, int x2)
{
    return state_ += x1+x2;
}

int f3(int & state_, int x1, int x2, int x3)
{
    return state_ += x1+x2+x3;
}

int f4(int & state_, int x1, int x2, int x3, int x4)
{
    return state_ += x1+x2+x3+x4;
}

int f5(int & state_, int x1, int x2, int x3, int x4, int x5)
{
    return state_ += x1+x2+x3+x4+x5;
}

int f6(int & state_, int x1, int x2, int x3, int x4, int x5, int x6)
{
    return state_ += x1+x2+x3+x4+x5+x6;
}

int f7(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7)
{
    return state_ += x1+x2+x3+x4+x5+x6+x7;
}

int f8(int & state_, int x1, int x2, int x3, int x4, int x5, int x6, int x7, int x8)
{
    return state_ += x1+x2+x3+x4+x5+x6+x7+x8;
}

template<class F> void test(F f, int a, int b)
{
    BOOST_TEST( f() == a +   b );
    BOOST_TEST( f() == a + 2*b );
    BOOST_TEST( f() == a + 3*b );
}

void stateful_function_object_test()
{
    test( boost::bind<int>( X() ), 0, 17041 );
    test( boost::bind<int>( X(), 1 ), 0, 1 );
    test( boost::bind<int>( X(), 1, 2 ), 0, 1+2 );
    test( boost::bind<int>( X(), 1, 2, 3 ), 0, 1+2+3 );
    test( boost::bind<int>( X(), 1, 2, 3, 4 ), 0, 1+2+3+4 );
    test( boost::bind<int>( X(), 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
    test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
    test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
    test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
    test( boost::bind<int>( X(), 1, 2, 3, 4, 5, 6, 7, 8, 9 ), 0, 1+2+3+4+5+6+7+8+9 );

    X x;

    int n = x.state();

    test( boost::bind<int>( boost::ref(x) ), n, 17041 );
    n += 3*17041;

    test( boost::bind<int>( boost::ref(x), 1 ), n, 1 );
    n += 3*1;

    test( boost::bind<int>( boost::ref(x), 1, 2 ), n, 1+2 );
    n += 3*(1+2);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3 ), n, 1+2+3 );
    n += 3*(1+2+3);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4 ), n, 1+2+3+4 );
    n += 3*(1+2+3+4);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5 ), n, 1+2+3+4+5 );
    n += 3*(1+2+3+4+5);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6 ), n, 1+2+3+4+5+6 );
    n += 3*(1+2+3+4+5+6);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7 ), n, 1+2+3+4+5+6+7 );
    n += 3*(1+2+3+4+5+6+7);

    test( boost::bind<int>( boost::ref(x), 1, 2, 3, 4, 5, 6, 7, 8 ), n, 1+2+3+4+5+6+7+8 );
    n += 3*(1+2+3+4+5+6+7+8);

    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 );
    n += 3*(1+2+3+4+5+6+7+8+9);

    BOOST_TEST( x.state() == n );
}

void stateful_function_test()
{
    test( boost::bind( f0, 0 ), 0, 17041 );
    test( boost::bind( f1, 0, 1 ), 0, 1 );
    test( boost::bind( f2, 0, 1, 2 ), 0, 1+2 );
    test( boost::bind( f3, 0, 1, 2, 3 ), 0, 1+2+3 );
    test( boost::bind( f4, 0, 1, 2, 3, 4 ), 0, 1+2+3+4 );
    test( boost::bind( f5, 0, 1, 2, 3, 4, 5 ), 0, 1+2+3+4+5 );
    test( boost::bind( f6, 0, 1, 2, 3, 4, 5, 6 ), 0, 1+2+3+4+5+6 );
    test( boost::bind( f7, 0, 1, 2, 3, 4, 5, 6, 7 ), 0, 1+2+3+4+5+6+7 );
    test( boost::bind( f8, 0, 1, 2, 3, 4, 5, 6, 7, 8 ), 0, 1+2+3+4+5+6+7+8 );
}

int main()
{
    stateful_function_object_test();
    stateful_function_test();
    return boost::report_errors();
}