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