alloc_function.pass.cpp 4.2 KB

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