xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.none_of/pstl.none_of.pass.cpp (revision 5fc3db7da7901a65db5fd37886b00c22cd376e82)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // REQUIRES: with-pstl
10 
11 // <algorithm>
12 
13 // template<class ExecutionPolicy, class ForwardIterator, class Predicate>
14 //   bool none_of(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last,
15 //                Predicate pred);
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <vector>
20 
21 #include "test_macros.h"
22 #include "test_execution_policies.h"
23 #include "test_iterators.h"
24 
25 EXECUTION_POLICY_SFINAE_TEST(none_of);
26 
27 static_assert(sfinae_test_none_of<int, int*, int*, bool (*)(int)>);
28 static_assert(!sfinae_test_none_of<std::execution::parallel_policy, int*, int*, bool (*)(int)>);
29 
30 template <class Iter>
31 struct Test {
32   template <class Policy>
33   void operator()(Policy&& policy) {
34     int a[] = {1, 2, 3, 4, 5, 6, 7, 8};
35     // simple test
36     assert(std::none_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i > 9; }));
37     assert(!std::none_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i >= 8; }));
38 
39     // check that an empty range works
40     assert(std::none_of(policy, Iter(std::begin(a)), Iter(std::begin(a)), [](int) { return false; }));
41 
42     // check that true is returned if no element satisfies the condition
43     assert(std::none_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i == 9; }));
44 
45     // check that false is returned if only one elements satisfies the condition
46     assert(!std::none_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i == 1; }));
47 
48     // check that a one-element range works
49     assert(std::none_of(policy, Iter(std::begin(a)), Iter(std::begin(a) + 1), [](int i) { return i != 1; }));
50 
51     // check that a two-element range works
52     assert(std::none_of(policy, Iter(std::begin(a)), Iter(std::begin(a) + 2), [](int i) { return i > 2; }));
53 
54     // check that a large number of elements works
55     std::vector<int> vec(100);
56     std::fill(vec.begin(), vec.end(), 3);
57     assert(std::none_of(Iter(vec.data()), Iter(vec.data() + vec.size()), [](int i) { return i != 3; }));
58   }
59 };
60 
61 int main(int, char**) {
62   types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
63 
64 #ifndef TEST_HAS_NO_EXCEPTIONS
65   std::set_terminate(terminate_successful);
66   int a[] = {1, 2};
67   try {
68     (void)std::none_of(std::execution::par, std::begin(a), std::end(a), [](int i) -> bool { throw i; });
69   } catch (int) {
70     assert(false);
71   }
72 #endif
73 
74   return 0;
75 }
76