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