1 // Copyright (C) 2019 T. Zachary Laine
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 #include <boost/stl_interfaces/iterator_interface.hpp>
7 #include <boost/stl_interfaces/reverse_iterator.hpp>
9 #include <boost/core/lightweight_test.hpp>
18 struct zip_iter
: boost::stl_interfaces::proxy_iterator_interface
<
20 std::random_access_iterator_tag
,
22 std::tuple
<int &, int &>>
24 zip_iter() : it1_(nullptr), it2_(nullptr) {}
25 zip_iter(int * it1
, int * it2
) : it1_(it1
), it2_(it2
) {}
27 std::tuple
<int &, int &> operator*() const
29 return std::tuple
<int &, int &>{*it1_
, *it2_
};
31 zip_iter
& operator+=(std::ptrdiff_t i
)
37 friend std::ptrdiff_t operator-(zip_iter lhs
, zip_iter rhs
) noexcept
39 return lhs
.it1_
- rhs
.it1_
;
52 std::list
<int> ints
= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
54 auto first
= boost::stl_interfaces::make_reverse_iterator(ints
.end());
55 auto last
= boost::stl_interfaces::make_reverse_iterator(ints
.begin());
58 auto cfirst
= boost::stl_interfaces::reverse_iterator
<
59 std::list
<int>::const_iterator
>(first
);
60 auto clast
= boost::stl_interfaces::reverse_iterator
<
61 std::list
<int>::const_iterator
>(last
);
62 BOOST_TEST(std::equal(first
, last
, cfirst
, clast
));
66 auto ints_copy
= ints
;
67 std::reverse(ints_copy
.begin(), ints_copy
.end());
69 std::equal(first
, last
, ints_copy
.begin(), ints_copy
.end()));
73 std::list
<int> ints_copy
;
74 std::reverse_copy(first
, last
, std::back_inserter(ints_copy
));
75 BOOST_TEST(ints_copy
== ints
);
79 std::size_t count
= 0;
80 for (auto it
= first
; it
!= last
; ++it
) {
83 BOOST_TEST(count
== ints
.size());
87 std::size_t count
= 0;
88 for (auto it
= first
; it
!= last
; it
++) {
91 BOOST_TEST(count
== ints
.size());
95 std::size_t count
= 0;
96 for (auto it
= last
; it
!= first
; --it
) {
99 BOOST_TEST(count
== ints
.size());
103 std::size_t count
= 0;
104 for (auto it
= last
; it
!= first
; it
--) {
107 BOOST_TEST(count
== ints
.size());
113 std::vector
<int> ints
= {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
115 auto first
= boost::stl_interfaces::make_reverse_iterator(ints
.end());
116 auto last
= boost::stl_interfaces::make_reverse_iterator(ints
.begin());
119 auto cfirst
= boost::stl_interfaces::reverse_iterator
<
120 std::vector
<int>::const_iterator
>(first
);
121 auto clast
= boost::stl_interfaces::reverse_iterator
<
122 std::vector
<int>::const_iterator
>(last
);
123 BOOST_TEST(std::equal(first
, last
, cfirst
, clast
));
127 auto ints_copy
= ints
;
128 std::reverse(ints_copy
.begin(), ints_copy
.end());
129 BOOST_TEST(first
- last
== ints_copy
.begin() - ints_copy
.end());
131 std::equal(first
, last
, ints_copy
.begin(), ints_copy
.end()));
135 std::vector
<int> ints_copy
;
136 std::reverse_copy(first
, last
, std::back_inserter(ints_copy
));
137 BOOST_TEST(ints_copy
== ints
);
141 std::size_t count
= 0;
142 for (auto it
= first
; it
!= last
; ++it
) {
145 BOOST_TEST(count
== ints
.size());
149 std::size_t count
= 0;
150 for (auto it
= first
; it
!= last
; it
++) {
153 BOOST_TEST(count
== ints
.size());
157 std::size_t count
= 0;
158 for (auto it
= last
; it
!= first
; --it
) {
161 BOOST_TEST(count
== ints
.size());
165 std::size_t count
= 0;
166 for (auto it
= last
; it
!= first
; it
--) {
169 BOOST_TEST(count
== ints
.size());
175 std::array
<int, 10> ints
= {{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}};
176 std::array
<int, 10> ones
= {{1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
177 std::array
<std::tuple
<int, int>, 10> tuples
= {{
190 auto first
= boost::stl_interfaces::make_reverse_iterator(
191 zip_iter(ints
.data() + ints
.size(), ones
.data() + ones
.size()));
192 auto last
= boost::stl_interfaces::make_reverse_iterator(
193 zip_iter(ints
.data(), ones
.data()));
196 auto tuples_copy
= tuples
;
197 std::reverse(tuples_copy
.begin(), tuples_copy
.end());
198 BOOST_TEST(first
- last
== tuples_copy
.begin() - tuples_copy
.end());
200 std::equal(first
, last
, tuples_copy
.begin(), tuples_copy
.end()));
204 std::array
<std::tuple
<int, int>, 10> tuples_copy
;
205 std::reverse_copy(first
, last
, tuples_copy
.begin());
206 BOOST_TEST(tuples_copy
== tuples
);
210 std::size_t count
= 0;
211 for (auto it
= first
; it
!= last
; ++it
) {
214 BOOST_TEST(count
== tuples
.size());
218 std::size_t count
= 0;
219 for (auto it
= first
; it
!= last
; it
++) {
222 BOOST_TEST(count
== tuples
.size());
226 std::size_t count
= 0;
227 for (auto it
= last
; it
!= first
; --it
) {
230 BOOST_TEST(count
== tuples
.size());
234 std::size_t count
= 0;
235 for (auto it
= last
; it
!= first
; it
--) {
238 BOOST_TEST(count
== tuples
.size());
242 return boost::report_errors();