]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/25_algorithms/pstl/alg_nonmodifying/for_each.cc
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-require-effective-target tbb-backend }
7 //===-- for_each.pass.cpp -------------------------------------------------===//
9 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
10 // See https://llvm.org/LICENSE.txt for license information.
11 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
13 //===----------------------------------------------------------------------===//
15 #include "pstl/pstl_test_config.h"
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
23 #endif // PSTL_STANDALONE_TESTS
25 #include "pstl/test_utils.h"
27 using namespace TestUtils
;
29 template <typename Type
>
33 operator()(std::size_t k
)
35 return Type(k
% 5 != 1 ? 3 * k
- 7 : 0);
43 Flip(int32_t y
) : val(y
) {}
45 operator()(T
& x
) const
51 struct test_one_policy
53 template <typename Policy
, typename Iterator
, typename Size
>
55 operator()(Policy
&& exec
, Iterator first
, Iterator last
, Iterator expected_first
, Iterator expected_last
, Size n
)
57 typedef typename
std::iterator_traits
<Iterator
>::value_type T
;
60 std::for_each(expected_first
, expected_last
, Flip
<T
>(1));
61 for_each(exec
, first
, last
, Flip
<T
>(1));
62 EXPECT_EQ_N(expected_first
, first
, n
, "wrong effect from for_each");
65 std::for_each_n(__pstl::execution::seq
, expected_first
, n
, Flip
<T
>(1));
66 for_each_n(exec
, first
, n
, Flip
<T
>(1));
67 EXPECT_EQ_N(expected_first
, first
, n
, "wrong effect from for_each_n");
75 for (size_t n
= 0; n
<= 100000; n
= n
<= 16 ? n
+ 1 : size_t(3.1415 * n
))
77 Sequence
<T
> inout(n
, Gen
<T
>());
78 Sequence
<T
> expected(n
, Gen
<T
>());
79 invoke_on_all_policies(test_one_policy(), inout
.begin(), inout
.end(), expected
.begin(), expected
.end(),
86 template <typename Policy
, typename Iterator
>
88 operator()(Policy
&& exec
, Iterator iter
)
90 invoke_if(exec
, [&]() {
91 auto f
= [](typename
std::iterator_traits
<Iterator
>::reference x
) { x
= x
+ 1; };
93 for_each(exec
, iter
, iter
, non_const(f
));
94 for_each_n(exec
, iter
, 0, non_const(f
));
106 test_algo_basic_single
<int32_t>(run_for_rnd_fw
<test_non_const
>());
108 std::cout
<< done() << std::endl
;