| 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_copy_equal.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 |
// Tests for unique_copy |
| 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_copy |
| 31 |
{ |
| 32 |
#if defined(_PSTL_ICC_16_VC14_TEST_PAR_TBB_RT_RELEASE_64_BROKEN) // dummy specializations to skip testing in case of broken configuration |
| 33 |
template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, |
| 34 |
typename Predicate, typename T> |
| 35 |
void |
| 36 |
operator()(__pstl::execution::parallel_policy, InputIterator first, InputIterator last, OutputIterator out_first, |
| 37 |
OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2 expected_last, Size n, |
| 38 |
Predicate pred, T trash) |
| 39 |
{ |
| 40 |
} |
| 41 |
|
| 42 |
template <typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, |
| 43 |
typename Predicate, typename T> |
| 44 |
void |
| 45 |
operator()(__pstl::execution::parallel_unsequenced_policy, InputIterator first, InputIterator last, |
| 46 |
OutputIterator out_first, OutputIterator out_last, OutputIterator2 expected_first, |
| 47 |
OutputIterator2 expected_last, Size n, Predicate pred, T trash) |
| 48 |
{ |
| 49 |
} |
| 50 |
#endif |
| 51 |
|
| 52 |
template <typename Policy, typename InputIterator, typename OutputIterator, typename OutputIterator2, typename Size, |
| 53 |
typename Predicate, typename T> |
| 54 |
void |
| 55 |
operator()(Policy&& exec, InputIterator first, InputIterator last, OutputIterator out_first, |
| 56 |
OutputIterator out_last, OutputIterator2 expected_first, OutputIterator2, Size n, Predicate pred, |
| 57 |
T trash) |
| 58 |
{ |
| 59 |
// Cleaning |
| 60 |
std::fill_n(expected_first, n, trash); |
| 61 |
std::fill_n(out_first, n, trash); |
| 62 |
|
| 63 |
// Run unique_copy |
| 64 |
auto i = unique_copy(first, last, expected_first); |
| 65 |
auto k = unique_copy(exec, first, last, out_first); |
| 66 |
EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy effect"); |
| 67 |
for (size_t j = 0; j < GuardSize; ++j) |
| 68 |
{ |
| 69 |
++k; |
| 70 |
} |
| 71 |
EXPECT_TRUE(out_last == k, "wrong return value from unique_copy"); |
| 72 |
|
| 73 |
// Cleaning |
| 74 |
std::fill_n(expected_first, n, trash); |
| 75 |
std::fill_n(out_first, n, trash); |
| 76 |
// Run unique_copy with predicate |
| 77 |
i = unique_copy(first, last, expected_first, pred); |
| 78 |
k = unique_copy(exec, first, last, out_first, pred); |
| 79 |
EXPECT_EQ_N(expected_first, out_first, n, "wrong unique_copy with predicate effect"); |
| 80 |
for (size_t j = 0; j < GuardSize; ++j) |
| 81 |
{ |
| 82 |
++k; |
| 83 |
} |
| 84 |
EXPECT_TRUE(out_last == k, "wrong return value from unique_copy with predicate"); |
| 85 |
} |
| 86 |
}; |
| 87 |
|
| 88 |
template <typename T, typename BinaryPredicate, typename Convert> |
| 89 |
void |
| 90 |
test(T trash, BinaryPredicate pred, Convert convert, bool check_weakness = true) |
| 91 |
{ |
| 92 |
// Try sequences of various lengths. |
| 93 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 94 |
{ |
| 95 |
// count is number of output elements, plus a handful |
| 96 |
// more for sake of detecting buffer overruns. |
| 97 |
Sequence<T> in(n, [&](size_t k) -> T { return convert(k ^ n); }); |
| 98 |
using namespace std; |
| 99 |
size_t count = GuardSize; |
| 100 |
for (size_t k = 0; k < in.size(); ++k) |
| 101 |
count += k == 0 || !pred(in[k], in[k - 1]) ? 1 : 0; |
| 102 |
Sequence<T> out(count, [=](size_t) { return trash; }); |
| 103 |
Sequence<T> expected(count, [=](size_t) { return trash; }); |
| 104 |
if (check_weakness) |
| 105 |
{ |
| 106 |
auto expected_result = unique_copy(in.begin(), in.end(), expected.begin(), pred); |
| 107 |
size_t m = expected_result - expected.begin(); |
| 108 |
EXPECT_TRUE(n / (n < 10000 ? 4 : 6) <= m && m <= (3 * n + 1) / 4, "weak test for unique_copy"); |
| 109 |
} |
| 110 |
invoke_on_all_policies(run_unique_copy(), in.begin(), in.end(), out.begin(), out.end(), expected.begin(), |
| 111 |
expected.end(), count, pred, trash); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
template <typename T> |
| 116 |
struct test_non_const |
| 117 |
{ |
| 118 |
template <typename Policy, typename InputIterator, typename OutputInterator> |
| 119 |
void |
| 120 |
operator()(Policy&& exec, InputIterator input_iter, OutputInterator out_iter) |
| 121 |
{ |
| 122 |
unique_copy(exec, input_iter, input_iter, out_iter, non_const(std::equal_to<T>())); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
int |
| 127 |
main() |
| 128 |
{ |
| 129 |
test<Number>(Number(42, OddTag()), std::equal_to<Number>(), |
| 130 |
[](int32_t j) { return Number(3 * j / 13 ^ (j & 8), OddTag()); }); |
| 131 |
|
| 132 |
test<float32_t>(float32_t(42), std::equal_to<float32_t>(), |
| 133 |
[](int32_t j) { return float32_t(5 * j / 23 ^ (j / 7)); }); |
| 134 |
#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) |
| 135 |
test<float32_t>(float32_t(42), [](float32_t, float32_t) { return false; }, [](int32_t j) { return float32_t(j); }, |
| 136 |
false); |
| 137 |
#endif |
| 138 |
|
| 139 |
test_algo_basic_double<int32_t>(run_for_rnd_fw<test_non_const<int32_t>>()); |
| 140 |
|
| 141 |
std::cout << done() << std::endl; |
| 142 |
return 0; |
| 143 |
} |