1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <functional> 11 12 // class function<R(ArgTypes...)> 13 14 // void swap(function& other); 15 16 // UNSUPPORTED: asan, msan 17 18 #include <functional> 19 #include <new> 20 #include <cstdlib> 21 #include <cassert> 22 23 int new_called = 0; 24 25 void* operator new(std::size_t s) throw(std::bad_alloc) 26 { 27 ++new_called; 28 return std::malloc(s); 29 } 30 31 void operator delete(void* p) throw() 32 { 33 --new_called; 34 std::free(p); 35 } 36 37 class A 38 { 39 int data_[10]; 40 public: 41 static int count; 42 43 explicit A(int j) 44 { 45 ++count; 46 data_[0] = j; 47 } 48 49 A(const A& a) 50 { 51 ++count; 52 for (int i = 0; i < 10; ++i) 53 data_[i] = a.data_[i]; 54 } 55 56 ~A() {--count;} 57 58 int operator()(int i) const 59 { 60 for (int j = 0; j < 10; ++j) 61 i += data_[j]; 62 return i; 63 } 64 65 int id() const {return data_[0];} 66 }; 67 68 int A::count = 0; 69 70 int g(int) {return 0;} 71 int h(int) {return 1;} 72 73 int main() 74 { 75 assert(new_called == 0); 76 { 77 std::function<int(int)> f1 = A(1); 78 std::function<int(int)> f2 = A(2); 79 assert(A::count == 2); 80 assert(new_called == 2); 81 assert(f1.target<A>()->id() == 1); 82 assert(f2.target<A>()->id() == 2); 83 f1.swap(f2); 84 assert(A::count == 2); 85 assert(new_called == 2); 86 assert(f1.target<A>()->id() == 2); 87 assert(f2.target<A>()->id() == 1); 88 } 89 assert(A::count == 0); 90 assert(new_called == 0); 91 { 92 std::function<int(int)> f1 = A(1); 93 std::function<int(int)> f2 = g; 94 assert(A::count == 1); 95 assert(new_called == 1); 96 assert(f1.target<A>()->id() == 1); 97 assert(*f2.target<int(*)(int)>() == g); 98 f1.swap(f2); 99 assert(A::count == 1); 100 assert(new_called == 1); 101 assert(*f1.target<int(*)(int)>() == g); 102 assert(f2.target<A>()->id() == 1); 103 } 104 assert(A::count == 0); 105 assert(new_called == 0); 106 { 107 std::function<int(int)> f1 = g; 108 std::function<int(int)> f2 = A(1); 109 assert(A::count == 1); 110 assert(new_called == 1); 111 assert(*f1.target<int(*)(int)>() == g); 112 assert(f2.target<A>()->id() == 1); 113 f1.swap(f2); 114 assert(A::count == 1); 115 assert(new_called == 1); 116 assert(f1.target<A>()->id() == 1); 117 assert(*f2.target<int(*)(int)>() == g); 118 } 119 assert(A::count == 0); 120 assert(new_called == 0); 121 { 122 std::function<int(int)> f1 = g; 123 std::function<int(int)> f2 = h; 124 assert(A::count == 0); 125 assert(new_called == 0); 126 assert(*f1.target<int(*)(int)>() == g); 127 assert(*f2.target<int(*)(int)>() == h); 128 f1.swap(f2); 129 assert(A::count == 0); 130 assert(new_called == 0); 131 assert(*f1.target<int(*)(int)>() == h); 132 assert(*f2.target<int(*)(int)>() == g); 133 } 134 assert(A::count == 0); 135 assert(new_called == 0); 136 } 137