1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <algorithm> 11 12 // template<InputIterator InIter, typename OutIter, 13 // Predicate<auto, InIter::value_type> Pred, class T> 14 // requires OutputIterator<OutIter, InIter::reference> 15 // && OutputIterator<OutIter, const T&> 16 // && CopyConstructible<Pred> 17 // constexpr OutIter // constexpr after C++17 18 // replace_copy_if(InIter first, InIter last, OutIter result, Pred pred, const T& new_value); 19 20 #include <algorithm> 21 #include <functional> 22 #include <cassert> 23 24 #include "test_macros.h" 25 #include "test_iterators.h" 26 27 TEST_CONSTEXPR bool equalToTwo(int v) { return v == 2; } 28 29 30 #if TEST_STD_VER > 17 31 TEST_CONSTEXPR bool test_constexpr() { 32 int ia[] = {0, 1, 2, 3, 4}; 33 int ib[] = {0, 0, 0, 0, 0, 0}; // one bigger 34 const int expected[] = {0, 1, 5, 3, 4}; 35 36 auto it = std::replace_copy_if(std::begin(ia), std::end(ia), std::begin(ib), equalToTwo, 5); 37 38 return it == (std::begin(ib) + std::size(ia)) 39 && *it == 0 // don't overwrite the last value in the output array 40 && std::equal(std::begin(ib), it, std::begin(expected), std::end(expected)) 41 ; 42 } 43 #endif 44 45 template <class InIter, class OutIter> 46 void 47 test() 48 { 49 int ia[] = {0, 1, 2, 3, 4}; 50 const unsigned sa = sizeof(ia)/sizeof(ia[0]); 51 int ib[sa] = {0}; 52 OutIter r = std::replace_copy_if(InIter(ia), InIter(ia+sa), 53 OutIter(ib), equalToTwo, 5); 54 assert(base(r) == ib + sa); 55 assert(ib[0] == 0); 56 assert(ib[1] == 1); 57 assert(ib[2] == 5); 58 assert(ib[3] == 3); 59 assert(ib[4] == 4); 60 } 61 62 int main() 63 { 64 test<input_iterator<const int*>, output_iterator<int*> >(); 65 test<input_iterator<const int*>, forward_iterator<int*> >(); 66 test<input_iterator<const int*>, bidirectional_iterator<int*> >(); 67 test<input_iterator<const int*>, random_access_iterator<int*> >(); 68 test<input_iterator<const int*>, int*>(); 69 70 test<forward_iterator<const int*>, output_iterator<int*> >(); 71 test<forward_iterator<const int*>, forward_iterator<int*> >(); 72 test<forward_iterator<const int*>, bidirectional_iterator<int*> >(); 73 test<forward_iterator<const int*>, random_access_iterator<int*> >(); 74 test<forward_iterator<const int*>, int*>(); 75 76 test<bidirectional_iterator<const int*>, output_iterator<int*> >(); 77 test<bidirectional_iterator<const int*>, forward_iterator<int*> >(); 78 test<bidirectional_iterator<const int*>, bidirectional_iterator<int*> >(); 79 test<bidirectional_iterator<const int*>, random_access_iterator<int*> >(); 80 test<bidirectional_iterator<const int*>, int*>(); 81 82 test<random_access_iterator<const int*>, output_iterator<int*> >(); 83 test<random_access_iterator<const int*>, forward_iterator<int*> >(); 84 test<random_access_iterator<const int*>, bidirectional_iterator<int*> >(); 85 test<random_access_iterator<const int*>, random_access_iterator<int*> >(); 86 test<random_access_iterator<const int*>, int*>(); 87 88 test<const int*, output_iterator<int*> >(); 89 test<const int*, forward_iterator<int*> >(); 90 test<const int*, bidirectional_iterator<int*> >(); 91 test<const int*, random_access_iterator<int*> >(); 92 test<const int*, int*>(); 93 94 #if TEST_STD_VER > 17 95 static_assert(test_constexpr()); 96 #endif 97 } 98