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