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 | // UNSUPPORTED: c++03, c++11, c++14, c++17 |
12 | |
13 | // template<input_iterator I, class Proj = identity, |
14 | // indirectly_unary_invocable<projected<I, Proj>> Fun> |
15 | // constexpr ranges::for_each_n_result<I, Fun> |
16 | // ranges::for_each_n(I first, iter_difference_t<I> n, Fun f, Proj proj = {}); |
17 | |
18 | #include <algorithm> |
19 | #include <array> |
20 | #include <ranges> |
21 | |
22 | #include "almost_satisfies_types.h" |
23 | #include "test_iterators.h" |
24 | |
25 | struct Callable { |
26 | void operator()(int); |
27 | }; |
28 | |
29 | template <class Iter> |
30 | concept HasForEachN = requires (Iter iter) { std::ranges::for_each_n(iter, 0, Callable{}); }; |
31 | |
32 | static_assert(HasForEachN<int*>); |
33 | static_assert(!HasForEachN<InputIteratorNotDerivedFrom>); |
34 | static_assert(!HasForEachN<InputIteratorNotIndirectlyReadable>); |
35 | static_assert(!HasForEachN<InputIteratorNotInputOrOutputIterator>); |
36 | |
37 | template <class Func> |
38 | concept HasForEachItFunc = requires(int* a, int b, Func func) { std::ranges::for_each_n(a, b, func); }; |
39 | |
40 | static_assert(HasForEachItFunc<Callable>); |
41 | static_assert(!HasForEachItFunc<IndirectUnaryPredicateNotPredicate>); |
42 | static_assert(!HasForEachItFunc<IndirectUnaryPredicateNotCopyConstructible>); |
43 | |
44 | template <class Iter> |
45 | constexpr void test_iterator() { |
46 | { // simple test |
47 | auto func = [i = 0](int& a) mutable { a += i++; }; |
48 | int a[] = {1, 6, 3, 4}; |
49 | std::same_as<std::ranges::for_each_result<Iter, decltype(func)>> auto ret = |
50 | std::ranges::for_each_n(Iter(a), 4, func); |
51 | assert(a[0] == 1); |
52 | assert(a[1] == 7); |
53 | assert(a[2] == 5); |
54 | assert(a[3] == 7); |
55 | assert(base(ret.in) == a + 4); |
56 | int i = 0; |
57 | ret.fun(i); |
58 | assert(i == 4); |
59 | } |
60 | |
61 | { // check that an empty range works |
62 | std::array<int, 0> a = {}; |
63 | std::ranges::for_each_n(Iter(a.data()), 0, [](auto&) { assert(false); }); |
64 | } |
65 | } |
66 | |
67 | constexpr bool test() { |
68 | test_iterator<cpp17_input_iterator<int*>>(); |
69 | test_iterator<cpp20_input_iterator<int*>>(); |
70 | test_iterator<forward_iterator<int*>>(); |
71 | test_iterator<bidirectional_iterator<int*>>(); |
72 | test_iterator<random_access_iterator<int*>>(); |
73 | test_iterator<contiguous_iterator<int*>>(); |
74 | test_iterator<int*>(); |
75 | |
76 | { // check that std::invoke is used |
77 | struct S { |
78 | int check; |
79 | int other; |
80 | }; |
81 | |
82 | S a[] = {{.check: 1, .other: 2}, {.check: 3, .other: 4}, {.check: 5, .other: 6}}; |
83 | std::ranges::for_each_n(a, 3, [](int& i) { i = 0; }, &S::check); |
84 | assert(a[0].check == 0); |
85 | assert(a[0].other == 2); |
86 | assert(a[1].check == 0); |
87 | assert(a[1].other == 4); |
88 | assert(a[2].check == 0); |
89 | assert(a[2].other == 6); |
90 | } |
91 | |
92 | return true; |
93 | } |
94 | |
95 | int main(int, char**) { |
96 | test(); |
97 | static_assert(test()); |
98 | |
99 | return 0; |
100 | } |
101 | |