Synchronize libstdc++ PSTL with upstream LLVM PSTL
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_nonmodifying / find_end.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- find_end.pass.cpp -------------------------------------------------===//
7 //
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "pstl/pstl_test_config.h"
15
16 #ifdef PSTL_STANDALONE_TESTS
17 #include "pstl/execution"
18 #include "pstl/algorithm"
19 #else
20 #include <execution>
21 #include <algorithm>
22 #endif // PSTL_STANDALONE_TESTS
23
24 #include "pstl/test_utils.h"
25
26 using namespace TestUtils;
27
28 struct test_one_policy
29 {
30 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \
31 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration
32 template <typename Iterator1, typename Iterator2, typename Predicate>
33 void
34 operator()(pstl::execution::unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
35 Predicate pred)
36 {
37 }
38 template <typename Iterator1, typename Iterator2, typename Predicate>
39 void
40 operator()(pstl::execution::parallel_unsequenced_policy, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub,
41 Predicate pred)
42 {
43 }
44 #endif
45
46 template <typename ExecutionPolicy, typename Iterator1, typename Iterator2, typename Predicate>
47 void
48 operator()(ExecutionPolicy&& exec, Iterator1 b, Iterator1 e, Iterator2 bsub, Iterator2 esub, Predicate pred)
49 {
50 using namespace std;
51 // For find_end
52 {
53 auto expected = find_end(b, e, bsub, esub, pred);
54 auto actual = find_end(exec, b, e, bsub, esub);
55 EXPECT_TRUE(actual == expected, "wrong return result from find_end");
56
57 actual = find_end(exec, b, e, bsub, esub, pred);
58 EXPECT_TRUE(actual == expected, "wrong return result from find_end with a predicate");
59 }
60
61 // For search
62 {
63 auto expected = search(b, e, bsub, esub, pred);
64 auto actual = search(exec, b, e, bsub, esub);
65 EXPECT_TRUE(actual == expected, "wrong return result from search");
66
67 actual = search(exec, b, e, bsub, esub, pred);
68 EXPECT_TRUE(actual == expected, "wrong return result from search with a predicate");
69 }
70 }
71 };
72
73 template <typename T>
74 void
75 test(const std::size_t bits)
76 {
77
78 const std::size_t max_n1 = 1000;
79 const std::size_t max_n2 = (max_n1 * 10) / 8;
80 Sequence<T> in(max_n1, [max_n1, bits](std::size_t k) { return T(2 * HashBits(max_n1, bits - 1) ^ 1); });
81 Sequence<T> sub(max_n2, [max_n1, bits](std::size_t k) { return T(2 * HashBits(max_n1, bits - 1)); });
82 for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
83 {
84 std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8};
85 std::size_t res[] = {0, 1, n1 / 2, n1};
86 for (auto n2 : sub_n)
87 {
88 for (auto r : res)
89 {
90 std::size_t i = r, isub = 0;
91 for (; i < n1 & isub < n2; ++i, ++isub)
92 in[i] = sub[isub];
93 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, sub.begin(), sub.begin() + n2,
94 std::equal_to<T>());
95 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, sub.cbegin(),
96 sub.cbegin() + n2, std::equal_to<T>());
97 }
98 }
99 }
100 }
101
102 template <typename T>
103 struct test_non_const
104 {
105 template <typename Policy, typename FirstIterator, typename SecondInterator>
106 void
107 operator()(Policy&& exec, FirstIterator first_iter, SecondInterator second_iter)
108 {
109 invoke_if(exec, [&]() {
110 find_end(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
111 search(exec, first_iter, first_iter, second_iter, second_iter, non_const(std::equal_to<T>()));
112 });
113 }
114 };
115
116 int32_t
117 main()
118 {
119 test<int32_t>(8 * sizeof(int32_t));
120 test<uint16_t>(8 * sizeof(uint16_t));
121 test<float64_t>(53);
122 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN
123 test<bool>(1);
124 #endif
125
126 test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>());
127
128 std::cout << done() << std::endl;
129 return 0;
130 }