| 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 |
//===-- count.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 count and count_if |
| 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 test_count |
| 31 |
{ |
| 32 |
template <typename Policy, typename Iterator, typename T> |
| 33 |
void |
| 34 |
operator()(Policy&& exec, Iterator first, Iterator last, T needle) |
| 35 |
{ |
| 36 |
auto expected = std::count(first, last, needle); |
| 37 |
auto result = std::count(exec, first, last, needle); |
| 38 |
EXPECT_EQ(expected, result, "wrong count result"); |
| 39 |
} |
| 40 |
}; |
| 41 |
|
| 42 |
struct test_count_if |
| 43 |
{ |
| 44 |
template <typename Policy, typename Iterator, typename Predicate> |
| 45 |
void |
| 46 |
operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) |
| 47 |
{ |
| 48 |
auto expected = std::count_if(first, last, pred); |
| 49 |
auto result = std::count_if(exec, first, last, pred); |
| 50 |
EXPECT_EQ(expected, result, "wrong count_if result"); |
| 51 |
} |
| 52 |
}; |
| 53 |
|
| 54 |
template <typename T> |
| 55 |
class IsEqual |
| 56 |
{ |
| 57 |
T value; |
| 58 |
|
| 59 |
public: |
| 60 |
IsEqual(T value_, OddTag) : value(value_) {} |
| 61 |
bool |
| 62 |
operator()(const T& x) const |
| 63 |
{ |
| 64 |
return x == value; |
| 65 |
} |
| 66 |
}; |
| 67 |
|
| 68 |
template <typename In, typename T, typename Predicate, typename Convert> |
| 69 |
void |
| 70 |
test(T needle, Predicate pred, Convert convert) |
| 71 |
{ |
| 72 |
// Try sequences of various lengths. |
| 73 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 74 |
{ |
| 75 |
Sequence<In> in(n, [=](size_t k) -> In { |
| 76 |
// Sprinkle "42" and "50" early, so that short sequences have non-zero count. |
| 77 |
return convert((n - k - 1) % 3 == 0 ? 42 : (n - k - 2) % 5 == 0 ? 50 : 3 * (int(k) % 1000 - 500)); |
| 78 |
}); |
| 79 |
invoke_on_all_policies(test_count(), in.begin(), in.end(), needle); |
| 80 |
invoke_on_all_policies(test_count_if(), in.begin(), in.end(), pred); |
| 81 |
|
| 82 |
invoke_on_all_policies(test_count(), in.cbegin(), in.cend(), needle); |
| 83 |
invoke_on_all_policies(test_count_if(), in.cbegin(), in.cend(), pred); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
struct test_non_const |
| 88 |
{ |
| 89 |
template <typename Policy, typename Iterator> |
| 90 |
void |
| 91 |
operator()(Policy&& exec, Iterator iter) |
| 92 |
{ |
| 93 |
auto is_even = [&](float64_t v) { |
| 94 |
uint32_t i = (uint32_t)v; |
| 95 |
return i % 2 == 0; |
| 96 |
}; |
| 97 |
count_if(exec, iter, iter, non_const(is_even)); |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
int |
| 102 |
main() |
| 103 |
{ |
| 104 |
test<int32_t>(42, IsEqual<int32_t>(50, OddTag()), [](int32_t j) { return j; }); |
| 105 |
#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN) |
| 106 |
test<int32_t>(42, [](const int32_t&) { return true; }, [](int32_t j) { return j; }); |
| 107 |
#endif |
| 108 |
test<float64_t>(42, IsEqual<float64_t>(50, OddTag()), [](int32_t j) { return float64_t(j); }); |
| 109 |
test<Number>(Number(42, OddTag()), IsEqual<Number>(Number(50, OddTag()), OddTag()), |
| 110 |
[](int32_t j) { return Number(j, OddTag()); }); |
| 111 |
|
| 112 |
test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); |
| 113 |
|
| 114 |
std::cout << done() << std::endl; |
| 115 |
return 0; |
| 116 |
} |