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