1// -*- C++ -*-
2//===-- search_n.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 Iterator, typename Size, typename T, typename Predicate>
26 void
27 operator()(pstl::execution::unsequenced_policy, Iterator b, Iterator e, Size count, const T& value, Predicate pred)
28 {
29 }
30 template <typename Iterator, typename Size, typename T, typename Predicate>
31 void
32 operator()(pstl::execution::parallel_unsequenced_policy, Iterator b, Iterator e, Size count, const T& value,
33 Predicate pred)
34 {
35 }
36#endif
37
38 template <typename ExecutionPolicy, typename Iterator, typename Size, typename T, typename Predicate>
39 void
40 operator()(ExecutionPolicy&& exec, Iterator b, Iterator e, Size count, const T& value, Predicate pred)
41 {
42 using namespace std;
43 auto expected = search_n(b, e, count, value, pred);
44 auto actual = search_n(exec, b, e, count, value);
45 EXPECT_TRUE(actual == expected, "wrong return result from search_n");
46
47 actual = search_n(exec, b, e, count, value, pred);
48 EXPECT_TRUE(actual == expected, "wrong return result from search_n with a predicate");
49 }
50};
51
52template <typename T>
53void
54test()
55{
56
57 const std::size_t max_n1 = 100000;
58 const T value = T(1);
59 for (std::size_t n1 = 0; n1 <= max_n1; n1 = n1 <= 16 ? n1 + 1 : size_t(3.1415 * n1))
60 {
61 std::size_t sub_n[] = {0, 1, 3, n1, (n1 * 10) / 8};
62 std::size_t res[] = {0, 1, n1 / 2, n1};
63 for (auto n2 : sub_n)
64 {
65 // Some of standard libraries return "first" in this case. We return "last" according to the standard
66 if (n2 == 0)
67 {
68 continue;
69 }
70 for (auto r : res)
71 {
72 Sequence<T> in(n1, [](std::size_t) { return T(0); });
73 std::size_t i = r, isub = 0;
74 for (; i < n1 && isub < n2; ++i, ++isub)
75 in[i] = value;
76
77 invoke_on_all_policies(test_one_policy(), in.begin(), in.begin() + n1, n2, value, std::equal_to<T>());
78 invoke_on_all_policies(test_one_policy(), in.cbegin(), in.cbegin() + n1, n2, value, std::equal_to<T>());
79 }
80 }
81 }
82}
83
84template <typename T>
85struct test_non_const
86{
87 template <typename Policy, typename Iterator>
88 void
89 operator()(Policy&& exec, Iterator iter)
90 {
91 invoke_if(exec, [&]() { search_n(exec, iter, iter, 0, T(0), non_const(std::equal_to<T>())); });
92 }
93};
94
95int
96main()
97{
98 test<int32_t>();
99 test<uint16_t>();
100 test<float64_t>();
101#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
102 test<bool>();
103#endif
104
105 test_algo_basic_single<int32_t>(f: run_for_rnd_fw<test_non_const<int32_t>>());
106
107 std::cout << done() << std::endl;
108 return 0;
109}
110

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