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.pass.cpp
blob751d6b74b0128a3ded1edc9e4c486f3869e7d954
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 ForwardIterator, class T>
13 // void replace(ExecutionPolicy&& exec,
14 // ForwardIterator first, ForwardIterator last,
15 // const T& old_value, const T& new_value);
17 #include <algorithm>
18 #include <array>
19 #include <cassert>
20 #include <vector>
22 #include "type_algorithms.h"
23 #include "test_execution_policies.h"
24 #include "test_iterators.h"
26 template <class Iter>
27 struct Test {
28 template <class ExecutionPolicy>
29 void operator()(ExecutionPolicy&& policy) {
30 { // simple test
31 std::array a = {1, 2, 3, 4, 5, 6, 7, 8};
32 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 6);
33 assert((a == std::array{1, 2, 6, 4, 5, 6, 7, 8}));
36 { // empty range works
37 std::array<int, 0> a = {};
38 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 6);
41 { // non-empty range without a match works
42 std::array a = {1, 2};
43 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 6);
46 { // single element range works
47 std::array a = {3};
48 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 6);
49 assert((a == std::array{6}));
52 { // two element range works
53 std::array a = {3, 4};
54 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 6);
55 assert((a == std::array{6, 4}));
58 { // multiple matching elements work
59 std::array a = {1, 2, 3, 4, 3, 3, 5, 6, 3};
60 std::replace(policy, Iter(std::begin(a)), Iter(std::end(a)), 3, 9);
61 assert((a == std::array{1, 2, 9, 4, 9, 9, 5, 6, 9}));
64 { // large range works
65 std::vector<int> a(150, 3);
66 a[45] = 5;
67 std::replace(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), 3, 6);
69 std::vector<int> comp(150, 6);
70 comp[45] = 5;
71 assert(std::equal(a.begin(), a.end(), comp.begin()));
76 int main(int, char**) {
77 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
79 return 0;