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

source code of pstl/test/std/algorithms/alg.nonmodifying/find_end.pass.cpp