1 // -*- C++ -*- 2 //===-- find.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++98, c++03, c++11, c++14 11 12 // Tests for find 13 #include "support/pstl_test_config.h" 14 15 #include <execution> 16 #include <algorithm> 17 18 #include "support/utils.h" 19 20 using namespace TestUtils; 21 22 struct test_find 23 { 24 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 25 _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 Value> 27 void 28 operator()(pstl::execution::unsequenced_policy, Iterator first, Iterator last, Value value) 29 { 30 } 31 template <typename Iterator, typename Value> 32 void 33 operator()(pstl::execution::parallel_unsequenced_policy, Iterator first, Iterator last, Value value) 34 { 35 } 36 #endif 37 38 template <typename Policy, typename Iterator, typename Value> 39 void 40 operator()(Policy&& exec, Iterator first, Iterator last, Value value) 41 { 42 auto i = std::find(first, last, value); 43 auto j = find(exec, first, last, value); 44 EXPECT_TRUE(i == j, "wrong return value from find"); 45 } 46 }; 47 48 template <typename T, typename Value, typename Hit, typename Miss> 49 void 50 test(Value value, Hit hit, Miss miss) 51 { 52 // Try sequences of various lengths. 53 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 54 { 55 Sequence<T> in(n, [&](size_t k) -> T { return miss(n ^ k); }); 56 // Try different find positions, including not found. 57 // By going backwards, we can add extra matches that are *not* supposed to be found. 58 // The decreasing exponential gives us O(n) total work for the loop since each find takes O(m) time. 59 for (size_t m = n; m > 0; m *= 0.6) 60 { 61 if (m < n) 62 in[m] = hit(n ^ m); 63 invoke_on_all_policies(test_find(), in.begin(), in.end(), value); 64 invoke_on_all_policies(test_find(), in.cbegin(), in.cend(), value); 65 } 66 } 67 } 68 69 // Type defined for sake of checking that std::find works with asymmetric ==. 70 class Weird 71 { 72 Number value; 73 74 public: 75 friend bool 76 operator==(Number x, Weird y) 77 { 78 return x == y.value; 79 } 80 Weird(int32_t val, OddTag) : value(val, OddTag()) {} 81 }; 82 83 int 84 main() 85 { 86 // Note that the "hit" and "miss" functions here avoid overflow issues. 87 test<Number>(Weird(42, OddTag()), [](int32_t) { return Number(42, OddTag()); }, // hit 88 [](int32_t j) { return Number(j == 42 ? 0 : j, OddTag()); }); // miss 89 90 // Test with value that is equal to two different bit patterns (-0.0 and 0.0) 91 test<float32_t>(-0.0, [](int32_t j) { return j & 1 ? 0.0 : -0.0; }, // hit 92 [](int32_t j) { return j == 0 ? ~j : j; }); // miss 93 94 std::cout << done() << std::endl; 95 return 0; 96 } 97