mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-23 10:07:41 +08:00

to reflect the new license. These used slightly different spellings that defeated my regular expressions. We understand that people may be surprised that we're moving the header entirely to discuss the new license. We checked this carefully with the Foundation's lawyer and we believe this is the correct approach. Essentially, all code in the project is now made available by the LLVM project under our new license, so you will see that the license headers include that license only. Some of our contributors have contributed code under our old license, and accordingly, we have retained a copy of our old license notice in the top-level files in each project and repository. git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@351648 91177308-0d34-0410-b5e6-96231b3b80d8
130 lines
4.3 KiB
C++
130 lines
4.3 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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++98 || c++03 || c++11 || c++14
|
|
|
|
// class function<R(ArgTypes...)>
|
|
|
|
// template<class F, class A> function(allocator_arg_t, const A&, F);
|
|
|
|
#include <functional>
|
|
#include <cassert>
|
|
|
|
#include "test_macros.h"
|
|
#include "min_allocator.h"
|
|
#include "test_allocator.h"
|
|
#include "count_new.hpp"
|
|
#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);
|
|
assert(globalMemCounter.checkOutstandingNewEq(0));
|
|
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(0));
|
|
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()
|
|
{
|
|
{
|
|
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
|
|
|
|
}
|