copy_assign.pass.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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& operator=(const function& f);
  12. #include <functional>
  13. #include <cassert>
  14. #include "test_macros.h"
  15. #include "count_new.hpp"
  16. class A {
  17. int data_[10];
  18. public:
  19. static int count;
  20. A() {
  21. ++count;
  22. for (int i = 0; i < 10; ++i)
  23. data_[i] = i;
  24. }
  25. A(const A &) { ++count; }
  26. ~A() { --count; }
  27. int operator()(int i) const {
  28. for (int j = 0; j < 10; ++j)
  29. i += data_[j];
  30. return i;
  31. }
  32. };
  33. int A::count = 0;
  34. int g0() { return 0; }
  35. int g(int) { return 0; }
  36. int g2(int, int) { return 2; }
  37. int g3(int, int, int) { return 3; }
  38. int main() {
  39. assert(globalMemCounter.checkOutstandingNewEq(0));
  40. {
  41. std::function<int(int)> f = A();
  42. assert(A::count == 1);
  43. assert(globalMemCounter.checkOutstandingNewEq(1));
  44. assert(f.target<A>());
  45. assert(f.target<int (*)(int)>() == 0);
  46. std::function<int(int)> f2;
  47. f2 = f;
  48. assert(A::count == 2);
  49. assert(globalMemCounter.checkOutstandingNewEq(2));
  50. assert(f2.target<A>());
  51. assert(f2.target<int (*)(int)>() == 0);
  52. }
  53. assert(A::count == 0);
  54. assert(globalMemCounter.checkOutstandingNewEq(0));
  55. {
  56. std::function<int(int)> f = g;
  57. assert(globalMemCounter.checkOutstandingNewEq(0));
  58. assert(f.target<int (*)(int)>());
  59. assert(f.target<A>() == 0);
  60. std::function<int(int)> f2;
  61. f2 = f;
  62. assert(globalMemCounter.checkOutstandingNewEq(0));
  63. assert(f2.target<int (*)(int)>());
  64. assert(f2.target<A>() == 0);
  65. }
  66. assert(globalMemCounter.checkOutstandingNewEq(0));
  67. {
  68. std::function<int(int)> f;
  69. assert(globalMemCounter.checkOutstandingNewEq(0));
  70. assert(f.target<int (*)(int)>() == 0);
  71. assert(f.target<A>() == 0);
  72. std::function<int(int)> f2;
  73. f2 = f;
  74. assert(globalMemCounter.checkOutstandingNewEq(0));
  75. assert(f2.target<int (*)(int)>() == 0);
  76. assert(f2.target<A>() == 0);
  77. }
  78. {
  79. typedef std::function<int()> Func;
  80. Func f = g0;
  81. Func& fr = (f = f);
  82. assert(&fr == &f);
  83. assert(*f.target<int(*)()>() == g0);
  84. }
  85. {
  86. typedef std::function<int(int)> Func;
  87. Func f = g;
  88. Func& fr = (f = f);
  89. assert(&fr == &f);
  90. assert(*f.target<int(*)(int)>() == g);
  91. }
  92. {
  93. typedef std::function<int(int, int)> Func;
  94. Func f = g2;
  95. Func& fr = (f = f);
  96. assert(&fr == &f);
  97. assert(*f.target<int(*)(int, int)>() == g2);
  98. }
  99. {
  100. typedef std::function<int(int, int, int)> Func;
  101. Func f = g3;
  102. Func& fr = (f = f);
  103. assert(&fr == &f);
  104. assert(*f.target<int(*)(int, int, int)>() == g3);
  105. }
  106. #if TEST_STD_VER >= 11
  107. assert(globalMemCounter.checkOutstandingNewEq(0));
  108. {
  109. std::function<int(int)> f = A();
  110. assert(A::count == 1);
  111. assert(globalMemCounter.checkOutstandingNewEq(1));
  112. assert(f.target<A>());
  113. assert(f.target<int (*)(int)>() == 0);
  114. std::function<int(int)> f2;
  115. f2 = std::move(f);
  116. assert(A::count == 1);
  117. assert(globalMemCounter.checkOutstandingNewEq(1));
  118. assert(f2.target<A>());
  119. assert(f2.target<int (*)(int)>() == 0);
  120. assert(f.target<A>() == 0);
  121. assert(f.target<int (*)(int)>() == 0);
  122. }
  123. #endif
  124. }