1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <functional>
10 
11 // class function<R(ArgTypes...)>
12 
13 // R operator()(ArgTypes... args) const
14 
15 #include <functional>
16 #include <cassert>
17 
18 
19 int count = 0;
20 
21 
22 // 0 args, return int
23 
24 int f_int_0()
25 {
26     return 3;
27 }
28 
29 struct A_int_0
30 {
31     int operator()() {return 4;}
32 };
33 
34 void test_int_0()
35 {
36     // function
37     {
38         std::function<int ()> r1(f_int_0);
39         assert(r1() == 3);
40     }
41     // function pointer
42     {
43         int (*fp)() = f_int_0;
44         std::function<int ()> r1(fp);
45         assert(r1() == 3);
46     }
47     // functor
48     {
49         A_int_0 a0;
50         std::function<int ()> r1(a0);
51         assert(r1() == 4);
52     }
53 }
54 
55 
56 // 0 args, return void
57 
58 void f_void_0()
59 {
60     ++count;
61 }
62 
63 struct A_void_0
64 {
65     void operator()() {++count;}
66 };
67 
68 void
69 test_void_0()
70 {
71     int save_count = count;
72     // function
73     {
74         std::function<void ()> r1(f_void_0);
75         r1();
76         assert(count == save_count+1);
77         save_count = count;
78     }
79     // function pointer
80     {
81         void (*fp)() = f_void_0;
82         std::function<void ()> r1(fp);
83         r1();
84         assert(count == save_count+1);
85         save_count = count;
86     }
87     // functor
88     {
89         A_void_0 a0;
90         std::function<void ()> r1(a0);
91         r1();
92         assert(count == save_count+1);
93         save_count = count;
94     }
95 }
96 
97 // 1 arg, return void
98 
99 void f_void_1(int i)
100 {
101     count += i;
102 }
103 
104 struct A_void_1
105 {
106     void operator()(int i)
107     {
108         count += i;
109     }
110 
111     void mem1() {++count;}
112     void mem2() const {++count;}
113 };
114 
115 void
116 test_void_1()
117 {
118     int save_count = count;
119     // function
120     {
121         std::function<void (int)> r1(f_void_1);
122         int i = 2;
123         r1(i);
124         assert(count == save_count+2);
125         save_count = count;
126     }
127     // function pointer
128     {
129         void (*fp)(int) = f_void_1;
130         std::function<void (int)> r1(fp);
131         int i = 3;
132         r1(i);
133         assert(count == save_count+3);
134         save_count = count;
135     }
136     // functor
137     {
138         A_void_1 a0;
139         std::function<void (int)> r1(a0);
140         int i = 4;
141         r1(i);
142         assert(count == save_count+4);
143         save_count = count;
144     }
145     // member function pointer
146     {
147         void (A_void_1::*fp)() = &A_void_1::mem1;
148         std::function<void (A_void_1)> r1(fp);
149         A_void_1 a;
150         r1(a);
151         assert(count == save_count+1);
152         save_count = count;
153         A_void_1* ap = &a;
154         std::function<void (A_void_1*)> r2 = fp;
155         r2(ap);
156         assert(count == save_count+1);
157         save_count = count;
158     }
159     // const member function pointer
160     {
161         void (A_void_1::*fp)() const = &A_void_1::mem2;
162         std::function<void (A_void_1)> r1(fp);
163         A_void_1 a;
164         r1(a);
165         assert(count == save_count+1);
166         save_count = count;
167         std::function<void (A_void_1*)> r2(fp);
168         A_void_1* ap = &a;
169         r2(ap);
170         assert(count == save_count+1);
171         save_count = count;
172     }
173 }
174 
175 // 1 arg, return int
176 
177 int f_int_1(int i)
178 {
179     return i + 1;
180 }
181 
182 struct A_int_1
183 {
184     A_int_1() : data_(5) {}
185     int operator()(int i)
186     {
187         return i - 1;
188     }
189 
190     int mem1() {return 3;}
191     int mem2() const {return 4;}
192     int data_;
193 };
194 
195 void
196 test_int_1()
197 {
198     // function
199     {
200         std::function<int (int)> r1(f_int_1);
201         int i = 2;
202         assert(r1(i) == 3);
203     }
204     // function pointer
205     {
206         int (*fp)(int) = f_int_1;
207         std::function<int (int)> r1(fp);
208         int i = 3;
209         assert(r1(i) == 4);
210     }
211     // functor
212     {
213         A_int_1 a0;
214         std::function<int (int)> r1(a0);
215         int i = 4;
216         assert(r1(i) == 3);
217     }
218     // member function pointer
219     {
220         int (A_int_1::*fp)() = &A_int_1::mem1;
221         std::function<int (A_int_1)> r1(fp);
222         A_int_1 a;
223         assert(r1(a) == 3);
224         std::function<int (A_int_1*)> r2(fp);
225         A_int_1* ap = &a;
226         assert(r2(ap) == 3);
227     }
228     // const member function pointer
229     {
230         int (A_int_1::*fp)() const = &A_int_1::mem2;
231         std::function<int (A_int_1)> r1(fp);
232         A_int_1 a;
233         assert(r1(a) == 4);
234         std::function<int (A_int_1*)> r2(fp);
235         A_int_1* ap = &a;
236         assert(r2(ap) == 4);
237     }
238     // member data pointer
239     {
240         int A_int_1::*fp = &A_int_1::data_;
241         std::function<int& (A_int_1&)> r1(fp);
242         A_int_1 a;
243         assert(r1(a) == 5);
244         r1(a) = 6;
245         assert(r1(a) == 6);
246         std::function<int& (A_int_1*)> r2(fp);
247         A_int_1* ap = &a;
248         assert(r2(ap) == 6);
249         r2(ap) = 7;
250         assert(r2(ap) == 7);
251     }
252 }
253 
254 // 2 arg, return void
255 
256 void f_void_2(int i, int j)
257 {
258     count += i+j;
259 }
260 
261 struct A_void_2
262 {
263     void operator()(int i, int j)
264     {
265         count += i+j;
266     }
267 
268     void mem1(int i) {count += i;}
269     void mem2(int i) const {count += i;}
270 };
271 
272 void
273 test_void_2()
274 {
275     int save_count = count;
276     // function
277     {
278         std::function<void (int, int)> r1(f_void_2);
279         int i = 2;
280         int j = 3;
281         r1(i, j);
282         assert(count == save_count+5);
283         save_count = count;
284     }
285     // function pointer
286     {
287         void (*fp)(int, int) = f_void_2;
288         std::function<void (int, int)> r1(fp);
289         int i = 3;
290         int j = 4;
291         r1(i, j);
292         assert(count == save_count+7);
293         save_count = count;
294     }
295     // functor
296     {
297         A_void_2 a0;
298         std::function<void (int, int)> r1(a0);
299         int i = 4;
300         int j = 5;
301         r1(i, j);
302         assert(count == save_count+9);
303         save_count = count;
304     }
305     // member function pointer
306     {
307         void (A_void_2::*fp)(int) = &A_void_2::mem1;
308         std::function<void (A_void_2, int)> r1(fp);
309         A_void_2 a;
310         int i = 3;
311         r1(a, i);
312         assert(count == save_count+3);
313         save_count = count;
314         std::function<void (A_void_2*, int)> r2(fp);
315         A_void_2* ap = &a;
316         r2(ap, i);
317         assert(count == save_count+3);
318         save_count = count;
319     }
320     // const member function pointer
321     {
322         void (A_void_2::*fp)(int) const = &A_void_2::mem2;
323         std::function<void (A_void_2, int)> r1(fp);
324         A_void_2 a;
325         int i = 4;
326         r1(a, i);
327         assert(count == save_count+4);
328         save_count = count;
329         std::function<void (A_void_2*, int)> r2(fp);
330         A_void_2* ap = &a;
331         r2(ap, i);
332         assert(count == save_count+4);
333         save_count = count;
334     }
335 }
336 
337 // 2 arg, return int
338 
339 int f_int_2(int i, int j)
340 {
341     return i+j;
342 }
343 
344 struct A_int_2
345 {
346     int operator()(int i, int j)
347     {
348         return i+j;
349     }
350 
351     int mem1(int i) {return i+1;}
352     int mem2(int i) const {return i+2;}
353 };
354 
355 void test_int_2()
356 {
357     // function
358     {
359         std::function<int (int, int)> r1(f_int_2);
360         int i = 2;
361         int j = 3;
362         assert(r1(i, j) == i+j);
363     }
364     // function pointer
365     {
366         int (*fp)(int, int) = f_int_2;
367         std::function<int (int, int)> r1(fp);
368         int i = 3;
369         int j = 4;
370         assert(r1(i, j) == i+j);
371     }
372     // functor
373     {
374         A_int_2 a0;
375         std::function<int (int, int)> r1(a0);
376         int i = 4;
377         int j = 5;
378         assert(r1(i, j) == i+j);
379     }
380     // member function pointer
381     {
382         int(A_int_2::*fp)(int) = &A_int_2::mem1;
383         std::function<int (A_int_2, int)> r1(fp);
384         A_int_2 a;
385         int i = 3;
386         assert(r1(a, i) == i+1);
387         std::function<int (A_int_2*, int)> r2(fp);
388         A_int_2* ap = &a;
389         assert(r2(ap, i) == i+1);
390     }
391     // const member function pointer
392     {
393         int (A_int_2::*fp)(int) const = &A_int_2::mem2;
394         std::function<int (A_int_2, int)> r1(fp);
395         A_int_2 a;
396         int i = 4;
397         assert(r1(a, i) == i+2);
398         std::function<int (A_int_2*, int)> r2(fp);
399         A_int_2* ap = &a;
400         assert(r2(ap, i) == i+2);
401     }
402 }
403 
404 int main(int, char**)
405 {
406     test_void_0();
407     test_int_0();
408     test_void_1();
409     test_int_1();
410     test_void_2();
411     test_int_2();
412 
413   return 0;
414 }
415