xref: /llvm-project/pstl/test/std/algorithms/alg.nonmodifying/find_first_of.pass.cpp (revision 3b9a1bb1af90db9472340ef2122d3855eb9ba3fc)
1 // -*- C++ -*-
2 //===-- find_first_of.pass.cpp --------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 #include "support/pstl_test_config.h"
13 
14 #include <execution>
15 #include <algorithm>
16 
17 #include "support/utils.h"
18 
19 using namespace TestUtils;
20 
21 struct test_one_policy
22 {
23 #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) ||                                                             \
24     defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration
25     template <typename Iterator1, typename Iterator2, typename Predicate>
26     void
operator ()test_one_policy27     operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
28                Predicate pred)
29     {
30     }
31     template <typename Iterator1, typename Iterator2, typename Predicate>
32     void
operator ()test_one_policy33     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
34                Predicate pred)
35     {
36     }
37 #endif
38 
39     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate>
40     void
operator ()test_one_policy41     operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred)
42     {
43         using namespace std;
44         Iterator1 expected = find_first_of(b, e, bsub, esub, pred);
45         Iterator1 actual = find_first_of(exec, b, e, bsub, esub, pred);
46         EXPECT_TRUE(actual == expected, "wrong return result from find_first_of with a predicate");
47 
48         expected = find_first_of(b, e, bsub, esub);
49         actual = find_first_of(exec, b, e, bsub, esub);
50         EXPECT_TRUE(actual == expected, "wrong return result from find_first_of");
51     }
52 };
53 
54 template <typename T, typename Predicate>
55 void
test(Predicate pred)56 test(Predicate pred)
57 {
58 
59     const std::size_t max_n1 = 1000;
60     const std::size_t max_n2 = (max_n1 * 10) / 8;
61     Sequence<T> in1(max_n1, [](std::size_t) { return T(1); });
62     Sequence<T> in2(max_n2, [](std::size_t) { return T(0); });
63     for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
64     {
65         std::size_t sub_n[] = {0, 1, n1 / 3, n1, (n1 * 10) / 8};
66         for (const auto n2 : sub_n)
67         {
68             invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + n1, in2.data(), in2.data() + n2, pred);
69 
70             in2[n2 / 2] = T(1);
71             invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.data(), in2.data() + n2,
72                                    pred);
73 
74             if (n2 >= 3)
75             {
76                 in2[2 * n2 / 3] = T(1);
77                 invoke_on_all_policies(test_one_policy(), in1.cbegin(), in1.cbegin() + n1, in2.begin(),
78                                        in2.begin() + n2, pred);
79                 in2[2 * n2 / 3] = T(0);
80             }
81             in2[n2 / 2] = T(0);
82         }
83     }
84     invoke_on_all_policies(test_one_policy(), in1.begin(), in1.begin() + max_n1 / 10, in1.data(),
85                            in1.data() + max_n1 / 10, pred);
86 }
87 
88 template <typename T>
89 struct test_non_const
90 {
91     template <typename Policy, typename FirstIterator, typename SecondInterator>
92     void
operator ()test_non_const93     operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
94     {
95         invoke_if(exec, [&]() {
96             find_first_of(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
97         });
98     }
99 };
100 
101 int
main()102 main()
103 {
104     test<int32_t>(std::equal_to<int32_t>());
105     test<uint16_t>(std::not_equal_to<uint16_t>());
106     test<float64_t>([](const float64_t x, const float64_t y) { return x * x == y * y; });
107 
108     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
109 
110     std::cout << done() << std::endl;
111     return 0;
112 }
113