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