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 ForwardIterator1, class ForwardIterator2, |
13 | // class Predicate, class T> |
14 | // ForwardIterator2 |
15 | // replace_copy_if(ExecutionPolicy&& exec, |
16 | // ForwardIterator1 first, ForwardIterator1 last, |
17 | // ForwardIterator2 result, |
18 | // Predicate pred, const T& new_value); |
19 | |
20 | #include <algorithm> |
21 | #include <array> |
22 | #include <cassert> |
23 | #include <vector> |
24 | |
25 | #include "type_algorithms.h" |
26 | #include "test_execution_policies.h" |
27 | #include "test_iterators.h" |
28 | |
29 | template <class Iter> |
30 | struct Test { |
31 | template <class ExecutionPolicy> |
32 | void operator()(ExecutionPolicy&& policy) { |
33 | { // simple test |
34 | std::array a = {1, 2, 3, 4, 5, 6, 7, 8}; |
35 | std::array<int, a.size()> out; |
36 | std::replace_copy_if( |
37 | policy, |
38 | Iter(std::data(cont&: a)), |
39 | Iter(std::data(cont&: a) + std::size(cont: a)), |
40 | Iter(std::data(cont&: out)), |
41 | [](int i) { return i == 3; }, |
42 | 6); |
43 | assert((out == std::array{1, 2, 6, 4, 5, 6, 7, 8})); |
44 | } |
45 | |
46 | { // empty range works |
47 | std::array<int, 0> a = {}; |
48 | std::replace_copy_if( |
49 | policy, |
50 | Iter(std::data(cont&: a)), |
51 | Iter(std::data(cont&: a) + std::size(cont: a)), |
52 | Iter(std::data(cont&: a)), |
53 | [](int i) { return i == 3; }, |
54 | 6); |
55 | } |
56 | |
57 | { // non-empty range without a match works |
58 | std::array a = {1, 2}; |
59 | std::array<int, a.size()> out; |
60 | std::replace_copy_if( |
61 | policy, |
62 | Iter(std::data(cont&: a)), |
63 | Iter(std::data(cont&: a) + std::size(cont: a)), |
64 | Iter(out.data()), |
65 | [](int i) { return i == 3; }, |
66 | 6); |
67 | assert((out == std::array{1, 2})); |
68 | } |
69 | |
70 | { // single element range works |
71 | std::array a = {3}; |
72 | std::array<int, a.size()> out; |
73 | std::replace_copy_if( |
74 | policy, |
75 | Iter(std::data(cont&: a)), |
76 | Iter(std::data(cont&: a) + std::size(cont: a)), |
77 | Iter(std::data(cont&: out)), |
78 | [](int i) { return i == 3; }, |
79 | 6); |
80 | assert((out == std::array{6})); |
81 | } |
82 | |
83 | { // two element range works |
84 | std::array a = {3, 4}; |
85 | std::array<int, a.size()> out; |
86 | std::replace_copy_if( |
87 | policy, |
88 | Iter(std::data(cont&: a)), |
89 | Iter(std::data(cont&: a) + std::size(cont: a)), |
90 | Iter(std::data(cont&: out)), |
91 | [](int i) { return i == 3; }, |
92 | 6); |
93 | assert((out == std::array{6, 4})); |
94 | } |
95 | |
96 | { // multiple matching elements work |
97 | std::array a = {1, 2, 3, 4, 3, 3, 5, 6, 3}; |
98 | std::array<int, a.size()> out; |
99 | std::replace_copy_if( |
100 | policy, |
101 | Iter(std::data(cont&: a)), |
102 | Iter(std::data(cont&: a) + std::size(cont: a)), |
103 | Iter(std::data(cont&: out)), |
104 | [](int i) { return i == 3; }, |
105 | 9); |
106 | assert((out == std::array{1, 2, 9, 4, 9, 9, 5, 6, 9})); |
107 | } |
108 | |
109 | { // large range works |
110 | std::vector<int> a(150, 3); |
111 | std::vector<int> out(a.size()); |
112 | a[45] = 5; |
113 | std::replace_copy_if( |
114 | policy, |
115 | Iter(std::data(cont&: a)), |
116 | Iter(std::data(cont&: a) + std::size(cont: a)), |
117 | Iter(out.data()), |
118 | [](int i) { return i == 3; }, |
119 | 6); |
120 | |
121 | std::vector<int> comp(150, 6); |
122 | comp[45] = 5; |
123 | assert(std::equal(out.begin(), out.end(), comp.begin())); |
124 | } |
125 | } |
126 | }; |
127 | |
128 | int main(int, char**) { |
129 | types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{}); |
130 | |
131 | return 0; |
132 | } |
133 | |