• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const function& f);
15 
16 #include <functional>
17 #include <memory>
18 #include <cstdlib>
19 #include <cassert>
20 
21 #include "test_macros.h"
22 #include "count_new.hpp"
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()53 int main()
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         std::function<int(int)> f2 = std::move(f);
112         assert(A::count == 1);
113         assert(globalMemCounter.checkOutstandingNewEq(1));
114         assert(f2.target<A>());
115         assert(f2.target<int(*)(int)>() == 0);
116         assert(f.target<A>() == 0);
117         assert(f.target<int(*)(int)>() == 0);
118     }
119     assert(globalMemCounter.checkOutstandingNewEq(0));
120     {
121         // Test that moving a function constructed from a reference wrapper
122         // is done without allocating.
123         DisableAllocationGuard g;
124         using Ref = std::reference_wrapper<A>;
125         A a;
126         Ref aref(a);
127         std::function<int(int)> f(aref);
128         assert(A::count == 1);
129         assert(f.target<A>() == nullptr);
130         assert(f.target<Ref>());
131         std::function<int(int)> f2(std::move(f));
132         assert(A::count == 1);
133         assert(f2.target<A>() == nullptr);
134         assert(f2.target<Ref>());
135         LIBCPP_ASSERT(f.target<Ref>()); // f is unchanged because the target is small
136     }
137     {
138         // Test that moving a function constructed from a function pointer
139         // is done without allocating
140         DisableAllocationGuard guard;
141         using Ptr = int(*)(int);
142         Ptr p = g;
143         std::function<int(int)> f(p);
144         assert(f.target<A>() == nullptr);
145         assert(f.target<Ptr>());
146         std::function<int(int)> f2(std::move(f));
147         assert(f2.target<A>() == nullptr);
148         assert(f2.target<Ptr>());
149         LIBCPP_ASSERT(f.target<Ptr>()); // f is unchanged because the target is small
150     }
151 #endif  // TEST_STD_VER >= 11
152 }
153