1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template<InputIterator InIter, typename OutIter,
12 // Predicate<auto, InIter::value_type> Pred, class T>
13 // requires OutputIterator<OutIter, InIter::reference>
14 // && OutputIterator<OutIter, const T&>
15 // && CopyConstructible<Pred>
16 // constexpr OutIter // constexpr after C++17
17 // replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);
23 #include "test_macros.h"
24 #include "test_iterators.h"
26 TEST_CONSTEXPR
bool equalToTwo(int v
) { return v
== 2; }
30 TEST_CONSTEXPR
bool test_constexpr() {
31 int ia
[] = {0, 1, 2, 3, 4};
32 int ib
[] = {0, 0, 0, 0, 0, 0}; // one bigger
33 const int expected
[] = {0, 1, 5, 3, 4};
35 auto it
= std::replace_copy_if(std::begin(ia
), std::end(ia
), std::begin(ib
), equalToTwo
, 5);
37 return it
== (std::begin(ib
) + std::size(ia
))
38 && *it
== 0 // don't overwrite the last value in the output array
39 && std::equal(std::begin(ib
), it
, std::begin(expected
), std::end(expected
))
44 template <class InIter
, class OutIter
>
48 int ia
[] = {0, 1, 2, 3, 4};
49 const unsigned sa
= sizeof(ia
)/sizeof(ia
[0]);
51 OutIter r
= std::replace_copy_if(InIter(ia
), InIter(ia
+sa
),
52 OutIter(ib
), equalToTwo
, 5);
53 assert(base(r
) == ib
+ sa
);
63 test
<input_iterator
<const int*>, output_iterator
<int*> >();
64 test
<input_iterator
<const int*>, forward_iterator
<int*> >();
65 test
<input_iterator
<const int*>, bidirectional_iterator
<int*> >();
66 test
<input_iterator
<const int*>, random_access_iterator
<int*> >();
67 test
<input_iterator
<const int*>, int*>();
69 test
<forward_iterator
<const int*>, output_iterator
<int*> >();
70 test
<forward_iterator
<const int*>, forward_iterator
<int*> >();
71 test
<forward_iterator
<const int*>, bidirectional_iterator
<int*> >();
72 test
<forward_iterator
<const int*>, random_access_iterator
<int*> >();
73 test
<forward_iterator
<const int*>, int*>();
75 test
<bidirectional_iterator
<const int*>, output_iterator
<int*> >();
76 test
<bidirectional_iterator
<const int*>, forward_iterator
<int*> >();
77 test
<bidirectional_iterator
<const int*>, bidirectional_iterator
<int*> >();
78 test
<bidirectional_iterator
<const int*>, random_access_iterator
<int*> >();
79 test
<bidirectional_iterator
<const int*>, int*>();
81 test
<random_access_iterator
<const int*>, output_iterator
<int*> >();
82 test
<random_access_iterator
<const int*>, forward_iterator
<int*> >();
83 test
<random_access_iterator
<const int*>, bidirectional_iterator
<int*> >();
84 test
<random_access_iterator
<const int*>, random_access_iterator
<int*> >();
85 test
<random_access_iterator
<const int*>, int*>();
87 test
<const int*, output_iterator
<int*> >();
88 test
<const int*, forward_iterator
<int*> >();
89 test
<const int*, bidirectional_iterator
<int*> >();
90 test
<const int*, random_access_iterator
<int*> >();
91 test
<const int*, int*>();
94 static_assert(test_constexpr());