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_void_test.cpp - test for bind<void> |
12 | // |
13 | // Copyright (c) 2008 Peter Dimov |
14 | // Copyright (c) 2014 Agustin Berge |
15 | // |
16 | // Distributed under the Boost Software License, Version 1.0. (See |
17 | // accompanying file LICENSE_1_0.txt or copy at |
18 | // http://www.boost.org/LICENSE_1_0.txt) |
19 | // |
20 | |
21 | #include <boost/bind/bind.hpp> |
22 | #include <boost/ref.hpp> |
23 | #include <boost/core/lightweight_test.hpp> |
24 | |
25 | using namespace boost::placeholders; |
26 | |
27 | // |
28 | |
29 | long global_result; |
30 | |
31 | long f_0() |
32 | { |
33 | return global_result = 17041L; |
34 | } |
35 | |
36 | long f_1(long a) |
37 | { |
38 | return global_result = a; |
39 | } |
40 | |
41 | long f_2(long a, long b) |
42 | { |
43 | return global_result = a + 10 * b; |
44 | } |
45 | |
46 | long f_3(long a, long b, long c) |
47 | { |
48 | return global_result = a + 10 * b + 100 * c; |
49 | } |
50 | |
51 | long f_4(long a, long b, long c, long d) |
52 | { |
53 | return global_result = a + 10 * b + 100 * c + 1000 * d; |
54 | } |
55 | |
56 | long f_5(long a, long b, long c, long d, long e) |
57 | { |
58 | return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e; |
59 | } |
60 | |
61 | long f_6(long a, long b, long c, long d, long e, long f) |
62 | { |
63 | return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f; |
64 | } |
65 | |
66 | long f_7(long a, long b, long c, long d, long e, long f, long g) |
67 | { |
68 | return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g; |
69 | } |
70 | |
71 | long f_8(long a, long b, long c, long d, long e, long f, long g, long h) |
72 | { |
73 | return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h; |
74 | } |
75 | |
76 | long f_9(long a, long b, long c, long d, long e, long f, long g, long h, long i) |
77 | { |
78 | return global_result = a + 10 * b + 100 * c + 1000 * d + 10000 * e + 100000 * f + 1000000 * g + 10000000 * h + 100000000 * i; |
79 | } |
80 | |
81 | void function_test() |
82 | { |
83 | using namespace boost; |
84 | |
85 | int const i = 1; |
86 | |
87 | BOOST_TEST( (bind<void>(f_0)(i), (global_result == 17041L)) ); |
88 | BOOST_TEST( (bind<void>(f_1, _1)(i), (global_result == 1L)) ); |
89 | BOOST_TEST( (bind<void>(f_2, _1, 2)(i), (global_result == 21L)) ); |
90 | BOOST_TEST( (bind<void>(f_3, _1, 2, 3)(i), (global_result == 321L)) ); |
91 | BOOST_TEST( (bind<void>(f_4, _1, 2, 3, 4)(i), (global_result == 4321L)) ); |
92 | BOOST_TEST( (bind<void>(f_5, _1, 2, 3, 4, 5)(i), (global_result == 54321L)) ); |
93 | BOOST_TEST( (bind<void>(f_6, _1, 2, 3, 4, 5, 6)(i), (global_result == 654321L)) ); |
94 | BOOST_TEST( (bind<void>(f_7, _1, 2, 3, 4, 5, 6, 7)(i), (global_result == 7654321L)) ); |
95 | BOOST_TEST( (bind<void>(f_8, _1, 2, 3, 4, 5, 6, 7, 8)(i), (global_result == 87654321L)) ); |
96 | BOOST_TEST( (bind<void>(f_9, _1, 2, 3, 4, 5, 6, 7, 8, 9)(i), (global_result == 987654321L)) ); |
97 | } |
98 | |
99 | // |
100 | |
101 | struct Y |
102 | { |
103 | short operator()(short & r) const { return static_cast<short>( global_result = ++r ); } |
104 | int operator()(int a, int b) const { return global_result = a + 10 * b; } |
105 | long operator() (long a, long b, long c) const { return global_result = a + 10 * b + 100 * c; } |
106 | void operator() (long a, long b, long c, long d) const { global_result = a + 10 * b + 100 * c + 1000 * d; } |
107 | }; |
108 | |
109 | void function_object_test() |
110 | { |
111 | using namespace boost; |
112 | |
113 | short i(6); |
114 | |
115 | int const k = 3; |
116 | |
117 | BOOST_TEST( (bind<void>(Y(), ref(i))(), (global_result == 7)) ); |
118 | BOOST_TEST( (bind<void>(Y(), ref(i))(), (global_result == 8)) ); |
119 | BOOST_TEST( (bind<void>(Y(), i, _1)(k), (global_result == 38)) ); |
120 | BOOST_TEST( (bind<void>(Y(), i, _1, 9)(k), (global_result == 938)) ); |
121 | BOOST_TEST( (bind<void>(Y(), i, _1, 9, 4)(k), (global_result == 4938)) ); |
122 | } |
123 | |
124 | int main() |
125 | { |
126 | function_test(); |
127 | function_object_test(); |
128 | |
129 | return boost::report_errors(); |
130 | } |
131 | |