2 //===-- all_of.pass.cpp ---------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
12 #include "support/pstl_test_config.h"
17 #include "support/utils.h"
20 TODO: consider implementing the following tests for a better code coverage
22 - bad input argument (if applicable)
23 - data corruption around/of input and output
24 - correctly work with nested parallelism
25 - check that algorithm does not require anything more than is described in its requirements section
28 using namespace TestUtils
;
32 template <typename ExecutionPolicy
, typename Iterator
, typename Predicate
>
34 operator()(ExecutionPolicy
&& exec
, Iterator begin
, Iterator end
, Predicate pred
, bool expected
)
37 auto actualr
= std::all_of(exec
, begin
, end
, pred
);
38 EXPECT_EQ(expected
, actualr
, "result for all_of");
48 Parity(bool parity_
) : parity(parity_
) {}
50 operator()(T value
) const
52 return (size_t(value
) ^ parity
) % 2 == 0;
60 for (size_t n
= 0; n
<= 100000; n
= n
<= 16 ? n
+ 1 : size_t(3.1415 * n
))
63 // Sequence of odd values
64 Sequence
<T
> in(n
, [n
, bits
](size_t) { return T(2 * HashBits(n
, bits
- 1) ^ 1); });
66 // Even value, or false when T is bool.
67 T
spike(2 * HashBits(n
, bits
- 1));
68 Sequence
<T
> inCopy(in
);
70 invoke_on_all_policies(test_all_of(), in
.begin(), in
.end(), Parity
<T
>(1), true);
71 invoke_on_all_policies(test_all_of(), in
.cbegin(), in
.cend(), Parity
<T
>(1), true);
72 EXPECT_EQ(in
, inCopy
, "all_of modified input sequence");
76 in
[2 * n
/ 3] = spike
;
77 invoke_on_all_policies(test_all_of(), in
.begin(), in
.end(), Parity
<T
>(1), false);
78 invoke_on_all_policies(test_all_of(), in
.cbegin(), in
.cend(), Parity
<T
>(1), false);
80 // Sprinkle in a few more misses
83 invoke_on_all_policies(test_all_of(), in
.begin(), in
.end(), Parity
<T
>(1), false);
84 invoke_on_all_policies(test_all_of(), in
.cbegin(), in
.cend(), Parity
<T
>(1), false);
91 template <typename Policy
, typename Iterator
>
93 operator()(Policy
&& exec
, Iterator iter
)
95 auto is_even
= [&](float64_t v
) {
96 uint32_t i
= (uint32_t)v
;
99 all_of(exec
, iter
, iter
, non_const(is_even
));
106 test
<int32_t>(8 * sizeof(int32_t));
107 test
<uint16_t>(8 * sizeof(uint16_t));
109 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
113 test_algo_basic_single
<int32_t>(run_for_rnd_fw
<test_non_const
>());
115 std::cout
<< done() << std::endl
;