reverse.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. //
  12. // Copyright 2005 - 2007 Adobe Systems Incorporated
  13. // Distributed under the MIT License(see accompanying file LICENSE_1_0_0.txt
  14. // or a copy at http://stlab.adobe.com/licenses.html)
  15. //===----------------------------------------------------------------------===//
  16. //
  17. // The LLVM Compiler Infrastructure
  18. //
  19. // This file is dual licensed under the MIT and the University of Illinois Open
  20. // Source Licenses. See LICENSE.TXT for details.
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #include <utility>
  24. #include <range/v3/core.hpp>
  25. #include <range/v3/algorithm/reverse.hpp>
  26. #include "../simple_test.hpp"
  27. #include "../test_utils.hpp"
  28. #include "../test_iterators.hpp"
  29. template<class Iter, class Sent = Iter>
  30. void test()
  31. {
  32. // iterators
  33. {
  34. int ia[] = {0};
  35. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  36. Iter i0 = ranges::reverse(Iter(ia), Sent(ia));
  37. ::check_equal(ia, {0});
  38. CHECK(i0 == Iter(ia));
  39. Iter i1 = ranges::reverse(Iter(ia), Sent(ia+sa));
  40. ::check_equal(ia, {0});
  41. CHECK(i1 == Iter(ia+sa));
  42. int ib[] = {0, 1};
  43. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  44. Iter i2 = ranges::reverse(Iter(ib), Sent(ib+sb));
  45. ::check_equal(ib, {1, 0});
  46. CHECK(i2 == Iter(ib+sb));
  47. int ic[] = {0, 1, 2};
  48. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  49. Iter i3 = ranges::reverse(Iter(ic), Sent(ic+sc));
  50. ::check_equal(ic, {2, 1, 0});
  51. CHECK(i3 == Iter(ic+sc));
  52. int id[] = {0, 1, 2, 3};
  53. const unsigned sd = sizeof(id)/sizeof(id[0]);
  54. Iter i4 = ranges::reverse(Iter(id), Sent(id+sd));
  55. ::check_equal(id, {3, 2, 1, 0});
  56. CHECK(i4 == Iter(id+sd));
  57. }
  58. // ranges
  59. {
  60. int ia[] = {0};
  61. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  62. Iter i0 = ranges::reverse(::as_lvalue(ranges::make_subrange(Iter(ia), Sent(ia))));
  63. ::check_equal(ia, {0});
  64. CHECK(i0 == Iter(ia));
  65. Iter i1 = ranges::reverse(::as_lvalue(ranges::make_subrange(Iter(ia), Sent(ia+sa))));
  66. ::check_equal(ia, {0});
  67. CHECK(i1 == Iter(ia+sa));
  68. int ib[] = {0, 1};
  69. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  70. Iter i2 = ranges::reverse(::as_lvalue(ranges::make_subrange(Iter(ib), Sent(ib+sb))));
  71. ::check_equal(ib, {1, 0});
  72. CHECK(i2 == Iter(ib+sb));
  73. int ic[] = {0, 1, 2};
  74. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  75. Iter i3 = ranges::reverse(::as_lvalue(ranges::make_subrange(Iter(ic), Sent(ic+sc))));
  76. ::check_equal(ic, {2, 1, 0});
  77. CHECK(i3 == Iter(ic+sc));
  78. int id[] = {0, 1, 2, 3};
  79. const unsigned sd = sizeof(id)/sizeof(id[0]);
  80. Iter i4 = ranges::reverse(::as_lvalue(ranges::make_subrange(Iter(id), Sent(id+sd))));
  81. ::check_equal(id, {3, 2, 1, 0});
  82. CHECK(i4 == Iter(id+sd));
  83. // rvalue range
  84. auto i5 = ranges::reverse(ranges::make_subrange(Iter(id), Sent(id+sd)));
  85. ::check_equal(id, {0, 1, 2, 3});
  86. CHECK(i5 == Iter(id+sd));
  87. }
  88. }
  89. constexpr bool test_constexpr()
  90. {
  91. using namespace ranges;
  92. int ia[] = {0, 1, 2, 3, 4};
  93. constexpr auto sa = ranges::size(ia);
  94. auto r = ranges::reverse(ia);
  95. STATIC_CHECK_RETURN(r == ia + sa);
  96. STATIC_CHECK_RETURN(ia[0] == 4);
  97. STATIC_CHECK_RETURN(ia[1] == 3);
  98. STATIC_CHECK_RETURN(ia[2] == 2);
  99. STATIC_CHECK_RETURN(ia[3] == 1);
  100. STATIC_CHECK_RETURN(ia[4] == 0);
  101. return true;
  102. }
  103. int main()
  104. {
  105. test<BidirectionalIterator<int *>>();
  106. test<RandomAccessIterator<int *>>();
  107. test<int*>();
  108. test<BidirectionalIterator<int *>, Sentinel<int*>>();
  109. test<RandomAccessIterator<int *>, Sentinel<int*>>();
  110. {
  111. STATIC_CHECK(test_constexpr());
  112. }
  113. return ::test_result();
  114. }