alloc_function.pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 A> function(allocator_arg_t, const A&, const function&);
  13. #include <functional>
  14. #include <cassert>
  15. #include "min_allocator.h"
  16. #include "test_allocator.h"
  17. #include "count_new.hpp"
  18. #include "../function_types.h"
  19. class DummyClass {};
  20. template <class FuncType, class AllocType>
  21. void test_FunctionObject(AllocType& alloc)
  22. {
  23. assert(globalMemCounter.checkOutstandingNewEq(0));
  24. {
  25. // Construct function from FunctionObject.
  26. std::function<FuncType> f = FunctionObject();
  27. assert(FunctionObject::count == 1);
  28. assert(globalMemCounter.checkOutstandingNewEq(1));
  29. assert(f.template target<FunctionObject>());
  30. assert(f.template target<FuncType>() == 0);
  31. assert(f.template target<FuncType*>() == 0);
  32. // Copy function with allocator
  33. std::function<FuncType> f2(std::allocator_arg, alloc, f);
  34. assert(FunctionObject::count == 2);
  35. assert(globalMemCounter.checkOutstandingNewEq(2));
  36. assert(f2.template target<FunctionObject>());
  37. assert(f2.template target<FuncType>() == 0);
  38. assert(f2.template target<FuncType*>() == 0);
  39. }
  40. assert(FunctionObject::count == 0);
  41. assert(globalMemCounter.checkOutstandingNewEq(0));
  42. }
  43. template <class FuncType, class AllocType>
  44. void test_FreeFunction(AllocType& alloc)
  45. {
  46. assert(globalMemCounter.checkOutstandingNewEq(0));
  47. {
  48. // Construct function from function pointer.
  49. FuncType* target = &FreeFunction;
  50. std::function<FuncType> f = target;
  51. assert(globalMemCounter.checkOutstandingNewEq(0));
  52. assert(f.template target<FuncType*>());
  53. assert(*f.template target<FuncType*>() == target);
  54. assert(f.template target<FuncType>() == 0);
  55. // Copy function with allocator
  56. std::function<FuncType> f2(std::allocator_arg, alloc, f);
  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. }
  62. assert(globalMemCounter.checkOutstandingNewEq(0));
  63. }
  64. template <class TargetType, class FuncType, class AllocType>
  65. void test_MemFunClass(AllocType& alloc)
  66. {
  67. assert(globalMemCounter.checkOutstandingNewEq(0));
  68. {
  69. // Construct function from function pointer.
  70. TargetType target = &MemFunClass::foo;
  71. std::function<FuncType> f = target;
  72. assert(globalMemCounter.checkOutstandingNewEq(0));
  73. assert(f.template target<TargetType>());
  74. assert(*f.template target<TargetType>() == target);
  75. assert(f.template target<FuncType*>() == 0);
  76. // Copy function with allocator
  77. std::function<FuncType> f2(std::allocator_arg, alloc, f);
  78. assert(globalMemCounter.checkOutstandingNewEq(0));
  79. assert(f2.template target<TargetType>());
  80. assert(*f2.template target<TargetType>() == target);
  81. assert(f2.template target<FuncType*>() == 0);
  82. }
  83. assert(globalMemCounter.checkOutstandingNewEq(0));
  84. }
  85. template <class Alloc>
  86. void test_for_alloc(Alloc& alloc)
  87. {
  88. // Large FunctionObject -- Allocation should occur
  89. test_FunctionObject<int()>(alloc);
  90. test_FunctionObject<int(int)>(alloc);
  91. test_FunctionObject<int(int, int)>(alloc);
  92. test_FunctionObject<int(int, int, int)>(alloc);
  93. // Free function -- No allocation should occur
  94. test_FreeFunction<int()>(alloc);
  95. test_FreeFunction<int(int)>(alloc);
  96. test_FreeFunction<int(int, int)>(alloc);
  97. test_FreeFunction<int(int, int, int)>(alloc);
  98. // Member function -- No allocation should occur.
  99. test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
  100. test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
  101. test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
  102. }
  103. int main()
  104. {
  105. {
  106. bare_allocator<DummyClass> alloc;
  107. test_for_alloc(alloc);
  108. }
  109. {
  110. non_default_test_allocator<DummyClass> alloc(42);
  111. test_for_alloc(alloc);
  112. }
  113. }