Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_nonmodifying/any_of.cc

raw
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 //===-- any_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_any_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::any_of(exec, begin, end, pred);
46 EXPECT_EQ(expected, actualr, "result for any_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 Sequence<T> inCopy(in);
63
64 invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), false);
65 invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false);
66 EXPECT_EQ(in, inCopy, "any_of modified input sequence");
67 if (n > 0)
68 {
69 // Sprinkle in a hit
70 in[2 * n / 3] = spike;
71 invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true);
72 invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true);
73
74 // Sprinkle in a few more hits
75 in[n / 2] = spike;
76 in[n / 3] = spike;
77 invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true);
78 invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true);
79 }
80 }
81 }
82
83 struct test_non_const
84 {
85 template <typename Policy, typename Iterator>
86 void
87 operator()(Policy&& exec, Iterator iter)
88 {
89 auto is_even = [&](float64_t v) {
90 uint32_t i = (uint32_t)v;
91 return i % 2 == 0;
92 };
93 any_of(exec, iter, iter, non_const(is_even));
94 }
95 };
96
97 int
98 main()
99 {
100 test<int32_t>(8 * sizeof(int32_t));
101 test<uint16_t>(8 * sizeof(uint16_t));
102 test<float64_t>(53);
103 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
104 test<bool>(1);
105 #endif
106
107 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
108
109 std::cout << done() << std::endl;
110 return 0;
111 }