xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count_if.pass.cpp (revision 773ae4412468433c134e668b4047c94f4599e0fd)
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 // <algorithm>
10 
11 // template<InputIterator Iter, Predicate<auto, Iter::value_type> Pred>
12 //   requires CopyConstructible<Pred>
13 //   constexpr Iter::difference_type   // constexpr after C++17
14 //   count_if(Iter first, Iter last, Pred pred);
15 
16 #include <algorithm>
17 #include <functional>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "test_iterators.h"
22 
23 struct eq {
eqeq24     TEST_CONSTEXPR eq (int val) : v(val) {}
operator ()eq25     TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
26     int v;
27     };
28 
29 #if TEST_STD_VER > 17
test_constexpr()30 TEST_CONSTEXPR bool test_constexpr() {
31     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
32     int ib[] = {1, 2, 3, 4, 5, 6};
33     return    (std::count_if(std::begin(ia), std::end(ia), eq(2)) == 3)
34            && (std::count_if(std::begin(ib), std::end(ib), eq(9)) == 0)
35            ;
36     }
37 #endif
38 
main(int,char **)39 int main(int, char**)
40 {
41     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
42     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
43     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
44                          cpp17_input_iterator<const int*>(ia + sa),
45                          eq(2)) == 3);
46     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
47                          cpp17_input_iterator<const int*>(ia + sa),
48                          eq(7)) == 0);
49     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
50                          cpp17_input_iterator<const int*>(ia),
51                          eq(2)) == 0);
52 
53 #if TEST_STD_VER > 17
54     static_assert(test_constexpr());
55 #endif
56 
57   return 0;
58 }
59