1 // -*- C++ -*-
2 //===-- find_end.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 #ifdef PSTL_STANDALONE_TESTS
13 #include "pstl/execution"
14 #include "pstl/algorithm"
15 #else
16 #include <execution>
17 #include <algorithm>
18 #endif // PSTL_STANDALONE_TESTS
19 
20 #include "support/utils.h"
21 
22 using namespace TestUtils;
23 
24 struct test_one_policy
25 {
26 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN ||                                                            \
27     _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
28     template <typename Iterator1, typename Iterator2, typename Predicate>
29     void
30     operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
31                Predicate pred)
32     {
33     }
34     template <typename Iterator1, typename Iterator2, typename Predicate>
35     void
36     operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
37                Predicate pred)
38     {
39     }
40 #endif
41 
42     template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate>
43     void
44     operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred)
45     {
46         using namespace std;
47         // For find_end
48         {
49             auto expected = find_end(b, e, bsub, esub, pred);
50             auto actual = find_end(exec, b, e, bsub, esub);
51             EXPECT_TRUE(actual == expected, "wrong return result from find_end");
52 
53             actual = find_end(exec, b, e, bsub, esub, pred);
54             EXPECT_TRUE(actual == expected, "wrong return result from find_end with a predicate");
55         }
56 
57         // For search
58         {
59             auto expected = search(b, e, bsub, esub, pred);
60             auto actual = search(exec, b, e, bsub, esub);
61             EXPECT_TRUE(actual == expected, "wrong return result from search");
62 
63             actual = search(exec, b, e, bsub, esub, pred);
64             EXPECT_TRUE(actual == expected, "wrong return result from search with a predicate");
65         }
66     }
67 };
68 
69 template <typename T>
70 void
71 test(const std::size_t bits)
72 {
73 
74     const std::size_t max_n1 = 1000;
75     const std::size_t max_n2 = (max_n1 * 10) / 8;
76     Sequence<T> in(max_n1, [max_n1, bits](std::size_t k) { return T(2 * HashBits(max_n1, bits - 1) ^ 1); });
77     Sequence<T> sub(max_n2, [max_n1, bits](std::size_t k) { return T(2 * HashBits(max_n1, bits - 1)); });
78     for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
79     {
80         std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8};
81         std::size_t res[] = {0, 1, n1 / 2, n1};
82         for (auto n2 : sub_n)
83         {
84             for (auto r : res)
85             {
86                 std::size_t i = r, isub = 0;
87                 for (; i < n1 & isub < n2; ++i, ++isub)
88                     in[i] = sub[isub];
89                 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, sub.begin(), sub.begin() + n2,
90                                        std::equal_to<T>());
91                 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, sub.cbegin(),
92                                        sub.cbegin() + n2, std::equal_to<T>());
93             }
94         }
95     }
96 }
97 
98 template <typename T>
99 struct test_non_const
100 {
101     template <typename Policy, typename FirstIterator, typename SecondInterator>
102     void
103     operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
104     {
105         invoke_if(exec, [&]() {
106             find_end(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
107             search(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
108         });
109     }
110 };
111 
112 int32_t
113 main()
114 {
115     test<int32_t>(8 * sizeof(int32_t));
116     test<uint16_t>(8 * sizeof(uint16_t));
117     test<float64_t>(53);
118 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN
119     test<bool>(1);
120 #endif
121 
122     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
123 
124     std::cout << done() << std::endl;
125     return 0;
126 }
127