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 Size, class ForwardIt>
14 // pair<InputIt, ForwardIt> uninitialized_move_n(InputIt, Size, 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_n(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_n(It(values), 1, FIt(p));
89 assert(ret.first == It(values +1));
90 assert(ret.second == FIt(p +1));
91 assert(Counted::constructed == 1);
92 assert(Counted::count == 1);
93 assert(p[0].value == 1);
94 assert(values[0] == 0);
95 ret = std::uninitialized_move_n(It(values+1), N-1, FIt(p+1));
96 assert(ret.first == It(values+N));
97 assert(ret.second == FIt(p + N));
98 assert(Counted::count == 5);
99 assert(Counted::constructed == 5);
100 assert(p[1].value == 2);
101 assert(p[2].value == 3);
102 assert(p[3].value == 4);
103 assert(p[4].value == 5);
104 assert(values[1] == 0);
105 assert(values[2] == 0);
106 assert(values[3] == 0);
107 assert(values[4] == 0);
108 std::destroy(p, p+N);
109 assert(Counted::count == 0);
110 }
111
main(int,char **)112 int main(int, char**)
113 {
114 test_counted();
115 test_ctor_throws();
116
117 return 0;
118 }
119