diff options
Diffstat (limited to 'test/std/utilities/function.objects/func.wrap/func.wrap.func')
28 files changed, 2190 insertions, 0 deletions
diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.alg/swap.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.alg/swap.pass.cpp new file mode 100644 index 0000000000000..58192c928d585 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.alg/swap.pass.cpp @@ -0,0 +1,123 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template <MoveConstructible R, MoveConstructible ... ArgTypes> +// void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&); + + +#include <functional> +#include <cstdlib> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + explicit A(int j) + { + ++count; + data_[0] = j; + } + + A(const A& a) + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = a.data_[i]; + } + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int id() const {return data_[0];} +}; + +int A::count = 0; + +int g(int) {return 0;} +int h(int) {return 1;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = A(1); + std::function<int(int)> f2 = A(2); + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f1.target<A>()->id() == 1); + assert(f2.target<A>()->id() == 2); + swap(f1, f2); + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f1.target<A>()->id() == 2); + assert(f2.target<A>()->id() == 1); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = A(1); + std::function<int(int)> f2 = g; + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f1.target<A>()->id() == 1); + assert(*f2.target<int(*)(int)>() == g); + swap(f1, f2); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(*f1.target<int(*)(int)>() == g); + assert(f2.target<A>()->id() == 1); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = g; + std::function<int(int)> f2 = A(1); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(*f1.target<int(*)(int)>() == g); + assert(f2.target<A>()->id() == 1); + swap(f1, f2); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f1.target<A>()->id() == 1); + assert(*f2.target<int(*)(int)>() == g); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = g; + std::function<int(int)> f2 = h; + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(*f1.target<int(*)(int)>() == g); + assert(*f2.target<int(*)(int)>() == h); + swap(f1, f2); + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(*f1.target<int(*)(int)>() == h); + assert(*f2.target<int(*)(int)>() == g); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.cap/operator_bool.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.cap/operator_bool.pass.cpp new file mode 100644 index 0000000000000..829763f79d822 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.cap/operator_bool.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// explicit operator bool() const + +#include <functional> +#include <cassert> + +int g(int) {return 0;} + +int main() +{ + { + std::function<int(int)> f; + assert(!f); + f = g; + assert(f); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F.pass.cpp new file mode 100644 index 0000000000000..cd86e4cbf8ebe --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F.pass.cpp @@ -0,0 +1,90 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// function(nullptr_t); + +#include <functional> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int foo(int) const {return 1;} +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = g; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = (int (*)(int))0; + assert(!f); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + } + { + std::function<int(const A*, int)> f = &A::foo; + assert(f); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int (A::*)(int) const>() != 0); + } + { + std::function<void(int)> f(&g); + assert(f); + assert(f.target<int(*)(int)>() != 0); + f(1); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_assign.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_assign.pass.cpp new file mode 100644 index 0000000000000..11716e7946b04 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_assign.pass.cpp @@ -0,0 +1,98 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class F> +// requires CopyConstructible<F> && Callable<F, ArgTypes..> +// && Convertible<Callable<F, ArgTypes...>::result_type +// operator=(F f); + +#include <functional> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int foo(int) const {return 1;} +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f; + f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f; + f = g; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f; + f = (int (*)(int))0; + assert(!f); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + } + { + std::function<int(const A*, int)> f; + f = &A::foo; + assert(f); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int (A::*)(int) const>() != 0); + } + { + std::function<void(int)> f; + f = &g; + assert(f); + assert(f.target<int(*)(int)>() != 0); + f(1); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_incomplete.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_incomplete.pass.cpp new file mode 100644 index 0000000000000..c8f4178a26bdd --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/F_incomplete.pass.cpp @@ -0,0 +1,29 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class F> function(F); + +// Allow incomplete argument types in the __is_callable check + +#include <functional> + +struct X{ + typedef std::function<void(X&)> callback_type; + virtual ~X() {} +private: + callback_type _cb; +}; + +int main() +{ +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc.pass.cpp new file mode 100644 index 0000000000000..f97e34d3f2cbe --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc.pass.cpp @@ -0,0 +1,27 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class A> function(allocator_arg_t, const A&); + +#include <functional> +#include <cassert> + +#include "min_allocator.h" + +int main() +{ + { + std::function<int(int)> f(std::allocator_arg, bare_allocator<int>()); + assert(!f); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp new file mode 100644 index 0000000000000..352ecfc602be4 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class F, class A> function(allocator_arg_t, const A&, F); + +#include <functional> +#include <cassert> + +#include "min_allocator.h" +#include "test_allocator.h" +#include "count_new.hpp" +#include "../function_types.h" + +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); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp new file mode 100644 index 0000000000000..371eb98de1a9b --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp @@ -0,0 +1,124 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class A> function(allocator_arg_t, const A&, const function&); + + +#include <functional> +#include <cassert> + +#include "min_allocator.h" +#include "test_allocator.h" +#include "count_new.hpp" +#include "../function_types.h" + +class DummyClass {}; + +template <class FuncType, class AllocType> +void test_FunctionObject(AllocType& alloc) +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + // Construct function from FunctionObject. + std::function<FuncType> f = FunctionObject(); + assert(FunctionObject::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.template target<FunctionObject>()); + assert(f.template target<FuncType>() == 0); + assert(f.template target<FuncType*>() == 0); + // Copy function with allocator + std::function<FuncType> f2(std::allocator_arg, alloc, f); + assert(FunctionObject::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + 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)); + { + // Construct function from function pointer. + FuncType* target = &FreeFunction; + std::function<FuncType> f = target; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.template target<FuncType*>()); + assert(*f.template target<FuncType*>() == target); + assert(f.template target<FuncType>() == 0); + // Copy function with allocator + std::function<FuncType> f2(std::allocator_arg, alloc, f); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f2.template target<FuncType*>()); + assert(*f2.template target<FuncType*>() == target); + assert(f2.template target<FuncType>() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); +} + +template <class TargetType, class FuncType, class AllocType> +void test_MemFunClass(AllocType& alloc) +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + // Construct function from function pointer. + TargetType target = &MemFunClass::foo; + std::function<FuncType> f = target; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.template target<TargetType>()); + assert(*f.template target<TargetType>() == target); + assert(f.template target<FuncType*>() == 0); + // Copy function with allocator + std::function<FuncType> f2(std::allocator_arg, alloc, f); + 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) +{ + // Large FunctionObject -- Allocation should occur + test_FunctionObject<int()>(alloc); + test_FunctionObject<int(int)>(alloc); + test_FunctionObject<int(int, int)>(alloc); + test_FunctionObject<int(int, int, int)>(alloc); + // Free function -- No allocation should occur + test_FreeFunction<int()>(alloc); + test_FreeFunction<int(int)>(alloc); + test_FreeFunction<int(int, int)>(alloc); + test_FreeFunction<int(int, int, int)>(alloc); + // Member function -- No allocation should occur. + 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> alloc; + test_for_alloc(alloc); + } + { + non_default_test_allocator<DummyClass> alloc(42); + test_for_alloc(alloc); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_nullptr.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_nullptr.pass.cpp new file mode 100644 index 0000000000000..2350f92f0f893 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_nullptr.pass.cpp @@ -0,0 +1,25 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class A> function(allocator_arg_t, const A&, nullptr_t); + +#include <functional> +#include <cassert> + +#include "min_allocator.h" + +int main() +{ + std::function<int(int)> f(std::allocator_arg, bare_allocator<int>(), nullptr); + assert(!f); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_rfunction.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_rfunction.pass.cpp new file mode 100644 index 0000000000000..aa6b743b5236b --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_rfunction.pass.cpp @@ -0,0 +1,68 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class A> function(allocator_arg_t, const A&, function&&); + +#include <functional> +#include <cassert> + +#include "min_allocator.h" +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } +}; + +int A::count = 0; + +int main() +{ +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + std::function<int(int)> f2(std::allocator_arg, bare_allocator<A>(), std::move(f)); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f2.target<A>()); + assert(f2.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + assert(f.target<int(*)(int)>() == 0); + } +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy.pass.cpp new file mode 100644 index 0000000000000..f603da9dd1319 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy.pass.cpp @@ -0,0 +1,118 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// function(const function& f); + +#include <functional> +#include <cstdlib> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + std::function<int(int)> f2 = f; + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f2.target<A>()); + assert(f2.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = g; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + std::function<int(int)> f2 = f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f2.target<int(*)(int)>()); + assert(f2.target<A>() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + std::function<int(int)> f2 = f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f2.target<int(*)(int)>() == 0); + assert(f2.target<A>() == 0); + } + { + std::function<int(int)> f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + assert(!f); + std::function<long(int)> g = f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(g.target<long(*)(int)>() == 0); + assert(g.target<A>() == 0); + assert(!g); + } +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + std::function<int(int)> f2 = std::move(f); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f2.target<A>()); + assert(f2.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + assert(f.target<int(*)(int)>() == 0); + } +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy_assign.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy_assign.pass.cpp new file mode 100644 index 0000000000000..c91eaa2d56744 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/copy_assign.pass.cpp @@ -0,0 +1,109 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// function& operator=(const function& f); + +#include <functional> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + std::function<int(int)> f2; + f2 = f; + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f2.target<A>()); + assert(f2.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = g; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + std::function<int(int)> f2; + f2 = f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f2.target<int(*)(int)>()); + assert(f2.target<A>() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + std::function<int(int)> f2; + f2 = f; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f2.target<int(*)(int)>() == 0); + assert(f2.target<A>() == 0); + } +#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + std::function<int(int)> f2; + f2 = std::move(f); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f2.target<A>()); + assert(f2.target<int(*)(int)>() == 0); + assert(f.target<A>() == 0); + assert(f.target<int(*)(int)>() == 0); + } +#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/default.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/default.pass.cpp new file mode 100644 index 0000000000000..83d61b6b2d89e --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/default.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// explicit function(); + +#include <functional> +#include <cassert> + +int main() +{ + std::function<int(int)> f; + assert(!f); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/no-variadics.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/no-variadics.pass.cpp new file mode 100644 index 0000000000000..7099c45fab81f --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/no-variadics.pass.cpp @@ -0,0 +1,24 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R()> + +// template<class F> function(F); + +#define _LIBCPP_HAS_NO_VARIADICS +#include <functional> +#include <cassert> + +int main() +{ + std::function<void()> f(static_cast<void(*)()>(0)); + assert(!f); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t.pass.cpp new file mode 100644 index 0000000000000..f0d6402d185e4 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t.pass.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// function(nullptr_t); + +#include <functional> +#include <cassert> + +int main() +{ + std::function<int(int)> f(nullptr); + assert(!f); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t_assign.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t_assign.pass.cpp new file mode 100644 index 0000000000000..9b2482fb9d544 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/nullptr_t_assign.pass.cpp @@ -0,0 +1,72 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// function& operator=(nullptr_t); + +#include <functional> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target<A>()); + f = nullptr; + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<A>() == 0); + } + { + std::function<int(int)> f = g; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + f = nullptr; + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(f.target<int(*)(int)>() == 0); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.fail.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.fail.cpp new file mode 100644 index 0000000000000..6dcd2857452cc --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.fail.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// R operator()(ArgTypes... args) const + +#include <functional> +#include <cassert> + +// member data pointer: cv qualifiers should transfer from argument to return type + +struct A_int_1 +{ + A_int_1() : data_(5) {} + + int data_; +}; + +void +test_int_1() +{ + // member data pointer + { + int A_int_1::*fp = &A_int_1::data_; + A_int_1 a; + std::function<int& (const A_int_1*)> r2(fp); + const A_int_1* ap = &a; + assert(r2(ap) == 6); + r2(ap) = 7; + assert(r2(ap) == 7); + } +} + +int main() +{ + test_int_1(); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.pass.cpp new file mode 100644 index 0000000000000..31b80c3323c10 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke.pass.cpp @@ -0,0 +1,335 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// R operator()(ArgTypes... args) const + +#include <functional> +#include <cassert> + +int count = 0; + +// 1 arg, return void + +void f_void_1(int i) +{ + count += i; +} + +struct A_void_1 +{ + void operator()(int i) + { + count += i; + } + + void mem1() {++count;} + void mem2() const {++count;} +}; + +void +test_void_1() +{ + int save_count = count; + // function + { + std::function<void (int)> r1(f_void_1); + int i = 2; + r1(i); + assert(count == save_count+2); + save_count = count; + } + // function pointer + { + void (*fp)(int) = f_void_1; + std::function<void (int)> r1(fp); + int i = 3; + r1(i); + assert(count == save_count+3); + save_count = count; + } + // functor + { + A_void_1 a0; + std::function<void (int)> r1(a0); + int i = 4; + r1(i); + assert(count == save_count+4); + save_count = count; + } + // member function pointer + { + void (A_void_1::*fp)() = &A_void_1::mem1; + std::function<void (A_void_1)> r1(fp); + A_void_1 a; + r1(a); + assert(count == save_count+1); + save_count = count; + A_void_1* ap = &a; + std::function<void (A_void_1*)> r2 = fp; + r2(ap); + assert(count == save_count+1); + save_count = count; + } + // const member function pointer + { + void (A_void_1::*fp)() const = &A_void_1::mem2; + std::function<void (A_void_1)> r1(fp); + A_void_1 a; + r1(a); + assert(count == save_count+1); + save_count = count; + std::function<void (A_void_1*)> r2(fp); + A_void_1* ap = &a; + r2(ap); + assert(count == save_count+1); + save_count = count; + } +} + +// 1 arg, return int + +int f_int_1(int i) +{ + return i + 1; +} + +struct A_int_1 +{ + A_int_1() : data_(5) {} + int operator()(int i) + { + return i - 1; + } + + int mem1() {return 3;} + int mem2() const {return 4;} + int data_; +}; + +void +test_int_1() +{ + // function + { + std::function<int (int)> r1(f_int_1); + int i = 2; + assert(r1(i) == 3); + } + // function pointer + { + int (*fp)(int) = f_int_1; + std::function<int (int)> r1(fp); + int i = 3; + assert(r1(i) == 4); + } + // functor + { + A_int_1 a0; + std::function<int (int)> r1(a0); + int i = 4; + assert(r1(i) == 3); + } + // member function pointer + { + int (A_int_1::*fp)() = &A_int_1::mem1; + std::function<int (A_int_1)> r1(fp); + A_int_1 a; + assert(r1(a) == 3); + std::function<int (A_int_1*)> r2(fp); + A_int_1* ap = &a; + assert(r2(ap) == 3); + } + // const member function pointer + { + int (A_int_1::*fp)() const = &A_int_1::mem2; + std::function<int (A_int_1)> r1(fp); + A_int_1 a; + assert(r1(a) == 4); + std::function<int (A_int_1*)> r2(fp); + A_int_1* ap = &a; + assert(r2(ap) == 4); + } + // member data pointer + { + int A_int_1::*fp = &A_int_1::data_; + std::function<int& (A_int_1&)> r1(fp); + A_int_1 a; + assert(r1(a) == 5); + r1(a) = 6; + assert(r1(a) == 6); + std::function<int& (A_int_1*)> r2(fp); + A_int_1* ap = &a; + assert(r2(ap) == 6); + r2(ap) = 7; + assert(r2(ap) == 7); + } +} + +// 2 arg, return void + +void f_void_2(int i, int j) +{ + count += i+j; +} + +struct A_void_2 +{ + void operator()(int i, int j) + { + count += i+j; + } + + void mem1(int i) {count += i;} + void mem2(int i) const {count += i;} +}; + +void +test_void_2() +{ + int save_count = count; + // function + { + std::function<void (int, int)> r1(f_void_2); + int i = 2; + int j = 3; + r1(i, j); + assert(count == save_count+5); + save_count = count; + } + // function pointer + { + void (*fp)(int, int) = f_void_2; + std::function<void (int, int)> r1(fp); + int i = 3; + int j = 4; + r1(i, j); + assert(count == save_count+7); + save_count = count; + } + // functor + { + A_void_2 a0; + std::function<void (int, int)> r1(a0); + int i = 4; + int j = 5; + r1(i, j); + assert(count == save_count+9); + save_count = count; + } + // member function pointer + { + void (A_void_2::*fp)(int) = &A_void_2::mem1; + std::function<void (A_void_2, int)> r1(fp); + A_void_2 a; + int i = 3; + r1(a, i); + assert(count == save_count+3); + save_count = count; + std::function<void (A_void_2*, int)> r2(fp); + A_void_2* ap = &a; + r2(ap, i); + assert(count == save_count+3); + save_count = count; + } + // const member function pointer + { + void (A_void_2::*fp)(int) const = &A_void_2::mem2; + std::function<void (A_void_2, int)> r1(fp); + A_void_2 a; + int i = 4; + r1(a, i); + assert(count == save_count+4); + save_count = count; + std::function<void (A_void_2*, int)> r2(fp); + A_void_2* ap = &a; + r2(ap, i); + assert(count == save_count+4); + save_count = count; + } +} + +// 2 arg, return int + +int f_int_2(int i, int j) +{ + return i+j; +} + +struct A_int_2 +{ + int operator()(int i, int j) + { + return i+j; + } + + int mem1(int i) {return i+1;} + int mem2(int i) const {return i+2;} +}; + +void +testint_2() +{ + // function + { + std::function<int (int, int)> r1(f_int_2); + int i = 2; + int j = 3; + assert(r1(i, j) == i+j); + } + // function pointer + { + int (*fp)(int, int) = f_int_2; + std::function<int (int, int)> r1(fp); + int i = 3; + int j = 4; + assert(r1(i, j) == i+j); + } + // functor + { + A_int_2 a0; + std::function<int (int, int)> r1(a0); + int i = 4; + int j = 5; + assert(r1(i, j) == i+j); + } + // member function pointer + { + int(A_int_2::*fp)(int) = &A_int_2::mem1; + std::function<int (A_int_2, int)> r1(fp); + A_int_2 a; + int i = 3; + assert(r1(a, i) == i+1); + std::function<int (A_int_2*, int)> r2(fp); + A_int_2* ap = &a; + assert(r2(ap, i) == i+1); + } + // const member function pointer + { + int (A_int_2::*fp)(int) const = &A_int_2::mem2; + std::function<int (A_int_2, int)> r1(fp); + A_int_2 a; + int i = 4; + assert(r1(a, i) == i+2); + std::function<int (A_int_2*, int)> r2(fp); + A_int_2* ap = &a; + assert(r2(ap, i) == i+2); + } +} + +int main() +{ + test_void_1(); + test_int_1(); + test_void_2(); + testint_2(); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_int_0.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_int_0.pass.cpp new file mode 100644 index 0000000000000..67b4ec22da8c7 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_int_0.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// R operator()(ArgTypes... args) const + +#include <functional> +#include <cassert> + +// 0 args, return int + +int count = 0; + +int f_int_0() +{ + return 3; +} + +struct A_int_0 +{ + int operator()() {return 4;} +}; + +void +test_int_0() +{ + // function + { + std::function<int ()> r1(f_int_0); + assert(r1() == 3); + } + // function pointer + { + int (*fp)() = f_int_0; + std::function<int ()> r1(fp); + assert(r1() == 3); + } + // functor + { + A_int_0 a0; + std::function<int ()> r1(a0); + assert(r1() == 4); + } +} + +int main() +{ + test_int_0(); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_no_variadics.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_no_variadics.pass.cpp new file mode 100644 index 0000000000000..c0a14fd96fcbc --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_no_variadics.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R()> + +// Test that we properly return both values and void for all non-variadic +// overloads of function::operator()(...) + +#define _LIBCPP_HAS_NO_VARIADICS +#include <functional> +#include <cassert> + +int foo0() { return 42; } +int foo1(int) { return 42; } +int foo2(int, int) { return 42; } +int foo3(int, int, int) { return 42; } + +int main() +{ + { + std::function<int()> f(&foo0); + assert(f() == 42); + } + { + std::function<int(int)> f(&foo1); + assert(f(1) == 42); + } + { + std::function<int(int, int)> f(&foo2); + assert(f(1, 1) == 42); + } + { + std::function<int(int, int, int)> f(&foo3); + assert(f(1, 1, 1) == 42); + } + { + std::function<void()> f(&foo0); + f(); + } + { + std::function<void(int)> f(&foo1); + f(1); + } + { + std::function<void(int, int)> f(&foo2); + f(1, 1); + } + { + std::function<void(int, int, int)> f(&foo3); + f(1, 1, 1); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_void_0.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_void_0.pass.cpp new file mode 100644 index 0000000000000..a820cb1b8f38e --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.inv/invoke_void_0.pass.cpp @@ -0,0 +1,67 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// R operator()(ArgTypes... args) const + +#include <functional> +#include <new> +#include <cstdlib> +#include <cassert> + +// 0 args, return void + +int count = 0; + +void f_void_0() +{ + ++count; +} + +struct A_void_0 +{ + void operator()() {++count;} +}; + +void +test_void_0() +{ + int save_count = count; + // function + { + std::function<void ()> r1(f_void_0); + r1(); + assert(count == save_count+1); + save_count = count; + } + // function pointer + { + void (*fp)() = f_void_0; + std::function<void ()> r1(fp); + r1(); + assert(count == save_count+1); + save_count = count; + } + // functor + { + A_void_0 a0; + std::function<void ()> r1(a0); + r1(); + assert(count == save_count+1); + save_count = count; + } +} + +int main() +{ + test_void_0(); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/assign_F_alloc.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/assign_F_alloc.pass.cpp new file mode 100644 index 0000000000000..e9ecfa5539ce5 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/assign_F_alloc.pass.cpp @@ -0,0 +1,60 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<class F, class A> void assign(F&&, const A&); + +#include <functional> +#include <cassert> + +#include "test_allocator.h" + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int foo(int) const {return 1;} +}; + +int A::count = 0; + +int main() +{ + { + std::function<int(int)> f; + f.assign(A(), test_allocator<A>()); + assert(A::count == 1); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + } + assert(A::count == 0); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/swap.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/swap.pass.cpp new file mode 100644 index 0000000000000..f94e689b2a6bc --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.mod/swap.pass.cpp @@ -0,0 +1,120 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// void swap(function& other); + +#include <functional> +#include <cassert> + +#include "count_new.hpp" + +class A +{ + int data_[10]; +public: + static int count; + + explicit A(int j) + { + ++count; + data_[0] = j; + } + + A(const A& a) + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = a.data_[i]; + } + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int id() const {return data_[0];} +}; + +int A::count = 0; + +int g(int) {return 0;} +int h(int) {return 1;} + +int main() +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = A(1); + std::function<int(int)> f2 = A(2); + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f1.target<A>()->id() == 1); + assert(f2.target<A>()->id() == 2); + f1.swap(f2); + assert(A::count == 2); + assert(globalMemCounter.checkOutstandingNewEq(2)); + assert(f1.target<A>()->id() == 2); + assert(f2.target<A>()->id() == 1); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = A(1); + std::function<int(int)> f2 = g; + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f1.target<A>()->id() == 1); + assert(*f2.target<int(*)(int)>() == g); + f1.swap(f2); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(*f1.target<int(*)(int)>() == g); + assert(f2.target<A>()->id() == 1); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = g; + std::function<int(int)> f2 = A(1); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(*f1.target<int(*)(int)>() == g); + assert(f2.target<A>()->id() == 1); + f1.swap(f2); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f1.target<A>()->id() == 1); + assert(*f2.target<int(*)(int)>() == g); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + std::function<int(int)> f1 = g; + std::function<int(int)> f2 = h; + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(*f1.target<int(*)(int)>() == g); + assert(*f2.target<int(*)(int)>() == h); + f1.swap(f2); + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); + assert(*f1.target<int(*)(int)>() == h); + assert(*f2.target<int(*)(int)>() == g); + } + assert(A::count == 0); + assert(globalMemCounter.checkOutstandingNewEq(0)); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.nullptr/operator_==.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.nullptr/operator_==.pass.cpp new file mode 100644 index 0000000000000..5bca0968702c0 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.nullptr/operator_==.pass.cpp @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template <MoveConstructible R, MoveConstructible ... ArgTypes> +// bool operator==(const function<R(ArgTypes...)>&, nullptr_t); +// +// template <MoveConstructible R, MoveConstructible ... ArgTypes> +// bool operator==(nullptr_t, const function<R(ArgTypes...)>&); +// +// template <MoveConstructible R, MoveConstructible ... ArgTypes> +// bool operator!=(const function<R(ArgTypes...)>&, nullptr_t); +// +// template <MoveConstructible R, MoveConstructible ... ArgTypes> +// bool operator!=(nullptr_t, const function<R(ArgTypes...)>&); + +#include <functional> +#include <cassert> + +int g(int) {return 0;} + +int main() +{ + { + std::function<int(int)> f; + assert(f == nullptr); + assert(nullptr == f); + f = g; + assert(f != nullptr); + assert(nullptr != f); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target.pass.cpp new file mode 100644 index 0000000000000..53476a2747357 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target.pass.cpp @@ -0,0 +1,89 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// template<typename T> +// requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R> +// T* +// target(); +// template<typename T> +// requires Callable<T, ArgTypes...> && Convertible<Callable<T, ArgTypes...>::result_type, R> +// const T* +// target() const; + +#include <functional> +#include <new> +#include <cstdlib> +#include <cassert> + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int foo(int) const {return 1;} +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + { + std::function<int(int)> f = A(); + assert(A::count == 1); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + { + std::function<int(int)> f = g; + assert(A::count == 0); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + } + assert(A::count == 0); + { + const std::function<int(int)> f = A(); + assert(A::count == 1); + assert(f.target<A>()); + assert(f.target<int(*)(int)>() == 0); + } + assert(A::count == 0); + { + const std::function<int(int)> f = g; + assert(A::count == 0); + assert(f.target<int(*)(int)>()); + assert(f.target<A>() == 0); + } + assert(A::count == 0); +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target_type.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target_type.pass.cpp new file mode 100644 index 0000000000000..7605e3bf666e8 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.targ/target_type.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// class function<R(ArgTypes...)> + +// const std::type_info& target_type() const; + +#include <functional> +#include <typeinfo> +#include <cassert> + +class A +{ + int data_[10]; +public: + static int count; + + A() + { + ++count; + for (int i = 0; i < 10; ++i) + data_[i] = i; + } + + A(const A&) {++count;} + + ~A() {--count;} + + int operator()(int i) const + { + for (int j = 0; j < 10; ++j) + i += data_[j]; + return i; + } + + int foo(int) const {return 1;} +}; + +int A::count = 0; + +int g(int) {return 0;} + +int main() +{ + { + std::function<int(int)> f = A(); + assert(f.target_type() == typeid(A)); + } + { + std::function<int(int)> f; + assert(f.target_type() == typeid(void)); + } +} diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/function_types.h b/test/std/utilities/function.objects/func.wrap/func.wrap.func/function_types.h new file mode 100644 index 0000000000000..55eb80f43ffe3 --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/function_types.h @@ -0,0 +1,57 @@ +#ifndef FUNCTION_TYPES_H +#define FUNCTION_TYPES_H + + +class FunctionObject +{ + int data_[10]; // dummy variable to prevent small object optimization in + // std::function +public: + static int count; + + FunctionObject() { + ++count; + for (int i = 0; i < 10; ++i) data_[i] = i; + } + + FunctionObject(const FunctionObject&) {++count;} + ~FunctionObject() {--count; ((void)data_); } + + int operator()() const { return 42; } + int operator()(int i) const { return i; } + int operator()(int i, int) const { return i; } + int operator()(int i, int, int) const { return i; } +}; + +int FunctionObject::count = 0; + +class MemFunClass +{ + int data_[10]; // dummy variable to prevent small object optimization in + // std::function +public: + static int count; + + MemFunClass() { + ++count; + for (int i = 0; i < 10; ++i) data_[i] = 0; + } + + MemFunClass(const MemFunClass&) {++count; ((void)data_); } + + ~MemFunClass() {--count;} + + int foo() const { return 42; } + int foo(int i) const { return i; } + int foo(int i, int) const { return i; } + int foo(int i, int, int) const { return i; } +}; + +int MemFunClass::count = 0; + +int FreeFunction() { return 42; } +int FreeFunction(int i) {return i;} +int FreeFunction(int i, int) { return i; } +int FreeFunction(int i, int, int) { return i; } + +#endif // FUNCTION_TYPES_H diff --git a/test/std/utilities/function.objects/func.wrap/func.wrap.func/types.pass.cpp b/test/std/utilities/function.objects/func.wrap/func.wrap.func/types.pass.cpp new file mode 100644 index 0000000000000..eb4eac65cd2cb --- /dev/null +++ b/test/std/utilities/function.objects/func.wrap/func.wrap.func/types.pass.cpp @@ -0,0 +1,108 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <functional> + +// template<Returnable R, CopyConstructible... ArgTypes> +// class function<R(ArgTypes...)> { +// public: +// typedef R result_type; +// typedef T1 argument_type; // iff sizeof...(ArgTypes) == 1 and +// // the type in ArgTypes is T1 +// typedef T1 first_argument_type; // iff sizeof...(ArgTypes) == 2 and +// // ArgTypes contains T1 and T2 +// typedef T2 second_argument_type; // iff sizeof...(ArgTypes) == 2 and +// // ArgTypes contains T1 and T2 +// ... +// }; + +#include <functional> +#include <type_traits> + + +template <typename T> +class has_argument_type +{ + typedef char yes; + typedef long no; + + template <typename C> static yes check( typename C::argument_type * ); + template <typename C> static no check(...); +public: + enum { value = sizeof(check<T>(0)) == sizeof(yes) }; +}; + +template <typename T> +class has_first_argument_type +{ + typedef char yes; + typedef long no; + + template <typename C> static yes check( typename C::first_argument_type * ); + template <typename C> static no check(...); +public: + enum { value = sizeof(check<T>(0)) == sizeof(yes) }; +}; + + +template <typename T> +class has_second_argument_type +{ + typedef char yes; + typedef long no; + + template <typename C> static yes check( typename C::second_argument_type *); + template <typename C> static no check(...); +public: + enum { value = sizeof(check<T>(0)) == sizeof(yes) }; +}; + +template <class F, class return_type> +void test_nullary_function () +{ + static_assert((std::is_same<typename F::result_type, return_type>::value), "" ); + static_assert((!has_argument_type<F>::value), "" ); + static_assert((!has_first_argument_type<F>::value), "" ); + static_assert((!has_second_argument_type<F>::value), "" ); +} + +template <class F, class return_type, class arg_type> +void test_unary_function () +{ + static_assert((std::is_same<typename F::result_type, return_type>::value), "" ); + static_assert((std::is_same<typename F::argument_type, arg_type>::value), "" ); + static_assert((!has_first_argument_type<F>::value), "" ); + static_assert((!has_second_argument_type<F>::value), "" ); +} + +template <class F, class return_type, class arg_type1, class arg_type2> +void test_binary_function () +{ + static_assert((std::is_same<typename F::result_type, return_type>::value), "" ); + static_assert((std::is_same<typename F::first_argument_type, arg_type1>::value), "" ); + static_assert((std::is_same<typename F::second_argument_type, arg_type2>::value), "" ); + static_assert((!has_argument_type<F>::value), "" ); +} + +template <class F, class return_type> +void test_other_function () +{ + static_assert((std::is_same<typename F::result_type, return_type>::value), "" ); + static_assert((!has_argument_type<F>::value), "" ); + static_assert((!has_first_argument_type<F>::value), "" ); + static_assert((!has_second_argument_type<F>::value), "" ); +} + +int main() +{ + test_nullary_function<std::function<int()>, int>(); + test_unary_function <std::function<double(int)>, double, int>(); + test_binary_function <std::function<double(int, char)>, double, int, char>(); + test_other_function <std::function<double(int, char, double)>, double>(); +} |