1 // -*- C++ -*- 2 //===-- unique.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 // Test for unique 11 #include "support/pstl_test_config.h" 12 13 #include <execution> 14 #include <algorithm> 15 16 #include "support/utils.h" 17 18 using namespace TestUtils; 19 20 struct run_unique 21 { 22 #if _PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN || \ 23 _PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN //dummy specialization by policy type, in case of broken configuration 24 template <typename ForwardIt, typename Generator> 25 void 26 operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, 27 ForwardIt last2, Generator generator) 28 { 29 } 30 31 template <typename ForwardIt, typename Generator> 32 void 33 operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, 34 ForwardIt last2, Generator generator) 35 { 36 } 37 38 template <typename ForwardIt, typename BinaryPred, typename Generator> 39 void 40 operator()(pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, 41 ForwardIt last2, BinaryPred pred, Generator generator) 42 { 43 } 44 45 template <typename ForwardIt, typename BinaryPred, typename Generator> 46 void 47 operator()(pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, 48 ForwardIt last2, BinaryPred pred, Generator generator) 49 { 50 } 51 #endif 52 53 template <typename Policy, typename ForwardIt, typename Generator> 54 void 55 operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, Generator generator) 56 { 57 using namespace std; 58 59 // Preparation 60 fill_data(first1, last1, generator); 61 fill_data(first2, last2, generator); 62 63 ForwardIt i = unique(first1, last1); 64 ForwardIt k = unique(exec, first2, last2); 65 66 auto n = std::distance(first1, i); 67 EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique without predicate"); 68 EXPECT_EQ_N(first1, first2, n, "wrong effect from unique without predicate"); 69 } 70 71 template <typename Policy, typename ForwardIt, typename BinaryPred, typename Generator> 72 void 73 operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, BinaryPred pred, 74 Generator generator) 75 { 76 using namespace std; 77 78 // Preparation 79 fill_data(first1, last1, generator); 80 fill_data(first2, last2, generator); 81 82 ForwardIt i = unique(first1, last1, pred); 83 ForwardIt k = unique(exec, first2, last2, pred); 84 85 auto n = std::distance(first1, i); 86 EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique with predicate"); 87 EXPECT_EQ_N(first1, first2, n, "wrong effect from unique with predicate"); 88 } 89 }; 90 91 template <typename T, typename Generator, typename Predicate> 92 void 93 test(Generator generator, Predicate pred) 94 { 95 const std::size_t max_size = 1000000; 96 Sequence<T> in(max_size, [](size_t v) { return T(v); }); 97 Sequence<T> exp(max_size, [](size_t v) { return T(v); }); 98 99 for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 100 { 101 invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, generator); 102 invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, pred, generator); 103 } 104 } 105 106 template <typename T> 107 struct LocalWrapper 108 { 109 T my_val; 110 111 explicit LocalWrapper(T k) : my_val(k) {} 112 LocalWrapper(LocalWrapper&& input) : my_val(std::move(input.my_val)) {} 113 LocalWrapper& 114 operator=(LocalWrapper&& input) 115 { 116 my_val = std::move(input.my_val); 117 return *this; 118 } 119 friend bool 120 operator==(const LocalWrapper<T>& x, const LocalWrapper<T>& y) 121 { 122 return x.my_val == y.my_val; 123 } 124 }; 125 126 template <typename T> 127 struct test_non_const 128 { 129 template <typename Policy, typename Iterator> 130 void 131 operator()(Policy&& exec, Iterator iter) 132 { 133 invoke_if(exec, [&]() { unique(exec, iter, iter, non_const(std::equal_to<T>())); }); 134 } 135 }; 136 137 int32_t 138 main() 139 { 140 #if !_PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN 141 test<int32_t>([](size_t j) { return j / 3; }, 142 [](const int32_t& val1, const int32_t& val2) { return val1 * val1 == val2 * val2; }); 143 test<float64_t>([](size_t) { return float64_t(1); }, 144 [](const float64_t& val1, const float64_t& val2) { return val1 != val2; }); 145 #endif 146 test<LocalWrapper<uint32_t>>([](size_t j) { return LocalWrapper<uint32_t>(j); }, 147 [](const LocalWrapper<uint32_t>& val1, const LocalWrapper<uint32_t>& val2) { 148 return val1.my_val != val2.my_val; 149 }); 150 151 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); 152 153 std::cout << done() << std::endl; 154 return 0; 155 } 156