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 
17 #include <functional>
18 #include <cassert>
19 
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "count_new.hpp"
23 
24 class A
25 {
26     int data_[10];
27 public:
28     static int count;
29 
30     A()
31     {
32         ++count;
33         for (int i = 0; i < 10; ++i)
34             data_[i] = i;
35     }
36 
37     A(const A&) {++count;}
38 
39     ~A() {--count;}
40 
41     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 
51 int g(int) {return 0;}
52 
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(std::allocator_arg, bare_allocator<A>(), 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(std::allocator_arg, bare_allocator<int(*)(int)>(), 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     assert(globalMemCounter.checkOutstandingNewEq(0));
83     non_default_test_allocator<std::function<int(int)> > al(1);
84     std::function<int(int)> f2(std::allocator_arg, al, g);
85     assert(globalMemCounter.checkOutstandingNewEq(0));
86     assert(f2.target<int(*)(int)>());
87     assert(f2.target<A>() == 0);
88     }
89     assert(globalMemCounter.checkOutstandingNewEq(0));
90     {
91     std::function<int(int)> f;
92     assert(globalMemCounter.checkOutstandingNewEq(0));
93     assert(f.target<int(*)(int)>() == 0);
94     assert(f.target<A>() == 0);
95     std::function<int(int)> f2(std::allocator_arg, bare_allocator<int>(), f);
96     assert(globalMemCounter.checkOutstandingNewEq(0));
97     assert(f2.target<int(*)(int)>() == 0);
98     assert(f2.target<A>() == 0);
99     }
100 }
101