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 // UNSUPPORTED: c++03
10 
11 // <functional>
12 
13 // class function<R(ArgTypes...)>
14 
15 // function& operator=(nullptr_t);
16 
17 #include <functional>
18 #include <cassert>
19 
20 #include "count_new.h"
21 
22 #include "test_macros.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     globalMemCounter.reset();
56     assert(globalMemCounter.checkOutstandingNewEq(0));
57     {
58     std::function<int(int)> f = A();
59     assert(A::count == 1);
60     assert(globalMemCounter.checkOutstandingNewLessThanOrEqual(1));
61     RTTI_ASSERT(f.target<A>());
62     f = nullptr;
63     assert(A::count == 0);
64     assert(globalMemCounter.checkOutstandingNewEq(0));
65     RTTI_ASSERT(f.target<A>() == 0);
66     }
67     {
68     std::function<int(int)> f = g;
69     assert(globalMemCounter.checkOutstandingNewEq(0));
70     RTTI_ASSERT(f.target<int(*)(int)>());
71     RTTI_ASSERT(f.target<A>() == 0);
72     f = nullptr;
73     assert(globalMemCounter.checkOutstandingNewEq(0));
74     RTTI_ASSERT(f.target<int(*)(int)>() == 0);
75     }
76 
77   return 0;
78 }
79