1 // -*- C++ -*-
2 // { dg-options "-ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-timeout-factor 3 }
5 // { dg-require-effective-target tbb-backend }
6 
7 //===-- find_first_of.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 struct test_one_policy
30 {
31 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
32     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
33     template <typename Iterator1, typename Iterator2, typename Predicate>
34     void
operator ()test_one_policy35     operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
36                Predicate pred)
37     {
38     }
39     template <typename Iterator1, typename Iterator2, typename Predicate>
40     void
operator ()test_one_policy41     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
42                Predicate pred)
43     {
44     }
45 #endif
46 
47     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate>
48     void
operator ()test_one_policy49     operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred)
50     {
51         using namespace std;
52         Iterator1 expected = find_first_of(b, e, bsub, esub, pred);
53         Iterator1 actual = find_first_of(exec, b, e, bsub, esub, pred);
54         EXPECT_TRUE(actual == expected, "wrong return result from find_first_of with a predicate");
55 
56         expected = find_first_of(b, e, bsub, esub);
57         actual = find_first_of(exec, b, e, bsub, esub);
58         EXPECT_TRUE(actual == expected, "wrong return result from find_first_of");
59     }
60 };
61 
62 template <typename T, typename Predicate>
63 void
test(Predicate pred)64 test(Predicate pred)
65 {
66 
67     const std::size_t max_n1 = 1000;
68     const std::size_t max_n2 = (max_n1 * 10) / 8;
69     Sequence<T> in1(max_n1, [](std::size_t k) { return T(1); });
70     Sequence<T> in2(max_n2, [](std::size_t k) { return T(0); });
71     for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
72     {
73         std::size_t sub_n[] = {0, 1, n1 / 3, n1, (n1 * 10) / 8};
74         for (const auto n2 : sub_n)
75         {
76             invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.data(), in2.data() + n2, pred);
77 
78             in2[n2 / 2] = T(1);
79             invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.data(), in2.data() + n2,
80                                    pred);
81 
82             if (n2 >= 3)
83             {
84                 in2[2 * n2 / 3] = T(1);
85                 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.begin(),
86                                        in2.begin() + n2, pred);
87                 in2[2 * n2 / 3] = T(0);
88             }
89             in2[n2 / 2] = T(0);
90         }
91     }
92     invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n1 / 10, in1.data(),
93                            in1.data() + max_n1 / 10, pred);
94 }
95 
96 template <typename T>
97 struct test_non_const
98 {
99     template <typename Policy, typename FirstIterator, typename SecondInterator>
100     void
operator ()test_non_const101     operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
102     {
103         invoke_if(exec, [&]() {
104             find_first_of(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
105         });
106     }
107 };
108 
109 int32_t
main()110 main()
111 {
112     test<int32_t>(std::equal_to<int32_t>());
113     test<uint16_t>(std::not_equal_to<uint16_t>());
114     test<float64_t>([](const float64_t x, const float64_t y) { return x * x == y * y; });
115 
116     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
117 
118     std::cout << done() << std::endl;
119     return 0;
120 }
121