count_if.pass.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. // <algorithm>
  10. // template<InputIterator Iter, Predicate<auto, Iter::value_type> Pred>
  11. // requires CopyConstructible<Pred>
  12. // constexpr Iter::difference_type // constexpr after C++17
  13. // count_if(Iter first, Iter last, Pred pred);
  14. #include <algorithm>
  15. #include <functional>
  16. #include <cassert>
  17. #include "test_macros.h"
  18. #include "test_iterators.h"
  19. struct eq {
  20. TEST_CONSTEXPR eq (int val) : v(val) {}
  21. TEST_CONSTEXPR bool operator () (int v2) const { return v == v2; }
  22. int v;
  23. };
  24. #if TEST_STD_VER > 17
  25. TEST_CONSTEXPR bool test_constexpr() {
  26. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  27. int ib[] = {1, 2, 3, 4, 5, 6};
  28. return (std::count_if(std::begin(ia), std::end(ia), eq(2)) == 3)
  29. && (std::count_if(std::begin(ib), std::end(ib), eq(9)) == 0)
  30. ;
  31. }
  32. #endif
  33. int main()
  34. {
  35. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  36. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  37. assert(std::count_if(input_iterator<const int*>(ia),
  38. input_iterator<const int*>(ia + sa),
  39. eq(2)) == 3);
  40. assert(std::count_if(input_iterator<const int*>(ia),
  41. input_iterator<const int*>(ia + sa),
  42. eq(7)) == 0);
  43. assert(std::count_if(input_iterator<const int*>(ia),
  44. input_iterator<const int*>(ia),
  45. eq(2)) == 0);
  46. #if TEST_STD_VER > 17
  47. static_assert(test_constexpr());
  48. #endif
  49. }