Debian Codegraph @

libstdc++-v3/testsuite/25_algorithms/pstl/alg_nonmodifying/all_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 //===-- all_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_all_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::all_of(exec, begin, end, pred);
46 EXPECT_EQ(expected, actualr, "result for all_of");
47 }
48 };
49
50 template <typename T>
51 struct Parity
52 {
53 bool parity;
54
55 public:
56 Parity(bool parity_) : parity(parity_) {}
57 bool
58 operator()(T value) const
59 {
60 return (size_t(value) ^ parity) % 2 == 0;
61 }
62 };
63
64 template <typename T>
65 void
66 test(size_t bits)
67 {
68 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
69 {
70
71 // Sequence of odd values
72 Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); });
73
74 // Even value, or false when T is bool.
75 T spike(2 * HashBits(n, bits - 1));
76 Sequence<T> inCopy(in);
77
78 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), true);
79 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), true);
80 EXPECT_EQ(in, inCopy, "all_of modified input sequence");
81 if (n > 0)
82 {
83 // Sprinkle in a miss
84 in[2 * n / 3] = spike;
85 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
86 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
87
88 // Sprinkle in a few more misses
89 in[n / 2] = spike;
90 in[n / 3] = spike;
91 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
92 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
93 }
94 }
95 }
96
97 struct test_non_const
98 {
99 template <typename Policy, typename Iterator>
100 void
101 operator()(Policy&& exec, Iterator iter)
102 {
103 auto is_even = [&](float64_t v) {
104 uint32_t i = (uint32_t)v;
105 return i % 2 == 0;
106 };
107 all_of(exec, iter, iter, non_const(is_even));
108 }
109 };
110
111 int
112 main()
113 {
114 test<int32_t>(8 * sizeof(int32_t));
115 test<uint16_t>(8 * sizeof(uint16_t));
116 test<float64_t>(53);
117 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
118 test<bool>(1);
119 #endif
120
121 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
122
123 std::cout << done() << std::endl;
124 return 0;
125 }