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