copy_move.pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // function(const function& f);
  12. #include <functional>
  13. #include <memory>
  14. #include <cstdlib>
  15. #include <cassert>
  16. #include "test_macros.h"
  17. #include "count_new.hpp"
  18. class A
  19. {
  20. int data_[10];
  21. public:
  22. static int count;
  23. A()
  24. {
  25. ++count;
  26. for (int i = 0; i < 10; ++i)
  27. data_[i] = i;
  28. }
  29. A(const A&) {++count;}
  30. ~A() {--count;}
  31. int operator()(int i) const
  32. {
  33. for (int j = 0; j < 10; ++j)
  34. i += data_[j];
  35. return i;
  36. }
  37. };
  38. int A::count = 0;
  39. int g(int) {return 0;}
  40. int main()
  41. {
  42. assert(globalMemCounter.checkOutstandingNewEq(0));
  43. {
  44. std::function<int(int)> f = A();
  45. assert(A::count == 1);
  46. assert(globalMemCounter.checkOutstandingNewEq(1));
  47. assert(f.target<A>());
  48. assert(f.target<int(*)(int)>() == 0);
  49. std::function<int(int)> f2 = f;
  50. assert(A::count == 2);
  51. assert(globalMemCounter.checkOutstandingNewEq(2));
  52. assert(f2.target<A>());
  53. assert(f2.target<int(*)(int)>() == 0);
  54. }
  55. assert(A::count == 0);
  56. assert(globalMemCounter.checkOutstandingNewEq(0));
  57. {
  58. std::function<int(int)> f = g;
  59. assert(globalMemCounter.checkOutstandingNewEq(0));
  60. assert(f.target<int(*)(int)>());
  61. assert(f.target<A>() == 0);
  62. std::function<int(int)> f2 = f;
  63. assert(globalMemCounter.checkOutstandingNewEq(0));
  64. assert(f2.target<int(*)(int)>());
  65. assert(f2.target<A>() == 0);
  66. }
  67. assert(globalMemCounter.checkOutstandingNewEq(0));
  68. {
  69. std::function<int(int)> f;
  70. assert(globalMemCounter.checkOutstandingNewEq(0));
  71. assert(f.target<int(*)(int)>() == 0);
  72. assert(f.target<A>() == 0);
  73. std::function<int(int)> f2 = f;
  74. assert(globalMemCounter.checkOutstandingNewEq(0));
  75. assert(f2.target<int(*)(int)>() == 0);
  76. assert(f2.target<A>() == 0);
  77. }
  78. {
  79. std::function<int(int)> f;
  80. assert(globalMemCounter.checkOutstandingNewEq(0));
  81. assert(f.target<int(*)(int)>() == 0);
  82. assert(f.target<A>() == 0);
  83. assert(!f);
  84. std::function<long(int)> g = f;
  85. assert(globalMemCounter.checkOutstandingNewEq(0));
  86. assert(g.target<long(*)(int)>() == 0);
  87. assert(g.target<A>() == 0);
  88. assert(!g);
  89. }
  90. #if TEST_STD_VER >= 11
  91. assert(globalMemCounter.checkOutstandingNewEq(0));
  92. { // Test rvalue references
  93. std::function<int(int)> f = A();
  94. assert(A::count == 1);
  95. assert(globalMemCounter.checkOutstandingNewEq(1));
  96. assert(f.target<A>());
  97. assert(f.target<int(*)(int)>() == 0);
  98. std::function<int(int)> f2 = std::move(f);
  99. assert(A::count == 1);
  100. assert(globalMemCounter.checkOutstandingNewEq(1));
  101. assert(f2.target<A>());
  102. assert(f2.target<int(*)(int)>() == 0);
  103. assert(f.target<A>() == 0);
  104. assert(f.target<int(*)(int)>() == 0);
  105. }
  106. assert(globalMemCounter.checkOutstandingNewEq(0));
  107. {
  108. // Test that moving a function constructed from a reference wrapper
  109. // is done without allocating.
  110. DisableAllocationGuard g;
  111. using Ref = std::reference_wrapper<A>;
  112. A a;
  113. Ref aref(a);
  114. std::function<int(int)> f(aref);
  115. assert(A::count == 1);
  116. assert(f.target<A>() == nullptr);
  117. assert(f.target<Ref>());
  118. std::function<int(int)> f2(std::move(f));
  119. assert(A::count == 1);
  120. assert(f2.target<A>() == nullptr);
  121. assert(f2.target<Ref>());
  122. LIBCPP_ASSERT(f.target<Ref>()); // f is unchanged because the target is small
  123. }
  124. {
  125. // Test that moving a function constructed from a function pointer
  126. // is done without allocating
  127. DisableAllocationGuard guard;
  128. using Ptr = int(*)(int);
  129. Ptr p = g;
  130. std::function<int(int)> f(p);
  131. assert(f.target<A>() == nullptr);
  132. assert(f.target<Ptr>());
  133. std::function<int(int)> f2(std::move(f));
  134. assert(f2.target<A>() == nullptr);
  135. assert(f2.target<Ptr>());
  136. LIBCPP_ASSERT(f.target<Ptr>()); // f is unchanged because the target is small
  137. }
  138. #endif // TEST_STD_VER >= 11
  139. }