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