slice.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #include <array>
  10. #include <vector>
  11. #include <range/v3/core.hpp>
  12. #include <range/v3/view/iota.hpp>
  13. #include <range/v3/view/stride.hpp>
  14. #include <range/v3/algorithm/copy.hpp>
  15. #include <range/v3/algorithm/move.hpp>
  16. #include <range/v3/algorithm/equal.hpp>
  17. #include <range/v3/action/slice.hpp>
  18. #include "../simple_test.hpp"
  19. #include "../test_utils.hpp"
  20. int main()
  21. {
  22. using namespace ranges;
  23. {
  24. auto v = views::ints(0, 100) | to<std::vector>();
  25. auto v2 = v | copy | actions::slice(10, 20);
  26. CHECK(size(v2) == 10u);
  27. CPP_assert(same_as<decltype(v), decltype(v2)>);
  28. ::check_equal(v2, {10, 11, 12, 13, 14, 15, 16, 17, 18, 19});
  29. v2 = v2 | move | actions::slice(2, 8);
  30. ::check_equal(v2, {12, 13, 14, 15, 16, 17});
  31. v2 |= actions::slice(0, 0);
  32. CHECK(v2.size() == 0u);
  33. auto &v3 = actions::slice(v, 90, 100);
  34. CHECK(&v3 == &v);
  35. ::check_equal(v, {90, 91, 92, 93, 94, 95, 96, 97, 98, 99});
  36. }
  37. {
  38. auto rng = views::ints(0, 100) | to<std::vector>();
  39. rng |= actions::slice(20, end - 70);
  40. CHECK(size(rng) == 10u);
  41. ::check_equal(rng, {20, 21, 22, 23, 24, 25, 26, 27, 28, 29});
  42. rng |= actions::slice(end - 10, end - 5);
  43. CHECK(size(rng) == 5u);
  44. ::check_equal(rng, {20, 21, 22, 23, 24});
  45. }
  46. {
  47. auto rng = views::ints(0, 100) | to<std::vector>();
  48. auto &rng_copy = actions::slice(rng, 90, end);
  49. CHECK(&rng_copy == &rng);
  50. CHECK(size(rng_copy) == 10u);
  51. ::check_equal(rng, {90, 91, 92, 93, 94, 95, 96, 97, 98, 99});
  52. rng |= actions::slice(end - 5, end);
  53. CHECK(&rng_copy == &rng);
  54. CHECK(size(rng_copy) == 5u);
  55. ::check_equal(rng, {95, 96, 97, 98, 99});
  56. }
  57. return ::test_result();
  58. }