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 // UNSUPPORTED: c++03, c++11, c++14 10 11 // REQUIRES: with-pstl 12 13 // <algorithm> 14 15 // template<class ExecutionPolicy, class ForwardIterator, class Predicate> 16 // bool any_of(ExecutionPolicy&& exec, ForwardIterator first, ForwardIterator last, 17 // Predicate pred); 18 19 #include <algorithm> 20 #include <cassert> 21 #include <vector> 22 23 #include "test_macros.h" 24 #include "test_execution_policies.h" 25 #include "test_iterators.h" 26 27 EXECUTION_POLICY_SFINAE_TEST(all_of); 28 29 static_assert(sfinae_test_all_of<int, int*, int*, bool (*)(int)>); 30 static_assert(!sfinae_test_all_of<std::execution::parallel_policy, int*, int*, bool (*)(int)>); 31 32 template <class Iter> 33 struct Test { 34 template <class Policy> 35 void operator()(Policy&& policy) { 36 int a[] = {1, 2, 3, 4, 5, 6, 7, 8}; 37 // simple test 38 assert(std::all_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 9; })); 39 assert(!std::all_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i < 8; })); 40 41 // check that an empty range works 42 assert(std::all_of(policy, Iter(std::begin(a)), Iter(std::begin(a)), [](int) { return true; })); 43 44 // check that a single-element range works 45 assert(std::all_of(policy, Iter(a), Iter(a + 1), [](int i) { return i < 2; })); 46 47 // check that a two-element range works 48 assert(std::all_of(policy, Iter(a), Iter(a + 2), [](int i) { return i < 3; })); 49 50 // check that false is returned if no element satisfies the condition 51 assert(!std::all_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i == 9; })); 52 53 // check that false is returned if only one elements satisfies the condition 54 assert(!std::all_of(policy, Iter(std::begin(a)), Iter(std::end(a)), [](int i) { return i == 1; })); 55 56 // check that a one-element range works 57 assert(std::all_of(policy, Iter(std::begin(a)), Iter(std::begin(a) + 1), [](int i) { return i == 1; })); 58 59 // check that a two-element range works 60 assert(std::all_of(policy, Iter(std::begin(a)), Iter(std::begin(a) + 2), [](int i) { return i < 3; })); 61 62 // check that a large number of elements works 63 std::vector<int> vec(100); 64 std::fill(vec.begin(), vec.end(), 3); 65 assert(std::all_of(Iter(vec.data()), Iter(vec.data() + vec.size()), [](int i) { return i == 3; })); 66 } 67 }; 68 69 int main(int, char**) { 70 types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{}); 71 72 #ifndef TEST_HAS_NO_EXCEPTIONS 73 std::set_terminate(terminate_successful); 74 int a[] = {1, 2}; 75 try { 76 (void)std::all_of(std::execution::par, std::begin(a), std::end(a), [](int i) -> bool { throw i; }); 77 } catch (int) { 78 assert(false); 79 } 80 #endif 81 82 return 0; 83 } 84