xref: /llvm-project/pstl/test/std/algorithms/alg.nonmodifying/find_end.pass.cpp (revision 3491119f9ba466ed871a789eb91e3b7577e36272)
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 #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         // For find_end
43         {
44             auto expected = find_end(b, e, bsub, esub, pred);
45             auto actual = find_end(exec, b, e, bsub, esub);
46             EXPECT_TRUE(actual == expected, "wrong return result from find_end");
47 
48             actual = find_end(exec, b, e, bsub, esub, pred);
49             EXPECT_TRUE(actual == expected, "wrong return result from find_end with a predicate");
50         }
51 
52         // For search
53         {
54             auto expected = search(b, e, bsub, esub, pred);
55             auto actual = search(exec, b, e, bsub, esub);
56             EXPECT_TRUE(actual == expected, "wrong return result from search");
57 
58             actual = search(exec, b, e, bsub, esub, pred);
59             EXPECT_TRUE(actual == expected, "wrong return result from search with a predicate");
60         }
61     }
62 };
63 
64 template <typename T>
65 void
66 test(const std::size_t bits)
67 {
68 
69     const std::size_t max_n1 = 1000;
70     const std::size_t max_n2 = (max_n1 * 10) / 8;
71     Sequence<T> in(max_n1, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1) ^ 1); });
72     Sequence<T> sub(max_n2, [bits](std::size_t) { return T(2 * HashBits(max_n1, bits - 1)); });
73     for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
74     {
75         std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8};
76         std::size_t res[] = {0, 1, n1 / 2, n1};
77         for (auto n2 : sub_n)
78         {
79             for (auto r : res)
80             {
81                 std::size_t i = r, isub = 0;
82                 for (; i < n1 & isub < n2; ++i, ++isub)
83                     in[i] = sub[isub];
84                 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, sub.begin(), sub.begin() + n2,
85                                        std::equal_to<T>());
86                 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, sub.cbegin(),
87                                        sub.cbegin() + n2, std::equal_to<T>());
88             }
89         }
90     }
91 }
92 
93 template <typename T>
94 struct test_non_const
95 {
96     template <typename Policy, typename FirstIterator, typename SecondInterator>
97     void
98     operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
99     {
100         invoke_if(exec, [&]() {
101             find_end(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
102             search(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
103         });
104     }
105 };
106 
107 int32_t
108 main()
109 {
110     test<int32_t>(8 * sizeof(int32_t));
111     test<uint16_t>(8 * sizeof(uint16_t));
112     test<float64_t>(53);
113 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN
114     test<bool>(1);
115 #endif
116 
117     test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
118 
119     std::cout << done() << std::endl;
120     return 0;
121 }
122