brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 489c7a7 Raw
87 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++03, c++11, c++1410 11// UNSUPPORTED: libcpp-has-no-incomplete-pstl12 13// <algorithm>14 15// template<class ExecutionPolicy, class ForwardIterator, class Predicate>16//   typename iterator_traits<ForwardIterator>::difference_type17//     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 29EXECUTION_POLICY_SFINAE_TEST(count_if);30 31static_assert(sfinae_test_count_if<int, int*, int*, bool (*)(int)>);32static_assert(!sfinae_test_count_if<std::execution::parallel_policy, int*, int*, int>);33 34template <class Iter>35struct Test {36  template <class Policy>37  void operator()(Policy&& policy) {38    { // simple test39      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 works46      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 works53      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 works60      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 works67      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 works74      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 82int main(int, char**) {83  types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});84 85  return 0;86}87