| 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<InputIterator InIter, typename OutIter, class T> |
| 12 | // requires OutputIterator<OutIter, InIter::reference> |
| 13 | // && OutputIterator<OutIter, const T&> |
| 14 | // && HasEqualTo<InIter::value_type, T> |
| 15 | // constexpr OutIter // constexpr after C++17 |
| 16 | // replace_copy(InIter first, InIter last, OutIter result, const T& old_value, |
| 17 | // const T& new_value); |
| 18 | |
| 19 | #include <algorithm> |
| 20 | #include <cassert> |
| 21 | |
| 22 | #include "test_macros.h" |
| 23 | #include "test_iterators.h" |
| 24 | |
| 25 | |
| 26 | #if TEST_STD_VER > 17 |
| 27 | TEST_CONSTEXPR bool test_constexpr() { |
| 28 | int ia[] = {0, 1, 2, 3, 4}; |
| 29 | int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger |
| 30 | const int expected[] = {0, 1, 5, 3, 4}; |
| 31 | |
| 32 | auto it = std::replace_copy(std::begin(ia), std::end(ia), std::begin(ib), 2, 5); |
| 33 | |
| 34 | return it == (std::begin(ib) + std::size(ia)) |
| 35 | && *it == 0 // don't overwrite the last value in the output array |
| 36 | && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected)) |
| 37 | ; |
| 38 | } |
| 39 | #endif |
| 40 | |
| 41 | template <class InIter, class OutIter> |
| 42 | void |
| 43 | test() |
| 44 | { |
| 45 | int ia[] = {0, 1, 2, 3, 4}; |
| 46 | const unsigned sa = sizeof(ia)/sizeof(ia[0]); |
| 47 | int ib[sa] = {0}; |
| 48 | OutIter r = std::replace_copy(InIter(ia), InIter(ia+sa), OutIter(ib), 2, 5); |
| 49 | assert(base(r) == ib + sa); |
| 50 | assert(ib[0] == 0); |
| 51 | assert(ib[1] == 1); |
| 52 | assert(ib[2] == 5); |
| 53 | assert(ib[3] == 3); |
| 54 | assert(ib[4] == 4); |
| 55 | } |
| 56 | |
| 57 | int main(int, char**) |
| 58 | { |
| 59 | test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 60 | test<cpp17_input_iterator<const int*>, forward_iterator<int*> >(); |
| 61 | test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 62 | test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >(); |
| 63 | test<cpp17_input_iterator<const int*>, int*>(); |
| 64 | |
| 65 | test<forward_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 66 | test<forward_iterator<const int*>, forward_iterator<int*> >(); |
| 67 | test<forward_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 68 | test<forward_iterator<const int*>, random_access_iterator<int*> >(); |
| 69 | test<forward_iterator<const int*>, int*>(); |
| 70 | |
| 71 | test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 72 | test<bidirectional_iterator<const int*>, forward_iterator<int*> >(); |
| 73 | test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 74 | test<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); |
| 75 | test<bidirectional_iterator<const int*>, int*>(); |
| 76 | |
| 77 | test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >(); |
| 78 | test<random_access_iterator<const int*>, forward_iterator<int*> >(); |
| 79 | test<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); |
| 80 | test<random_access_iterator<const int*>, random_access_iterator<int*> >(); |
| 81 | test<random_access_iterator<const int*>, int*>(); |
| 82 | |
| 83 | test<const int*, cpp17_output_iterator<int*> >(); |
| 84 | test<const int*, forward_iterator<int*> >(); |
| 85 | test<const int*, bidirectional_iterator<int*> >(); |
| 86 | test<const int*, random_access_iterator<int*> >(); |
| 87 | test<const int*, int*>(); |
| 88 | |
| 89 | #if TEST_STD_VER > 17 |
| 90 | static_assert(test_constexpr()); |
| 91 | #endif |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |