xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.replace/replace_if.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template<ForwardIterator Iter, Predicate<auto, Iter::value_type> Pred, class T>
125a83710eSEric Fiselier //   requires OutputIterator<Iter, Iter::reference>
135a83710eSEric Fiselier //         && OutputIterator<Iter, const T&>
145a83710eSEric Fiselier //         && CopyConstructible<Pred>
1512c7423fSMarshall Clow //   constexpr void      // constexpr after C++17
165a83710eSEric Fiselier //   replace_if(Iter first, Iter last, Pred pred, const T& new_value);
175a83710eSEric Fiselier 
185a83710eSEric Fiselier #include <algorithm>
195a83710eSEric Fiselier #include <functional>
205a83710eSEric Fiselier #include <cassert>
215a83710eSEric Fiselier 
2212c7423fSMarshall Clow #include "test_macros.h"
235a83710eSEric Fiselier #include "test_iterators.h"
245a83710eSEric Fiselier 
equalToTwo(int v)2512c7423fSMarshall Clow TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; }
2612c7423fSMarshall Clow 
2712c7423fSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()2812c7423fSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
2912c7423fSMarshall Clow           int ia[]       = {0, 1, 2, 3, 4};
3012c7423fSMarshall Clow     const int expected[] = {0, 1, 5, 3, 4};
3112c7423fSMarshall Clow 
3212c7423fSMarshall Clow     std::replace_if(std::begin(ia), std::end(ia), equalToTwo, 5);
3312c7423fSMarshall Clow     return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
3412c7423fSMarshall Clow         ;
3512c7423fSMarshall Clow     }
3612c7423fSMarshall Clow #endif
3712c7423fSMarshall Clow 
38315cd1feSMarshall Clow 
395a83710eSEric Fiselier template <class Iter>
405a83710eSEric Fiselier void
test()415a83710eSEric Fiselier test()
425a83710eSEric Fiselier {
435a83710eSEric Fiselier     int ia[] = {0, 1, 2, 3, 4};
445a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
45315cd1feSMarshall Clow     std::replace_if(Iter(ia), Iter(ia+sa), equalToTwo, 5);
465a83710eSEric Fiselier     assert(ia[0] == 0);
475a83710eSEric Fiselier     assert(ia[1] == 1);
485a83710eSEric Fiselier     assert(ia[2] == 5);
495a83710eSEric Fiselier     assert(ia[3] == 3);
505a83710eSEric Fiselier     assert(ia[4] == 4);
515a83710eSEric Fiselier }
525a83710eSEric Fiselier 
main(int,char **)53*2df59c50SJF Bastien int main(int, char**)
545a83710eSEric Fiselier {
555a83710eSEric Fiselier     test<forward_iterator<int*> >();
565a83710eSEric Fiselier     test<bidirectional_iterator<int*> >();
575a83710eSEric Fiselier     test<random_access_iterator<int*> >();
585a83710eSEric Fiselier     test<int*>();
5912c7423fSMarshall Clow 
6012c7423fSMarshall Clow #if TEST_STD_VER > 17
6112c7423fSMarshall Clow     static_assert(test_constexpr());
6212c7423fSMarshall Clow #endif
63*2df59c50SJF Bastien 
64*2df59c50SJF Bastien   return 0;
655a83710eSEric Fiselier }
66