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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 // template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T>
14 // replace_copy(ExecutionPolicy&& exec,
15 // ForwardIterator1 first, ForwardIterator1 last,
16 // ForwardIterator2 result,
17 // const T& old_value, const T& new_value);
24 #include "type_algorithms.h"
25 #include "test_execution_policies.h"
26 #include "test_iterators.h"
30 template <class ExecutionPolicy
>
31 void operator()(ExecutionPolicy
&& policy
) {
33 std::array a
= {1, 2, 3, 4, 5, 6, 7, 8};
34 std::array
<int, a
.size()> out
;
35 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(std::begin(out
)), 3, 6);
36 assert((out
== std::array
{1, 2, 6, 4, 5, 6, 7, 8}));
39 { // empty range works
40 std::array
<int, 0> a
= {};
41 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(std::begin(a
)), 3, 6);
44 { // non-empty range without a match works
45 std::array a
= {1, 2};
46 std::array
<int, a
.size()> out
;
47 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(out
.data()), 3, 6);
48 assert((out
== std::array
{1, 2}));
51 { // single element range works
53 std::array
<int, a
.size()> out
;
54 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(std::begin(out
)), 3, 6);
55 assert((out
== std::array
{6}));
58 { // two element range works
59 std::array a
= {3, 4};
60 std::array
<int, a
.size()> out
;
61 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(std::begin(out
)), 3, 6);
62 assert((out
== std::array
{6, 4}));
65 { // multiple matching elements work
66 std::array a
= {1, 2, 3, 4, 3, 3, 5, 6, 3};
67 std::array
<int, a
.size()> out
;
68 std::replace_copy(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), Iter(std::begin(out
)), 3, 9);
69 assert((out
== std::array
{1, 2, 9, 4, 9, 9, 5, 6, 9}));
72 { // large range works
73 std::vector
<int> a(150, 3);
74 std::vector
<int> out(a
.size());
76 std::replace_copy(policy
, Iter(std::data(a
)), Iter(std::data(a
) + std::size(a
)), Iter(out
.data()), 3, 6);
78 std::vector
<int> comp(150, 6);
80 assert(std::equal(out
.begin(), out
.end(), comp
.begin()));
85 int main(int, char**) {
86 types::for_each(types::forward_iterator_list
<int*>{}, TestIteratorWithPolicies
<Test
>{});