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 // reference_wrapper
12
13 // template <class... ArgTypes>
14 // requires Callable<T, ArgTypes&&...>
15 // Callable<T, ArgTypes&&...>::result_type
16 // operator()(ArgTypes&&... args) const;
17
18 #include <functional>
19 #include <cassert>
20
21 #include "test_macros.h"
22
23 int count = 0;
24
25 // 1 arg, return void
26
f_void_1(int i)27 void f_void_1(int i)
28 {
29 count += i;
30 }
31
32 struct A_void_1
33 {
operator ()A_void_134 void operator()(int i)
35 {
36 count += i;
37 }
38
mem1A_void_139 void mem1() {++count;}
mem2A_void_140 void mem2() const {++count;}
41 };
42
43 void
test_void_1()44 test_void_1()
45 {
46 int save_count = count;
47 // function
48 {
49 std::reference_wrapper<void (int)> r1(f_void_1);
50 int i = 2;
51 r1(i);
52 assert(count == save_count+2);
53 save_count = count;
54 }
55 // function pointer
56 {
57 void (*fp)(int) = f_void_1;
58 std::reference_wrapper<void (*)(int)> r1(fp);
59 int i = 3;
60 r1(i);
61 assert(count == save_count+3);
62 save_count = count;
63 }
64 // functor
65 {
66 A_void_1 a0;
67 std::reference_wrapper<A_void_1> r1(a0);
68 int i = 4;
69 r1(i);
70 assert(count == save_count+4);
71 save_count = count;
72 }
73 // member function pointer
74 {
75 void (A_void_1::*fp)() = &A_void_1::mem1;
76 std::reference_wrapper<void (A_void_1::*)()> r1(fp);
77 A_void_1 a;
78 r1(a);
79 assert(count == save_count+1);
80 save_count = count;
81 A_void_1* ap = &a;
82 r1(ap);
83 assert(count == save_count+1);
84 save_count = count;
85 }
86 // const member function pointer
87 {
88 void (A_void_1::*fp)() const = &A_void_1::mem2;
89 std::reference_wrapper<void (A_void_1::*)() const> r1(fp);
90 A_void_1 a;
91 r1(a);
92 assert(count == save_count+1);
93 save_count = count;
94 A_void_1* ap = &a;
95 r1(ap);
96 assert(count == save_count+1);
97 save_count = count;
98 }
99 }
100
101 // 1 arg, return int
102
f_int_1(int i)103 int f_int_1(int i)
104 {
105 return i + 1;
106 }
107
108 struct A_int_1
109 {
A_int_1A_int_1110 A_int_1() : data_(5) {}
operator ()A_int_1111 int operator()(int i)
112 {
113 return i - 1;
114 }
115
mem1A_int_1116 int mem1() {return 3;}
mem2A_int_1117 int mem2() const {return 4;}
118 int data_;
119 };
120
121 void
test_int_1()122 test_int_1()
123 {
124 // function
125 {
126 std::reference_wrapper<int (int)> r1(f_int_1);
127 int i = 2;
128 assert(r1(i) == 3);
129 }
130 // function pointer
131 {
132 int (*fp)(int) = f_int_1;
133 std::reference_wrapper<int (*)(int)> r1(fp);
134 int i = 3;
135 assert(r1(i) == 4);
136 }
137 // functor
138 {
139 A_int_1 a0;
140 std::reference_wrapper<A_int_1> r1(a0);
141 int i = 4;
142 assert(r1(i) == 3);
143 }
144 // member function pointer
145 {
146 int (A_int_1::*fp)() = &A_int_1::mem1;
147 std::reference_wrapper<int (A_int_1::*)()> r1(fp);
148 A_int_1 a;
149 assert(r1(a) == 3);
150 A_int_1* ap = &a;
151 assert(r1(ap) == 3);
152 }
153 // const member function pointer
154 {
155 int (A_int_1::*fp)() const = &A_int_1::mem2;
156 std::reference_wrapper<int (A_int_1::*)() const> r1(fp);
157 A_int_1 a;
158 assert(r1(a) == 4);
159 A_int_1* ap = &a;
160 assert(r1(ap) == 4);
161 }
162 // member data pointer
163 {
164 int A_int_1::*fp = &A_int_1::data_;
165 std::reference_wrapper<int A_int_1::*> r1(fp);
166 A_int_1 a;
167 assert(r1(a) == 5);
168 r1(a) = 6;
169 assert(r1(a) == 6);
170 A_int_1* ap = &a;
171 assert(r1(ap) == 6);
172 r1(ap) = 7;
173 assert(r1(ap) == 7);
174 }
175 }
176
177 // 2 arg, return void
178
f_void_2(int i,int j)179 void f_void_2(int i, int j)
180 {
181 count += i+j;
182 }
183
184 struct A_void_2
185 {
operator ()A_void_2186 void operator()(int i, int j)
187 {
188 count += i+j;
189 }
190
mem1A_void_2191 void mem1(int i) {count += i;}
mem2A_void_2192 void mem2(int i) const {count += i;}
193 };
194
195 void
test_void_2()196 test_void_2()
197 {
198 int save_count = count;
199 // function
200 {
201 std::reference_wrapper<void (int, int)> r1(f_void_2);
202 int i = 2;
203 int j = 3;
204 r1(i, j);
205 assert(count == save_count+5);
206 save_count = count;
207 }
208 // function pointer
209 {
210 void (*fp)(int, int) = f_void_2;
211 std::reference_wrapper<void (*)(int, int)> r1(fp);
212 int i = 3;
213 int j = 4;
214 r1(i, j);
215 assert(count == save_count+7);
216 save_count = count;
217 }
218 // functor
219 {
220 A_void_2 a0;
221 std::reference_wrapper<A_void_2> r1(a0);
222 int i = 4;
223 int j = 5;
224 r1(i, j);
225 assert(count == save_count+9);
226 save_count = count;
227 }
228 // member function pointer
229 {
230 void (A_void_2::*fp)(int) = &A_void_2::mem1;
231 std::reference_wrapper<void (A_void_2::*)(int)> r1(fp);
232 A_void_2 a;
233 int i = 3;
234 r1(a, i);
235 assert(count == save_count+3);
236 save_count = count;
237 A_void_2* ap = &a;
238 r1(ap, i);
239 assert(count == save_count+3);
240 save_count = count;
241 }
242 // const member function pointer
243 {
244 void (A_void_2::*fp)(int) const = &A_void_2::mem2;
245 std::reference_wrapper<void (A_void_2::*)(int) const> r1(fp);
246 A_void_2 a;
247 int i = 4;
248 r1(a, i);
249 assert(count == save_count+4);
250 save_count = count;
251 A_void_2* ap = &a;
252 r1(ap, i);
253 assert(count == save_count+4);
254 save_count = count;
255 }
256 }
257
258 // 2 arg, return int
259
f_int_2(int i,int j)260 int f_int_2(int i, int j)
261 {
262 return i+j;
263 }
264
265 struct A_int_2
266 {
operator ()A_int_2267 int operator()(int i, int j)
268 {
269 return i+j;
270 }
271
mem1A_int_2272 int mem1(int i) {return i+1;}
mem2A_int_2273 int mem2(int i) const {return i+2;}
274 };
275
276 void
testint_2()277 testint_2()
278 {
279 // function
280 {
281 std::reference_wrapper<int (int, int)> r1(f_int_2);
282 int i = 2;
283 int j = 3;
284 assert(r1(i, j) == i+j);
285 }
286 // function pointer
287 {
288 int (*fp)(int, int) = f_int_2;
289 std::reference_wrapper<int (*)(int, int)> r1(fp);
290 int i = 3;
291 int j = 4;
292 assert(r1(i, j) == i+j);
293 }
294 // functor
295 {
296 A_int_2 a0;
297 std::reference_wrapper<A_int_2> r1(a0);
298 int i = 4;
299 int j = 5;
300 assert(r1(i, j) == i+j);
301 }
302 // member function pointer
303 {
304 int(A_int_2::*fp)(int) = &A_int_2::mem1;
305 std::reference_wrapper<int (A_int_2::*)(int)> r1(fp);
306 A_int_2 a;
307 int i = 3;
308 assert(r1(a, i) == i+1);
309 A_int_2* ap = &a;
310 assert(r1(ap, i) == i+1);
311 }
312 // const member function pointer
313 {
314 int (A_int_2::*fp)(int) const = &A_int_2::mem2;
315 std::reference_wrapper<int (A_int_2::*)(int) const> r1(fp);
316 A_int_2 a;
317 int i = 4;
318 assert(r1(a, i) == i+2);
319 A_int_2* ap = &a;
320 assert(r1(ap, i) == i+2);
321 }
322 }
323
main(int,char **)324 int main(int, char**)
325 {
326 test_void_1();
327 test_int_1();
328 test_void_2();
329 testint_2();
330
331 return 0;
332 }
333