uninitialized_move.pass.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14
  9. // <memory>
  10. // template <class InputIt, class ForwardIt>
  11. // ForwardIt uninitialized_move(InputIt, InputIt, ForwardIt);
  12. #include <memory>
  13. #include <cstdlib>
  14. #include <cassert>
  15. #include "test_macros.h"
  16. #include "test_iterators.h"
  17. struct Counted {
  18. static int count;
  19. static int constructed;
  20. static void reset() { count = constructed = 0; }
  21. explicit Counted(int&& x) : value(x) { x = 0; ++count; ++constructed; }
  22. Counted(Counted const&) { assert(false); }
  23. ~Counted() { assert(count > 0); --count; }
  24. friend void operator&(Counted) = delete;
  25. int value;
  26. };
  27. int Counted::count = 0;
  28. int Counted::constructed = 0;
  29. struct ThrowsCounted {
  30. static int count;
  31. static int constructed;
  32. static int throw_after;
  33. static void reset() { throw_after = count = constructed = 0; }
  34. explicit ThrowsCounted(int&& x) {
  35. ++constructed;
  36. if (throw_after > 0 && --throw_after == 0) {
  37. TEST_THROW(1);
  38. }
  39. ++count;
  40. x = 0;
  41. }
  42. ThrowsCounted(ThrowsCounted const&) { assert(false); }
  43. ~ThrowsCounted() { assert(count > 0); --count; }
  44. friend void operator&(ThrowsCounted) = delete;
  45. };
  46. int ThrowsCounted::count = 0;
  47. int ThrowsCounted::constructed = 0;
  48. int ThrowsCounted::throw_after = 0;
  49. void test_ctor_throws()
  50. {
  51. #ifndef TEST_HAS_NO_EXCEPTIONS
  52. using It = forward_iterator<ThrowsCounted*>;
  53. const int N = 5;
  54. int values[N] = {1, 2, 3, 4, 5};
  55. alignas(ThrowsCounted) char pool[sizeof(ThrowsCounted)*N] = {};
  56. ThrowsCounted* p = (ThrowsCounted*)pool;
  57. try {
  58. ThrowsCounted::throw_after = 4;
  59. std::uninitialized_move(values, values + N, It(p));
  60. assert(false);
  61. } catch (...) {}
  62. assert(ThrowsCounted::count == 0);
  63. assert(ThrowsCounted::constructed == 4); // forth construction throws
  64. assert(values[0] == 0);
  65. assert(values[1] == 0);
  66. assert(values[2] == 0);
  67. assert(values[3] == 4);
  68. assert(values[4] == 5);
  69. #endif
  70. }
  71. void test_counted()
  72. {
  73. using It = input_iterator<int*>;
  74. using FIt = forward_iterator<Counted*>;
  75. const int N = 5;
  76. int values[N] = {1, 2, 3, 4, 5};
  77. alignas(Counted) char pool[sizeof(Counted)*N] = {};
  78. Counted* p = (Counted*)pool;
  79. auto ret = std::uninitialized_move(It(values), It(values + 1), FIt(p));
  80. assert(ret == FIt(p +1));
  81. assert(Counted::constructed == 1);
  82. assert(Counted::count == 1);
  83. assert(p[0].value == 1);
  84. assert(values[0] == 0);
  85. ret = std::uninitialized_move(It(values+1), It(values+N), FIt(p+1));
  86. assert(ret == FIt(p + N));
  87. assert(Counted::count == 5);
  88. assert(Counted::constructed == 5);
  89. assert(p[1].value == 2);
  90. assert(p[2].value == 3);
  91. assert(p[3].value == 4);
  92. assert(p[4].value == 5);
  93. assert(values[1] == 0);
  94. assert(values[2] == 0);
  95. assert(values[3] == 0);
  96. assert(values[4] == 0);
  97. std::destroy(p, p+N);
  98. assert(Counted::count == 0);
  99. }
  100. int main(int, char**) {
  101. test_counted();
  102. test_ctor_throws();
  103. return 0;
  104. }