| 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 |
//===-- none_of.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 |
#include "pstl/pstl_test_config.h" |
| 16 |
|
| 17 |
#ifdef PSTL_STANDALONE_TESTS |
| 18 |
#include "pstl/execution" |
| 19 |
#include "pstl/algorithm" |
| 20 |
#else |
| 21 |
#include <execution> |
| 22 |
#include <algorithm> |
| 23 |
#endif // PSTL_STANDALONE_TESTS |
| 24 |
|
| 25 |
#include "pstl/test_utils.h" |
| 26 |
|
| 27 |
/* |
| 28 |
TODO: consider implementing the following tests for a better code coverage |
| 29 |
- correctness |
| 30 |
- bad input argument (if applicable) |
| 31 |
- data corruption around/of input and output |
| 32 |
- correctly work with nested parallelism |
| 33 |
- check that algorithm does not require anything more than is described in its requirements section |
| 34 |
*/ |
| 35 |
|
| 36 |
using namespace TestUtils; |
| 37 |
|
| 38 |
struct test_none_of |
| 39 |
{ |
| 40 |
template <typename ExecutionPolicy, typename Iterator, typename Predicate> |
| 41 |
void |
| 42 |
operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) |
| 43 |
{ |
| 44 |
|
| 45 |
auto actualr = std::none_of(exec, begin, end, pred); |
| 46 |
EXPECT_EQ(expected, actualr, "result for none_of"); |
| 47 |
} |
| 48 |
}; |
| 49 |
|
| 50 |
template <typename T> |
| 51 |
void |
| 52 |
test(size_t bits) |
| 53 |
{ |
| 54 |
for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) |
| 55 |
{ |
| 56 |
|
| 57 |
// Sequence of odd values |
| 58 |
Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); }); |
| 59 |
|
| 60 |
// Even value, or false when T is bool. |
| 61 |
T spike(2 * HashBits(n, bits - 1)); |
| 62 |
|
| 63 |
invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), true); |
| 64 |
invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true); |
| 65 |
if (n > 0) |
| 66 |
{ |
| 67 |
// Sprinkle in a hit |
| 68 |
in[2 * n / 3] = spike; |
| 69 |
invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), false); |
| 70 |
invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false); |
| 71 |
|
| 72 |
// Sprinkle in a few more hits |
| 73 |
in[n / 3] = spike; |
| 74 |
in[n / 2] = spike; |
| 75 |
invoke_on_all_policies(test_none_of(), in.begin(), in.end(), is_equal_to<T>(spike), false); |
| 76 |
invoke_on_all_policies(test_none_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false); |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
struct test_non_const |
| 82 |
{ |
| 83 |
template <typename Policy, typename Iterator> |
| 84 |
void |
| 85 |
operator()(Policy&& exec, Iterator iter) |
| 86 |
{ |
| 87 |
auto is_even = [&](float64_t v) { |
| 88 |
uint32_t i = (uint32_t)v; |
| 89 |
return i % 2 == 0; |
| 90 |
}; |
| 91 |
none_of(exec, iter, iter, non_const(is_even)); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
int |
| 96 |
main() |
| 97 |
{ |
| 98 |
test<int32_t>(8 * sizeof(int32_t)); |
| 99 |
test<uint16_t>(8 * sizeof(uint16_t)); |
| 100 |
test<float64_t>(53); |
| 101 |
#if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN) |
| 102 |
test<bool>(1); |
| 103 |
#endif |
| 104 |
|
| 105 |
test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); |
| 106 |
|
| 107 |
std::cout << done() << std::endl; |
| 108 |
return 0; |
| 109 |
} |