Home | History | Annotate | Download | only in alg.foreach
      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 Iter, Callable<auto, Iter::reference> Function>
     13 //   requires CopyConstructible<Function>
     14 //   constexpr Function   // constexpr after C++17
     15 //   for_each(Iter first, Iter last, Function f);
     16 
     17 #include <algorithm>
     18 #include <cassert>
     19 
     20 #include "test_macros.h"
     21 #include "test_iterators.h"
     22 
     23 #if TEST_STD_VER > 17
     24 TEST_CONSTEXPR bool test_constexpr() {
     25     int ia[] = {1, 3, 6, 7};
     26     int expected[] = {3, 5, 8, 9};
     27 
     28     std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; });
     29     return std::equal(std::begin(ia), std::end(ia), std::begin(expected))
     30         ;
     31     }
     32 #endif
     33 
     34 struct for_each_test
     35 {
     36     for_each_test(int c) : count(c) {}
     37     int count;
     38     void operator()(int& i) {++i; ++count;}
     39 };
     40 
     41 int main()
     42 {
     43     int ia[] = {0, 1, 2, 3, 4, 5};
     44     const unsigned s = sizeof(ia)/sizeof(ia[0]);
     45     for_each_test f = std::for_each(input_iterator<int*>(ia),
     46                                     input_iterator<int*>(ia+s),
     47                                     for_each_test(0));
     48     assert(f.count == s);
     49     for (unsigned i = 0; i < s; ++i)
     50         assert(ia[i] == static_cast<int>(i+1));
     51 
     52 #if TEST_STD_VER > 17
     53     static_assert(test_constexpr());
     54 #endif
     55 }
     56