| 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(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 |
|
| 53 |
ranges::uninitialized_copy_result res = {ix.begin(), rx.begin()}; |
| 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(), |
| 64 |
rx.begin(), rx.end()); |
| 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, |
| 69 |
rx.begin(), rx.end()); |
| 70 |
else if (k == 6) |
| 71 |
res = ranges::uninitialized_copy_n(ix.begin(), size/2, |
| 72 |
rx.begin(), rx.end()); |
| 73 |
else if (k == 7) |
| 74 |
res = ranges::uninitialized_copy_n(ix.begin(), size, |
| 75 |
rx.begin(), rx.begin()+size/2); |
| 76 |
else |
| 77 |
__builtin_abort(); |
| 78 |
|
| 79 |
if (k == 6 || k == 7) |
| 80 |
{ |
| 81 |
VERIFY( ranges::distance(ix.begin(), res.in) == size/2 ); |
| 82 |
VERIFY( ranges::distance(rx.begin(), res.out) == size/2 ); |
| 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 |
{ |
| 89 |
VERIFY( res.in == ix.end() ); |
| 90 |
VERIFY( res.out == rx.end() ); |
| 91 |
VERIFY( ranges::equal(ix, rx) ); |
| 92 |
ranges::destroy(rx); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
struct 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 |
|
| 128 |
template<bool test_sized> |
| 129 |
void |
| 130 |
test02() |
| 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 |
|
| 156 |
void |
| 157 |
test03() |
| 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 |
|
| 178 |
void |
| 179 |
test_pr101587() |
| 180 |
{ |
| 181 |
short in[1]{}; |
| 182 |
__gnu_test::test_contiguous_range r(in); // difference_type is integer-like |
| 183 |
long out[1]; |
| 184 |
std::span<long> o(out); // difference_type is ptrdiff_t |
| 185 |
ranges::uninitialized_copy(r, o); |
| 186 |
ranges::uninitialized_copy_n(ranges::begin(r), 0, o.begin(), o.end()); |
| 187 |
|
| 188 |
// iterator that has an integer-like class type for difference_type |
| 189 |
struct Iter |
| 190 |
{ |
| 191 |
using value_type = long; |
| 192 |
using difference_type = std::ranges::__detail::__max_diff_type; |
| 193 |
|
| 194 |
long& operator*() const { return *p; } |
| 195 |
|
| 196 |
Iter& operator++() { ++p; return *this; } |
| 197 |
Iter operator++(int) { return Iter{p++}; } |
| 198 |
|
| 199 |
difference_type operator-(Iter i) const { return p - i.p; } |
| 200 |
bool operator==(const Iter&) const = default; |
| 201 |
|
| 202 |
long* p = nullptr; |
| 203 |
}; |
| 204 |
static_assert(std::sized_sentinel_for<Iter, Iter>); |
| 205 |
|
| 206 |
std::ranges::subrange<Iter> rmax(Iter{out+0}, Iter{out+1}); |
| 207 |
// Check with integer-like class type for output range: |
| 208 |
std::ranges::uninitialized_copy(in, rmax); |
| 209 |
std::ranges::uninitialized_copy_n(in+0, 1, rmax.begin(), rmax.end()); |
| 210 |
|
| 211 |
int to[1]; |
| 212 |
// And for input range: |
| 213 |
std::ranges::uninitialized_copy(rmax, to); |
| 214 |
std::ranges::uninitialized_copy_n(rmax.begin(), 1, to+0, to+1); |
| 215 |
} |
| 216 |
|
| 217 |
int |
| 218 |
main() |
| 219 |
{ |
| 220 |
test01<char>({1,2,3,4,5}); |
| 221 |
test01<int>({1,2,3,4,5}); |
| 222 |
test01<long long>({1,2,3,4,5}); |
| 223 |
test01<float>({1.1,2.1,3.1,4.1}); |
| 224 |
test01<double>({1.1,2.1,3.1,4.1}); |
| 225 |
test01<std::vector<char>>({{'a','b'}, {'c','d'}, {'e','f'}}); |
| 226 |
test01<std::string>({"the", "quick", "brown", "fox"}); |
| 227 |
|
| 228 |
test02<false>(); |
| 229 |
test02<true>(); |
| 230 |
|
| 231 |
test_pr101587(); |
| 232 |
} |