| 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 |
// UNSUPPORTED: libcpp-has-no-incomplete-pstl |
| 12 |
|
| 13 |
// <algorithm> |
| 14 |
|
| 15 |
// template<class ExecutionPolicy, class ForwardIterator, class Predicate> |
| 16 |
// typename iterator_traits<ForwardIterator>::difference_type |
| 17 |
// count_if(ExecutionPolicy&& exec, |
| 18 |
// ForwardIterator first, ForwardIterator last, Predicate pred); |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <array> |
| 22 |
#include <cassert> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
#include "test_macros.h" |
| 26 |
#include "test_execution_policies.h" |
| 27 |
#include "test_iterators.h" |
| 28 |
|
| 29 |
EXECUTION_POLICY_SFINAE_TEST(count_if); |
| 30 |
|
| 31 |
static_assert(sfinae_test_count_if<int, int*, int*, bool (*)(int)>); |
| 32 |
static_assert(!sfinae_test_count_if<std::execution::parallel_policy, int*, int*, int>); |
| 33 |
|
| 34 |
template <class Iter> |
| 35 |
struct Test { |
| 36 |
template <class Policy> |
| 37 |
void operator()(Policy&& policy) { |
| 38 |
{ // simple test |
| 39 |
int a[] = {1, 2, 3, 4, 5}; |
| 40 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 41 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 42 |
assert(ret == 2); |
| 43 |
} |
| 44 |
|
| 45 |
{ // test that an empty range works |
| 46 |
std::array<int, 0> a; |
| 47 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 48 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 49 |
assert(ret == 0); |
| 50 |
} |
| 51 |
|
| 52 |
{ // test that a single-element range works |
| 53 |
int a[] = {1}; |
| 54 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 55 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 56 |
assert(ret == 1); |
| 57 |
} |
| 58 |
|
| 59 |
{ // test that a two-element range works |
| 60 |
int a[] = {1, 3}; |
| 61 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 62 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 63 |
assert(ret == 1); |
| 64 |
} |
| 65 |
|
| 66 |
{ // test that a three-element range works |
| 67 |
int a[] = {2, 3, 2}; |
| 68 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 69 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 70 |
assert(ret == 2); |
| 71 |
} |
| 72 |
|
| 73 |
{ // test that a large range works |
| 74 |
std::vector<int> a(100, 2); |
| 75 |
decltype(auto) ret = std::count_if(policy, std::begin(a), std::end(a), [](int i) { return i < 3; }); |
| 76 |
static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>); |
| 77 |
assert(ret == 100); |
| 78 |
} |
| 79 |
} |
| 80 |
}; |
| 81 |
|
| 82 |
int main(int, char**) { |
| 83 |
types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{}); |
| 84 |
|
| 85 |
return 0; |
| 86 |
} |