counted.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 <list>
  12. #include <range/v3/core.hpp>
  13. #include <range/v3/view/counted.hpp>
  14. #include "../simple_test.hpp"
  15. #include "../test_utils.hpp"
  16. #include "../test_iterators.hpp"
  17. struct fortytwo_erator {
  18. using difference_type = int;
  19. using value_type = int;
  20. fortytwo_erator() = default;
  21. int operator*() const { return 42; }
  22. fortytwo_erator& operator++() { return *this; }
  23. void operator++(int) {}
  24. };
  25. int main()
  26. {
  27. using namespace ranges;
  28. std::cout << "\nTesting counted\n";
  29. {
  30. int rgi[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  31. auto rng = views::counted(ForwardIterator<int*>{rgi}, 10);
  32. rng.size();
  33. CPP_assert(sized_range<decltype(rng)> && view_<decltype(rng)>);
  34. auto i = rng.begin();
  35. auto b = i.base();
  36. auto c = i.count();
  37. decltype(i) j{b, c};
  38. ::check_equal(rng, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
  39. static_assert(std::is_same<decltype(i), counted_iterator<ForwardIterator<int*>>>::value, "");
  40. }
  41. {
  42. std::list<int> l;
  43. counted_iterator<std::list<int>::iterator> a(l.begin(), 0);
  44. counted_iterator<std::list<int>::const_iterator> b(l.begin(), 0);
  45. detail::ignore_unused(
  46. a-a,
  47. b-b,
  48. a-b,
  49. b-a);
  50. counted_iterator<char*> c(nullptr, 0);
  51. counted_iterator<char const*> d(nullptr, 0);
  52. detail::ignore_unused(
  53. c-c,
  54. d-d,
  55. c-d,
  56. d-c);
  57. }
  58. {
  59. // Regression test: ensure that we can post-increment a counted_iterator<I>
  60. // when decltype(declval<I &>()++) is void.
  61. CPP_assert(ranges::input_iterator<fortytwo_erator>);
  62. ranges::counted_iterator<fortytwo_erator> c{{}, 42};
  63. c++;
  64. }
  65. return ::test_result();
  66. }