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