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 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
11 
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);
16 
17 #include <algorithm>
18 #include <array>
19 #include <cassert>
20 #include <vector>
21 
22 #include "type_algorithms.h"
23 #include "test_execution_policies.h"
24 #include "test_iterators.h"
25 
26 template <class Iter>
27 struct Test {
28   template <class ExecutionPolicy>
operator ()Test29   void operator()(ExecutionPolicy&& policy) {
30     { // simple test
31       std::array a = {1, 2, 3, 4, 5, 6, 7, 8};
32       std::replace_if(
33           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int i) { return i == 3 || i == 7; }, 6);
34       assert((a == std::array{1, 2, 6, 4, 5, 6, 6, 8}));
35     }
36 
37     { // empty range works
38       std::array<int, 0> a = {};
39       std::replace_if(
40           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int) { return false; }, 6);
41     }
42 
43     { // non-empty range without a match works
44       std::array a = {1, 2};
45       std::replace_if(
46           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int) { return false; }, 6);
47     }
48 
49     { // single element range works
50       std::array a = {3};
51       std::replace_if(
52           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int i) { return i == 3; }, 6);
53       assert((a == std::array{6}));
54     }
55 
56     { // two element range works
57       std::array a = {3, 4};
58       std::replace_if(
59           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int i) { return i == 3; }, 6);
60       assert((a == std::array{6, 4}));
61     }
62 
63     { // multiple matching elements work
64       std::array a = {1, 2, 3, 4, 3, 3, 5, 6, 3};
65       std::replace_if(
66           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int i) { return i == 3; }, 9);
67       assert((a == std::array{1, 2, 9, 4, 9, 9, 5, 6, 9}));
68     }
69 
70     { // large range works
71       std::vector<int> a(150, 3);
72       a[45] = 5;
73       std::replace_if(
74           policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), [](int i) { return i == 3; }, 6);
75 
76       std::vector<int> comp(150, 6);
77       comp[45] = 5;
78       assert(std::equal(a.begin(), a.end(), comp.begin()));
79     }
80   }
81 };
82 
main(int,char **)83 int main(int, char**) {
84   types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
85 
86   return 0;
87 }
88