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 // template<class A> function(allocator_arg_t, const A&, const function&);
15 
16 // UNSUPPORTED: asan, msan
17 
18 #include <functional>
19 #include <new>
20 #include <cstdlib>
21 #include <cassert>
22 
23 #include "test_allocator.h"
24 
25 int new_called = 0;
26 
27 void* operator new(std::size_t s) throw(std::bad_alloc)
28 {
29     ++new_called;
30     return std::malloc(s);
31 }
32 
33 void  operator delete(void* p) throw()
34 {
35     --new_called;
36     std::free(p);
37 }
38 
39 class A
40 {
41     int data_[10];
42 public:
43     static int count;
44 
45     A()
46     {
47         ++count;
48         for (int i = 0; i < 10; ++i)
49             data_[i] = i;
50     }
51 
52     A(const A&) {++count;}
53 
54     ~A() {--count;}
55 
56     int operator()(int i) const
57     {
58         for (int j = 0; j < 10; ++j)
59             i += data_[j];
60         return i;
61     }
62 };
63 
64 int A::count = 0;
65 
66 int g(int) {return 0;}
67 
68 int main()
69 {
70     assert(new_called == 0);
71     {
72     std::function<int(int)> f = A();
73     assert(A::count == 1);
74     assert(new_called == 1);
75     assert(f.target<A>());
76     assert(f.target<int(*)(int)>() == 0);
77     std::function<int(int)> f2(std::allocator_arg, test_allocator<A>(), f);
78     assert(A::count == 2);
79     assert(new_called == 2);
80     assert(f2.target<A>());
81     assert(f2.target<int(*)(int)>() == 0);
82     }
83     assert(A::count == 0);
84     assert(new_called == 0);
85     {
86     std::function<int(int)> f = g;
87     assert(new_called == 0);
88     assert(f.target<int(*)(int)>());
89     assert(f.target<A>() == 0);
90     std::function<int(int)> f2(std::allocator_arg, test_allocator<int(*)(int)>(), f);
91     assert(new_called == 0);
92     assert(f2.target<int(*)(int)>());
93     assert(f2.target<A>() == 0);
94     }
95     assert(new_called == 0);
96     {
97     assert(new_called == 0);
98     non_default_test_allocator<std::function<int(int)>> al(1);
99     std::function<int(int)> f2(std::allocator_arg, al, g);
100     assert(new_called == 0);
101     assert(f2.target<int(*)(int)>());
102     assert(f2.target<A>() == 0);
103     }
104     assert(new_called == 0);
105     {
106     std::function<int(int)> f;
107     assert(new_called == 0);
108     assert(f.target<int(*)(int)>() == 0);
109     assert(f.target<A>() == 0);
110     std::function<int(int)> f2(std::allocator_arg, test_allocator<int>(), f);
111     assert(new_called == 0);
112     assert(f2.target<int(*)(int)>() == 0);
113     assert(f2.target<A>() == 0);
114     }
115 }
116