99 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// <algorithm>10 11// template<InputIterator InIter, typename OutIter,12// Predicate<auto, InIter::value_type> Pred, class T>13// requires OutputIterator<OutIter, InIter::reference>14// && OutputIterator<OutIter, const T&>15// && CopyConstructible<Pred>16// constexpr OutIter // constexpr after C++1717// replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value);18 19#include <algorithm>20#include <functional>21#include <cassert>22 23#include "test_macros.h"24#include "test_iterators.h"25 26TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }27 28 29#if TEST_STD_VER > 1730TEST_CONSTEXPR bool test_constexpr() {31 int ia[] = {0, 1, 2, 3, 4};32 int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger33 const int expected[] = {0, 1, 5, 3, 4};34 35 auto it = std::replace_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo, 5);36 37 return it == (std::begin(ib) + std::size(ia))38 && *it == 0 // don't overwrite the last value in the output array39 && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))40 ;41 }42#endif43 44template <class InIter, class OutIter>45void46test()47{48 int ia[] = {0, 1, 2, 3, 4};49 const unsigned sa = sizeof(ia)/sizeof(ia[0]);50 int ib[sa] = {0};51 OutIter r = std::replace_copy_if(InIter(ia), InIter(ia+sa),52 OutIter(ib), equalToTwo, 5);53 assert(base(r) == ib + sa);54 assert(ib[0] == 0);55 assert(ib[1] == 1);56 assert(ib[2] == 5);57 assert(ib[3] == 3);58 assert(ib[4] == 4);59}60 61int main(int, char**)62{63 test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();64 test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();65 test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();66 test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();67 test<cpp17_input_iterator<const int*>, int*>();68 69 test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();70 test<forward_iterator<const int*>, forward_iterator<int*> >();71 test<forward_iterator<const int*>, bidirectional_iterator<int*> >();72 test<forward_iterator<const int*>, random_access_iterator<int*> >();73 test<forward_iterator<const int*>, int*>();74 75 test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();76 test<bidirectional_iterator<const int*>, forward_iterator<int*> >();77 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();78 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();79 test<bidirectional_iterator<const int*>, int*>();80 81 test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();82 test<random_access_iterator<const int*>, forward_iterator<int*> >();83 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();84 test<random_access_iterator<const int*>, random_access_iterator<int*> >();85 test<random_access_iterator<const int*>, int*>();86 87 test<const int*, cpp17_output_iterator<int*> >();88 test<const int*, forward_iterator<int*> >();89 test<const int*, bidirectional_iterator<int*> >();90 test<const int*, random_access_iterator<int*> >();91 test<const int*, int*>();92 93#if TEST_STD_VER > 1794 static_assert(test_constexpr());95#endif96 97 return 0;98}99