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