xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each.pass.cpp (revision c81bfc61da7e3a17a0ecd42ad049c9cd74384192)
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 {
for_each_testfor_each_test27   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;
operator ()for_each_test36   TEST_CONSTEXPR_CXX14 void operator()(int& i) {
37     ++i;
38     ++count;
39   }
40 };
41 
42 struct Test {
43   template <class Iter>
operator ()Test44   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 
test()56 TEST_CONSTEXPR_CXX20 bool test() {
57   types::for_each(types::cpp17_input_iterator_list<int*>(), Test());
58 
59   // TODO: Remove the `_LIBCPP_ENABLE_EXPERIMENTAL` check once we have the FTM guarded or views::join isn't
60   // experimental anymore
61 #if TEST_STD_VER >= 20 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
62   { // Make sure that the segmented iterator optimization works during constant evaluation
63     std::vector<std::vector<int>> vecs = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
64     auto v                             = std::views::join(vecs);
65     std::for_each(v.begin(), v.end(), [i = 0](int& a) mutable { assert(a == ++i); });
66   }
67 #endif
68 
69   return true;
70 }
71 
72 struct deque_test {
73   std::deque<int>* d_;
74   int* i_;
75 
deque_testdeque_test76   deque_test(std::deque<int>& d, int& i) : d_(&d), i_(&i) {}
77 
operator ()deque_test78   void operator()(int& v) {
79     assert(&(*d_)[*i_] == &v);
80     ++*i_;
81   }
82 };
83 
main(int,char **)84 int main(int, char**) {
85   test();
86 #if TEST_STD_VER >= 20
87   static_assert(test());
88 #endif
89 
90   // check that segmented iterators work properly
91   int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
92   for (const int size : sizes) {
93     std::deque<int> d(size);
94     int index = 0;
95 
96     std::for_each(d.begin(), d.end(), deque_test(d, index));
97   }
98 
99   return 0;
100 }
101