count_if.pass.cpp 1.8 KB

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