| 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 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 | #if TEST_STD_VER >= 11 |
| 26 | struct RValueCallable { |
| 27 | template <class ...Args> |
| 28 | void operator()(Args&&...) && {} |
| 29 | }; |
| 30 | struct LValueCallable { |
| 31 | template <class ...Args> |
| 32 | void operator()(Args&&...) & {} |
| 33 | }; |
| 34 | #endif |
| 35 | |
| 36 | template <class T> |
| 37 | struct non_default_test_allocator : test_allocator<T> { |
| 38 | non_default_test_allocator() = delete; |
| 39 | using test_allocator<T>::test_allocator; |
| 40 | }; |
| 41 | |
| 42 | test_allocator_statistics alloc_stats; |
| 43 | |
| 44 | class DummyClass {}; |
| 45 | |
| 46 | template <class FuncType, class AllocType> |
| 47 | void test_FunctionObject(AllocType& alloc) |
| 48 | { |
| 49 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 50 | { |
| 51 | FunctionObject target; |
| 52 | assert(FunctionObject::count == 1); |
| 53 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 54 | std::function<FuncType> f2(std::allocator_arg, alloc, target); |
| 55 | assert(FunctionObject::count == 2); |
| 56 | assert(globalMemCounter.checkOutstandingNewEq(1)); |
| 57 | assert(f2.template target<FunctionObject>()); |
| 58 | assert(f2.template target<FuncType>() == 0); |
| 59 | assert(f2.template target<FuncType*>() == 0); |
| 60 | } |
| 61 | assert(FunctionObject::count == 0); |
| 62 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 63 | } |
| 64 | |
| 65 | |
| 66 | template <class FuncType, class AllocType> |
| 67 | void test_FreeFunction(AllocType& alloc) |
| 68 | { |
| 69 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 70 | { |
| 71 | FuncType* target = &FreeFunction; |
| 72 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 73 | std::function<FuncType> f2(std::allocator_arg, alloc, target); |
| 74 | // The allocator may not fit in the small object buffer, if we allocated |
| 75 | // check it was done via the allocator. |
| 76 | assert(globalMemCounter.checkOutstandingNewEq(alloc_stats.alloc_count)); |
| 77 | assert(f2.template target<FuncType*>()); |
| 78 | assert(*f2.template target<FuncType*>() == target); |
| 79 | assert(f2.template target<FuncType>() == 0); |
| 80 | assert(f2.template target<DummyClass>() == 0); |
| 81 | } |
| 82 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 83 | } |
| 84 | |
| 85 | template <class TargetType, class FuncType, class AllocType> |
| 86 | void test_MemFunClass(AllocType& alloc) |
| 87 | { |
| 88 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 89 | { |
| 90 | TargetType target = &MemFunClass::foo; |
| 91 | assert(globalMemCounter.checkOutstandingNewEq(0)); |
| 92 | std::function<FuncType> f2(std::allocator_arg, alloc, target); |
| 93 | assert(globalMemCounter.checkOutstandingNewEq(alloc_stats.alloc_count)); |
| 94 | assert(f2.template target<TargetType>()); |
| 95 | assert(*f2.template target<TargetType>() == target); |
| 96 | 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 | test_FunctionObject<int()>(alloc); |
| 104 | test_FunctionObject<int(int)>(alloc); |
| 105 | test_FunctionObject<int(int, int)>(alloc); |
| 106 | test_FunctionObject<int(int, int, int)>(alloc); |
| 107 | |
| 108 | test_FreeFunction<int()>(alloc); |
| 109 | test_FreeFunction<int(int)>(alloc); |
| 110 | test_FreeFunction<int(int, int)>(alloc); |
| 111 | test_FreeFunction<int(int, int, int)>(alloc); |
| 112 | |
| 113 | test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc); |
| 114 | test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc); |
| 115 | test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc); |
| 116 | } |
| 117 | |
| 118 | int main(int, char**) { |
| 119 | globalMemCounter.reset(); |
| 120 | { |
| 121 | bare_allocator<DummyClass> bare_alloc; |
| 122 | test_for_alloc(alloc&: bare_alloc); |
| 123 | } |
| 124 | { |
| 125 | non_default_test_allocator<DummyClass> non_default_alloc(42, &alloc_stats); |
| 126 | test_for_alloc(alloc&: non_default_alloc); |
| 127 | } |
| 128 | #if TEST_STD_VER >= 11 |
| 129 | { |
| 130 | using Fn = std::function<void(int, int, int)>; |
| 131 | static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable&>::value, "" ); |
| 132 | static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable>::value, "" ); |
| 133 | static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable&>::value, "" ); |
| 134 | static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable>::value, "" ); |
| 135 | } |
| 136 | #endif |
| 137 | |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |