From 51072bd6bf79ef2bc6a922079bff57c31c1effbc Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sat, 23 Jul 2016 20:47:26 +0000 Subject: Vendor import of libc++ release_39 branch r276489: https://llvm.org/svn/llvm-project/libcxx/branches/release_39@276489 --- .../func.wrap.func.con/alloc_rfunction.pass.cpp | 66 +++++++++++++++++----- 1 file changed, 52 insertions(+), 14 deletions(-) (limited to 'test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_rfunction.pass.cpp') 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 index aa6b743b5236..403d646f4216 100644 --- 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 @@ -7,6 +7,8 @@ // //===----------------------------------------------------------------------===// +// UNSUPPORTED: c++98, c++03 + // // class function @@ -14,8 +16,10 @@ // template function(allocator_arg_t, const A&, function&&); #include +#include #include +#include "test_macros.h" #include "min_allocator.h" #include "count_new.hpp" @@ -46,23 +50,57 @@ public: int A::count = 0; +int g(int) { return 0; } + int main() { -#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES assert(globalMemCounter.checkOutstandingNewEq(0)); { - std::function f = A(); - assert(A::count == 1); - assert(globalMemCounter.checkOutstandingNewEq(1)); - assert(f.target()); - assert(f.target() == 0); - std::function f2(std::allocator_arg, bare_allocator(), std::move(f)); - assert(A::count == 1); - assert(globalMemCounter.checkOutstandingNewEq(1)); - assert(f2.target()); - assert(f2.target() == 0); - assert(f.target() == 0); - assert(f.target() == 0); + std::function f = A(); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f.target()); + assert(f.target() == 0); + std::function f2(std::allocator_arg, bare_allocator(), std::move(f)); + assert(A::count == 1); + assert(globalMemCounter.checkOutstandingNewEq(1)); + assert(f2.target()); + assert(f2.target() == 0); + assert(f.target() == 0); + assert(f.target() == 0); + } + assert(globalMemCounter.checkOutstandingNewEq(0)); + { + // Test that moving a function constructed from a reference wrapper + // is done without allocating. + DisableAllocationGuard g; + using Ref = std::reference_wrapper; + A a; + Ref aref(a); + std::function f(aref); + assert(A::count == 1); + assert(f.target() == nullptr); + assert(f.target()); + std::function f2(std::allocator_arg, std::allocator{}, + std::move(f)); + assert(A::count == 1); + assert(f2.target() == nullptr); + assert(f2.target()); + assert(f.target()); // f is unchanged because the target is small + } + { + // Test that moving a function constructed from a function pointer + // is done without allocating + DisableAllocationGuard guard; + using Ptr = int(*)(int); + Ptr p = g; + std::function f(p); + assert(f.target() == nullptr); + assert(f.target()); + std::function f2(std::allocator_arg, std::allocator(), + std::move(f)); + assert(f2.target() == nullptr); + assert(f2.target()); + assert(f.target()); // f is unchanged because the target is small } -#endif // _LIBCPP_HAS_NO_RVALUE_REFERENCES } -- cgit v1.3