enumerate.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Range v3 library
  2. //
  3. // Copyright MikeGitb 2018-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/view/enumerate.hpp>
  12. #include <range/v3/view/iota.hpp>
  13. #include <range/v3/view/indices.hpp>
  14. #include <range/v3/view/transform.hpp>
  15. #include "../simple_test.hpp"
  16. #include "../test_utils.hpp"
  17. #include <list>
  18. #include <vector>
  19. #include <tuple>
  20. #include <iterator>
  21. using std::begin;
  22. template<class RangeT>
  23. void test_enumerate_with(RangeT &&range)
  24. {
  25. auto enumerated_range = ranges::views::enumerate(range);
  26. CPP_assert(ranges::borrowed_range<decltype(enumerated_range)>);
  27. std::size_t idx_ref = 0;
  28. auto it_ref = begin( range );
  29. for(auto it = enumerated_range.begin(); it != enumerated_range.end(); ++it)
  30. {
  31. const auto idx = std::get<0>(*it);
  32. const auto value = std::get<1>(*it);
  33. CHECK(idx == idx_ref++);
  34. CHECK(value == *it_ref++);
  35. }
  36. }
  37. int main()
  38. {
  39. { // test array
  40. int const es[] = { 9,8,7,6,5,4,3,2,1,0 };
  41. test_enumerate_with(es);
  42. }
  43. { // test with vector of complex value type
  44. std::vector<std::list<int>> range{ {1, 2, 3}, { 3,5,6,7 }, { 10,5,6,1 }, { 1,2,3,4 } };
  45. const auto rcopy = range;
  46. test_enumerate_with(range);
  47. // check that range hasn't accidentially been modified
  48. CHECK(rcopy == range);
  49. // check with empty range
  50. range.clear();
  51. test_enumerate_with(range);
  52. }
  53. { // test with list
  54. std::list<int> range{ 9,8,7,6,5,4,3,2,1 };
  55. test_enumerate_with(range);
  56. range.clear();
  57. test_enumerate_with(range);
  58. }
  59. { // test with initializer_list
  60. test_enumerate_with(std::initializer_list<int>{9, 8, 7, 6, 5, 4, 3, 2, 1});
  61. }
  62. {
  63. auto range = ranges::views::iota(0, 0);
  64. test_enumerate_with(range);
  65. range = ranges::views::iota(-10000, 10000);
  66. test_enumerate_with(range);
  67. }
  68. {
  69. auto range = ranges::views::iota((std::uintmax_t)0, (std::uintmax_t)0);
  70. test_enumerate_with(range);
  71. auto range2 = ranges::views::iota((std::intmax_t) -10000, (std::intmax_t) 10000);
  72. test_enumerate_with(range2);
  73. }
  74. // https://github.com/ericniebler/range-v3/issues/1141
  75. {
  76. using namespace ranges;
  77. auto x = views::indices( std::uintmax_t( 100 ) )
  78. | views::transform([](std::uintmax_t) { return "";})
  79. | views::enumerate;
  80. using X = decltype(x);
  81. CPP_assert(same_as<range_difference_t<X>, detail::diffmax_t>);
  82. CPP_assert(same_as<range_value_t<X>, std::pair<detail::diffmax_t, char const*>>);
  83. }
  84. }