blob: 8f4471b09a3cf7a452fbb20019aefe6593e81160 [file] [log] [blame]
Howard Hinnantc52f43e2010-08-22 00:59:46 +00001//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <functional>
11
12// class function<R(ArgTypes...)>
13
14// function(const function& f);
15
16#include <functional>
17#include <new>
18#include <cstdlib>
19#include <cassert>
20
21int new_called = 0;
22
23void* operator new(std::size_t s) throw(std::bad_alloc)
24{
25 ++new_called;
26 return std::malloc(s);
27}
28
29void operator delete(void* p) throw()
30{
31 --new_called;
32 std::free(p);
33}
34
35class A
36{
37 int data_[10];
38public:
39 static int count;
40
41 A()
42 {
43 ++count;
44 for (int i = 0; i < 10; ++i)
45 data_[i] = i;
46 }
47
48 A(const A&) {++count;}
49
50 ~A() {--count;}
51
52 int operator()(int i) const
53 {
54 for (int j = 0; j < 10; ++j)
55 i += data_[j];
56 return i;
57 }
58};
59
60int A::count = 0;
61
62int g(int) {return 0;}
63
64int main()
65{
66 assert(new_called == 0);
67 {
68 std::function<int(int)> f = A();
69 assert(A::count == 1);
70 assert(new_called == 1);
71 assert(f.target<A>());
72 assert(f.target<int(*)(int)>() == 0);
73 std::function<int(int)> f2 = f;
74 assert(A::count == 2);
75 assert(new_called == 2);
76 assert(f2.target<A>());
77 assert(f2.target<int(*)(int)>() == 0);
78 }
79 assert(A::count == 0);
80 assert(new_called == 0);
81 {
82 std::function<int(int)> f = g;
83 assert(new_called == 0);
84 assert(f.target<int(*)(int)>());
85 assert(f.target<A>() == 0);
86 std::function<int(int)> f2 = f;
87 assert(new_called == 0);
88 assert(f2.target<int(*)(int)>());
89 assert(f2.target<A>() == 0);
90 }
91 assert(new_called == 0);
92 {
93 std::function<int(int)> f;
94 assert(new_called == 0);
95 assert(f.target<int(*)(int)>() == 0);
96 assert(f.target<A>() == 0);
97 std::function<int(int)> f2 = f;
98 assert(new_called == 0);
99 assert(f2.target<int(*)(int)>() == 0);
100 assert(f2.target<A>() == 0);
101 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000102#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +0000103 assert(new_called == 0);
104 {
105 std::function<int(int)> f = A();
106 assert(A::count == 1);
107 assert(new_called == 1);
108 assert(f.target<A>());
109 assert(f.target<int(*)(int)>() == 0);
110 std::function<int(int)> f2 = _STD::move(f);
111 assert(A::count == 1);
112 assert(new_called == 1);
113 assert(f2.target<A>());
114 assert(f2.target<int(*)(int)>() == 0);
115 assert(f.target<A>() == 0);
116 assert(f.target<int(*)(int)>() == 0);
117 }
Howard Hinnant73d21a42010-09-04 23:28:19 +0000118#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES
Howard Hinnantc52f43e2010-08-22 00:59:46 +0000119}