• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // void swap(function& other);
15 
16 #include <functional>
17 #include <cassert>
18 
19 #include "count_new.hpp"
20 
21 class A
22 {
23     int data_[10];
24 public:
25     static int count;
26 
A(int j)27     explicit A(int j)
28     {
29         ++count;
30         data_[0] = j;
31     }
32 
A(const A & a)33     A(const A& a)
34     {
35         ++count;
36         for (int i = 0; i < 10; ++i)
37             data_[i] = a.data_[i];
38     }
39 
~A()40     ~A() {--count;}
41 
operator ()(int i) const42     int operator()(int i) const
43     {
44         for (int j = 0; j < 10; ++j)
45             i += data_[j];
46         return i;
47     }
48 
id() const49     int id() const {return data_[0];}
50 };
51 
52 int A::count = 0;
53 
g(int)54 int g(int) {return 0;}
h(int)55 int h(int) {return 1;}
56 
main()57 int main()
58 {
59     assert(globalMemCounter.checkOutstandingNewEq(0));
60     {
61     std::function<int(int)> f1 = A(1);
62     std::function<int(int)> f2 = A(2);
63     assert(A::count == 2);
64     assert(globalMemCounter.checkOutstandingNewEq(2));
65     assert(f1.target<A>()->id() == 1);
66     assert(f2.target<A>()->id() == 2);
67     f1.swap(f2);
68     assert(A::count == 2);
69     assert(globalMemCounter.checkOutstandingNewEq(2));
70     assert(f1.target<A>()->id() == 2);
71     assert(f2.target<A>()->id() == 1);
72     }
73     assert(A::count == 0);
74     assert(globalMemCounter.checkOutstandingNewEq(0));
75     {
76     std::function<int(int)> f1 = A(1);
77     std::function<int(int)> f2 = g;
78     assert(A::count == 1);
79     assert(globalMemCounter.checkOutstandingNewEq(1));
80     assert(f1.target<A>()->id() == 1);
81     assert(*f2.target<int(*)(int)>() == g);
82     f1.swap(f2);
83     assert(A::count == 1);
84     assert(globalMemCounter.checkOutstandingNewEq(1));
85     assert(*f1.target<int(*)(int)>() == g);
86     assert(f2.target<A>()->id() == 1);
87     }
88     assert(A::count == 0);
89     assert(globalMemCounter.checkOutstandingNewEq(0));
90     {
91     std::function<int(int)> f1 = g;
92     std::function<int(int)> f2 = A(1);
93     assert(A::count == 1);
94     assert(globalMemCounter.checkOutstandingNewEq(1));
95     assert(*f1.target<int(*)(int)>() == g);
96     assert(f2.target<A>()->id() == 1);
97     f1.swap(f2);
98     assert(A::count == 1);
99     assert(globalMemCounter.checkOutstandingNewEq(1));
100     assert(f1.target<A>()->id() == 1);
101     assert(*f2.target<int(*)(int)>() == g);
102     }
103     assert(A::count == 0);
104     assert(globalMemCounter.checkOutstandingNewEq(0));
105     {
106     std::function<int(int)> f1 = g;
107     std::function<int(int)> f2 = h;
108     assert(A::count == 0);
109     assert(globalMemCounter.checkOutstandingNewEq(0));
110     assert(*f1.target<int(*)(int)>() == g);
111     assert(*f2.target<int(*)(int)>() == h);
112     f1.swap(f2);
113     assert(A::count == 0);
114     assert(globalMemCounter.checkOutstandingNewEq(0));
115     assert(*f1.target<int(*)(int)>() == h);
116     assert(*f2.target<int(*)(int)>() == g);
117     }
118     assert(A::count == 0);
119     assert(globalMemCounter.checkOutstandingNewEq(0));
120 }
121