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 <cstddef> 20 #include <vector> 21 22 #include "test_macros.h" 23 #include "test_iterators.h" 24 25 template <class Iter, class Container> 26 TEST_CONSTEXPR_CXX20 void 27 test(Container in, size_t from, size_t to, typename Container::value_type value, Container expected) { 28 std::fill(Iter(in.data() + from), Iter(in.data() + to), value); 29 assert(in == expected); 30 } 31 32 template <class T> 33 struct Test { 34 template <class Iter> 35 TEST_CONSTEXPR_CXX20 void operator()() { 36 { 37 std::array<T, 4> in = {1, 2, 3, 4}; 38 std::array<T, 4> expected = {5, 5, 5, 5}; 39 test<Iter>(in, 0, 4, 5, expected); 40 } 41 { 42 std::array<T, 4> in = {1, 2, 3, 4}; 43 std::array<T, 4> expected = {1, 5, 5, 4}; 44 test<Iter>(in, 1, 3, 5, expected); 45 } 46 } 47 }; 48 49 TEST_CONSTEXPR_CXX20 bool test() { 50 types::for_each(types::forward_iterator_list<char*>(), Test<char>()); 51 types::for_each(types::forward_iterator_list<int*>(), Test<int>()); 52 { // test vector<bool>::iterator optimization 53 { // simple case 54 std::vector<bool> in(4, false); 55 std::vector<bool> expected(4, true); 56 std::fill(in.begin(), in.end(), true); 57 assert(in == expected); 58 } 59 { // partial byte in the front is not filled 60 std::vector<bool> in(8, false); 61 std::vector<bool> expected(8, true); 62 expected[0] = false; 63 expected[1] = false; 64 std::fill(in.begin() + 2, in.end(), true); 65 assert(in == expected); 66 } 67 { // partial byte in the back is not filled 68 std::vector<bool> in(8, false); 69 std::vector<bool> expected(8, true); 70 expected[6] = false; 71 expected[7] = false; 72 std::fill(in.begin(), in.end() - 2, true); 73 assert(in == expected); 74 } 75 { // partial byte in the front and back is not filled 76 std::vector<bool> in(16, false); 77 std::vector<bool> expected(16, true); 78 expected[0] = false; 79 expected[1] = false; 80 expected[14] = false; 81 expected[15] = false; 82 std::fill(in.begin() + 2, in.end() - 2, true); 83 assert(in == expected); 84 } 85 { // only a few bits of a byte are set 86 std::vector<bool> in(8, false); 87 std::vector<bool> expected(8, true); 88 expected[0] = false; 89 expected[1] = false; 90 expected[6] = false; 91 expected[7] = false; 92 std::fill(in.begin() + 2, in.end() - 2, true); 93 assert(in == expected); 94 } 95 } 96 return true; 97 } 98 99 int main(int, char**) { 100 test(); 101 #if TEST_STD_VER >= 20 102 static_assert(test()); 103 #endif 104 105 return 0; 106 } 107