90 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410// UNSUPPORTED: libcpp-has-no-incomplete-pstl11 12// template<class ExecutionPolicy, class ForwardIterator1, class ForwardIterator2, class T>13// ForwardIterator214// 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 28template <class Iter>29struct Test {30 template <class ExecutionPolicy>31 void operator()(ExecutionPolicy&& policy) {32 { // simple test33 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::data(a)), Iter(std::data(a) + std::size(a)), Iter(std::data(out)), 3, 6);36 assert((out == std::array{1, 2, 6, 4, 5, 6, 7, 8}));37 }38 39 { // empty range works40 std::array<int, 0> a = {};41 std::replace_copy(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), Iter(std::data(a)), 3, 6);42 }43 44 { // non-empty range without a match works45 std::array a = {1, 2};46 std::array<int, a.size()> out;47 std::replace_copy(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), Iter(out.data()), 3, 6);48 assert((out == std::array{1, 2}));49 }50 51 { // single element range works52 std::array a = {3};53 std::array<int, a.size()> out;54 std::replace_copy(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), Iter(std::data(out)), 3, 6);55 assert((out == std::array{6}));56 }57 58 { // two element range works59 std::array a = {3, 4};60 std::array<int, a.size()> out;61 std::replace_copy(policy, Iter(std::data(a)), Iter(std::data(a) + std::size(a)), Iter(std::data(out)), 3, 6);62 assert((out == std::array{6, 4}));63 }64 65 { // multiple matching elements work66 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::data(a)), Iter(std::data(a) + std::size(a)), Iter(std::data(out)), 3, 9);69 assert((out == std::array{1, 2, 9, 4, 9, 9, 5, 6, 9}));70 }71 72 { // large range works73 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 85int main(int, char**) {86 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});87 88 return 0;89}90