xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/pstl.count.pass.cpp (revision 7a3b528e1b540b4c98f4b557f917447481872749)
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 T>
16 //   typename iterator_traits<ForwardIterator>::difference_type
17 //     count(ExecutionPolicy&& exec,
18 //           ForwardIterator first, ForwardIterator last, const T& value);
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);
30 
31 static_assert(sfinae_test_count<int, int*, int*, bool (*)(int)>);
32 static_assert(!sfinae_test_count<std::execution::parallel_policy, int*, int*, int>);
33 
34 template <class Iter>
35 struct Test {
36   template <class Policy>
operator ()Test37   void operator()(Policy&& policy) {
38     { // simple test
39       int a[]            = {1, 2, 3, 4, 5};
40       decltype(auto) ret = std::count(policy, std::begin(a), std::end(a), 3);
41       static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>);
42       assert(ret == 1);
43     }
44 
45     { // test that an empty range works
46       std::array<int, 0> a;
47       decltype(auto) ret = std::count(policy, std::begin(a), std::end(a), 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(policy, std::begin(a), std::end(a), 1);
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(policy, std::begin(a), std::end(a), 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[] = {3, 1, 3};
68       decltype(auto) ret = std::count(policy, std::begin(a), std::end(a), 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(policy, std::begin(a), std::end(a), 2);
76       static_assert(std::is_same_v<decltype(ret), typename std::iterator_traits<Iter>::difference_type>);
77       assert(ret == 100);
78     }
79   }
80 };
81 
main(int,char **)82 int main(int, char**) {
83   types::for_each(types::forward_iterator_list<int*>{}, TestIteratorWithPolicies<Test>{});
84 
85   return 0;
86 }
87