66 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<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred, class T>12// requires OutputIterator<Iter, Iter::reference>13// && OutputIterator<Iter, const T&>14// && CopyConstructible<Pred>15// constexpr void // constexpr after C++1716// replace_if(Iter first, Iter last, Pred pred, const T& new_value);17 18#include <algorithm>19#include <functional>20#include <cassert>21 22#include "test_macros.h"23#include "test_iterators.h"24 25TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }26 27#if TEST_STD_VER > 1728TEST_CONSTEXPR bool test_constexpr() {29 int ia[] = {0, 1, 2, 3, 4};30 const int expected[] = {0, 1, 5, 3, 4};31 32 std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);33 return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))34 ;35 }36#endif37 38 39template <class Iter>40void41test()42{43 int ia[] = {0, 1, 2, 3, 4};44 const unsigned sa = sizeof(ia)/sizeof(ia[0]);45 std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);46 assert(ia[0] == 0);47 assert(ia[1] == 1);48 assert(ia[2] == 5);49 assert(ia[3] == 3);50 assert(ia[4] == 4);51}52 53int main(int, char**)54{55 test<forward_iterator<int*> >();56 test<bidirectional_iterator<int*> >();57 test<random_access_iterator<int*> >();58 test<int*>();59 60#if TEST_STD_VER > 1761 static_assert(test_constexpr());62#endif63 64 return 0;65}66