count_if.pass.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. // Iter::difference_type
  13. // count_if(Iter first, Iter last, Pred pred);
  14. #include <algorithm>
  15. #include <functional>
  16. #include <cassert>
  17. #include "test_iterators.h"
  18. struct eq {
  19. eq (int val) : v(val) {}
  20. bool operator () (int v2) const { return v == v2; }
  21. int v;
  22. };
  23. int main()
  24. {
  25. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  26. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  27. assert(std::count_if(input_iterator<const int*>(ia),
  28. input_iterator<const int*>(ia + sa),
  29. eq(2)) == 3);
  30. assert(std::count_if(input_iterator<const int*>(ia),
  31. input_iterator<const int*>(ia + sa),
  32. eq(7)) == 0);
  33. assert(std::count_if(input_iterator<const int*>(ia),
  34. input_iterator<const int*>(ia),
  35. eq(2)) == 0);
  36. }