reverse_copy.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <cstring>
  24. #include <utility>
  25. #include <range/v3/core.hpp>
  26. #include <range/v3/algorithm/reverse_copy.hpp>
  27. #include "../simple_test.hpp"
  28. #include "../test_utils.hpp"
  29. #include "../test_iterators.hpp"
  30. template<class Iter, class OutIter, class Sent = Iter>
  31. void test()
  32. {
  33. using P = ranges::reverse_copy_result<Iter, OutIter>;
  34. // iterators
  35. {
  36. const int ia[] = {0};
  37. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  38. int ja[sa] = {-1};
  39. P p0 = ranges::reverse_copy(Iter(ia), Sent(ia), OutIter(ja));
  40. ::check_equal(ja, {-1});
  41. CHECK(p0.in == Iter(ia));
  42. CHECK(base(p0.out) == ja);
  43. P p1 = ranges::reverse_copy(Iter(ia), Sent(ia+sa), OutIter(ja));
  44. ::check_equal(ja, {0});
  45. CHECK(p1.in == Iter(ia+sa));
  46. CHECK(base(p1.out) == ja+sa);
  47. const int ib[] = {0, 1};
  48. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  49. int jb[sb] = {-1};
  50. P p2 = ranges::reverse_copy(Iter(ib), Sent(ib+sb), OutIter(jb));
  51. ::check_equal(jb, {1, 0});
  52. CHECK(p2.in == Iter(ib+sb));
  53. CHECK(base(p2.out) == jb+sb);
  54. const int ic[] = {0, 1, 2};
  55. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  56. int jc[sc] = {-1};
  57. P p3 = ranges::reverse_copy(Iter(ic), Sent(ic+sc), OutIter(jc));
  58. ::check_equal(jc, {2, 1, 0});
  59. CHECK(p3.in == Iter(ic+sc));
  60. CHECK(base(p3.out) == jc+sc);
  61. const int id[] = {0, 1, 2, 3};
  62. const unsigned sd = sizeof(id)/sizeof(id[0]);
  63. int jd[sd] = {-1};
  64. P p4 = ranges::reverse_copy(Iter(id), Sent(id+sd), OutIter(jd));
  65. ::check_equal(jd, {3, 2, 1, 0});
  66. CHECK(p4.in == Iter(id+sd));
  67. CHECK(base(p4.out) == jd+sd);
  68. }
  69. // ranges
  70. {
  71. const int ia[] = {0};
  72. const unsigned sa = sizeof(ia)/sizeof(ia[0]);
  73. int ja[sa] = {-1};
  74. P p0 = ranges::reverse_copy(ranges::make_subrange(Iter(ia), Sent(ia)), OutIter(ja));
  75. ::check_equal(ja, {-1});
  76. CHECK(p0.in == Iter(ia));
  77. CHECK(base(p0.out) == ja);
  78. P p1 = ranges::reverse_copy(ranges::make_subrange(Iter(ia), Sent(ia+sa)), OutIter(ja));
  79. ::check_equal(ja, {0});
  80. CHECK(p1.in == Iter(ia+sa));
  81. CHECK(base(p1.out) == ja+sa);
  82. const int ib[] = {0, 1};
  83. const unsigned sb = sizeof(ib)/sizeof(ib[0]);
  84. int jb[sb] = {-1};
  85. P p2 = ranges::reverse_copy(ranges::make_subrange(Iter(ib), Sent(ib+sb)), OutIter(jb));
  86. ::check_equal(jb, {1, 0});
  87. CHECK(p2.in == Iter(ib+sb));
  88. CHECK(base(p2.out) == jb+sb);
  89. const int ic[] = {0, 1, 2};
  90. const unsigned sc = sizeof(ic)/sizeof(ic[0]);
  91. int jc[sc] = {-1};
  92. P p3 = ranges::reverse_copy(ranges::make_subrange(Iter(ic), Sent(ic+sc)), OutIter(jc));
  93. ::check_equal(jc, {2, 1, 0});
  94. CHECK(p3.in == Iter(ic+sc));
  95. CHECK(base(p3.out) == jc+sc);
  96. const int id[] = {0, 1, 2, 3};
  97. const unsigned sd = sizeof(id)/sizeof(id[0]);
  98. int jd[sd] = {-1};
  99. P p4 = ranges::reverse_copy(ranges::make_subrange(Iter(id), Sent(id+sd)), OutIter(jd));
  100. ::check_equal(jd, {3, 2, 1, 0});
  101. CHECK(p4.in == Iter(id+sd));
  102. CHECK(base(p4.out) == jd+sd);
  103. // test rvalue ranges
  104. std::memset(jd, 0, sizeof(jd));
  105. auto p5 = ranges::reverse_copy(::MakeTestRange(Iter(id), Sent(id+sd)), OutIter(jd));
  106. ::check_equal(jd, {3, 2, 1, 0});
  107. CHECK(::is_dangling(p5.in));
  108. CHECK(base(p4.out) == jd+sd);
  109. }
  110. }
  111. constexpr bool test_constexpr()
  112. {
  113. using namespace ranges;
  114. int ia[] = {0, 1, 2, 3, 4};
  115. int ib[5] = {0};
  116. constexpr auto sa = ranges::size(ia);
  117. const auto r = ranges::reverse_copy(ia, ib);
  118. STATIC_CHECK_RETURN(r.in == ia + sa);
  119. STATIC_CHECK_RETURN(r.out == ib + sa);
  120. STATIC_CHECK_RETURN(ia[0] == 0);
  121. STATIC_CHECK_RETURN(ia[1] == 1);
  122. STATIC_CHECK_RETURN(ia[2] == 2);
  123. STATIC_CHECK_RETURN(ia[3] == 3);
  124. STATIC_CHECK_RETURN(ia[4] == 4);
  125. STATIC_CHECK_RETURN(ib[0] == 4);
  126. STATIC_CHECK_RETURN(ib[1] == 3);
  127. STATIC_CHECK_RETURN(ib[2] == 2);
  128. STATIC_CHECK_RETURN(ib[3] == 1);
  129. STATIC_CHECK_RETURN(ib[4] == 0);
  130. return true;
  131. }
  132. int main()
  133. {
  134. test<BidirectionalIterator<const int*>, OutputIterator<int*> >();
  135. test<BidirectionalIterator<const int*>, ForwardIterator<int*> >();
  136. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*> >();
  137. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*> >();
  138. test<BidirectionalIterator<const int*>, int*>();
  139. test<RandomAccessIterator<const int*>, OutputIterator<int*> >();
  140. test<RandomAccessIterator<const int*>, ForwardIterator<int*> >();
  141. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*> >();
  142. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*> >();
  143. test<RandomAccessIterator<const int*>, int*>();
  144. test<const int*, OutputIterator<int*> >();
  145. test<const int*, ForwardIterator<int*> >();
  146. test<const int*, BidirectionalIterator<int*> >();
  147. test<const int*, RandomAccessIterator<int*> >();
  148. test<const int*, int*>();
  149. test<BidirectionalIterator<const int*>, OutputIterator<int*>, Sentinel<const int *> >();
  150. test<BidirectionalIterator<const int*>, ForwardIterator<int*>, Sentinel<const int *> >();
  151. test<BidirectionalIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int *> >();
  152. test<BidirectionalIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int *> >();
  153. test<BidirectionalIterator<const int*>, int*>();
  154. test<RandomAccessIterator<const int*>, OutputIterator<int*>, Sentinel<const int *> >();
  155. test<RandomAccessIterator<const int*>, ForwardIterator<int*>, Sentinel<const int *> >();
  156. test<RandomAccessIterator<const int*>, BidirectionalIterator<int*>, Sentinel<const int *> >();
  157. test<RandomAccessIterator<const int*>, RandomAccessIterator<int*>, Sentinel<const int *> >();
  158. test<RandomAccessIterator<const int*>, int*>();
  159. test<const int*, OutputIterator<int*>, Sentinel<const int *> >();
  160. test<const int*, ForwardIterator<int*>, Sentinel<const int *> >();
  161. test<const int*, BidirectionalIterator<int*>, Sentinel<const int *> >();
  162. test<const int*, RandomAccessIterator<int*>, Sentinel<const int *> >();
  163. test<const int*, int*>();
  164. {
  165. STATIC_CHECK(test_constexpr());
  166. }
  167. return ::test_result();
  168. }