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