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