]>
Commit | Line | Data |
---|---|---|
99dee823 | 1 | // Copyright (C) 2020-2021 Free Software Foundation, Inc. |
613c932f PP |
2 | // This file is part of the GNU ISO C++ Library. This library is free |
3 | // software; you can redistribute it and/or modify it under the | |
4 | // terms of the GNU General Public License as published by the | |
5 | // Free Software Foundation; either version 3, or (at your option) | |
6 | // any later version. | |
7 | ||
8 | // This library is distributed in the hope that it will be useful, | |
9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | // GNU General Public License for more details. | |
12 | ||
13 | // You should have received a copy of the GNU General Public License along | |
14 | // with this library; see the file COPYING3. If not see | |
15 | // <http://www.gnu.org/licenses/>. | |
16 | ||
17 | // { dg-options "-std=gnu++2a" } | |
18 | // { dg-do run { target c++2a } } | |
19 | ||
20 | #include <algorithm> | |
21 | #include <cstring> | |
22 | #include <deque> | |
23 | #include <list> | |
24 | #include <memory> | |
25 | #include <span> | |
26 | #include <string> | |
27 | #include <vector> | |
28 | ||
29 | #include <testsuite_hooks.h> | |
30 | #include <testsuite_iterators.h> | |
31 | ||
32 | using __gnu_test::test_input_range; | |
33 | using __gnu_test::test_forward_range; | |
144dfc68 PP |
34 | using __gnu_test::test_range; |
35 | using __gnu_test::test_sized_range_sized_sent; | |
36 | using __gnu_test::input_iterator_wrapper_nocopy; | |
613c932f PP |
37 | |
38 | namespace ranges = std::ranges; | |
39 | ||
40 | template<typename T> | |
41 | void | |
42 | test01(std::vector<T> ix) | |
43 | { | |
44 | static_assert(std::move_constructible<T>); | |
45 | static_assert(std::equality_comparable<T>); | |
46 | ||
47 | const auto saved_ix = ix; | |
48 | ||
49 | for (int k = 0; k < 7; k++) | |
50 | { | |
51 | ix = saved_ix; | |
52 | ||
53 | int size = ix.size(); | |
54 | auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]); | |
55 | std::span<T> rx((T *)buffer.get(), size); | |
56 | ||
247f410b | 57 | ranges::uninitialized_move_result res = {ix.begin(), rx.begin()}; |
613c932f PP |
58 | if (k == 0) |
59 | res = ranges::uninitialized_move(ix.begin(), ix.end(), | |
60 | rx.begin(), rx.end()); | |
61 | else if (k == 1) | |
62 | res = ranges::uninitialized_move(ix, rx); | |
63 | else if (k == 2) | |
64 | res = ranges::uninitialized_move_n(ix.begin(), size, | |
65 | rx.begin(), rx.end()); | |
66 | else if (k == 3) | |
67 | res = ranges::uninitialized_move(ix.begin(), ix.end(), | |
247f410b | 68 | rx.begin(), rx.end()); |
613c932f PP |
69 | else if (k == 4) |
70 | res = ranges::uninitialized_move(ix, std::as_const(rx)); | |
71 | else if (k == 5) | |
72 | res = ranges::uninitialized_move_n(ix.begin(), size, | |
247f410b | 73 | rx.begin(), rx.end()); |
613c932f PP |
74 | else if (k == 6) |
75 | res = ranges::uninitialized_move_n(ix.begin(), size/2, | |
247f410b | 76 | rx.begin(), rx.end()); |
613c932f PP |
77 | else if (k == 7) |
78 | res = ranges::uninitialized_move_n(ix.begin(), size, | |
247f410b | 79 | rx.begin(), rx.begin()+size/2); |
613c932f PP |
80 | else |
81 | __builtin_abort(); | |
82 | ||
83 | if (k == 6 || k == 7) | |
84 | { | |
247f410b JW |
85 | VERIFY( ranges::distance(ix.begin(), res.in) == size/2 ); |
86 | VERIFY( ranges::distance(rx.begin(), res.out) == size/2 ); | |
613c932f PP |
87 | VERIFY( ranges::equal(saved_ix.begin(), saved_ix.begin()+size/2, |
88 | rx.begin(), rx.begin()+size/2) ); | |
89 | ranges::destroy(rx.begin(), rx.begin()+size/2); | |
90 | } | |
91 | else | |
92 | { | |
247f410b JW |
93 | VERIFY( res.in == ix.end() ); |
94 | VERIFY( res.out == rx.end() ); | |
613c932f PP |
95 | VERIFY( ranges::equal(saved_ix, rx) ); |
96 | ranges::destroy(rx); | |
97 | } | |
98 | } | |
99 | } | |
100 | ||
101 | struct X | |
102 | { | |
103 | static constexpr int limit = 67; | |
104 | static inline int move_construct_count = 0; | |
105 | static inline int destruct_count = 0; | |
106 | ||
107 | struct exception {}; | |
108 | ||
109 | bool live = false; | |
110 | bool moved_from = false; | |
111 | ||
112 | X() | |
113 | { live = true; moved_from = false; } | |
114 | ||
115 | X& operator=(const X&) = delete; | |
116 | X(const X&) = delete; | |
117 | ||
118 | X&& operator=(X&&) = delete; | |
119 | ||
120 | X(X&& other) | |
121 | { | |
122 | VERIFY( !other.moved_from ); | |
123 | other.moved_from = true; | |
124 | live = true; | |
125 | if (move_construct_count >= limit) | |
126 | throw exception{}; | |
127 | move_construct_count++; | |
128 | } | |
129 | ||
130 | ~X() | |
131 | { | |
132 | VERIFY( live ); | |
133 | live = false; | |
134 | destruct_count++; | |
135 | } | |
136 | }; | |
137 | ||
138 | template<bool test_sized> | |
139 | void | |
140 | test02() | |
141 | { | |
142 | constexpr int size = 100; | |
143 | X x[size]; | |
144 | // FIXME: Should be test_input_range? | |
145 | test_forward_range<X> ix(x); | |
146 | ||
147 | auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]); | |
148 | test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size); | |
149 | try | |
150 | { | |
151 | X::move_construct_count = 0; | |
152 | X::destruct_count = 0; | |
153 | if constexpr (test_sized) | |
154 | ranges::uninitialized_move_n(ix.begin(), size, rx.begin(), rx.end()); | |
155 | else | |
156 | ranges::uninitialized_move(ix, rx); | |
157 | VERIFY( false && "exception not thrown" ); | |
158 | } | |
159 | catch (const X::exception&) | |
160 | { | |
161 | VERIFY( X::move_construct_count == X::limit ); | |
162 | VERIFY( X::destruct_count == X::limit ); | |
163 | } | |
164 | } | |
165 | ||
144dfc68 PP |
166 | void |
167 | test03() | |
168 | { | |
169 | // LWG 3355 | |
170 | { | |
171 | int x[3] = {0}; | |
172 | int y[3]; | |
173 | test_sized_range_sized_sent<int, input_iterator_wrapper_nocopy> rx(x); | |
174 | ranges::uninitialized_move(rx, y); | |
175 | ranges::uninitialized_move_n(rx.begin(), 3, y, y+3); | |
176 | } | |
177 | ||
178 | { | |
179 | int x[3] = {0}; | |
180 | int y[3]; | |
181 | test_range<int, input_iterator_wrapper_nocopy> rx(x); | |
182 | test_forward_range<int> ry(y); | |
183 | ranges::uninitialized_move(rx, y); | |
184 | ranges::uninitialized_move_n(rx.begin(), 3, ry.begin(), ry.end()); | |
185 | } | |
186 | } | |
187 | ||
613c932f PP |
188 | int |
189 | main() | |
190 | { | |
191 | test01<char>({1,2,3,4,5}); | |
192 | test01<int>({1,2,3,4,5}); | |
193 | test01<long long>({1,2,3,4,5}); | |
194 | test01<float>({1.1,2.1,3.1,4.1}); | |
195 | test01<double>({1.1,2.1,3.1,4.1}); | |
196 | test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}}); | |
197 | test01<std::string>({"the", "quick", "brown", "fox"}); | |
198 | ||
199 | test02<false>(); | |
200 | test02<true>(); | |
201 | } |