xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.foreach/pstl.for_each_n.pass.cpp (revision ad01447d30ed48e127254e0c45350c938d72c966)
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 // UNSUPPORTED: c++03, c++11, c++14
10 
11 // UNSUPPORTED: libcpp-has-no-incomplete-pstl
12 
13 // <algorithm>
14 
15 // template<class ExecutionPolicy, class ForwardIterator, class Size, class Function>
16 //   ForwardIterator for_each_n(ExecutionPolicy&& exec, ForwardIterator first, Size n,
17 //                              Function f);
18 
19 #include <algorithm>
20 #include <atomic>
21 #include <cassert>
22 #include <vector>
23 
24 #include "test_macros.h"
25 #include "test_execution_policies.h"
26 #include "test_iterators.h"
27 
28 EXECUTION_POLICY_SFINAE_TEST(for_each_n);
29 
30 static_assert(sfinae_test_for_each_n<int, int*, int, bool (*)(int)>);
31 static_assert(!sfinae_test_for_each_n<std::execution::parallel_policy, int*, int, bool (*)(int)>);
32 
33 template <class Iter>
34 struct Test {
35   template <class Policy>
operator ()Test36   void operator()(Policy&& policy) {
37     int sizes[] = {0, 1, 2, 100};
38     for (auto size : sizes) {
39       std::vector<int> a(size);
40       std::vector<Bool> called(size);
41       std::for_each_n(policy, Iter(std::data(a)), std::size(a), [&](int& v) {
42         assert(!called[&v - a.data()]);
43         called[&v - a.data()] = true;
44       });
45       assert(std::all_of(std::begin(called), std::end(called), [](bool b) { return b; }));
46     }
47   }
48 };
49 
main(int,char **)50 int main(int, char**) {
51   types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
52 
53   return 0;
54 }
55