diff options
author | Eric Fiselier <eric@efcs.ca> | 2015-06-14 23:30:09 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2015-06-14 23:30:09 +0000 |
commit | b11df184ad642c8601d4073fff54974fd76b70c7 (patch) | |
tree | 2f8021a139db2aaafbcb564043a66556ba0505a0 /libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con | |
parent | 9a03362a08941620e3fb4f583b4c85bc6b4dd6e5 (diff) | |
download | bcm5719-llvm-b11df184ad642c8601d4073fff54974fd76b70c7.tar.gz bcm5719-llvm-b11df184ad642c8601d4073fff54974fd76b70c7.zip |
Fix std::function allocator constructors in C++03.
The C++03 version of function tried to default construct the allocator
in the uses allocator constructors when no allocation was performed. These
constructors would fail to compile when used with allocators that had no
default constructor.
llvm-svn: 239708
Diffstat (limited to 'libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con')
2 files changed, 149 insertions, 119 deletions
diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp index 741a3b9a955..352ecfc602b 100644 --- a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_F.pass.cpp @@ -17,84 +17,90 @@ #include <cassert> #include "min_allocator.h" +#include "test_allocator.h" +#include "count_new.hpp" +#include "../function_types.h" -class A -{ - int data_[10]; -public: - static int count; +class DummyClass {}; - A() +template <class FuncType, class AllocType> +void test_FunctionObject(AllocType& alloc) +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); { - ++count; - for (int i = 0; i < 10; ++i) - data_[i] = i; + 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)); +} - A(const A&) {++count;} - - ~A() {--count;} - int operator()(int i) const +template <class FuncType, class AllocType> +void test_FreeFunction(AllocType& alloc) +{ + assert(globalMemCounter.checkOutstandingNewEq(0)); { - for (int j = 0; j < 10; ++j) - i += data_[j]; - return i; + 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)); +} - int foo(int) const {return 1;} -}; +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)); +} -int A::count = 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); -int g(int) {return 0;} + test_FreeFunction<int()>(alloc); + test_FreeFunction<int(int)>(alloc); + test_FreeFunction<int(int, int)>(alloc); + test_FreeFunction<int(int, int, int)>(alloc); -class Foo { -public: - void bar(int k) { } -}; + 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() { { - std::function<int(int)> f(std::allocator_arg, bare_allocator<A>(), A()); - assert(A::count == 1); - assert(f.target<A>()); - assert(f.target<int(*)(int)>() == 0); - } - assert(A::count == 0); - { - std::function<int(int)> f(std::allocator_arg, bare_allocator<int(*)(int)>(), g); - assert(f.target<int(*)(int)>()); - assert(f.target<A>() == 0); - } - { - std::function<int(int)> f(std::allocator_arg, bare_allocator<int(*)(int)>(), - (int (*)(int))0); - assert(!f); - assert(f.target<int(*)(int)>() == 0); - assert(f.target<A>() == 0); - } - { - std::function<int(const A*, int)> f(std::allocator_arg, - bare_allocator<int(A::*)(int)const>(), - &A::foo); - assert(f); - assert(f.target<int (A::*)(int) const>() != 0); - } -#if __cplusplus >= 201103L - { - Foo f; - std::function<void(int)> fun = std::bind(&Foo::bar, &f, std::placeholders::_1); - fun(10); + bare_allocator<DummyClass> bare_alloc; + test_for_alloc(bare_alloc); } -#endif { - std::function<void(int)> fun(std::allocator_arg, - bare_allocator<int(*)(int)>(), - &g); - assert(fun); - assert(fun.target<int(*)(int)>() != 0); - fun(10); + non_default_test_allocator<DummyClass> non_default_alloc(42); + test_for_alloc(non_default_alloc); } } diff --git a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp index e89636a69d6..371eb98de1a 100644 --- a/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp +++ b/libcxx/test/std/utilities/function.objects/func.wrap/func.wrap.func/func.wrap.func.con/alloc_function.pass.cpp @@ -20,81 +20,105 @@ #include "min_allocator.h" #include "test_allocator.h" #include "count_new.hpp" +#include "../function_types.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 A::count = 0; +class DummyClass {}; -int g(int) {return 0;} - -int main() +template <class FuncType, class AllocType> +void test_FunctionObject(AllocType& alloc) { assert(globalMemCounter.checkOutstandingNewEq(0)); { - std::function<int(int)> f = A(); - assert(A::count == 1); + // Construct function from FunctionObject. + std::function<FuncType> f = FunctionObject(); + assert(FunctionObject::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>(), f); - assert(A::count == 2); + 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.target<A>()); - assert(f2.target<int(*)(int)>() == 0); + assert(f2.template target<FunctionObject>()); + assert(f2.template target<FuncType>() == 0); + assert(f2.template target<FuncType*>() == 0); } - assert(A::count == 0); + assert(FunctionObject::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(std::allocator_arg, bare_allocator<int(*)(int)>(), f); - assert(globalMemCounter.checkOutstandingNewEq(0)); - assert(f2.target<int(*)(int)>()); - assert(f2.target<A>() == 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)); - non_default_test_allocator<std::function<int(int)> > al(1); - std::function<int(int)> f2(std::allocator_arg, al, g); + 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.target<int(*)(int)>()); - assert(f2.target<A>() == 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)); { - std::function<int(int)> f; + // Construct function from function pointer. + TargetType target = &MemFunClass::foo; + std::function<FuncType> f = target; assert(globalMemCounter.checkOutstandingNewEq(0)); - assert(f.target<int(*)(int)>() == 0); - assert(f.target<A>() == 0); - std::function<int(int)> f2(std::allocator_arg, bare_allocator<int>(), f); + 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.target<int(*)(int)>() == 0); - assert(f2.target<A>() == 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); + } } |