Synchronize libstdc++ PSTL with upstream LLVM PSTL
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / pstl / alg_nonmodifying / all_of.cc
1 // -*- C++ -*-
2 // { dg-options "-std=gnu++17 -ltbb" }
3 // { dg-do run { target c++17 } }
4 // { dg-require-effective-target tbb-backend }
5
6 //===-- all_of.pass.cpp ---------------------------------------------------===//
7 //
8 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
9 // See https://llvm.org/LICENSE.txt for license information.
10 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "pstl/pstl_test_config.h"
15
16 #ifdef PSTL_STANDALONE_TESTS
17 #include "pstl/execution"
18 #include "pstl/algorithm"
19 #else
20 #include <execution>
21 #include <algorithm>
22 #endif // PSTL_STANDALONE_TESTS
23
24 #include "pstl/test_utils.h"
25
26 /*
27 TODO: consider implementing the following tests for a better code coverage
28 - correctness
29 - bad input argument (if applicable)
30 - data corruption around/of input and output
31 - correctly work with nested parallelism
32 - check that algorithm does not require anything more than is described in its requirements section
33 */
34
35 using namespace TestUtils;
36
37 struct test_all_of
38 {
39 template <typename ExecutionPolicy, typename Iterator, typename Predicate>
40 void
41 operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected)
42 {
43
44 auto actualr = std::all_of(exec, begin, end, pred);
45 EXPECT_EQ(expected, actualr, "result for all_of");
46 }
47 };
48
49 template <typename T>
50 struct Parity
51 {
52 bool parity;
53
54 public:
55 Parity(bool parity_) : parity(parity_) {}
56 bool
57 operator()(T value) const
58 {
59 return (size_t(value) ^ parity) % 2 == 0;
60 }
61 };
62
63 template <typename T>
64 void
65 test(size_t bits)
66 {
67 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
68 {
69
70 // Sequence of odd values
71 Sequence<T> in(n, [n, bits](size_t k) { return T(2 * HashBits(n, bits - 1) ^ 1); });
72
73 // Even value, or false when T is bool.
74 T spike(2 * HashBits(n, bits - 1));
75 Sequence<T> inCopy(in);
76
77 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), true);
78 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), true);
79 EXPECT_EQ(in, inCopy, "all_of modified input sequence");
80 if (n > 0)
81 {
82 // Sprinkle in a miss
83 in[2 * n / 3] = spike;
84 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
85 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
86
87 // Sprinkle in a few more misses
88 in[n / 2] = spike;
89 in[n / 3] = spike;
90 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
91 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
92 }
93 }
94 }
95
96 struct test_non_const
97 {
98 template <typename Policy, typename Iterator>
99 void
100 operator()(Policy&& exec, Iterator iter)
101 {
102 auto is_even = [&](float64_t v) {
103 uint32_t i = (uint32_t)v;
104 return i % 2 == 0;
105 };
106 all_of(exec, iter, iter, non_const(is_even));
107 }
108 };
109
110 int32_t
111 main()
112 {
113 test<int32_t>(8 * sizeof(int32_t));
114 test<uint16_t>(8 * sizeof(uint16_t));
115 test<float64_t>(53);
116 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN
117 test<bool>(1);
118 #endif
119
120 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
121
122 std::cout << done() << std::endl;
123 return 0;
124 }