1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // Copyright (C) 2014 Vicente J. Botet Escriba
11 //
12 // Distributed under the Boost Software License, Version 1.0. (See accompanying
13 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14
15 // <boost/thread/detail/invoker.hpp>
16
17 #include <boost/thread/detail/invoker.hpp>
18 #include <boost/detail/lightweight_test.hpp>
19
20 int count = 0;
21
22 // 1 arg, return void
23
f_void_1(int i)24 void f_void_1(int i)
25 {
26 count += i;
27 }
28
29 struct A_void_1
30 {
31 typedef void result_type;
operator ()A_void_132 void operator()(int i)
33 {
34 count += i;
35 }
36
mem1A_void_137 void mem1() {
38 std::cout << "mem1 " << count << std::endl;
39 ++count;
40 std::cout << "mem1 " << count << std::endl;
41 }
mem2A_void_142 void mem2() const {count += 2;}
43 };
44
45 void
test_void_1()46 test_void_1()
47 {
48 int save_count = count;
49 // function
50 #if defined BOOST_THREAD_PROVIDES_INVOKE
51 {
52 int i = 2;
53 boost::detail::invoker<void(*)(int), int>(f_void_1, i)();
54 BOOST_TEST(count == save_count + 2);
55 save_count = count;
56 }
57 #endif
58 // {
59 // int i = 2;
60 // boost::detail::invoke<void>(f_void_1, i);
61 // BOOST_TEST(count == save_count + 2);
62 // save_count = count;
63 // }
64 // function pointer
65 #if defined BOOST_THREAD_PROVIDES_INVOKE
66 {
67 void (*fp)(int) = f_void_1;
68 int i = 3;
69 boost::detail::invoker<void(*)(int), int>(fp, i)();
70 BOOST_TEST(count == save_count+3);
71 save_count = count;
72 }
73 #endif
74 // {
75 // void (*fp)(int) = f_void_1;
76 // int i = 3;
77 // boost::detail::invoke<void>(fp, i);
78 // BOOST_TEST(count == save_count+3);
79 // save_count = count;
80 // }
81 #if defined BOOST_THREAD_PROVIDES_INVOKE
82 {
83 void (*fp)(int) = f_void_1;
84 int i = 3;
85 boost::detail::invoker<void(*)(int), int>(fp, i)();
86 BOOST_TEST(count == save_count+3);
87 save_count = count;
88 }
89 #endif
90 // {
91 // void (*fp)(int) = f_void_1;
92 // int i = 3;
93 // boost::detail::invoke<void>(fp, i);
94 // BOOST_TEST(count == save_count+3);
95 // save_count = count;
96 // }
97 // functor
98 #if defined BOOST_THREAD_PROVIDES_INVOKE
99 {
100 A_void_1 a0;
101 int i = 4;
102 boost::detail::invoker<A_void_1, int>(a0, i)();
103 BOOST_TEST(count == save_count+4);
104 save_count = count;
105 }
106 #endif
107 // {
108 // A_void_1 a0;
109 // int i = 4;
110 // boost::detail::invoke<void>(a0, i);
111 // BOOST_TEST(count == save_count+4);
112 // save_count = count;
113 // }
114 // member function pointer
115 #if defined BOOST_THREAD_PROVIDES_INVOKE
116 {
117 void (A_void_1::*fp)() = &A_void_1::mem1;
118 A_void_1 a;
119 //BUG
120 boost::detail::invoker<void (A_void_1::*)(), A_void_1>(fp, a)();
121 BOOST_TEST_EQ(count, save_count+1);
122 save_count = count;
123 A_void_1* ap = &a;
124 boost::detail::invoker<void (A_void_1::*)(), A_void_1*>(fp, ap)();
125 BOOST_TEST_EQ(count, save_count+1);
126 save_count = count;
127 }
128 #endif
129 // {
130 // void (A_void_1::*fp)() = &A_void_1::mem1;
131 // A_void_1 a;
132 // boost::detail::invoke<void>(fp, a);
133 // BOOST_TEST(count == save_count+1);
134 // save_count = count;
135 // A_void_1* ap = &a;
136 // boost::detail::invoke<void>(fp, ap);
137 // BOOST_TEST(count == save_count+1);
138 // save_count = count;
139 // }
140 // const member function pointer
141 #if defined BOOST_THREAD_PROVIDES_INVOKE
142 {
143 void (A_void_1::*fp)() const = &A_void_1::mem2;
144 A_void_1 a;
145 boost::detail::invoker<void (A_void_1::*)() const, A_void_1>(fp, a)();
146 BOOST_TEST(count == save_count+2);
147 save_count = count;
148 A_void_1* ap = &a;
149 boost::detail::invoker<void (A_void_1::*)() const, A_void_1*>(fp, ap)();
150 BOOST_TEST_EQ(count, save_count+2);
151 save_count = count;
152 }
153 #endif
154 // {
155 // void (A_void_1::*fp)() const = &A_void_1::mem2;
156 // A_void_1 a;
157 // boost::detail::invoke<void>(fp, a);
158 // BOOST_TEST(count == save_count+2);
159 // save_count = count;
160 // A_void_1* ap = &a;
161 // boost::detail::invoke<void>(fp, ap);
162 // BOOST_TEST(count == save_count+2);
163 // save_count = count;
164 // }
165 }
166
167 // 1 arg, return int
168
f_int_1(int i)169 int f_int_1(int i)
170 {
171 return i + 1;
172 }
173
174 struct A_int_1
175 {
A_int_1A_int_1176 A_int_1() : data_(5) {}
operator ()A_int_1177 int operator()(int i)
178 {
179 return i - 1;
180 }
181
mem1A_int_1182 int mem1() {return 3;}
mem2A_int_1183 int mem2() const {return 4;}
184 int data_;
185 };
186
187 void
test_int_1()188 test_int_1()
189 {
190 // function
191 {
192 int i = 2;
193 #if defined BOOST_THREAD_PROVIDES_INVOKE
194 BOOST_TEST((boost::detail::invoker<int(*)(int), int>(f_int_1, i)() == 3));
195 #endif
196 // BOOST_TEST(boost::detail::invoke<int>(f_int_1, i) == 3);
197 }
198 // function pointer
199 {
200 int (*fp)(int) = f_int_1;
201 int i = 3;
202 #if defined BOOST_THREAD_PROVIDES_INVOKE
203 BOOST_TEST((boost::detail::invoker<int (*)(int), int>(fp, i)() == 4));
204 #endif
205 // BOOST_TEST(boost::detail::invoke<int>(fp, i) == 4);
206 }
207 // functor
208 {
209 int i = 4;
210 #if defined BOOST_THREAD_PROVIDES_INVOKE
211 BOOST_TEST((boost::detail::invoker<A_int_1, int>(A_int_1(), i)() == 3));
212 #endif
213 // const A_int_1 ca;
214 // A_int_1 a;
215 // BOOST_TEST(boost::detail::invoke<int>(a, i) == 3);
216 // //BOOST_TEST(boost::detail::invoke<int>(ca, i) == 3);
217 //#if defined BOOST_THREAD_PROVIDES_INVOKE
218 // BOOST_TEST(boost::detail::invoke<int>(A_int_1(), i) == 3);
219 //#endif
220 }
221 // member function pointer
222 {
223 A_int_1 a;
224 #if defined BOOST_THREAD_PROVIDES_INVOKE
225 BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1>(&A_int_1::mem1, a)() == 3));
226 #endif
227 // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, a) == 3);
228 A_int_1* ap = &a;
229 #if defined BOOST_THREAD_PROVIDES_INVOKE
230 BOOST_TEST((boost::detail::invoker<int (A_int_1::*)(), A_int_1*>(&A_int_1::mem1, ap)() == 3));
231 #endif
232 // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem1, ap) == 3);
233 }
234 // const member function pointer
235 {
236 A_int_1 a;
237 #if defined BOOST_THREAD_PROVIDES_INVOKE
238 BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1>(&A_int_1::mem2, A_int_1())() == 4));
239 #endif
240 // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem2, A_int_1()) == 4);
241 A_int_1* ap = &a;
242 #if defined BOOST_THREAD_PROVIDES_INVOKE
243 BOOST_TEST((boost::detail::invoker<int (A_int_1::*)() const, A_int_1*>(&A_int_1::mem2, ap)() == 4));
244 #endif
245 // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::mem2, ap) == 4);
246 }
247 // member data pointer
248 {
249 A_int_1 a;
250 #if defined BOOST_THREAD_PROVIDES_INVOKE
251 // BUG
252 //BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) == 5);
253
254 // BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, a) == 5);
255
256 #endif
257 //#if defined BOOST_THREAD_PROVIDES_INVOKE
258 // A_int_1* ap = &a;
259 //
260 // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) = 6;
261 // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, a) == 6);
262 //
263 //// boost::detail::invoke<int>(&A_int_1::data_, a) = 6;
264 //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, a) == 6);
265 //
266 //#endif
267 //
268 //#if defined BOOST_THREAD_PROVIDES_INVOKE
269 // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) == 6);
270 // boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) = 7;
271 // BOOST_TEST(boost::detail::invoker<int A_int_1::*>(&A_int_1::data_, ap) == 7);
272 //
273 //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, ap) == 7);
274 //// boost::detail::invoke<int>(&A_int_1::data_, ap) = 8;
275 //// BOOST_TEST(boost::detail::invoke<int>(&A_int_1::data_, ap) == 8);
276 //#endif
277
278 }
279 }
280
281 // 2 arg, return void
282
f_void_2(int i,int j)283 void f_void_2(int i, int j)
284 {
285 count += i+j;
286 }
287
288 struct A_void_2
289 {
operator ()A_void_2290 void operator()(int i, int j)
291 {
292 count += i+j;
293 }
294
mem1A_void_2295 void mem1(int i) {count += i;}
mem2A_void_2296 void mem2(int i) const {count += i;}
297 };
298
299 void
test_void_2()300 test_void_2()
301 {
302 int save_count = count;
303 // function
304 {
305 int i = 2;
306 int j = 3;
307 #if defined BOOST_THREAD_PROVIDES_INVOKE
308 boost::detail::invoke(f_void_2, i, j);
309 BOOST_TEST(count == save_count+5);
310 save_count = count;
311 #endif
312 // boost::detail::invoke<void>(f_void_2, i, j);
313 // BOOST_TEST(count == save_count+5);
314 // save_count = count;
315 }
316 // member function pointer
317 {
318 #if defined BOOST_THREAD_PROVIDES_INVOKE
319 int j = 3;
320 boost::detail::invoke(&A_void_2::mem1, A_void_2(), j);
321 BOOST_TEST(count == save_count+3);
322 save_count = count;
323
324 // boost::detail::invoke<void>(&A_void_2::mem1, A_void_2(), j);
325 // BOOST_TEST(count == save_count+3);
326 // save_count = count;
327 #endif
328 // A_void_2 a2;
329 // boost::detail::invoke<void>(&A_void_2::mem1, a2, j);
330 // BOOST_TEST(count == save_count+3);
331 // save_count = count;
332 }
333 }
334
main()335 int main()
336 {
337 test_void_1();
338 test_int_1();
339 test_void_2();
340 return boost::report_errors();
341
342 }
343