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// UNSUPPORTED: c++03, c++11, c++14
11
12// template<class InputIterator, class Size, class Function>
13// constexpr InputIterator // constexpr after C++17
14// for_each_n(InputIterator first, Size n, Function f);
15
16#include <algorithm>
17#include <cassert>
18#include <deque>
19#include <functional>
20#include <iterator>
21#include <list>
22#include <ranges>
23#include <vector>
24
25#include "test_macros.h"
26#include "test_iterators.h"
27
28struct for_each_test {
29 TEST_CONSTEXPR for_each_test(int c) : count(c) {}
30 int count;
31 TEST_CONSTEXPR_CXX14 void operator()(int& i) {
32 ++i;
33 ++count;
34 }
35};
36
37struct deque_test {
38 std::deque<int>* d_;
39 int* i_;
40
41 deque_test(std::deque<int>& d, int& i) : d_(&d), i_(&i) {}
42
43 void operator()(int& v) {
44 assert(&(*d_)[*i_] == &v);
45 ++*i_;
46 }
47};
48
49/*TEST_CONSTEXPR_CXX26*/
50void test_deque_and_join_view_iterators() { // TODO: Mark as TEST_CONSTEXPR_CXX26 once std::deque is constexpr
51 { // Verify that segmented deque iterators work properly
52 int sizes[] = {0, 1, 2, 1023, 1024, 1025, 2047, 2048, 2049};
53 for (const int size : sizes) {
54 std::deque<int> d(size);
55 int index = 0;
56
57 std::for_each_n(first: d.begin(), n: d.size(), f: deque_test(d, index));
58 }
59 }
60#if TEST_STD_VER >= 20
61 { // Verify that join_view of lists work properly. Note that join_view of (non-random access) lists does
62 // not produce segmented iterators.
63 std::list<std::list<int>> lst = {{}, {0}, {1, 2}, {}, {3, 4, 5}, {6, 7, 8, 9}, {10}, {11, 12, 13}};
64 auto v = lst | std::views::join;
65 std::for_each_n(v.begin(), std::ranges::distance(v), [i = 0](int& a) mutable { assert(a == i++); });
66 }
67#endif
68}
69
70TEST_CONSTEXPR_CXX20 bool test() {
71 {
72 typedef cpp17_input_iterator<int*> Iter;
73 int ia[] = {0, 1, 2, 3, 4, 5};
74 const unsigned s = sizeof(ia) / sizeof(ia[0]);
75
76 {
77 unsigned count = 0;
78 Iter it = std::for_each_n(first: Iter(ia), n: 0, f: [&count](int& i) {
79 ++i;
80 ++count;
81 });
82 assert(it == Iter(ia));
83 assert(count == 0);
84 }
85
86 {
87 unsigned count = 0;
88 Iter it = std::for_each_n(first: Iter(ia), n: s, f: [&count](int& i) {
89 ++i;
90 ++count;
91 });
92 assert(it == Iter(ia + s));
93 assert(count == s);
94 for (unsigned i = 0; i < s; ++i)
95 assert(ia[i] == static_cast<int>(i + 1));
96 }
97
98 {
99 unsigned count = 0;
100 Iter it = std::for_each_n(first: Iter(ia), n: 1, f: [&count](int& i) {
101 ++i;
102 ++count;
103 });
104 assert(it == Iter(ia + 1));
105 assert(count == 1);
106 for (unsigned i = 0; i < 1; ++i)
107 assert(ia[i] == static_cast<int>(i + 2));
108 }
109 }
110
111 {
112 int ia[] = {1, 3, 6, 7};
113 int expected[] = {3, 5, 8, 9};
114 const std::size_t N = 4;
115
116 auto it = std::for_each_n(first: std::begin(arr&: ia), n: N, f: [](int& a) { a += 2; });
117 assert(it == (std::begin(ia) + N) && std::equal(std::begin(ia), std::end(ia), std::begin(expected)));
118 }
119
120 if (!TEST_IS_CONSTANT_EVALUATED) // TODO: Use TEST_STD_AT_LEAST_26_OR_RUNTIME_EVALUATED when std::deque is made constexpr
121 test_deque_and_join_view_iterators();
122
123#if TEST_STD_VER >= 20
124 { // join_views of (random-access) vectors yield segmented iterators
125 std::vector<std::vector<int>> vec = {{}, {0}, {1, 2}, {}, {3, 4, 5}, {6, 7, 8, 9}, {10}, {11, 12, 13}};
126 auto v = vec | std::views::join;
127 std::for_each_n(v.begin(), std::ranges::distance(v), [i = 0](int& a) mutable { assert(a == i++); });
128 }
129#endif
130
131 return true;
132}
133
134int main(int, char**) {
135 assert(test());
136#if TEST_STD_VER > 17
137 static_assert(test());
138#endif
139
140 return 0;
141}
142

source code of libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/for_each_n.pass.cpp