alloc_function.pass.cpp revision b64f8b07c104c6cc986570ac8ee0ed16a9f23976
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#include <functional>
17#include <new>
18#include <cstdlib>
19#include <cassert>
20
21#include "../test_allocator.h"
22
23int new_called = 0;
24
25void* operator new(std::size_t s) throw(std::bad_alloc)
26{
27    ++new_called;
28    return std::malloc(s);
29}
30
31void  operator delete(void* p) throw()
32{
33    --new_called;
34    std::free(p);
35}
36
37class A
38{
39    int data_[10];
40public:
41    static int count;
42
43    A()
44    {
45        ++count;
46        for (int i = 0; i < 10; ++i)
47            data_[i] = i;
48    }
49
50    A(const A&) {++count;}
51
52    ~A() {--count;}
53
54    int operator()(int i) const
55    {
56        for (int j = 0; j < 10; ++j)
57            i += data_[j];
58        return i;
59    }
60};
61
62int A::count = 0;
63
64int g(int) {return 0;}
65
66int main()
67{
68    assert(new_called == 0);
69    {
70    std::function<int(int)> f = A();
71    assert(A::count == 1);
72    assert(new_called == 1);
73    assert(f.target<A>());
74    assert(f.target<int(*)(int)>() == 0);
75    std::function<int(int)> f2(std::allocator_arg, test_allocator<A>(), f);
76    assert(A::count == 2);
77    assert(new_called == 2);
78    assert(f2.target<A>());
79    assert(f2.target<int(*)(int)>() == 0);
80    }
81    assert(A::count == 0);
82    assert(new_called == 0);
83    {
84    std::function<int(int)> f = g;
85    assert(new_called == 0);
86    assert(f.target<int(*)(int)>());
87    assert(f.target<A>() == 0);
88    std::function<int(int)> f2(std::allocator_arg, test_allocator<int(*)(int)>(), f);
89    assert(new_called == 0);
90    assert(f2.target<int(*)(int)>());
91    assert(f2.target<A>() == 0);
92    }
93    assert(new_called == 0);
94    {
95    std::function<int(int)> f;
96    assert(new_called == 0);
97    assert(f.target<int(*)(int)>() == 0);
98    assert(f.target<A>() == 0);
99    std::function<int(int)> f2(std::allocator_arg, test_allocator<int>(), f);
100    assert(new_called == 0);
101    assert(f2.target<int(*)(int)>() == 0);
102    assert(f2.target<A>() == 0);
103    }
104}
105