alloc_F.pass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 F, class A> function(allocator_arg_t, const A&, F);
  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. FunctionObject target;
  25. assert(FunctionObject::count == 1);
  26. assert(globalMemCounter.checkOutstandingNewEq(0));
  27. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  28. assert(FunctionObject::count == 2);
  29. assert(globalMemCounter.checkOutstandingNewEq(1));
  30. assert(f2.template target<FunctionObject>());
  31. assert(f2.template target<FuncType>() == 0);
  32. assert(f2.template target<FuncType*>() == 0);
  33. }
  34. assert(FunctionObject::count == 0);
  35. assert(globalMemCounter.checkOutstandingNewEq(0));
  36. }
  37. template <class FuncType, class AllocType>
  38. void test_FreeFunction(AllocType& alloc)
  39. {
  40. assert(globalMemCounter.checkOutstandingNewEq(0));
  41. {
  42. FuncType* target = &FreeFunction;
  43. assert(globalMemCounter.checkOutstandingNewEq(0));
  44. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  45. assert(globalMemCounter.checkOutstandingNewEq(0));
  46. assert(f2.template target<FuncType*>());
  47. assert(*f2.template target<FuncType*>() == target);
  48. assert(f2.template target<FuncType>() == 0);
  49. assert(f2.template target<DummyClass>() == 0);
  50. }
  51. assert(globalMemCounter.checkOutstandingNewEq(0));
  52. }
  53. template <class TargetType, class FuncType, class AllocType>
  54. void test_MemFunClass(AllocType& alloc)
  55. {
  56. assert(globalMemCounter.checkOutstandingNewEq(0));
  57. {
  58. TargetType target = &MemFunClass::foo;
  59. assert(globalMemCounter.checkOutstandingNewEq(0));
  60. std::function<FuncType> f2(std::allocator_arg, alloc, target);
  61. assert(globalMemCounter.checkOutstandingNewEq(0));
  62. assert(f2.template target<TargetType>());
  63. assert(*f2.template target<TargetType>() == target);
  64. assert(f2.template target<FuncType*>() == 0);
  65. }
  66. assert(globalMemCounter.checkOutstandingNewEq(0));
  67. }
  68. template <class Alloc>
  69. void test_for_alloc(Alloc& alloc) {
  70. test_FunctionObject<int()>(alloc);
  71. test_FunctionObject<int(int)>(alloc);
  72. test_FunctionObject<int(int, int)>(alloc);
  73. test_FunctionObject<int(int, int, int)>(alloc);
  74. test_FreeFunction<int()>(alloc);
  75. test_FreeFunction<int(int)>(alloc);
  76. test_FreeFunction<int(int, int)>(alloc);
  77. test_FreeFunction<int(int, int, int)>(alloc);
  78. test_MemFunClass<int(MemFunClass::*)() const, int(MemFunClass&)>(alloc);
  79. test_MemFunClass<int(MemFunClass::*)(int) const, int(MemFunClass&, int)>(alloc);
  80. test_MemFunClass<int(MemFunClass::*)(int, int) const, int(MemFunClass&, int, int)>(alloc);
  81. }
  82. int main()
  83. {
  84. {
  85. bare_allocator<DummyClass> bare_alloc;
  86. test_for_alloc(bare_alloc);
  87. }
  88. {
  89. non_default_test_allocator<DummyClass> non_default_alloc(42);
  90. test_for_alloc(non_default_alloc);
  91. }
  92. }