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