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 // function& operator=(const function& f); 14 15 #include <functional> 16 #include <cassert> 17 18 #include "test_macros.h" 19 #include "count_new.h" 20 21 class A { 22 int data_[10]; 23 24 public: 25 static int count; 26 27 A() { 28 ++count; 29 for (int i = 0; i < 10; ++i) 30 data_[i] = i; 31 } 32 33 A(const A &) { ++count; } 34 35 ~A() { --count; } 36 37 int operator()(int i) const { 38 for (int j = 0; j < 10; ++j) 39 i += data_[j]; 40 return i; 41 } 42 }; 43 44 int A::count = 0; 45 46 int g0() { return 0; } 47 int g(int) { return 0; } 48 int g2(int, int) { return 2; } 49 int g3(int, int, int) { return 3; } 50 51 int main(int, char**) { 52 globalMemCounter.reset(); 53 assert(globalMemCounter.checkOutstandingNewEq(0)); 54 { 55 std::function<int(int)> f = A(); 56 assert(A::count == 1); 57 assert(globalMemCounter.checkOutstandingNewEq(1)); 58 assert(f.target<A>()); 59 assert(f.target<int (*)(int)>() == 0); 60 std::function<int(int)> f2; 61 f2 = f; 62 assert(A::count == 2); 63 assert(globalMemCounter.checkOutstandingNewEq(2)); 64 assert(f2.target<A>()); 65 assert(f2.target<int (*)(int)>() == 0); 66 } 67 assert(A::count == 0); 68 assert(globalMemCounter.checkOutstandingNewEq(0)); 69 { 70 std::function<int(int)> f = g; 71 assert(globalMemCounter.checkOutstandingNewEq(0)); 72 assert(f.target<int (*)(int)>()); 73 assert(f.target<A>() == 0); 74 std::function<int(int)> f2; 75 f2 = f; 76 assert(globalMemCounter.checkOutstandingNewEq(0)); 77 assert(f2.target<int (*)(int)>()); 78 assert(f2.target<A>() == 0); 79 } 80 assert(globalMemCounter.checkOutstandingNewEq(0)); 81 { 82 std::function<int(int)> f; 83 assert(globalMemCounter.checkOutstandingNewEq(0)); 84 assert(f.target<int (*)(int)>() == 0); 85 assert(f.target<A>() == 0); 86 std::function<int(int)> f2; 87 f2 = f; 88 assert(globalMemCounter.checkOutstandingNewEq(0)); 89 assert(f2.target<int (*)(int)>() == 0); 90 assert(f2.target<A>() == 0); 91 } 92 { 93 typedef std::function<int()> Func; 94 Func f = g0; 95 Func& fr = (f = (Func &)f); 96 assert(&fr == &f); 97 assert(*f.target<int(*)()>() == g0); 98 } 99 { 100 typedef std::function<int(int)> Func; 101 Func f = g; 102 Func& fr = (f = (Func &)f); 103 assert(&fr == &f); 104 assert(*f.target<int(*)(int)>() == g); 105 } 106 { 107 typedef std::function<int(int, int)> Func; 108 Func f = g2; 109 Func& fr = (f = (Func &)f); 110 assert(&fr == &f); 111 assert(*f.target<int(*)(int, int)>() == g2); 112 } 113 { 114 typedef std::function<int(int, int, int)> Func; 115 Func f = g3; 116 Func& fr = (f = (Func &)f); 117 assert(&fr == &f); 118 assert(*f.target<int(*)(int, int, int)>() == g3); 119 } 120 #if TEST_STD_VER >= 11 121 assert(globalMemCounter.checkOutstandingNewEq(0)); 122 { 123 std::function<int(int)> f = A(); 124 assert(A::count == 1); 125 assert(globalMemCounter.checkOutstandingNewEq(1)); 126 assert(f.target<A>()); 127 assert(f.target<int (*)(int)>() == 0); 128 std::function<int(int)> f2; 129 f2 = std::move(f); 130 assert(A::count == 1); 131 assert(globalMemCounter.checkOutstandingNewEq(1)); 132 assert(f2.target<A>()); 133 assert(f2.target<int (*)(int)>() == 0); 134 assert(f.target<A>() == 0); 135 assert(f.target<int (*)(int)>() == 0); 136 } 137 #endif 138 139 return 0; 140 } 141