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