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++11 || c++14 |
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 <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 | template <class T> |
26 | struct non_default_test_allocator : test_allocator<T> { |
27 | non_default_test_allocator() = delete; |
28 | using test_allocator<T>::test_allocator; |
29 | }; |
30 | |
31 | class DummyClass {}; |
32 | |
33 | template <class FuncType, class AllocType> |
34 | void test_FunctionObject(AllocType& alloc) |
35 | { |
36 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
37 | { |
38 | // Construct function from FunctionObject. |
39 | std::function<FuncType> f = FunctionObject(); |
40 | assert(FunctionObject::count == 1); |
41 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
42 | RTTI_ASSERT(f.template target<FunctionObject>()); |
43 | RTTI_ASSERT(f.template target<FuncType>() == 0); |
44 | RTTI_ASSERT(f.template target<FuncType*>() == 0); |
45 | // Copy function with allocator |
46 | std::function<FuncType> f2(std::allocator_arg, alloc, f); |
47 | assert(FunctionObject::count == 2); |
48 | assert(globalMemCounter.checkOutstandingNewEq(2)); |
49 | RTTI_ASSERT(f2.template target<FunctionObject>()); |
50 | RTTI_ASSERT(f2.template target<FuncType>() == 0); |
51 | RTTI_ASSERT(f2.template target<FuncType*>() == 0); |
52 | } |
53 | assert(FunctionObject::count == 0); |
54 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
55 | } |
56 | |
57 | template <class FuncType, class AllocType> |
58 | void test_FreeFunction(AllocType& alloc) |
59 | { |
60 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
61 | { |
62 | // Construct function from function pointer. |
63 | FuncType* target = &FreeFunction; |
64 | std::function<FuncType> f = target; |
65 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
66 | RTTI_ASSERT(f.template target<FuncType*>()); |
67 | RTTI_ASSERT(*f.template target<FuncType*>() == target); |
68 | RTTI_ASSERT(f.template target<FuncType>() == 0); |
69 | // Copy function with allocator |
70 | std::function<FuncType> f2(std::allocator_arg, alloc, f); |
71 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
72 | RTTI_ASSERT(f2.template target<FuncType*>()); |
73 | RTTI_ASSERT(*f2.template target<FuncType*>() == target); |
74 | RTTI_ASSERT(f2.template target<FuncType>() == 0); |
75 | } |
76 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
77 | } |
78 | |
79 | template <class TargetType, class FuncType, class AllocType> |
80 | void test_MemFunClass(AllocType& alloc) |
81 | { |
82 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
83 | { |
84 | // Construct function from function pointer. |
85 | TargetType target = &MemFunClass::foo; |
86 | std::function<FuncType> f = target; |
87 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
88 | RTTI_ASSERT(f.template target<TargetType>()); |
89 | RTTI_ASSERT(*f.template target<TargetType>() == target); |
90 | RTTI_ASSERT(f.template target<FuncType*>() == 0); |
91 | // Copy function with allocator |
92 | std::function<FuncType> f2(std::allocator_arg, alloc, f); |
93 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
94 | RTTI_ASSERT(f2.template target<TargetType>()); |
95 | RTTI_ASSERT(*f2.template target<TargetType>() == target); |
96 | RTTI_ASSERT(f2.template target<FuncType*>() == 0); |
97 | } |
98 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
99 | } |
100 | |
101 | template <class Alloc> |
102 | void test_for_alloc(Alloc& alloc) |
103 | { |
104 | // Large FunctionObject -- Allocation should occur |
105 | test_FunctionObject<int()>(alloc); |
106 | test_FunctionObject<int(int)>(alloc); |
107 | test_FunctionObject<int(int, int)>(alloc); |
108 | test_FunctionObject<int(int, int, int)>(alloc); |
109 | // Free function -- No allocation should occur |
110 | test_FreeFunction<int()>(alloc); |
111 | test_FreeFunction<int(int)>(alloc); |
112 | test_FreeFunction<int(int, int)>(alloc); |
113 | test_FreeFunction<int(int, int, int)>(alloc); |
114 | // Member function -- No allocation should occur. |
115 | test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc); |
116 | test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc); |
117 | test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc); |
118 | } |
119 | |
120 | int main(int, char**) |
121 | { |
122 | globalMemCounter.reset(); |
123 | { |
124 | bare_allocator<DummyClass> alloc; |
125 | test_for_alloc(alloc); |
126 | } |
127 | { |
128 | non_default_test_allocator<DummyClass> alloc(42); |
129 | test_for_alloc(alloc); |
130 | } |
131 | |
132 | return 0; |
133 | } |
134 | |