1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 // <algorithm> 11 12 // template<InputIterator Iter, Predicate<auto, Iter::value_type> Pred> 13 // requires CopyConstructible<Pred> 14 // constexpr Iter::difference_type // constexpr after C++17 15 // count_if(Iter first, Iter last, Pred pred); 16 17 #include <algorithm> 18 #include <functional> 19 #include <cassert> 20 21 #include "test_macros.h" 22 #include "test_iterators.h" 23 24 struct eq { 25 TEST_CONSTEXPR eq (int val) : v(val) {} 26 TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; } 27 int v; 28 }; 29 30 #if TEST_STD_VER > 17 31 TEST_CONSTEXPR bool test_constexpr() { 32 int ia[] = {0, 1, 2, 2, 0, 1, 2, 3}; 33 int ib[] = {1, 2, 3, 4, 5, 6}; 34 return (std::count_if(std::begin(ia), std::end(ia), eq(2)) == 3) 35 && (std::count_if(std::begin(ib), std::end(ib), eq(9)) == 0) 36 ; 37 } 38 #endif 39 40 int main() 41 { 42 int ia[] = {0, 1, 2, 2, 0, 1, 2, 3}; 43 const unsigned sa = sizeof(ia)/sizeof(ia[0]); 44 assert(std::count_if(input_iterator<const int*>(ia), 45 input_iterator<const int*>(ia + sa), 46 eq(2)) == 3); 47 assert(std::count_if(input_iterator<const int*>(ia), 48 input_iterator<const int*>(ia + sa), 49 eq(7)) == 0); 50 assert(std::count_if(input_iterator<const int*>(ia), 51 input_iterator<const int*>(ia), 52 eq(2)) == 0); 53 54 #if TEST_STD_VER > 17 55 static_assert(test_constexpr()); 56 #endif 57 } 58