Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.replace / pstl.replace_copy_if.pass.cpp
blobf7c746f382d117faf9c45a54a05dc077b20d5ed6
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14
10 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
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);
20 #include <algorithm>
21 #include <array>
22 #include <cassert>
23 #include <vector>
25 #include "type_algorithms.h"
26 #include "test_execution_policies.h"
27 #include "test_iterators.h"
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, Iter(std::begin(a)), Iter(std::end(a)), Iter(std::begin(out)), [](int i) { return i == 3; }, 6);
38 assert((out == std::array{1, 2, 6, 4, 5, 6, 7, 8}));
41 { // empty range works
42 std::array<int, 0> a = {};
43 std::replace_copy_if(
44 policy, Iter(std::begin(a)), Iter(std::end(a)), Iter(std::begin(a)), [](int i) { return i == 3; }, 6);
47 { // non-empty range without a match works
48 std::array a = {1, 2};
49 std::array<int, a.size()> out;
50 std::replace_copy_if(
51 policy, Iter(std::begin(a)), Iter(std::end(a)), Iter(out.data()), [](int i) { return i == 3; }, 6);
52 assert((out == std::array{1, 2}));
55 { // single element range works
56 std::array a = {3};
57 std::array<int, a.size()> out;
58 std::replace_copy_if(
59 policy, Iter(std::begin(a)), Iter(std::end(a)), Iter(std::begin(out)), [](int i) { return i == 3; }, 6);
60 assert((out == std::array{6}));
63 { // two element range works
64 std::array a = {3, 4};
65 std::array<int, a.size()> out;
66 std::replace_copy_if(
67 policy, Iter(std::begin(a)), Iter(std::end(a)), Iter(std::begin(out)), [](int i) { return i == 3; }, 6);
68 assert((out == std::array{6, 4}));
71 { // multiple matching elements work
72 std::array a = {1, 2, 3, 4, 3, 3, 5, 6, 3};
73 std::array<int, a.size()> out;
74 std::replace_copy_if(
75 policy, Iter(std::begin(a)), Iter(std::end(a)), Iter(std::begin(out)), [](int i) { return i == 3; }, 9);
76 assert((out == std::array{1, 2, 9, 4, 9, 9, 5, 6, 9}));
79 { // large range works
80 std::vector<int> a(150, 3);
81 std::vector<int> out(a.size());
82 a[45] = 5;
83 std::replace_copy_if(
84 policy,
85 Iter(std::data(a)),
86 Iter(std::data(a) + std::size(a)),
87 Iter(out.data()),
88 [](int i) { return i == 3; },
89 6);
91 std::vector<int> comp(150, 6);
92 comp[45] = 5;
93 assert(std::equal(out.begin(), out.end(), comp.begin()));
98 int main(int, char**) {
99 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
101 return 0;