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