]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_move/constrained.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 20_util / specialized_algorithms / uninitialized_move / constrained.cc
1 // Copyright (C) 2020-2023 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-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 #include <utility>
29
30 #include <testsuite_hooks.h>
31 #include <testsuite_iterators.h>
32
33 using __gnu_test::test_input_range;
34 using __gnu_test::test_forward_range;
35 using __gnu_test::test_range;
36 using __gnu_test::test_sized_range_sized_sent;
37 using __gnu_test::input_iterator_wrapper_nocopy;
38
39 namespace ranges = std::ranges;
40
41 template<typename T>
42 void
43 test01(std::vector<T> ix)
44 {
45 static_assert(std::move_constructible<T>);
46 static_assert(std::equality_comparable<T>);
47
48 const auto saved_ix = ix;
49
50 for (int k = 0; k < 7; k++)
51 {
52 ix = saved_ix;
53
54 int size = ix.size();
55 auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]);
56 std::span<T> rx((T *)buffer.get(), size);
57
58 ranges::uninitialized_move_result res = {ix.begin(), rx.begin()};
59 if (k == 0)
60 res = ranges::uninitialized_move(ix.begin(), ix.end(),
61 rx.begin(), rx.end());
62 else if (k == 1)
63 res = ranges::uninitialized_move(ix, rx);
64 else if (k == 2)
65 res = ranges::uninitialized_move_n(ix.begin(), size,
66 rx.begin(), rx.end());
67 else if (k == 3)
68 res = ranges::uninitialized_move(ix.begin(), ix.end(),
69 rx.begin(), rx.end());
70 else if (k == 4)
71 res = ranges::uninitialized_move(ix, std::as_const(rx));
72 else if (k == 5)
73 res = ranges::uninitialized_move_n(ix.begin(), size,
74 rx.begin(), rx.end());
75 else if (k == 6)
76 res = ranges::uninitialized_move_n(ix.begin(), size/2,
77 rx.begin(), rx.end());
78 else if (k == 7)
79 res = ranges::uninitialized_move_n(ix.begin(), size,
80 rx.begin(), rx.begin()+size/2);
81 else
82 __builtin_abort();
83
84 if (k == 6 || k == 7)
85 {
86 VERIFY( ranges::distance(ix.begin(), res.in) == size/2 );
87 VERIFY( ranges::distance(rx.begin(), res.out) == size/2 );
88 VERIFY( ranges::equal(saved_ix.begin(), saved_ix.begin()+size/2,
89 rx.begin(), rx.begin()+size/2) );
90 ranges::destroy(rx.begin(), rx.begin()+size/2);
91 }
92 else
93 {
94 VERIFY( res.in == ix.end() );
95 VERIFY( res.out == rx.end() );
96 VERIFY( ranges::equal(saved_ix, rx) );
97 ranges::destroy(rx);
98 }
99 }
100 }
101
102 struct X
103 {
104 static constexpr int limit = 67;
105 static inline int move_construct_count = 0;
106 static inline int destruct_count = 0;
107
108 struct exception {};
109
110 bool live = false;
111 bool moved_from = false;
112
113 X()
114 { live = true; moved_from = false; }
115
116 X& operator=(const X&) = delete;
117 X(const X&) = delete;
118
119 X&& operator=(X&&) = delete;
120
121 X(X&& other)
122 {
123 VERIFY( !other.moved_from );
124 other.moved_from = true;
125 live = true;
126 if (move_construct_count >= limit)
127 throw exception{};
128 move_construct_count++;
129 }
130
131 ~X()
132 {
133 VERIFY( live );
134 live = false;
135 destruct_count++;
136 }
137 };
138
139 template<bool test_sized>
140 void
141 test02()
142 {
143 constexpr int size = 100;
144 X x[size];
145 // FIXME: Should be test_input_range?
146 test_forward_range<X> ix(x);
147
148 auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]);
149 test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size);
150 try
151 {
152 X::move_construct_count = 0;
153 X::destruct_count = 0;
154 if constexpr (test_sized)
155 ranges::uninitialized_move_n(ix.begin(), size, rx.begin(), rx.end());
156 else
157 ranges::uninitialized_move(ix, rx);
158 VERIFY( false && "exception not thrown" );
159 }
160 catch (const X::exception&)
161 {
162 VERIFY( X::move_construct_count == X::limit );
163 VERIFY( X::destruct_count == X::limit );
164 }
165 }
166
167 void
168 test03()
169 {
170 // LWG 3355
171 {
172 int x[3] = {0};
173 int y[3];
174 test_sized_range_sized_sent<int, input_iterator_wrapper_nocopy> rx(x);
175 ranges::uninitialized_move(rx, y);
176 ranges::uninitialized_move_n(rx.begin(), 3, y, y+3);
177 }
178
179 {
180 int x[3] = {0};
181 int y[3];
182 test_range<int, input_iterator_wrapper_nocopy> rx(x);
183 test_forward_range<int> ry(y);
184 ranges::uninitialized_move(rx, y);
185 ranges::uninitialized_move_n(rx.begin(), 3, ry.begin(), ry.end());
186 }
187 }
188
189 int
190 main()
191 {
192 test01<char>({1,2,3,4,5});
193 test01<int>({1,2,3,4,5});
194 test01<long long>({1,2,3,4,5});
195 test01<float>({1.1,2.1,3.1,4.1});
196 test01<double>({1.1,2.1,3.1,4.1});
197 test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}});
198 test01<std::string>({"the", "quick", "brown", "fox"});
199
200 test02<false>();
201 test02<true>();
202 }