alloc_F.pass.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <functional>
  9. // REQUIRES: c++98 || c++03 || c++11 || c++14
  10. // class function<R(ArgTypes...)>
  11. // template<class F, class A> function(allocator_arg_t, const A&, F);
  12. #include <functional>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "min_allocator.h"
  16. #include "test_allocator.h"
  17. #include "count_new.hpp"
  18. #include "../function_types.h"
  19. #if TEST_STD_VER >= 11
  20. struct RValueCallable {
  21. template <class ...Args>
  22. void operator()(Args&&...) && {}
  23. };
  24. struct LValueCallable {
  25. template <class ...Args>
  26. void operator()(Args&&...) & {}
  27. };
  28. #endif
  29. class DummyClass {};
  30. template <class FuncType, class AllocType>
  31. void test_FunctionObject(AllocType& alloc)
  32. {
  33. assert(globalMemCounter.checkOutstandingNewEq(0));
  34. {
  35. FunctionObject target;
  36. assert(FunctionObject::count == 1);
  37. assert(globalMemCounter.checkOutstandingNewEq(0));
  38. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  39. assert(FunctionObject::count == 2);
  40. assert(globalMemCounter.checkOutstandingNewEq(1));
  41. assert(f2.template target<FunctionObject>());
  42. assert(f2.template target<FuncType>() == 0);
  43. assert(f2.template target<FuncType*>() == 0);
  44. }
  45. assert(FunctionObject::count == 0);
  46. assert(globalMemCounter.checkOutstandingNewEq(0));
  47. }
  48. template <class FuncType, class AllocType>
  49. void test_FreeFunction(AllocType& alloc)
  50. {
  51. assert(globalMemCounter.checkOutstandingNewEq(0));
  52. {
  53. FuncType* target = &FreeFunction;
  54. assert(globalMemCounter.checkOutstandingNewEq(0));
  55. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  56. // The allocator may not fit in the small object buffer, if we allocated
  57. // check it was done via the allocator.
  58. assert(globalMemCounter.checkOutstandingNewEq(test_alloc_base::alloc_count));
  59. assert(f2.template target<FuncType*>());
  60. assert(*f2.template target<FuncType*>() == target);
  61. assert(f2.template target<FuncType>() == 0);
  62. assert(f2.template target<DummyClass>() == 0);
  63. }
  64. assert(globalMemCounter.checkOutstandingNewEq(0));
  65. }
  66. template <class TargetType, class FuncType, class AllocType>
  67. void test_MemFunClass(AllocType& alloc)
  68. {
  69. assert(globalMemCounter.checkOutstandingNewEq(0));
  70. {
  71. TargetType target = &MemFunClass::foo;
  72. assert(globalMemCounter.checkOutstandingNewEq(0));
  73. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  74. assert(globalMemCounter.checkOutstandingNewEq(test_alloc_base::alloc_count));
  75. assert(f2.template target<TargetType>());
  76. assert(*f2.template target<TargetType>() == target);
  77. assert(f2.template target<FuncType*>() == 0);
  78. }
  79. assert(globalMemCounter.checkOutstandingNewEq(0));
  80. }
  81. template <class Alloc>
  82. void test_for_alloc(Alloc& alloc) {
  83. test_FunctionObject<int()>(alloc);
  84. test_FunctionObject<int(int)>(alloc);
  85. test_FunctionObject<int(int, int)>(alloc);
  86. test_FunctionObject<int(int, int, int)>(alloc);
  87. test_FreeFunction<int()>(alloc);
  88. test_FreeFunction<int(int)>(alloc);
  89. test_FreeFunction<int(int, int)>(alloc);
  90. test_FreeFunction<int(int, int, int)>(alloc);
  91. test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
  92. test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
  93. test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
  94. }
  95. int main(int, char**)
  96. {
  97. {
  98. bare_allocator<DummyClass> bare_alloc;
  99. test_for_alloc(bare_alloc);
  100. }
  101. {
  102. non_default_test_allocator<DummyClass> non_default_alloc(42);
  103. test_for_alloc(non_default_alloc);
  104. }
  105. #if TEST_STD_VER >= 11
  106. {
  107. using Fn = std::function<void(int, int, int)>;
  108. static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable&>::value, "");
  109. static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable>::value, "");
  110. static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable&>::value, "");
  111. static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable>::value, "");
  112. }
  113. #endif
  114. return 0;
  115. }