count.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Range v3 library
  2. //
  3. // Copyright Andrew Sutton 2014
  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.hpp>
  13. #include "../simple_test.hpp"
  14. #include "../test_iterators.hpp"
  15. struct S
  16. {
  17. int i;
  18. };
  19. int main()
  20. {
  21. using namespace ranges;
  22. int ia[] = {0, 1, 2, 2, 0, 1, 2, 3};
  23. constexpr auto cia = size(ia);
  24. CHECK(count(InputIterator<const int*>(ia),
  25. Sentinel<const int*>(ia + cia), 2) == 3);
  26. CHECK(count(InputIterator<const int*>(ia),
  27. Sentinel<const int*>(ia + cia), 7) == 0);
  28. CHECK(count(InputIterator<const int*>(ia),
  29. Sentinel<const int*>(ia), 2) == 0);
  30. CHECK(count(make_subrange(InputIterator<const int*>(ia),
  31. Sentinel<const int*>(ia + cia)), 2) == 3);
  32. CHECK(count(make_subrange(InputIterator<const int*>(ia),
  33. Sentinel<const int*>(ia + cia)), 7) == 0);
  34. CHECK(count(make_subrange(InputIterator<const int*>(ia),
  35. Sentinel<const int*>(ia)), 2) == 0);
  36. S sa[] = {{0}, {1}, {2}, {2}, {0}, {1}, {2}, {3}};
  37. constexpr auto csa = size(ia);
  38. CHECK(count(InputIterator<const S*>(sa),
  39. Sentinel<const S*>(sa + csa), 2, &S::i) == 3);
  40. CHECK(count(InputIterator<const S*>(sa),
  41. Sentinel<const S*>(sa + csa), 7, &S::i) == 0);
  42. CHECK(count(InputIterator<const S*>(sa),
  43. Sentinel<const S*>(sa), 2, &S::i) == 0);
  44. CHECK(count(make_subrange(InputIterator<const S*>(sa),
  45. Sentinel<const S*>(sa + csa)), 2, &S::i) == 3);
  46. CHECK(count(make_subrange(InputIterator<const S*>(sa),
  47. Sentinel<const S*>(sa + csa)), 7, &S::i) == 0);
  48. CHECK(count(make_subrange(InputIterator<const S*>(sa),
  49. Sentinel<const S*>(sa)), 2, &S::i) == 0);
  50. {
  51. using IL = std::initializer_list<int>;
  52. STATIC_CHECK(ranges::count(IL{0, 1, 2, 1, 3, 1, 4}, 1) == 3);
  53. STATIC_CHECK(ranges::count(IL{0, 1, 2, 1, 3, 1, 4}, 5) == 0);
  54. }
  55. return ::test_result();
  56. }