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.hpp"
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() {
52   assert(globalMemCounter.checkOutstandingNewEq(0));
53   {
54     std::function<int(int)> f = A();
55     assert(A::count == 1);
56     assert(globalMemCounter.checkOutstandingNewEq(1));
57     assert(f.target<A>());
58     assert(f.target<int (*)(int)>() == 0);
59     std::function<int(int)> f2;
60     f2 = f;
61     assert(A::count == 2);
62     assert(globalMemCounter.checkOutstandingNewEq(2));
63     assert(f2.target<A>());
64     assert(f2.target<int (*)(int)>() == 0);
65   }
66   assert(A::count == 0);
67   assert(globalMemCounter.checkOutstandingNewEq(0));
68   {
69     std::function<int(int)> f = g;
70     assert(globalMemCounter.checkOutstandingNewEq(0));
71     assert(f.target<int (*)(int)>());
72     assert(f.target<A>() == 0);
73     std::function<int(int)> f2;
74     f2 = f;
75     assert(globalMemCounter.checkOutstandingNewEq(0));
76     assert(f2.target<int (*)(int)>());
77     assert(f2.target<A>() == 0);
78   }
79   assert(globalMemCounter.checkOutstandingNewEq(0));
80   {
81     std::function<int(int)> f;
82     assert(globalMemCounter.checkOutstandingNewEq(0));
83     assert(f.target<int (*)(int)>() == 0);
84     assert(f.target<A>() == 0);
85     std::function<int(int)> f2;
86     f2 = f;
87     assert(globalMemCounter.checkOutstandingNewEq(0));
88     assert(f2.target<int (*)(int)>() == 0);
89     assert(f2.target<A>() == 0);
90   }
91   {
92     typedef std::function<int()> Func;
93     Func f = g0;
94     Func& fr = (f = (Func &)f);
95     assert(&fr == &f);
96     assert(*f.target<int(*)()>() == g0);
97   }
98   {
99     typedef std::function<int(int)> Func;
100     Func f = g;
101     Func& fr = (f = (Func &)f);
102     assert(&fr == &f);
103     assert(*f.target<int(*)(int)>() == g);
104   }
105   {
106     typedef std::function<int(int, int)> Func;
107     Func f = g2;
108     Func& fr = (f = (Func &)f);
109     assert(&fr == &f);
110     assert(*f.target<int(*)(int, int)>() == g2);
111   }
112   {
113     typedef std::function<int(int, int, int)> Func;
114     Func f = g3;
115     Func& fr = (f = (Func &)f);
116     assert(&fr == &f);
117     assert(*f.target<int(*)(int, int, int)>() == g3);
118   }
119 #if TEST_STD_VER >= 11
120   assert(globalMemCounter.checkOutstandingNewEq(0));
121   {
122     std::function<int(int)> f = A();
123     assert(A::count == 1);
124     assert(globalMemCounter.checkOutstandingNewEq(1));
125     assert(f.target<A>());
126     assert(f.target<int (*)(int)>() == 0);
127     std::function<int(int)> f2;
128     f2 = std::move(f);
129     assert(A::count == 1);
130     assert(globalMemCounter.checkOutstandingNewEq(1));
131     assert(f2.target<A>());
132     assert(f2.target<int (*)(int)>() == 0);
133     assert(f.target<A>() == 0);
134     assert(f.target<int (*)(int)>() == 0);
135   }
136 #endif
137 }
138