1 // -*- C++ -*-
2 //===-- all_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 #include "support/pstl_test_config.h"
11 
12 #include <execution>
13 #include <algorithm>
14 
15 #include "support/utils.h"
16 
17 /*
18   TODO: consider implementing the following tests for a better code coverage
19   - correctness
20   - bad input argument (if applicable)
21   - data corruption around/of input and output
22   - correctly work with nested parallelism
23   - check that algorithm does not require anything more than is described in its requirements section
24 */
25 
26 using namespace TestUtils;
27 
28 struct test_all_of
29 {
30     template <typename ExecutionPolicy, typename Iterator, typename Predicate>
31     void
32     operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected)
33     {
34 
35         auto actualr = std::all_of(exec, begin, end, pred);
36         EXPECT_EQ(expected, actualr, "result for all_of");
37     }
38 };
39 
40 template <typename T>
41 struct Parity
42 {
43     bool parity;
44 
45   public:
46     Parity(bool parity_) : parity(parity_) {}
47     bool
48     operator()(T value) const
49     {
50         return (size_t(value) ^ parity) % 2 == 0;
51     }
52 };
53 
54 template <typename T>
55 void
56 test(size_t bits)
57 {
58     for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n))
59     {
60 
61         // Sequence of odd values
62         Sequence<T> in(n, [n, bits](size_t k) { return T(2 * HashBits(n, bits - 1) ^ 1); });
63 
64         // Even value, or false when T is bool.
65         T spike(2 * HashBits(n, bits - 1));
66         Sequence<T> inCopy(in);
67 
68         invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), true);
69         invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), true);
70         EXPECT_EQ(in, inCopy, "all_of modified input sequence");
71         if (n > 0)
72         {
73             // Sprinkle in a miss
74             in[2 * n / 3] = spike;
75             invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
76             invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
77 
78             // Sprinkle in a few more misses
79             in[n / 2] = spike;
80             in[n / 3] = spike;
81             invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false);
82             invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false);
83         }
84     }
85 }
86 
87 struct test_non_const
88 {
89     template <typename Policy, typename Iterator>
90     void
91     operator()(Policy&& exec, Iterator iter)
92     {
93         auto is_even = [&](float64_t v) {
94             uint32_t i = (uint32_t)v;
95             return i % 2 == 0;
96         };
97         all_of(exec, iter, iter, non_const(is_even));
98     }
99 };
100 
101 int32_t
102 main()
103 {
104     test<int32_t>(8 * sizeof(int32_t));
105     test<uint16_t>(8 * sizeof(uint16_t));
106     test<float64_t>(53);
107 #if !_PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN
108     test<bool>(1);
109 #endif
110 
111     test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>());
112 
113     std::cout << done() << std::endl;
114     return 0;
115 }
116