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