1// -*- C++ -*-
2//===-- find_if.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// Tests for find_if and find_if_not
13#include "support/pstl_test_config.h"
14
15#include <execution>
16#include <algorithm>
17
18#include "support/utils.h"
19
20using namespace TestUtils;
21
22struct test_find_if
23{
24#if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \
25 defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration
26 template <typename Iterator, typename Predicate, typename NotPredicate>
27 void
28 operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Predicate pred,
29 NotPredicate not_pred)
30 {
31 }
32 template <typename Iterator, typename Predicate, typename NotPredicate>
33 void
34 operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Predicate pred,
35 NotPredicate not_pred)
36 {
37 }
38#endif
39
40 template <typename Policy, typename Iterator, typename Predicate, typename NotPredicate>
41 void
42 operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred, NotPredicate not_pred)
43 {
44 auto i = std::find_if(first, last, pred);
45 auto j = find_if(exec, first, last, pred);
46 EXPECT_TRUE(i == j, "wrong return value from find_if");
47 auto i_not = find_if_not(exec, first, last, not_pred);
48 EXPECT_TRUE(i_not == i, "wrong return value from find_if_not");
49 }
50};
51
52template <typename T, typename Predicate, typename Hit, typename Miss>
53void
54test(Predicate pred, Hit hit, Miss miss)
55{
56 auto not_pred = [pred](T x) { return !pred(x); };
57 // Try sequences of various lengths.
58 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
59 {
60 Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); });
61 // Try different find positions, including not found.
62 // By going backwards, we can add extra matches that are *not* supposed to be found.
63 // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time.
64 for (size_t m = n; m > 0; m *= 0.6)
65 {
66 if (m < n)
67 in[m] = hit(n ^ m);
68 invoke_on_all_policies(test_find_if(), in.begin(), in.end(), pred, not_pred);
69 invoke_on_all_policies(test_find_if(), in.cbegin(), in.cend(), pred, not_pred);
70 }
71 }
72}
73
74struct test_non_const
75{
76 template <typename Policy, typename Iterator>
77 void
78 operator()(Policy&& exec, Iterator iter)
79 {
80 auto is_even = [&](float64_t v) {
81 uint32_t i = (uint32_t)v;
82 return i % 2 == 0;
83 };
84
85 invoke_if(exec, [&]() {
86 find_if(exec, iter, iter, non_const(is_even));
87 find_if_not(exec, iter, iter, non_const(is_even));
88 });
89 }
90};
91
92int
93main()
94{
95#if !defined(_PSTL_ICC_17_TEST_MAC_RELEASE_32_BROKEN)
96 // Note that the "hit" and "miss" functions here avoid overflow issues.
97 test<Number>(pred: IsMultiple(5, OddTag()), hit: [](int32_t j) { return Number(j - j % 5, OddTag()); }, // hit
98 miss: [](int32_t j) { return Number(j % 5 == 0 ? j ^ 1 : j, OddTag()); }); // miss
99#endif
100
101 // Try type for which algorithm can really be vectorized.
102 test<float32_t>(pred: [](float32_t x) { return x >= 0; }, hit: [](float32_t j) { return j * j; },
103 miss: [](float32_t j) { return -1 - j * j; });
104
105 test_algo_basic_single<int32_t>(f: run_for_rnd_fw<test_non_const>());
106
107 std::cout << done() << std::endl;
108 return 0;
109}
110

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