| 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_forward_range; |
| 33 |
|
| 34 |
namespace ranges = std::ranges; |
| 35 |
|
| 36 |
template<typename T> |
| 37 |
void |
| 38 |
test01() |
| 39 |
{ |
| 40 |
static_assert(std::default_initializable<T>); |
| 41 |
static_assert(std::equality_comparable<T>); |
| 42 |
|
| 43 |
for (int k = 0; k < 6; k++) |
| 44 |
{ |
| 45 |
constexpr int size = 1024; |
| 46 |
auto buffer = std::unique_ptr<char[]>(new char[sizeof(T)*size]); |
| 47 |
std::span<T> rx((T *)buffer.get(), size); |
| 48 |
|
| 49 |
T t{}; |
| 50 |
|
| 51 |
auto i = rx.begin(); |
| 52 |
if (k == 0) |
| 53 |
i = ranges::uninitialized_value_construct(rx.begin(), rx.end()); |
| 54 |
else if (k == 1) |
| 55 |
i = ranges::uninitialized_value_construct(rx); |
| 56 |
else if (k == 2) |
| 57 |
i = ranges::uninitialized_value_construct_n(rx.begin(), 1024); |
| 58 |
else if (k == 3) |
| 59 |
i = ranges::uninitialized_value_construct(rx.begin(), rx.end()); |
| 60 |
else if (k == 4) |
| 61 |
i = ranges::uninitialized_value_construct(std::as_const(rx)); |
| 62 |
else if (k == 5) |
| 63 |
i = ranges::uninitialized_value_construct_n(rx.begin(), 1024); |
| 64 |
else |
| 65 |
__builtin_abort(); |
| 66 |
|
| 67 |
VERIFY( i == rx.end() ); |
| 68 |
VERIFY( ranges::find_if(rx, [&t](const T& v) { return t != v; }) == i ); |
| 69 |
|
| 70 |
ranges::destroy(rx); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
struct X |
| 75 |
{ |
| 76 |
static constexpr int limit = 67; |
| 77 |
static inline int construct_count = 0; |
| 78 |
static inline int destruct_count = 0; |
| 79 |
|
| 80 |
struct exception {}; |
| 81 |
|
| 82 |
bool live = false; |
| 83 |
|
| 84 |
X() |
| 85 |
{ |
| 86 |
if (construct_count >= limit) |
| 87 |
throw exception{}; |
| 88 |
construct_count++; |
| 89 |
live = true; |
| 90 |
} |
| 91 |
|
| 92 |
~X() |
| 93 |
{ |
| 94 |
VERIFY( live ); |
| 95 |
live = false; |
| 96 |
destruct_count++; |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
template<bool test_sized> |
| 101 |
void |
| 102 |
test02() |
| 103 |
{ |
| 104 |
constexpr int size = 100; |
| 105 |
auto buffer = std::unique_ptr<char[]>(new char[sizeof(X)*size]); |
| 106 |
test_forward_range<X> rx((X *)buffer.get(), (X *)buffer.get() + size); |
| 107 |
try |
| 108 |
{ |
| 109 |
X::construct_count = 0; |
| 110 |
X::destruct_count = 0; |
| 111 |
if constexpr (test_sized) |
| 112 |
ranges::uninitialized_value_construct_n(rx.begin(), size); |
| 113 |
else |
| 114 |
ranges::uninitialized_value_construct(rx); |
| 115 |
VERIFY( false && "exception not thrown" ); |
| 116 |
} |
| 117 |
catch (const X::exception&) |
| 118 |
{ |
| 119 |
VERIFY( X::construct_count == X::limit ); |
| 120 |
VERIFY( X::destruct_count == X::limit ); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
int |
| 125 |
main() |
| 126 |
{ |
| 127 |
test01<char>(); |
| 128 |
test01<int>(); |
| 129 |
test01<long long>(); |
| 130 |
test01<float>(); |
| 131 |
test01<double>(); |
| 132 |
test01<std::vector<char>>(); |
| 133 |
test01<std::string>(); |
| 134 |
test01<std::deque<double>>(); |
| 135 |
test01<std::list<std::vector<std::deque<double>>>>(); |
| 136 |
test01<std::unique_ptr<std::string>>(); |
| 137 |
|
| 138 |
test02<false>(); |
| 139 |
test02<true>(); |
| 140 |
} |