| 1 |
// Copyright (C) 2020-2025 Free Software Foundation, Inc. |
| 2 |
// |
| 3 |
// This file is part of the GNU ISO C++ Library. This library is free |
| 4 |
// software; you can redistribute it and/or modify it under the |
| 5 |
// terms of the GNU General Public License as published by the |
| 6 |
// Free Software Foundation; either version 3, or (at your option) |
| 7 |
// any later version. |
| 8 |
|
| 9 |
// This library is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
|
| 14 |
// You should have received a copy of the GNU General Public License along |
| 15 |
// with this library; see the file COPYING3. If not see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
// { dg-do run { target c++20 } } |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <testsuite_hooks.h> |
| 22 |
#include <testsuite_iterators.h> |
| 23 |
#include <list> |
| 24 |
|
| 25 |
using __gnu_test::test_container; |
| 26 |
using __gnu_test::test_range; |
| 27 |
using __gnu_test::input_iterator_wrapper; |
| 28 |
using __gnu_test::output_iterator_wrapper; |
| 29 |
using __gnu_test::forward_iterator_wrapper; |
| 30 |
|
| 31 |
namespace ranges = std::ranges; |
| 32 |
|
| 33 |
struct X |
| 34 |
{ |
| 35 |
int i; |
| 36 |
}; |
| 37 |
|
| 38 |
void |
| 39 |
test01() |
| 40 |
{ |
| 41 |
const int c[6] = { 17, 17, 17, 17, 17, 17 }; |
| 42 |
{ |
| 43 |
X x[6]; |
| 44 |
VERIFY( ranges::fill(x, X{17}) == x+6 ); |
| 45 |
VERIFY( ranges::equal(x, c, {}, &X::i) ); |
| 46 |
} |
| 47 |
|
| 48 |
{ |
| 49 |
char x[6]; |
| 50 |
VERIFY( ranges::fill(x, 17) == x+6 ); |
| 51 |
VERIFY( ranges::equal(x, c) ); |
| 52 |
} |
| 53 |
|
| 54 |
{ |
| 55 |
X x[6]; |
| 56 |
test_container<X, forward_iterator_wrapper> cx(x); |
| 57 |
VERIFY( ranges::fill(cx, X{17}) == cx.end() ); |
| 58 |
VERIFY( ranges::equal(cx, c, {}, &X::i) ); |
| 59 |
} |
| 60 |
|
| 61 |
{ |
| 62 |
int x[6]; |
| 63 |
test_range<int, output_iterator_wrapper> rx(x); |
| 64 |
VERIFY( ranges::fill(rx, 17) == rx.end() ); |
| 65 |
VERIFY( ranges::equal(x, c) ); |
| 66 |
} |
| 67 |
|
| 68 |
{ |
| 69 |
std::list<int> list(6); |
| 70 |
ranges::fill(list, 17); |
| 71 |
VERIFY( ranges::equal(list, c) ); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
constexpr bool |
| 76 |
test02() |
| 77 |
{ |
| 78 |
bool ok = true; |
| 79 |
int x[5]; |
| 80 |
ranges::fill(x, 17); |
| 81 |
for (auto v : x) |
| 82 |
ok &= v == 17; |
| 83 |
return ok; |
| 84 |
} |
| 85 |
|
| 86 |
void |
| 87 |
test03() |
| 88 |
{ |
| 89 |
// Bug libstdc++/117094 - ranges::fill misses std::move for output_iterator |
| 90 |
|
| 91 |
// Move-only output iterator |
| 92 |
struct Iterator |
| 93 |
{ |
| 94 |
using difference_type = long; |
| 95 |
Iterator(int* p) : p(p) { } |
| 96 |
Iterator(Iterator&&) = default; |
| 97 |
Iterator& operator=(Iterator&&) = default; |
| 98 |
int& operator*() const { return *p; } |
| 99 |
Iterator& operator++() { ++p; return *this; } |
| 100 |
Iterator operator++(int) { return Iterator(p++ ); } |
| 101 |
int* p; |
| 102 |
|
| 103 |
struct Sentinel |
| 104 |
{ |
| 105 |
const int* p; |
| 106 |
bool operator==(const Iterator& i) const { return p == i.p; } |
| 107 |
long operator-(const Iterator& i) const { return p - i.p; } |
| 108 |
}; |
| 109 |
|
| 110 |
long operator-(Sentinel s) const { return p - s.p; } |
| 111 |
}; |
| 112 |
static_assert(std::sized_sentinel_for<Iterator::Sentinel, Iterator>); |
| 113 |
int a[2]; |
| 114 |
std::ranges::fill(Iterator(a), Iterator::Sentinel{a+2}, 999); |
| 115 |
VERIFY( a[0] == 999 ); |
| 116 |
VERIFY( a[1] == 999 ); |
| 117 |
} |
| 118 |
|
| 119 |
int |
| 120 |
main() |
| 121 |
{ |
| 122 |
test01(); |
| 123 |
static_assert(test02()); |
| 124 |
test03(); |
| 125 |
} |