xref: /llvm-project/pstl/test/std/algorithms/alg.nonmodifying/any_of.pass.cpp (revision 3b9a1bb1af90db9472340ef2122d3855eb9ba3fc)
1 // -*- C++ -*-
2 //===-- any_of.pass.cpp ---------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++03, c++11, c++14
11 
12 #include "support/pstl_test_config.h"
13 
14 #include <execution>
15 #include <algorithm>
16 
17 #include "support/utils.h"
18 
19 /*
20   TODO: consider implementing the following tests for a better code coverage
21   - correctness
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
26 */
27 
28 using namespace TestUtils;
29 
30 struct test_any_of
31 {
32     template <typename ExecutionPolicy, typename Iterator, typename Predicate>
33     void
operator ()test_any_of34     operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected)
35     {
36 
37         auto actualr = std::any_of(exec, begin, end, pred);
38         EXPECT_EQ(expected, actualr, "result for any_of");
39     }
40 };
41 
42 template <typename T>
43 void
test(size_t bits)44 test(size_t bits)
45 {
46     for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
47     {
48 
49         // Sequence of odd values
50         Sequence<T> in(n, [n, bits](size_t) { return T(2 * HashBits(n, bits - 1) ^ 1); });
51 
52         // Even value, or false when T is bool.
53         T spike(2 * HashBits(n, bits - 1));
54         Sequence<T> inCopy(in);
55 
56         invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), false);
57         invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), false);
58         EXPECT_EQ(in, inCopy, "any_of modified input sequence");
59         if (n > 0)
60         {
61             // Sprinkle in a hit
62             in[2 * n / 3] = spike;
63             invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true);
64             invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true);
65 
66             // Sprinkle in a few more hits
67             in[n / 2] = spike;
68             in[n / 3] = spike;
69             invoke_on_all_policies(test_any_of(), in.begin(), in.end(), is_equal_to<T>(spike), true);
70             invoke_on_all_policies(test_any_of(), in.cbegin(), in.cend(), is_equal_to<T>(spike), true);
71         }
72     }
73 }
74 
75 struct test_non_const
76 {
77     template <typename Policy, typename Iterator>
78     void
operator ()test_non_const79     operator()(Policy&& exec, Iterator iter)
80     {
81         auto is_even = [&](float64_t v) {
82             uint32_t i = (uint32_t)v;
83             return i % 2 == 0;
84         };
85         any_of(exec, iter, iter, non_const(is_even));
86     }
87 };
88 
89 int
main()90 main()
91 {
92     test<int32_t>(8 * sizeof(int32_t));
93     test<uint16_t>(8 * sizeof(uint16_t));
94     test<float64_t>(53);
95 #if !defined(_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN)
96     test<bool>(1);
97 #endif
98 
99     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
100 
101     std::cout << done() << std::endl;
102     return 0;
103 }
104