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