1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <functional>
10 // REQUIRES: c++98 || c++03 || c++11 || c++14
11 
12 // class function<R(ArgTypes...)>
13 
14 // template<class F, class A> function(allocator_arg_t, const A&, F);
15 
16 #include <functional>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 #include "count_new.h"
23 #include "../function_types.h"
24 
25 
26 #if TEST_STD_VER >= 11
27 struct RValueCallable {
28     template <class ...Args>
29     void operator()(Args&&...) && {}
30 };
31 struct LValueCallable {
32     template <class ...Args>
33     void operator()(Args&&...) & {}
34 };
35 #endif
36 
37 class DummyClass {};
38 
39 template <class FuncType, class AllocType>
40 void test_FunctionObject(AllocType& alloc)
41 {
42     assert(globalMemCounter.checkOutstandingNewEq(0));
43     {
44     FunctionObject target;
45     assert(FunctionObject::count == 1);
46     assert(globalMemCounter.checkOutstandingNewEq(0));
47     std::function<FuncType> f2(std::allocator_arg, alloc, target);
48     assert(FunctionObject::count == 2);
49     assert(globalMemCounter.checkOutstandingNewEq(1));
50     assert(f2.template target<FunctionObject>());
51     assert(f2.template target<FuncType>() == 0);
52     assert(f2.template target<FuncType*>() == 0);
53     }
54     assert(FunctionObject::count == 0);
55     assert(globalMemCounter.checkOutstandingNewEq(0));
56 }
57 
58 
59 template <class FuncType, class AllocType>
60 void test_FreeFunction(AllocType& alloc)
61 {
62     assert(globalMemCounter.checkOutstandingNewEq(0));
63     {
64     FuncType* target = &FreeFunction;
65     assert(globalMemCounter.checkOutstandingNewEq(0));
66     std::function<FuncType> f2(std::allocator_arg, alloc, target);
67     // The allocator may not fit in the small object buffer, if we allocated
68     // check it was done via the allocator.
69     assert(globalMemCounter.checkOutstandingNewEq(test_alloc_base::alloc_count));
70     assert(f2.template target<FuncType*>());
71     assert(*f2.template target<FuncType*>() == target);
72     assert(f2.template target<FuncType>() == 0);
73     assert(f2.template target<DummyClass>() == 0);
74     }
75     assert(globalMemCounter.checkOutstandingNewEq(0));
76 }
77 
78 template <class TargetType, class FuncType, class AllocType>
79 void test_MemFunClass(AllocType& alloc)
80 {
81     assert(globalMemCounter.checkOutstandingNewEq(0));
82     {
83     TargetType target = &MemFunClass::foo;
84     assert(globalMemCounter.checkOutstandingNewEq(0));
85     std::function<FuncType> f2(std::allocator_arg, alloc, target);
86     assert(globalMemCounter.checkOutstandingNewEq(test_alloc_base::alloc_count));
87     assert(f2.template target<TargetType>());
88     assert(*f2.template target<TargetType>() == target);
89     assert(f2.template target<FuncType*>() == 0);
90     }
91     assert(globalMemCounter.checkOutstandingNewEq(0));
92 }
93 
94 template <class Alloc>
95 void test_for_alloc(Alloc& alloc) {
96     test_FunctionObject<int()>(alloc);
97     test_FunctionObject<int(int)>(alloc);
98     test_FunctionObject<int(int, int)>(alloc);
99     test_FunctionObject<int(int, int, int)>(alloc);
100 
101     test_FreeFunction<int()>(alloc);
102     test_FreeFunction<int(int)>(alloc);
103     test_FreeFunction<int(int, int)>(alloc);
104     test_FreeFunction<int(int, int, int)>(alloc);
105 
106     test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
107     test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
108     test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
109 }
110 
111 int main(int, char**)
112 {
113   globalMemCounter.reset();
114   {
115     bare_allocator<DummyClass> bare_alloc;
116     test_for_alloc(bare_alloc);
117   }
118     {
119         non_default_test_allocator<DummyClass> non_default_alloc(42);
120         test_for_alloc(non_default_alloc);
121     }
122 #if TEST_STD_VER >= 11
123     {
124         using Fn = std::function<void(int, int, int)>;
125         static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable&>::value, "");
126         static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable>::value, "");
127         static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable&>::value, "");
128         static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable>::value, "");
129     }
130 #endif
131 
132 
133   return 0;
134 }
135