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 ForwardIterator, class Predicate, class T>
13 // void replace_if(ExecutionPolicy&& exec,
14 // ForwardIterator first, ForwardIterator last,
15 // Predicate pred, const T& new_value);
22 #include "type_algorithms.h"
23 #include "test_execution_policies.h"
24 #include "test_iterators.h"
28 template <class ExecutionPolicy
>
29 void operator()(ExecutionPolicy
&& policy
) {
31 std::array a
= {1, 2, 3, 4, 5, 6, 7, 8};
33 policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int i
) { return i
== 3 || i
== 7; }, 6);
34 assert((a
== std::array
{1, 2, 6, 4, 5, 6, 6, 8}));
37 { // empty range works
38 std::array
<int, 0> a
= {};
40 policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int) { return false; }, 6);
43 { // non-empty range without a match works
44 std::array a
= {1, 2};
45 std::replace_if(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int) { return false; }, 6);
48 { // single element range works
51 policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int i
) { return i
== 3; }, 6);
52 assert((a
== std::array
{6}));
55 { // two element range works
56 std::array a
= {3, 4};
58 policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int i
) { return i
== 3; }, 6);
59 assert((a
== std::array
{6, 4}));
62 { // multiple matching elements work
63 std::array a
= {1, 2, 3, 4, 3, 3, 5, 6, 3};
64 std::replace_if(policy
, Iter(std::begin(a
)), Iter(std::end(a
)), [](int i
) { return i
== 3; }, 9);
65 assert((a
== std::array
{1, 2, 9, 4, 9, 9, 5, 6, 9}));
68 { // large range works
69 std::vector
<int> a(150, 3);
72 policy
, Iter(std::data(a
)), Iter(std::data(a
) + std::size(a
)), [](int i
) { return i
== 3; }, 6);
74 std::vector
<int> comp(150, 6);
76 assert(std::equal(a
.begin(), a
.end(), comp
.begin()));
81 int main(int, char**) {
82 types::for_each(types::forward_iterator_list
<int*>{}, TestIteratorWithPolicies
<Test
>{});