1 // -*- C++ -*-
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 }
6 
7 //===-- for_each.pass.cpp -------------------------------------------------===//
8 //
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
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "pstl/pstl_test_config.h"
16 
17 #ifdef PSTL_STANDALONE_TESTS
18 #include "pstl/execution"
19 #include "pstl/algorithm"
20 #else
21 #include <execution>
22 #include <algorithm>
23 #endif // PSTL_STANDALONE_TESTS
24 
25 #include "pstl/test_utils.h"
26 
27 using namespace TestUtils;
28 
29 template <typename Type>
30 struct Gen
31 {
32     Type
operator ()Gen33     operator()(std::size_t k)
34     {
35         return Type(k % 5 != 1 ? 3 * k - 7 : 0);
36     };
37 };
38 
39 template <typename T>
40 struct Flip
41 {
42     int32_t val;
FlipFlip43     Flip(int32_t y) : val(y) {}
44     T
operator ()Flip45     operator()(T& x) const
46     {
47         return x = val - x;
48     }
49 };
50 
51 struct test_one_policy
52 {
53     template <typename Policy, typename Iterator, typename Size>
54     void
operator ()test_one_policy55     operator()(Policy&& exec, Iterator first, Iterator last, Iterator expected_first, Iterator expected_last, Size n)
56     {
57         typedef typename std::iterator_traits<Iterator>::value_type T;
58 
59         // Try for_each
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");
63 
64         // Try for_each_n
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");
68     }
69 };
70 
71 template <typename T>
72 void
test()73 test()
74 {
75     for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
76     {
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(),
80                                inout.size());
81     }
82 }
83 
84 struct test_non_const
85 {
86     template <typename Policy, typename Iterator>
87     void
operator ()test_non_const88     operator()(Policy&& exec, Iterator iter)
89     {
90         invoke_if(exec, [&]() {
91             auto f = [](typename std::iterator_traits<Iterator>::reference x) { x = x + 1; };
92 
93             for_each(exec, iter, iter, non_const(f));
94             for_each_n(exec, iter, 0, non_const(f));
95         });
96     }
97 };
98 
99 int32_t
main()100 main()
101 {
102     test<int32_t>();
103     test<uint16_t>();
104     test<float64_t>();
105 
106     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
107 
108     std::cout << done() << std::endl;
109     return 0;
110 }
111