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