| 1 | // -*- C++ -*- |
| 2 | // { dg-options "-ltbb" } |
| 3 | // { dg-do run { target c++17 } } |
| 4 | // { dg-timeout-factor 3 } |
| 5 | // { dg-require-effective-target tbb_backend } |
| 6 | |
| 7 | //===-- unique.pass.cpp ---------------------------------------------------===// |
| 8 | // |
| 9 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 10 | // See https://llvm.org/LICENSE.txt for license information. |
| 11 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | // Test for unique |
| 16 | #include "pstl/pstl_test_config.h" |
| 17 | |
| 18 | #ifdef PSTL_STANDALONE_TESTS |
| 19 | #include "pstl/execution" |
| 20 | #include "pstl/algorithm" |
| 21 | #else |
| 22 | #include <execution> |
| 23 | #include <algorithm> |
| 24 | #endif // PSTL_STANDALONE_TESTS |
| 25 | |
| 26 | #include "pstl/test_utils.h" |
| 27 | |
| 28 | using namespace TestUtils; |
| 29 | |
| 30 | struct run_unique |
| 31 | { |
| 32 | #if defined(_PSTL_ICC_17_VC141_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) || \ |
| 33 | defined(_PSTL_ICC_16_VC14_TEST_SIMD_LAMBDA_DEBUG_32_BROKEN) //dummy specialization by policy type, in case of broken configuration |
| 34 | template <typename ForwardIt, typename Generator> |
| 35 | void |
| 36 | operator()(__pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, |
| 37 | ForwardIt last2, Generator generator) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | template <typename ForwardIt, typename Generator> |
| 42 | void |
| 43 | operator()(__pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, |
| 44 | ForwardIt last2, Generator generator) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | template <typename ForwardIt, typename BinaryPred, typename Generator> |
| 49 | void |
| 50 | operator()(__pstl::execution::unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, |
| 51 | ForwardIt last2, BinaryPred pred, Generator generator) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | template <typename ForwardIt, typename BinaryPred, typename Generator> |
| 56 | void |
| 57 | operator()(__pstl::execution::parallel_unsequenced_policy, ForwardIt first1, ForwardIt last1, ForwardIt first2, |
| 58 | ForwardIt last2, BinaryPred pred, Generator generator) |
| 59 | { |
| 60 | } |
| 61 | #endif |
| 62 | |
| 63 | template <typename Policy, typename ForwardIt, typename Generator> |
| 64 | void |
| 65 | operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, Generator generator) |
| 66 | { |
| 67 | using namespace std; |
| 68 | |
| 69 | // Preparation |
| 70 | fill_data(first1, last1, generator); |
| 71 | fill_data(first2, last2, generator); |
| 72 | |
| 73 | ForwardIt i = unique(first1, last1); |
| 74 | ForwardIt k = unique(exec, first2, last2); |
| 75 | |
| 76 | auto n = std::distance(first1, i); |
| 77 | EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique without predicate"); |
| 78 | EXPECT_EQ_N(first1, first2, n, "wrong effect from unique without predicate"); |
| 79 | } |
| 80 | |
| 81 | template <typename Policy, typename ForwardIt, typename BinaryPred, typename Generator> |
| 82 | void |
| 83 | operator()(Policy&& exec, ForwardIt first1, ForwardIt last1, ForwardIt first2, ForwardIt last2, BinaryPred pred, |
| 84 | Generator generator) |
| 85 | { |
| 86 | using namespace std; |
| 87 | |
| 88 | // Preparation |
| 89 | fill_data(first1, last1, generator); |
| 90 | fill_data(first2, last2, generator); |
| 91 | |
| 92 | ForwardIt i = unique(first1, last1, pred); |
| 93 | ForwardIt k = unique(exec, first2, last2, pred); |
| 94 | |
| 95 | auto n = std::distance(first1, i); |
| 96 | EXPECT_TRUE(std::distance(first2, k) == n, "wrong return value from unique with predicate"); |
| 97 | EXPECT_EQ_N(first1, first2, n, "wrong effect from unique with predicate"); |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | template <typename T, typename Generator, typename Predicate> |
| 102 | void |
| 103 | test(Generator generator, Predicate pred) |
| 104 | { |
| 105 | const std::size_t max_size = 1000000; |
| 106 | Sequence<T> in(max_size, [](size_t v) { return T(v); }); |
| 107 | Sequence<T> exp(max_size, [](size_t v) { return T(v); }); |
| 108 | |
| 109 | for (size_t n = 0; n <= max_size; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 110 | { |
| 111 | invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, generator); |
| 112 | invoke_on_all_policies(run_unique(), exp.begin(), exp.begin() + n, in.begin(), in.begin() + n, pred, generator); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | template <typename T> |
| 117 | struct LocalWrapper |
| 118 | { |
| 119 | T my_val; |
| 120 | |
| 121 | explicit LocalWrapper(T k) : my_val(k) {} |
| 122 | LocalWrapper(LocalWrapper&& input) : my_val(std::move(input.my_val)) {} |
| 123 | LocalWrapper& |
| 124 | operator=(LocalWrapper&& input) |
| 125 | { |
| 126 | my_val = std::move(input.my_val); |
| 127 | return *this; |
| 128 | } |
| 129 | friend bool |
| 130 | operator==(const LocalWrapper<T>& x, const LocalWrapper<T>& y) |
| 131 | { |
| 132 | return x.my_val == y.my_val; |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | template <typename T> |
| 137 | struct test_non_const |
| 138 | { |
| 139 | template <typename Policy, typename Iterator> |
| 140 | void |
| 141 | operator()(Policy&& exec, Iterator iter) |
| 142 | { |
| 143 | invoke_if(exec, [&]() { unique(exec, iter, iter, non_const(std::equal_to<T>())); }); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | int |
| 148 | main() |
| 149 | { |
| 150 | #if !defined(_PSTL_ICC_16_17_18_TEST_UNIQUE_MASK_RELEASE_BROKEN) |
| 151 | test<int32_t>([](size_t j) { return j / 3; }, |
| 152 | [](const int32_t& val1, const int32_t& val2) { return val1 * val1 == val2 * val2; }); |
| 153 | test<float64_t>([](size_t) { return float64_t(1); }, |
| 154 | [](const float64_t& val1, const float64_t& val2) { return val1 != val2; }); |
| 155 | #endif |
| 156 | test<LocalWrapper<uint32_t>>([](size_t j) { return LocalWrapper<uint32_t>(j); }, |
| 157 | [](const LocalWrapper<uint32_t>& val1, const LocalWrapper<uint32_t>& val2) { |
| 158 | return val1.my_val != val2.my_val; |
| 159 | }); |
| 160 | |
| 161 | test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); |
| 162 | |
| 163 | test<MemoryChecker>( |
| 164 | [](std::size_t idx){ return MemoryChecker{std::int32_t(idx / 3)}; }, |
| 165 | [](const MemoryChecker& val1, const MemoryChecker& val2){ return val1.value() == val2.value(); }); |
| 166 | EXPECT_FALSE(MemoryChecker::alive_objects() < 0, "wrong effect from unique: number of ctors calls < num of dtors calls"); |
| 167 | EXPECT_FALSE(MemoryChecker::alive_objects() > 0, "wrong effect from unique: number of ctors calls > num of dtors calls"); |
| 168 | |
| 169 | std::cout << done() << std::endl; |
| 170 | return 0; |
| 171 | } |