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 // function& operator=(const function& f);
15
16 #include <functional>
17 #include <cassert>
18
19 #include "test_macros.h"
20 #include "count_new.hpp"
21
22 class A {
23 int data_[10];
24
25 public:
26 static int count;
27
A()28 A() {
29 ++count;
30 for (int i = 0; i < 10; ++i)
31 data_[i] = i;
32 }
33
A(const A &)34 A(const A &) { ++count; }
35
~A()36 ~A() { --count; }
37
operator ()(int i) const38 int operator()(int i) const {
39 for (int j = 0; j < 10; ++j)
40 i += data_[j];
41 return i;
42 }
43 };
44
45 int A::count = 0;
46
g0()47 int g0() { return 0; }
g(int)48 int g(int) { return 0; }
g2(int,int)49 int g2(int, int) { return 2; }
g3(int,int,int)50 int g3(int, int, int) { return 3; }
51
main()52 int main() {
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 = 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 = 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 = 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 = 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