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 Iter, Callable<auto, Iter::reference> Function>
125a83710eSEric Fiselier // requires CopyConstructible<Function>
131b9a4ffdSMarshall Clow // constexpr Function // constexpr after C++17
145a83710eSEric Fiselier // for_each(Iter first, Iter last, Function f);
155a83710eSEric Fiselier
165a83710eSEric Fiselier #include <algorithm>
175a83710eSEric Fiselier #include <cassert>
185a83710eSEric Fiselier
191b9a4ffdSMarshall Clow #include "test_macros.h"
205a83710eSEric Fiselier #include "test_iterators.h"
215a83710eSEric Fiselier
221b9a4ffdSMarshall Clow #if TEST_STD_VER > 17
test_constexpr()231b9a4ffdSMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
241b9a4ffdSMarshall Clow int ia[] = {1, 3, 6, 7};
251b9a4ffdSMarshall Clow int expected[] = {3, 5, 8, 9};
261b9a4ffdSMarshall Clow
271b9a4ffdSMarshall Clow std::for_each(std::begin(ia), std::end(ia), [](int &a) { a += 2; });
281b9a4ffdSMarshall Clow return std::equal(std::begin(ia), std::end(ia), std::begin(expected))
291b9a4ffdSMarshall Clow ;
301b9a4ffdSMarshall Clow }
311b9a4ffdSMarshall Clow #endif
321b9a4ffdSMarshall Clow
335a83710eSEric Fiselier struct for_each_test
345a83710eSEric Fiselier {
for_each_testfor_each_test355a83710eSEric Fiselier for_each_test(int c) : count(c) {}
365a83710eSEric Fiselier int count;
operator ()for_each_test375a83710eSEric Fiselier void operator()(int& i) {++i; ++count;}
385a83710eSEric Fiselier };
395a83710eSEric Fiselier
main(int,char **)402df59c50SJF Bastien int main(int, char**)
415a83710eSEric Fiselier {
425a83710eSEric Fiselier int ia[] = {0, 1, 2, 3, 4, 5};
435a83710eSEric Fiselier const unsigned s = sizeof(ia)/sizeof(ia[0]);
44*773ae441SChristopher Di Bella for_each_test f = std::for_each(cpp17_input_iterator<int*>(ia),
45*773ae441SChristopher Di Bella cpp17_input_iterator<int*>(ia+s),
465a83710eSEric Fiselier for_each_test(0));
475a83710eSEric Fiselier assert(f.count == s);
485a83710eSEric Fiselier for (unsigned i = 0; i < s; ++i)
49e17a155cSStephan T. Lavavej assert(ia[i] == static_cast<int>(i+1));
501b9a4ffdSMarshall Clow
511b9a4ffdSMarshall Clow #if TEST_STD_VER > 17
521b9a4ffdSMarshall Clow static_assert(test_constexpr());
531b9a4ffdSMarshall Clow #endif
542df59c50SJF Bastien
552df59c50SJF Bastien return 0;
565a83710eSEric Fiselier }
57