xref: /llvm-project/libcxx/test/std/algorithms/alg.nonmodifying/alg.count/count_if.pass.cpp (revision 773ae4412468433c134e668b4047c94f4599e0fd)
15a83710eSEric Fiselier //===----------------------------------------------------------------------===//
25a83710eSEric Fiselier //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65a83710eSEric Fiselier //
75a83710eSEric Fiselier //===----------------------------------------------------------------------===//
85a83710eSEric Fiselier 
95a83710eSEric Fiselier // <algorithm>
105a83710eSEric Fiselier 
115a83710eSEric Fiselier // template<InputIterator Iter, Predicate<auto, Iter::value_type> Pred>
125a83710eSEric Fiselier //   requires CopyConstructible<Pred>
13056f15e3SMarshall Clow //   constexpr Iter::difference_type   // constexpr after C++17
145a83710eSEric Fiselier //   count_if(Iter first, Iter last, Pred pred);
155a83710eSEric Fiselier 
165a83710eSEric Fiselier #include <algorithm>
175a83710eSEric Fiselier #include <functional>
185a83710eSEric Fiselier #include <cassert>
195a83710eSEric Fiselier 
20056f15e3SMarshall Clow #include "test_macros.h"
215a83710eSEric Fiselier #include "test_iterators.h"
225a83710eSEric Fiselier 
23315cd1feSMarshall Clow struct eq {
eqeq24056f15e3SMarshall Clow     TEST_CONSTEXPR eq (int val) : v(val) {}
operator ()eq25056f15e3SMarshall Clow     TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
26315cd1feSMarshall Clow     int v;
27315cd1feSMarshall Clow     };
28315cd1feSMarshall Clow 
29056f15e3SMarshall Clow #if TEST_STD_VER > 17
test_constexpr()30056f15e3SMarshall Clow TEST_CONSTEXPR bool test_constexpr() {
31056f15e3SMarshall Clow     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
32056f15e3SMarshall Clow     int ib[] = {1, 2, 3, 4, 5, 6};
33056f15e3SMarshall Clow     return    (std::count_if(std::begin(ia), std::end(ia), eq(2)) == 3)
34056f15e3SMarshall Clow            && (std::count_if(std::begin(ib), std::end(ib), eq(9)) == 0)
35056f15e3SMarshall Clow            ;
36056f15e3SMarshall Clow     }
37056f15e3SMarshall Clow #endif
38315cd1feSMarshall Clow 
main(int,char **)392df59c50SJF Bastien int main(int, char**)
405a83710eSEric Fiselier {
415a83710eSEric Fiselier     int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
425a83710eSEric Fiselier     const unsigned sa = sizeof(ia)/sizeof(ia[0]);
43*773ae441SChristopher Di Bella     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
44*773ae441SChristopher Di Bella                          cpp17_input_iterator<const int*>(ia + sa),
45315cd1feSMarshall Clow                          eq(2)) == 3);
46*773ae441SChristopher Di Bella     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
47*773ae441SChristopher Di Bella                          cpp17_input_iterator<const int*>(ia + sa),
48315cd1feSMarshall Clow                          eq(7)) == 0);
49*773ae441SChristopher Di Bella     assert(std::count_if(cpp17_input_iterator<const int*>(ia),
50*773ae441SChristopher Di Bella                          cpp17_input_iterator<const int*>(ia),
51315cd1feSMarshall Clow                          eq(2)) == 0);
52056f15e3SMarshall Clow 
53056f15e3SMarshall Clow #if TEST_STD_VER > 17
54056f15e3SMarshall Clow     static_assert(test_constexpr());
55056f15e3SMarshall Clow #endif
562df59c50SJF Bastien 
572df59c50SJF Bastien   return 0;
585a83710eSEric Fiselier }
59