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 "test_allocator.h"
21 #include "count_new.hpp"
22 
23 class A
24 {
25     int data_[10];
26 public:
27     static int count;
28 
29     A()
30     {
31         ++count;
32         for (int i = 0; i < 10; ++i)
33             data_[i] = i;
34     }
35 
36     A(const A&) {++count;}
37 
38     ~A() {--count;}
39 
40     int operator()(int i) const
41     {
42         for (int j = 0; j < 10; ++j)
43             i += data_[j];
44         return i;
45     }
46 };
47 
48 int A::count = 0;
49 
50 int g(int) {return 0;}
51 
52 int main()
53 {
54     assert(globalMemCounter.checkOutstandingNewEq(0));
55     {
56     std::function<int(int)> f = A();
57     assert(A::count == 1);
58     assert(globalMemCounter.checkOutstandingNewEq(1));
59     assert(f.target<A>());
60     assert(f.target<int(*)(int)>() == 0);
61     std::function<int(int)> f2(std::allocator_arg, test_allocator<A>(), f);
62     assert(A::count == 2);
63     assert(globalMemCounter.checkOutstandingNewEq(2));
64     assert(f2.target<A>());
65     assert(f2.target<int(*)(int)>() == 0);
66     }
67     assert(A::count == 0);
68     assert(globalMemCounter.checkOutstandingNewEq(0));
69     {
70     std::function<int(int)> f = g;
71     assert(globalMemCounter.checkOutstandingNewEq(0));
72     assert(f.target<int(*)(int)>());
73     assert(f.target<A>() == 0);
74     std::function<int(int)> f2(std::allocator_arg, test_allocator<int(*)(int)>(), f);
75     assert(globalMemCounter.checkOutstandingNewEq(0));
76     assert(f2.target<int(*)(int)>());
77     assert(f2.target<A>() == 0);
78     }
79     assert(globalMemCounter.checkOutstandingNewEq(0));
80     {
81     assert(globalMemCounter.checkOutstandingNewEq(0));
82     non_default_test_allocator<std::function<int(int)> > al(1);
83     std::function<int(int)> f2(std::allocator_arg, al, g);
84     assert(globalMemCounter.checkOutstandingNewEq(0));
85     assert(f2.target<int(*)(int)>());
86     assert(f2.target<A>() == 0);
87     }
88     assert(globalMemCounter.checkOutstandingNewEq(0));
89     {
90     std::function<int(int)> f;
91     assert(globalMemCounter.checkOutstandingNewEq(0));
92     assert(f.target<int(*)(int)>() == 0);
93     assert(f.target<A>() == 0);
94     std::function<int(int)> f2(std::allocator_arg, test_allocator<int>(), f);
95     assert(globalMemCounter.checkOutstandingNewEq(0));
96     assert(f2.target<int(*)(int)>() == 0);
97     assert(f2.target<A>() == 0);
98     }
99 }
100