[AMDGPU][True16][MC] Generate op_sel operands for VOPC instructions (#125561)
[llvm-project.git] / libcxx / test / std / utilities / function.objects / bind / func.bind / func.bind.bind / invoke_rvalue.pass.cpp
blob91ccbcf9eec9eecd55a98444731669cb5f118bf0
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <functional>
13 // template<CopyConstructible Fn, CopyConstructible... Types>
14 // unspecified bind(Fn, Types...); // constexpr since C++20
15 // template<Returnable R, CopyConstructible Fn, CopyConstructible... Types>
16 // unspecified bind(Fn, Types...); // constexpr since C++20
18 #include <stdio.h>
20 #include <functional>
21 #include <cassert>
23 #include "test_macros.h"
25 int count = 0;
27 // 1 arg, return void
29 void f_void_1(int i)
31 count += i;
34 struct A_void_1
36 void operator()(int i)
38 count += i;
41 void mem1() {++count;}
42 void mem2() const {count += 2;}
45 void
46 test_void_1()
48 using namespace std::placeholders;
49 int save_count = count;
50 // function
52 std::bind(f_void_1, _1)(2);
53 assert(count == save_count + 2);
54 save_count = count;
57 std::bind(f_void_1, 2)();
58 assert(count == save_count + 2);
59 save_count = count;
61 // function pointer
63 void (*fp)(int) = f_void_1;
64 std::bind(fp, _1)(3);
65 assert(count == save_count+3);
66 save_count = count;
69 void (*fp)(int) = f_void_1;
70 std::bind(fp, 3)();
71 assert(count == save_count+3);
72 save_count = count;
74 // functor
76 A_void_1 a0;
77 std::bind(a0, _1)(4);
78 assert(count == save_count+4);
79 save_count = count;
82 A_void_1 a0;
83 std::bind(a0, 4)();
84 assert(count == save_count+4);
85 save_count = count;
87 // member function pointer
89 void (A_void_1::*fp)() = &A_void_1::mem1;
90 std::bind(fp, _1)(A_void_1());
91 assert(count == save_count+1);
92 save_count = count;
93 A_void_1 a;
94 std::bind(fp, _1)(&a);
95 assert(count == save_count+1);
96 save_count = count;
99 void (A_void_1::*fp)() = &A_void_1::mem1;
100 std::bind(fp, A_void_1())();
101 assert(count == save_count+1);
102 save_count = count;
103 A_void_1 a;
104 std::bind(fp, &a)();
105 assert(count == save_count+1);
106 save_count = count;
108 // const member function pointer
110 void (A_void_1::*fp)() const = &A_void_1::mem2;
111 std::bind(fp, _1)(A_void_1());
112 assert(count == save_count+2);
113 save_count = count;
114 A_void_1 a;
115 std::bind(fp, _1)(&a);
116 assert(count == save_count+2);
117 save_count = count;
120 void (A_void_1::*fp)() const = &A_void_1::mem2;
121 std::bind(fp, A_void_1())();
122 assert(count == save_count+2);
123 save_count = count;
124 A_void_1 a;
125 std::bind(fp, &a)();
126 assert(count == save_count+2);
127 save_count = count;
131 // 1 arg, return int
133 TEST_CONSTEXPR_CXX20 int f_int_1(int i) {
134 return i + 1;
137 struct A_int_1 {
138 TEST_CONSTEXPR_CXX20 A_int_1() : data_(5) {}
139 TEST_CONSTEXPR_CXX20 int operator()(int i) {
140 return i - 1;
143 TEST_CONSTEXPR_CXX20 int mem1() { return 3; }
144 TEST_CONSTEXPR_CXX20 int mem2() const { return 4; }
145 int data_;
148 TEST_CONSTEXPR_CXX20 bool test_int_1() {
149 using namespace std::placeholders;
150 // function
152 assert(std::bind(f_int_1, _1)(2) == 3);
153 assert(std::bind(f_int_1, 2)() == 3);
155 // function pointer
157 int (*fp)(int) = f_int_1;
158 assert(std::bind(fp, _1)(3) == 4);
159 assert(std::bind(fp, 3)() == 4);
161 // functor
163 assert(std::bind(A_int_1(), _1)(4) == 3);
164 assert(std::bind(A_int_1(), 4)() == 3);
166 // member function pointer
168 assert(std::bind(&A_int_1::mem1, _1)(A_int_1()) == 3);
169 assert(std::bind(&A_int_1::mem1, A_int_1())() == 3);
170 A_int_1 a;
171 assert(std::bind(&A_int_1::mem1, _1)(&a) == 3);
172 assert(std::bind(&A_int_1::mem1, &a)() == 3);
174 // const member function pointer
176 assert(std::bind(&A_int_1::mem2, _1)(A_int_1()) == 4);
177 assert(std::bind(&A_int_1::mem2, A_int_1())() == 4);
178 A_int_1 a;
179 assert(std::bind(&A_int_1::mem2, _1)(&a) == 4);
180 assert(std::bind(&A_int_1::mem2, &a)() == 4);
182 // member data pointer
184 assert(std::bind(&A_int_1::data_, _1)(A_int_1()) == 5);
185 assert(std::bind(&A_int_1::data_, A_int_1())() == 5);
186 A_int_1 a;
187 assert(std::bind(&A_int_1::data_, _1)(a) == 5);
188 std::bind(&A_int_1::data_, _1)(a) = 6;
189 assert(std::bind(&A_int_1::data_, _1)(a) == 6);
190 assert(std::bind(&A_int_1::data_, _1)(&a) == 6);
191 std::bind(&A_int_1::data_, _1)(&a) = 7;
192 assert(std::bind(&A_int_1::data_, _1)(&a) == 7);
194 return true;
197 // 2 arg, return void
199 void f_void_2(int i, int j)
201 count += i+j;
204 struct A_void_2
206 void operator()(int i, int j)
208 count += i+j;
211 void mem1(int i) {count += i;}
212 void mem2(int i) const {count += i;}
215 void
216 test_void_2()
218 using namespace std::placeholders;
219 int save_count = count;
220 // function
222 std::bind(f_void_2, _1, _2)(2, 3);
223 assert(count == save_count+5);
224 save_count = count;
225 std::bind(f_void_2, 2, _1)(3);
226 assert(count == save_count+5);
227 save_count = count;
228 std::bind(f_void_2, 2, 3)();
229 assert(count == save_count+5);
230 save_count = count;
232 // member function pointer
234 std::bind(&A_void_2::mem1, _1, _2)(A_void_2(), 3);
235 assert(count == save_count+3);
236 save_count = count;
237 std::bind(&A_void_2::mem1, _2, _1)(3, A_void_2());
238 assert(count == save_count+3);
239 save_count = count;
243 TEST_CONSTEXPR_CXX20 int f_nested(int i) {
244 return i+1;
247 TEST_CONSTEXPR_CXX20 int g_nested(int i) {
248 return i*10;
251 TEST_CONSTEXPR_CXX20 bool test_nested() {
252 using namespace std::placeholders;
253 assert(std::bind(f_nested, std::bind(g_nested, _1))(3) == 31);
254 return true;
257 TEST_CONSTEXPR_CXX20 bool test_many_args() {
258 using namespace std::placeholders;
259 auto f = [](int a, char, float, long) { return a; };
260 auto bound = std::bind(f, _4, _3, _2, _1);
261 assert(bound(0l, 1.0f, '2', 3) == 3);
262 return true;
265 int main(int, char**) {
266 test_void_1();
267 test_int_1();
268 test_void_2();
269 test_nested();
271 // The other tests are not constexpr-friendly since they need to use a global variable
272 #if TEST_STD_VER >= 20
273 static_assert(test_int_1());
274 static_assert(test_nested());
275 #endif
277 return 0;