alloc_F.pass.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <functional>
  10. // REQUIRES-ANY: c++98, c++03, c++11, c++14
  11. // class function<R(ArgTypes...)>
  12. // template<class F, class A> function(allocator_arg_t, const A&, F);
  13. #include <functional>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "min_allocator.h"
  17. #include "test_allocator.h"
  18. #include "count_new.hpp"
  19. #include "../function_types.h"
  20. #if TEST_STD_VER >= 11
  21. struct RValueCallable {
  22. template <class ...Args>
  23. void operator()(Args&&...) && {}
  24. };
  25. struct LValueCallable {
  26. template <class ...Args>
  27. void operator()(Args&&...) & {}
  28. };
  29. #endif
  30. class DummyClass {};
  31. template <class FuncType, class AllocType>
  32. void test_FunctionObject(AllocType& alloc)
  33. {
  34. assert(globalMemCounter.checkOutstandingNewEq(0));
  35. {
  36. FunctionObject target;
  37. assert(FunctionObject::count == 1);
  38. assert(globalMemCounter.checkOutstandingNewEq(0));
  39. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  40. assert(FunctionObject::count == 2);
  41. assert(globalMemCounter.checkOutstandingNewEq(1));
  42. assert(f2.template target<FunctionObject>());
  43. assert(f2.template target<FuncType>() == 0);
  44. assert(f2.template target<FuncType*>() == 0);
  45. }
  46. assert(FunctionObject::count == 0);
  47. assert(globalMemCounter.checkOutstandingNewEq(0));
  48. }
  49. template <class FuncType, class AllocType>
  50. void test_FreeFunction(AllocType& alloc)
  51. {
  52. assert(globalMemCounter.checkOutstandingNewEq(0));
  53. {
  54. FuncType* target = &FreeFunction;
  55. assert(globalMemCounter.checkOutstandingNewEq(0));
  56. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  57. assert(globalMemCounter.checkOutstandingNewEq(0));
  58. assert(f2.template target<FuncType*>());
  59. assert(*f2.template target<FuncType*>() == target);
  60. assert(f2.template target<FuncType>() == 0);
  61. assert(f2.template target<DummyClass>() == 0);
  62. }
  63. assert(globalMemCounter.checkOutstandingNewEq(0));
  64. }
  65. template <class TargetType, class FuncType, class AllocType>
  66. void test_MemFunClass(AllocType& alloc)
  67. {
  68. assert(globalMemCounter.checkOutstandingNewEq(0));
  69. {
  70. TargetType target = &MemFunClass::foo;
  71. assert(globalMemCounter.checkOutstandingNewEq(0));
  72. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  73. assert(globalMemCounter.checkOutstandingNewEq(0));
  74. assert(f2.template target<TargetType>());
  75. assert(*f2.template target<TargetType>() == target);
  76. assert(f2.template target<FuncType*>() == 0);
  77. }
  78. assert(globalMemCounter.checkOutstandingNewEq(0));
  79. }
  80. template <class Alloc>
  81. void test_for_alloc(Alloc& alloc) {
  82. test_FunctionObject<int()>(alloc);
  83. test_FunctionObject<int(int)>(alloc);
  84. test_FunctionObject<int(int, int)>(alloc);
  85. test_FunctionObject<int(int, int, int)>(alloc);
  86. test_FreeFunction<int()>(alloc);
  87. test_FreeFunction<int(int)>(alloc);
  88. test_FreeFunction<int(int, int)>(alloc);
  89. test_FreeFunction<int(int, int, int)>(alloc);
  90. test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
  91. test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
  92. test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
  93. }
  94. int main()
  95. {
  96. {
  97. bare_allocator<DummyClass> bare_alloc;
  98. test_for_alloc(bare_alloc);
  99. }
  100. {
  101. non_default_test_allocator<DummyClass> non_default_alloc(42);
  102. test_for_alloc(non_default_alloc);
  103. }
  104. #if TEST_STD_VER >= 11
  105. {
  106. using Fn = std::function<void(int, int, int)>;
  107. static_assert(std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, LValueCallable&>::value, "");
  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>, RValueCallable&>::value, "");
  110. static_assert(!std::is_constructible<Fn, std::allocator_arg_t, std::allocator<int>, RValueCallable>::value, "");
  111. }
  112. #endif
  113. }