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