1 //===----------------------------------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 // REQUIRES-ANY: c++98, c++03, c++11, c++14
13 // class function<R(ArgTypes...)>
15 // template<class F, class A> function(allocator_arg_t, const A&, F);
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 #include "test_allocator.h"
23 #include "count_new.hpp"
24 #include "../function_types.h"
27 #if TEST_STD_VER >= 11
28 struct RValueCallable {
29 template <class ...Args>
30 void operator()(Args&&...) && {}
32 struct LValueCallable {
33 template <class ...Args>
34 void operator()(Args&&...) & {}
40 template <class FuncType, class AllocType>
41 void test_FunctionObject(AllocType& alloc)
43 assert(globalMemCounter.checkOutstandingNewEq(0));
45 FunctionObject target;
46 assert(FunctionObject::count == 1);
47 assert(globalMemCounter.checkOutstandingNewEq(0));
48 std::function<FuncType> f2(std::allocator_arg, alloc, target);
49 assert(FunctionObject::count == 2);
50 assert(globalMemCounter.checkOutstandingNewEq(1));
51 assert(f2.template target<FunctionObject>());
52 assert(f2.template target<FuncType>() == 0);
53 assert(f2.template target<FuncType*>() == 0);
55 assert(FunctionObject::count == 0);
56 assert(globalMemCounter.checkOutstandingNewEq(0));
60 template <class FuncType, class AllocType>
61 void test_FreeFunction(AllocType& alloc)
63 assert(globalMemCounter.checkOutstandingNewEq(0));
65 FuncType* target = &FreeFunction;
66 assert(globalMemCounter.checkOutstandingNewEq(0));
67 std::function<FuncType> f2(std::allocator_arg, alloc, target);
68 assert(globalMemCounter.checkOutstandingNewEq(0));
69 assert(f2.template target<FuncType*>());
70 assert(*f2.template target<FuncType*>() == target);
71 assert(f2.template target<FuncType>() == 0);
72 assert(f2.template target<DummyClass>() == 0);
74 assert(globalMemCounter.checkOutstandingNewEq(0));
77 template <class TargetType, class FuncType, class AllocType>
78 void test_MemFunClass(AllocType& alloc)
80 assert(globalMemCounter.checkOutstandingNewEq(0));
82 TargetType target = &MemFunClass::foo;
83 assert(globalMemCounter.checkOutstandingNewEq(0));
84 std::function<FuncType> f2(std::allocator_arg, alloc, target);
85 assert(globalMemCounter.checkOutstandingNewEq(0));
86 assert(f2.template target<TargetType>());
87 assert(*f2.template target<TargetType>() == target);
88 assert(f2.template target<FuncType*>() == 0);
90 assert(globalMemCounter.checkOutstandingNewEq(0));
93 template <class Alloc>
94 void test_for_alloc(Alloc& alloc) {
95 test_FunctionObject<int()>(alloc);
96 test_FunctionObject<int(int)>(alloc);
97 test_FunctionObject<int(int, int)>(alloc);
98 test_FunctionObject<int(int, int, int)>(alloc);
100 test_FreeFunction<int()>(alloc);
101 test_FreeFunction<int(int)>(alloc);
102 test_FreeFunction<int(int, int)>(alloc);
103 test_FreeFunction<int(int, int, int)>(alloc);
105 test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
106 test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
107 test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
113 bare_allocator<DummyClass> bare_alloc;
114 test_for_alloc(bare_alloc);
117 non_default_test_allocator<DummyClass> non_default_alloc(42);
118 test_for_alloc(non_default_alloc);
120 #if TEST_STD_VER >= 11
122 using Fn = std::function<void(int, int, int)>;
123 static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable&>::value, "");
124 static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable>::value, "");
125 static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable&>::value, "");
126 static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable>::value, "");