xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.replace/replace.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, class T>
125a83710eSEric Fiselier //   requires OutputIterator<Iter, Iter::reference>
135a83710eSEric Fiselier //         && OutputIterator<Iter, const T&>
145a83710eSEric Fiselier //         && HasEqualTo<Iter::value_type, T>
1512c7423fSMarshall Clow //   constexpr void      // constexpr after C++17
165a83710eSEric Fiselier //   replace(Iter first, Iter last, const T& old_value, const T& new_value);
175a83710eSEric Fiselier 
185a83710eSEric Fiselier #include <algorithm>
195a83710eSEric Fiselier #include <cassert>
205a83710eSEric Fiselier 
2112c7423fSMarshall Clow #include "test_macros.h"
225a83710eSEric Fiselier #include "test_iterators.h"
235a83710eSEric Fiselier 
2412c7423fSMarshall Clow 
2512c7423fSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()2612c7423fSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
2712c7423fSMarshall Clow           int ia[]       = {0, 1, 2, 3, 4};
2812c7423fSMarshall Clow     const int expected[] = {0, 1, 5, 3, 4};
2912c7423fSMarshall Clow 
3012c7423fSMarshall Clow     std::replace(std::begin(ia), std::end(ia), 2, 5);
3112c7423fSMarshall Clow     return std::equal(std::begin(ia), std::end(ia), std::begin(expected), std::end(expected))
3212c7423fSMarshall Clow         ;
3312c7423fSMarshall Clow     }
3412c7423fSMarshall Clow #endif
3512c7423fSMarshall Clow 
365a83710eSEric Fiselier template <class Iter>
375a83710eSEric Fiselier void
test()385a83710eSEric Fiselier test()
395a83710eSEric Fiselier {
405a83710eSEric Fiselier     int ia[] = {0, 1, 2, 3, 4};
415a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
425a83710eSEric Fiselier     std::replace(Iter(ia), Iter(ia+sa), 2, 5);
435a83710eSEric Fiselier     assert(ia[0] == 0);
445a83710eSEric Fiselier     assert(ia[1] == 1);
455a83710eSEric Fiselier     assert(ia[2] == 5);
465a83710eSEric Fiselier     assert(ia[3] == 3);
475a83710eSEric Fiselier     assert(ia[4] == 4);
485a83710eSEric Fiselier }
495a83710eSEric Fiselier 
main(int,char **)50*2df59c50SJF Bastien int main(int, char**)
515a83710eSEric Fiselier {
525a83710eSEric Fiselier     test<forward_iterator<int*> >();
535a83710eSEric Fiselier     test<bidirectional_iterator<int*> >();
545a83710eSEric Fiselier     test<random_access_iterator<int*> >();
555a83710eSEric Fiselier     test<int*>();
5612c7423fSMarshall Clow 
5712c7423fSMarshall Clow #if TEST_STD_VER > 17
5812c7423fSMarshall Clow     static_assert(test_constexpr());
5912c7423fSMarshall Clow #endif
60*2df59c50SJF Bastien 
61*2df59c50SJF Bastien   return 0;
625a83710eSEric Fiselier }
63