| 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | // <algorithm> |
| 10 | |
| 11 | // template<InputIterator Iter, Callable<auto, Iter::reference> Function> |
| 12 | // constexpr Function // constexpr since C++20 |
| 13 | // for_each(Iter first, Iter last, Function f); |
| 14 | |
| 15 | #include <algorithm> |
| 16 | #include <cassert> |
| 17 | #include <deque> |
| 18 | #if __has_include(<ranges>) |
| 19 | # include <ranges> |
| 20 | #endif |
| 21 | #include <vector> |
| 22 | |
| 23 | #include "test_macros.h" |
| 24 | #include "test_iterators.h" |
| 25 | |
| 26 | struct for_each_test { |
| 27 | TEST_CONSTEXPR for_each_test(int c) : count(c) {} |
| 28 | |
| 29 | // for_each functors only have to be move constructible |
| 30 | for_each_test(const for_each_test&) = delete; |
| 31 | for_each_test(for_each_test&&) = default; |
| 32 | for_each_test& operator=(const for_each_test&) = delete; |
| 33 | for_each_test& operator=(for_each_test&&) = delete; |
| 34 | |
| 35 | int count; |
| 36 | TEST_CONSTEXPR_CXX14 void operator()(int& i) { |
| 37 | ++i; |
| 38 | ++count; |
| 39 | } |
| 40 | }; |
| 41 | |
| 42 | struct Test { |
| 43 | template <class Iter> |
| 44 | TEST_CONSTEXPR_CXX20 void operator()() { |
| 45 | int sizes[] = {0, 1, 6}; |
| 46 | for (const int size : sizes) { |
| 47 | int ia[] = {0, 1, 2, 3, 4, 5}; |
| 48 | for_each_test f = std::for_each(Iter(ia), Iter(ia + size), for_each_test(0)); |
| 49 | assert(f.count == size); |
| 50 | for (int i = 0; i < size; ++i) |
| 51 | assert(ia[i] == static_cast<int>(i + 1)); |
| 52 | } |
| 53 | } |
| 54 | }; |
| 55 | |
| 56 | TEST_CONSTEXPR_CXX20 bool test() { |
| 57 | types::for_each(types::cpp17_input_iterator_list<int*>(), Test()); |
| 58 | |
| 59 | #if TEST_STD_VER >= 20 |
| 60 | { // Make sure that the segmented iterator optimization works during constant evaluation |
| 61 | std::vector<std::vector<int>> vecs = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; |
| 62 | auto v = std::views::join(vecs); |
| 63 | std::for_each(v.begin(), v.end(), [i = 0](int& a) mutable { assert(a == ++i); }); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | struct deque_test { |
| 71 | std::deque<int>* d_; |
| 72 | int* i_; |
| 73 | |
| 74 | deque_test(std::deque<int>& d, int& i) : d_(&d), i_(&i) {} |
| 75 | |
| 76 | void operator()(int& v) { |
| 77 | assert(&(*d_)[*i_] == &v); |
| 78 | ++*i_; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | int main(int, char**) { |
| 83 | test(); |
| 84 | #if TEST_STD_VER >= 20 |
| 85 | static_assert(test()); |
| 86 | #endif |
| 87 | |
| 88 | // check that segmented iterators work properly |
| 89 | int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049}; |
| 90 | for (const int size : sizes) { |
| 91 | std::deque<int> d(size); |
| 92 | int index = 0; |
| 93 | |
| 94 | std::for_each(first: d.begin(), last: d.end(), f: deque_test(d, index)); |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |