copy_move.pass.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. // <functional>
  9. // class function<R(ArgTypes...)>
  10. // function(const function& f);
  11. // function(function&& f); // noexcept in C++20
  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(int, char**)
  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. LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  99. #if TEST_STD_VER > 17
  100. ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  101. #endif
  102. std::function<int(int)> f2 = std::move(f);
  103. assert(A::count == 1);
  104. assert(globalMemCounter.checkOutstandingNewEq(1));
  105. assert(f2.target<A>());
  106. assert(f2.target<int(*)(int)>() == 0);
  107. assert(f.target<A>() == 0);
  108. assert(f.target<int(*)(int)>() == 0);
  109. }
  110. assert(globalMemCounter.checkOutstandingNewEq(0));
  111. {
  112. // Test that moving a function constructed from a reference wrapper
  113. // is done without allocating.
  114. DisableAllocationGuard g;
  115. using Ref = std::reference_wrapper<A>;
  116. A a;
  117. Ref aref(a);
  118. std::function<int(int)> f(aref);
  119. assert(A::count == 1);
  120. assert(f.target<A>() == nullptr);
  121. assert(f.target<Ref>());
  122. LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  123. #if TEST_STD_VER > 17
  124. ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  125. #endif
  126. std::function<int(int)> f2(std::move(f));
  127. assert(A::count == 1);
  128. assert(f2.target<A>() == nullptr);
  129. assert(f2.target<Ref>());
  130. LIBCPP_ASSERT(f.target<Ref>()); // f is unchanged because the target is small
  131. }
  132. {
  133. // Test that moving a function constructed from a function pointer
  134. // is done without allocating
  135. DisableAllocationGuard guard;
  136. using Ptr = int(*)(int);
  137. Ptr p = g;
  138. std::function<int(int)> f(p);
  139. assert(f.target<A>() == nullptr);
  140. assert(f.target<Ptr>());
  141. LIBCPP_ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  142. #if TEST_STD_VER > 17
  143. ASSERT_NOEXCEPT(std::function<int(int)>(std::move(f)));
  144. #endif
  145. std::function<int(int)> f2(std::move(f));
  146. assert(f2.target<A>() == nullptr);
  147. assert(f2.target<Ptr>());
  148. LIBCPP_ASSERT(f.target<Ptr>()); // f is unchanged because the target is small
  149. }
  150. #endif // TEST_STD_VER >= 11
  151. return 0;
  152. }