| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <algorithm> |
| 10 |
|
| 11 |
// template<ForwardIterator Iter, class T> |
| 12 |
// requires OutputIterator<Iter, const T&> |
| 13 |
// constexpr void // constexpr after C++17 |
| 14 |
// fill(Iter first, Iter last, const T& value); |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <array> |
| 18 |
#include <cassert> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "test_macros.h" |
| 22 |
#include "test_iterators.h" |
| 23 |
|
| 24 |
template <class Iter, class Container> |
| 25 |
TEST_CONSTEXPR_CXX20 void |
| 26 |
test(Container in, size_t from, size_t to, typename Container::value_type value, Container expected) { |
| 27 |
std::fill(Iter(in.data() + from), Iter(in.data() + to), value); |
| 28 |
assert(in == expected); |
| 29 |
} |
| 30 |
|
| 31 |
template <class T> |
| 32 |
struct Test { |
| 33 |
template <class Iter> |
| 34 |
TEST_CONSTEXPR_CXX20 void operator()() { |
| 35 |
{ |
| 36 |
std::array<T, 4> in = {1, 2, 3, 4}; |
| 37 |
std::array<T, 4> expected = {5, 5, 5, 5}; |
| 38 |
test<Iter>(in, 0, 4, 5, expected); |
| 39 |
} |
| 40 |
{ |
| 41 |
std::array<T, 4> in = {1, 2, 3, 4}; |
| 42 |
std::array<T, 4> expected = {1, 5, 5, 4}; |
| 43 |
test<Iter>(in, 1, 3, 5, expected); |
| 44 |
} |
| 45 |
} |
| 46 |
}; |
| 47 |
|
| 48 |
TEST_CONSTEXPR_CXX20 bool test() { |
| 49 |
types::for_each(types::forward_iterator_list<char*>(), Test<char>()); |
| 50 |
types::for_each(types::forward_iterator_list<int*>(), Test<int>()); |
| 51 |
{ // test vector<bool>::iterator optimization |
| 52 |
{ // simple case |
| 53 |
std::vector<bool> in(4, false); |
| 54 |
std::vector<bool> expected(4, true); |
| 55 |
std::fill(in.begin(), in.end(), true); |
| 56 |
assert(in == expected); |
| 57 |
} |
| 58 |
{ // partial byte in the front is not filled |
| 59 |
std::vector<bool> in(8, false); |
| 60 |
std::vector<bool> expected(8, true); |
| 61 |
expected[0] = false; |
| 62 |
expected[1] = false; |
| 63 |
std::fill(in.begin() + 2, in.end(), true); |
| 64 |
assert(in == expected); |
| 65 |
} |
| 66 |
{ // partial byte in the back is not filled |
| 67 |
std::vector<bool> in(8, false); |
| 68 |
std::vector<bool> expected(8, true); |
| 69 |
expected[6] = false; |
| 70 |
expected[7] = false; |
| 71 |
std::fill(in.begin(), in.end() - 2, true); |
| 72 |
assert(in == expected); |
| 73 |
} |
| 74 |
{ // partial byte in the front and back is not filled |
| 75 |
std::vector<bool> in(16, false); |
| 76 |
std::vector<bool> expected(16, true); |
| 77 |
expected[0] = false; |
| 78 |
expected[1] = false; |
| 79 |
expected[14] = false; |
| 80 |
expected[15] = false; |
| 81 |
std::fill(in.begin() + 2, in.end() - 2, true); |
| 82 |
assert(in == expected); |
| 83 |
} |
| 84 |
{ // only a few bits of a byte are set |
| 85 |
std::vector<bool> in(8, false); |
| 86 |
std::vector<bool> expected(8, true); |
| 87 |
expected[0] = false; |
| 88 |
expected[1] = false; |
| 89 |
expected[6] = false; |
| 90 |
expected[7] = false; |
| 91 |
std::fill(in.begin() + 2, in.end() - 2, true); |
| 92 |
assert(in == expected); |
| 93 |
} |
| 94 |
} |
| 95 |
return true; |
| 96 |
} |
| 97 |
|
| 98 |
int main(int, char**) { |
| 99 |
test(); |
| 100 |
#if TEST_STD_VER >= 20 |
| 101 |
static_assert(test()); |
| 102 |
#endif |
| 103 |
|
| 104 |
return 0; |
| 105 |
} |