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(const function&  f);
14 // function(function&& f); // noexcept in C++20
15 
16 #include <functional>
17 #include <memory>
18 #include <cstdlib>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "count_new.h"
23 
24 class A
25 {
26     int data_[10];
27 public:
28     static int count;
29 
A()30     A()
31     {
32         ++count;
33         for (int i = 0; i < 10; ++i)
34             data_[i] = i;
35     }
36 
A(const A &)37     A(const A&) {++count;}
38 
~A()39     ~A() {--count;}
40 
operator ()(int i) const41     int operator()(int i) const
42     {
43         for (int j = 0; j < 10; ++j)
44             i += data_[j];
45         return i;
46     }
47 };
48 
49 int A::count = 0;
50 
g(int)51 int g(int) {return 0;}
52 
main(int,char **)53 int main(int, char**)
54 {
55     assert(globalMemCounter.checkOutstandingNewEq(0));
56     {
57     std::function<int(int)> f = A();
58     assert(A::count == 1);
59     assert(globalMemCounter.checkOutstandingNewEq(1));
60     assert(f.target<A>());
61     assert(f.target<int(*)(int)>() == 0);
62     std::function<int(int)> f2 = f;
63     assert(A::count == 2);
64     assert(globalMemCounter.checkOutstandingNewEq(2));
65     assert(f2.target<A>());
66     assert(f2.target<int(*)(int)>() == 0);
67     }
68     assert(A::count == 0);
69     assert(globalMemCounter.checkOutstandingNewEq(0));
70     {
71     std::function<int(int)> f = g;
72     assert(globalMemCounter.checkOutstandingNewEq(0));
73     assert(f.target<int(*)(int)>());
74     assert(f.target<A>() == 0);
75     std::function<int(int)> 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 = f;
87     assert(globalMemCounter.checkOutstandingNewEq(0));
88     assert(f2.target<int(*)(int)>() == 0);
89     assert(f2.target<A>() == 0);
90     }
91     {
92     std::function<int(int)> f;
93     assert(globalMemCounter.checkOutstandingNewEq(0));
94     assert(f.target<int(*)(int)>() == 0);
95     assert(f.target<A>() == 0);
96     assert(!f);
97     std::function<long(int)> g = f;
98     assert(globalMemCounter.checkOutstandingNewEq(0));
99     assert(g.target<long(*)(int)>() == 0);
100     assert(g.target<A>() == 0);
101     assert(!g);
102     }
103 #if TEST_STD_VER >= 11
104     assert(globalMemCounter.checkOutstandingNewEq(0));
105     { // Test rvalue references
106         std::function<int(int)> f = A();
107         assert(A::count == 1);
108         assert(globalMemCounter.checkOutstandingNewEq(1));
109         assert(f.target<A>());
110         assert(f.target<int(*)(int)>() == 0);
111 		LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
112 #if TEST_STD_VER > 17
113 		ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
114 #endif
115         std::function<int(int)> f2 = std::move(f);
116         assert(A::count == 1);
117         assert(globalMemCounter.checkOutstandingNewEq(1));
118         assert(f2.target<A>());
119         assert(f2.target<int(*)(int)>() == 0);
120         assert(f.target<A>() == 0);
121         assert(f.target<int(*)(int)>() == 0);
122     }
123     assert(globalMemCounter.checkOutstandingNewEq(0));
124     {
125         // Test that moving a function constructed from a reference wrapper
126         // is done without allocating.
127         DisableAllocationGuard g;
128         using Ref = std::reference_wrapper<A>;
129         A a;
130         Ref aref(a);
131         std::function<int(int)> f(aref);
132         assert(A::count == 1);
133         assert(f.target<A>() == nullptr);
134         assert(f.target<Ref>());
135 		LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
136 #if TEST_STD_VER > 17
137 		ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
138 #endif
139         std::function<int(int)> f2(std::move(f));
140         assert(A::count == 1);
141         assert(f2.target<A>() == nullptr);
142         assert(f2.target<Ref>());
143         LIBCPP_ASSERT(f.target<Ref>()); // f is unchanged because the target is small
144     }
145     {
146         // Test that moving a function constructed from a function pointer
147         // is done without allocating
148         DisableAllocationGuard guard;
149         using Ptr = int(*)(int);
150         Ptr p = g;
151         std::function<int(int)> f(p);
152         assert(f.target<A>() == nullptr);
153         assert(f.target<Ptr>());
154 		LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
155 #if TEST_STD_VER > 17
156 		ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
157 #endif
158         std::function<int(int)> f2(std::move(f));
159         assert(f2.target<A>() == nullptr);
160         assert(f2.target<Ptr>());
161         LIBCPP_ASSERT(f.target<Ptr>()); // f is unchanged because the target is small
162     }
163 #endif  // TEST_STD_VER >= 11
164 
165   return 0;
166 }
167