xref: /llvm-project/libcxx/test/std/algorithms/alg.modifying.operations/alg.replace/replace_copy.pass.cpp (revision 5e97d37b960840cabf32dfef3503b28ba5d21dc2)
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<InputIterator InIter, typename OutIter, class T>
125a83710eSEric Fiselier //   requires OutputIterator<OutIter, InIter::reference>
135a83710eSEric Fiselier //         && OutputIterator<OutIter, const T&>
145a83710eSEric Fiselier //         && HasEqualTo<InIter::value_type, T>
1512c7423fSMarshall Clow //   constexpr OutIter      // constexpr after C++17
165a83710eSEric Fiselier //   replace_copy(InIter first, InIter last, OutIter result, const T& old_value,
175a83710eSEric Fiselier //                                                           const T& new_value);
185a83710eSEric Fiselier 
195a83710eSEric Fiselier #include <algorithm>
205a83710eSEric Fiselier #include <cassert>
215a83710eSEric Fiselier 
2212c7423fSMarshall Clow #include "test_macros.h"
235a83710eSEric Fiselier #include "test_iterators.h"
245a83710eSEric Fiselier 
2512c7423fSMarshall Clow 
2612c7423fSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()2712c7423fSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
2812c7423fSMarshall Clow     int ia[] = {0, 1, 2, 3, 4};
2912c7423fSMarshall Clow     int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger
3012c7423fSMarshall Clow     const int expected[] = {0, 1, 5, 3, 4};
3112c7423fSMarshall Clow 
3212c7423fSMarshall Clow     auto it = std::replace_copy(std::begin(ia), std::end(ia), std::begin(ib), 2, 5);
3312c7423fSMarshall Clow 
3412c7423fSMarshall Clow     return it == (std::begin(ib) + std::size(ia))
3512c7423fSMarshall Clow         && *it == 0 // don't overwrite the last value in the output array
3612c7423fSMarshall Clow         && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected))
3712c7423fSMarshall Clow         ;
3812c7423fSMarshall Clow     }
3912c7423fSMarshall Clow #endif
4012c7423fSMarshall Clow 
415a83710eSEric Fiselier template <class InIter, class OutIter>
425a83710eSEric Fiselier void
test()435a83710eSEric Fiselier test()
445a83710eSEric Fiselier {
455a83710eSEric Fiselier     int ia[] = {0, 1, 2, 3, 4};
465a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
475a83710eSEric Fiselier     int ib[sa] = {0};
485a83710eSEric Fiselier     OutIter r = std::replace_copy(InIter(ia), InIter(ia+sa), OutIter(ib), 2, 5);
495a83710eSEric Fiselier     assert(base(r) == ib + sa);
505a83710eSEric Fiselier     assert(ib[0] == 0);
515a83710eSEric Fiselier     assert(ib[1] == 1);
525a83710eSEric Fiselier     assert(ib[2] == 5);
535a83710eSEric Fiselier     assert(ib[3] == 3);
545a83710eSEric Fiselier     assert(ib[4] == 4);
555a83710eSEric Fiselier }
565a83710eSEric Fiselier 
main(int,char **)572df59c50SJF Bastien int main(int, char**)
585a83710eSEric Fiselier {
59*5e97d37bSMark de Wever     test<cpp17_input_iterator<const int*>, cpp17_output_iterator<int*> >();
60773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, forward_iterator<int*> >();
61773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, bidirectional_iterator<int*> >();
62773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, random_access_iterator<int*> >();
63773ae441SChristopher Di Bella     test<cpp17_input_iterator<const int*>, int*>();
645a83710eSEric Fiselier 
65*5e97d37bSMark de Wever     test<forward_iterator<const int*>, cpp17_output_iterator<int*> >();
665a83710eSEric Fiselier     test<forward_iterator<const int*>, forward_iterator<int*> >();
675a83710eSEric Fiselier     test<forward_iterator<const int*>, bidirectional_iterator<int*> >();
685a83710eSEric Fiselier     test<forward_iterator<const int*>, random_access_iterator<int*> >();
695a83710eSEric Fiselier     test<forward_iterator<const int*>, int*>();
705a83710eSEric Fiselier 
71*5e97d37bSMark de Wever     test<bidirectional_iterator<const int*>, cpp17_output_iterator<int*> >();
725a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, forward_iterator<int*> >();
735a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >();
745a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, random_access_iterator<int*> >();
755a83710eSEric Fiselier     test<bidirectional_iterator<const int*>, int*>();
765a83710eSEric Fiselier 
77*5e97d37bSMark de Wever     test<random_access_iterator<const int*>, cpp17_output_iterator<int*> >();
785a83710eSEric Fiselier     test<random_access_iterator<const int*>, forward_iterator<int*> >();
795a83710eSEric Fiselier     test<random_access_iterator<const int*>, bidirectional_iterator<int*> >();
805a83710eSEric Fiselier     test<random_access_iterator<const int*>, random_access_iterator<int*> >();
815a83710eSEric Fiselier     test<random_access_iterator<const int*>, int*>();
825a83710eSEric Fiselier 
83*5e97d37bSMark de Wever     test<const int*, cpp17_output_iterator<int*> >();
845a83710eSEric Fiselier     test<const int*, forward_iterator<int*> >();
855a83710eSEric Fiselier     test<const int*, bidirectional_iterator<int*> >();
865a83710eSEric Fiselier     test<const int*, random_access_iterator<int*> >();
875a83710eSEric Fiselier     test<const int*, int*>();
8812c7423fSMarshall Clow 
8912c7423fSMarshall Clow #if TEST_STD_VER > 17
9012c7423fSMarshall Clow     static_assert(test_constexpr());
9112c7423fSMarshall Clow #endif
922df59c50SJF Bastien 
932df59c50SJF Bastien   return 0;
945a83710eSEric Fiselier }
95