Debian Codegraph @

libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc

raw
1 // Copyright (C) 2020-2025 Free Software Foundation, Inc.
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-do run { target c++20 } }
18
19 #include <algorithm>
20 #include <cstring>
21 #include <deque>
22 #include <list>
23 #include <memory>
24 #include <span>
25 #include <string>
26 #include <vector>
27 #include <utility>
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;
34 using __gnu_test::test_range;
35 using __gnu_test::test_sized_range_sized_sent;
36 using __gnu_test::input_iterator_wrapper_nocopy;
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
57 ranges::uninitialized_move_result res = {ix.begin(), rx.begin()};
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(),
68 rx.begin(), rx.end());
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,
73 rx.begin(), rx.end());
74 else if (k == 6)
75 res = ranges::uninitialized_move_n(ix.begin(), size/2,
76 rx.begin(), rx.end());
77 else if (k == 7)
78 res = ranges::uninitialized_move_n(ix.begin(), size,
79 rx.begin(), rx.begin()+size/2);
80 else
81 __builtin_abort();
82
83 if (k == 6 || k == 7)
84 {
85 VERIFY( ranges::distance(ix.begin(), res.in) == size/2 );
86 VERIFY( ranges::distance(rx.begin(), res.out) == size/2 );
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 {
93 VERIFY( res.in == ix.end() );
94 VERIFY( res.out == rx.end() );
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
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
188 void
189 test_pr101587()
190 {
191 short in[1]{};
192 __gnu_test::test_contiguous_range r(in); // difference_type is integer-like
193 long out[1];
194 std::span<long> o(out); // difference_type is ptrdiff_t
195 ranges::uninitialized_move(r, o);
196 ranges::uninitialized_move_n(ranges::begin(r), 0, o.begin(), o.end());
197
198 struct Iter
199 {
200 using value_type = long;
201 using difference_type = std::ranges::__detail::__max_diff_type;
202
203 long& operator*() const { return *p; }
204
205 Iter& operator++() { ++p; return *this; }
206 Iter operator++(int) { return Iter{p++}; }
207
208 difference_type operator-(Iter i) const { return p - i.p; }
209 bool operator==(const Iter&) const = default;
210
211 long* p = nullptr;
212 };
213 static_assert(std::sized_sentinel_for<Iter, Iter>);
214
215 std::ranges::subrange<Iter> rmax(Iter{out+0}, Iter{out+1});
216 // Check with integer-like class type for output range:
217 std::ranges::uninitialized_move(in, rmax);
218 std::ranges::uninitialized_move_n(in+0, 1, rmax.begin(), rmax.end());
219
220 int to[1];
221 // And for input range:
222 std::ranges::uninitialized_copy(rmax, to);
223 std::ranges::uninitialized_copy_n(rmax.begin(), 1, to+0, to+1);
224 }
225
226 int
227 main()
228 {
229 test01<char>({1,2,3,4,5});
230 test01<int>({1,2,3,4,5});
231 test01<long long>({1,2,3,4,5});
232 test01<float>({1.1,2.1,3.1,4.1});
233 test01<double>({1.1,2.1,3.1,4.1});
234 test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}});
235 test01<std::string>({"the", "quick", "brown", "fox"});
236
237 test02<false>();
238 test02<true>();
239
240 test_pr101587();
241 }