count_if.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Range v3 library
  2. //
  3. // Copyright Eric Niebler 2014-present
  4. //
  5. // Use, modification and distribution is subject to the
  6. // Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. // Project home: https://github.com/ericniebler/range-v3
  11. #include <range/v3/core.hpp>
  12. #include <range/v3/algorithm/count_if.hpp>
  13. #include "../simple_test.hpp"
  14. #include "../test_iterators.hpp"
  15. struct S
  16. {
  17. int i;
  18. };
  19. struct T
  20. {
  21. bool b;
  22. bool m() { return b; }
  23. };
  24. constexpr bool even(int i)
  25. {
  26. return i % 2 == 0;
  27. }
  28. int main()
  29. {
  30. using namespace ranges;
  31. auto equals = [](int i){ return std::bind(equal_to{}, i, std::placeholders::_1); };
  32. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  33. constexpr auto cia = size(ia);
  34. CHECK(count_if(InputIterator<const int*>(ia),
  35. Sentinel<const int*>(ia + cia), equals(2)) == 3);
  36. CHECK(count_if(InputIterator<const int*>(ia),
  37. Sentinel<const int*>(ia + cia), equals(7)) == 0);
  38. CHECK(count_if(InputIterator<const int*>(ia),
  39. Sentinel<const int*>(ia), equals(2)) == 0);
  40. CHECK(count_if(make_subrange(InputIterator<const int*>(ia),
  41. Sentinel<const int*>(ia + cia)), equals(2)) == 3);
  42. CHECK(count_if(make_subrange(InputIterator<const int*>(ia),
  43. Sentinel<const int*>(ia + cia)), equals(7)) == 0);
  44. CHECK(count_if(make_subrange(InputIterator<const int*>(ia),
  45. Sentinel<const int*>(ia)), equals(2)) == 0);
  46. S sa[] = {{0}, {1}, {2}, {2}, {0}, {1}, {2}, {3}};
  47. constexpr auto csa = size(ia);
  48. CHECK(count_if(InputIterator<const S*>(sa),
  49. Sentinel<const S*>(sa + csa), equals(2), &S::i) == 3);
  50. CHECK(count_if(InputIterator<const S*>(sa),
  51. Sentinel<const S*>(sa + csa), equals(7), &S::i) == 0);
  52. CHECK(count_if(InputIterator<const S*>(sa),
  53. Sentinel<const S*>(sa), equals(2), &S::i) == 0);
  54. CHECK(count_if(make_subrange(InputIterator<const S*>(sa),
  55. Sentinel<const S*>(sa + csa)), equals(2), &S::i) == 3);
  56. CHECK(count_if(make_subrange(InputIterator<const S*>(sa),
  57. Sentinel<const S*>(sa + csa)), equals(7), &S::i) == 0);
  58. CHECK(count_if(make_subrange(InputIterator<const S*>(sa),
  59. Sentinel<const S*>(sa)), equals(2), &S::i) == 0);
  60. T ta[] = {{true}, {false}, {true}, {false}, {false}, {true}, {false}, {false}, {true}, {false}};
  61. CHECK(count_if(InputIterator<T*>(ta),
  62. Sentinel<T*>(ta + size(ta)), &T::m) == 4);
  63. CHECK(count_if(InputIterator<T*>(ta),
  64. Sentinel<T*>(ta + size(ta)), &T::b) == 4);
  65. CHECK(count_if(make_subrange(InputIterator<T*>(ta),
  66. Sentinel<T*>(ta + size(ta))), &T::m) == 4);
  67. CHECK(count_if(make_subrange(InputIterator<T*>(ta),
  68. Sentinel<T*>(ta + size(ta))), &T::b) == 4);
  69. {
  70. using IL = std::initializer_list<int>;
  71. STATIC_CHECK(ranges::count_if(IL{0, 1, 2, 1, 3, 1, 4}, even) == 3);
  72. STATIC_CHECK(ranges::count_if(IL{1, 1, 3, 1}, even) == 0);
  73. }
  74. return ::test_result();
  75. }