• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2019 T. Zachary Laine
2 //
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>
8 
9 #include <boost/core/lightweight_test.hpp>
10 
11 #include <algorithm>
12 #include <array>
13 #include <list>
14 #include <tuple>
15 #include <vector>
16 
17 
18 struct zip_iter : boost::stl_interfaces::proxy_iterator_interface<
19                       zip_iter,
20                       std::random_access_iterator_tag,
21                       std::tuple<int, int>,
22                       std::tuple<int &, int &>>
23 {
zip_iterzip_iter24     zip_iter() : it1_(nullptr), it2_(nullptr) {}
zip_iterzip_iter25     zip_iter(int * it1, int * it2) : it1_(it1), it2_(it2) {}
26 
operator *zip_iter27     std::tuple<int &, int &> operator*() const
28     {
29         return std::tuple<int &, int &>{*it1_, *it2_};
30     }
operator +=zip_iter31     zip_iter & operator+=(std::ptrdiff_t i)
32     {
33         it1_ += i;
34         it2_ += i;
35         return *this;
36     }
operator -(zip_iter lhs,zip_iter rhs)37     friend std::ptrdiff_t operator-(zip_iter lhs, zip_iter rhs) noexcept
38     {
39         return lhs.it1_ - rhs.it1_;
40     }
41 
42 private:
43     int * it1_;
44     int * it2_;
45 };
46 
47 
main()48 int main()
49 {
50 
51 {
52     std::list<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
53 
54     auto first = boost::stl_interfaces::make_reverse_iterator(ints.end());
55     auto last = boost::stl_interfaces::make_reverse_iterator(ints.begin());
56 
57     {
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));
63     }
64 
65     {
66         auto ints_copy = ints;
67         std::reverse(ints_copy.begin(), ints_copy.end());
68         BOOST_TEST(
69             std::equal(first, last, ints_copy.begin(), ints_copy.end()));
70     }
71 
72     {
73         std::list<int> ints_copy;
74         std::reverse_copy(first, last, std::back_inserter(ints_copy));
75         BOOST_TEST(ints_copy == ints);
76     }
77 
78     {
79         std::size_t count = 0;
80         for (auto it = first; it != last; ++it) {
81             ++count;
82         }
83         BOOST_TEST(count == ints.size());
84     }
85 
86     {
87         std::size_t count = 0;
88         for (auto it = first; it != last; it++) {
89             ++count;
90         }
91         BOOST_TEST(count == ints.size());
92     }
93 
94     {
95         std::size_t count = 0;
96         for (auto it = last; it != first; --it) {
97             ++count;
98         }
99         BOOST_TEST(count == ints.size());
100     }
101 
102     {
103         std::size_t count = 0;
104         for (auto it = last; it != first; it--) {
105             ++count;
106         }
107         BOOST_TEST(count == ints.size());
108     }
109 }
110 
111 
112 {
113     std::vector<int> ints = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
114 
115     auto first = boost::stl_interfaces::make_reverse_iterator(ints.end());
116     auto last = boost::stl_interfaces::make_reverse_iterator(ints.begin());
117 
118     {
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));
124     }
125 
126     {
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());
130         BOOST_TEST(
131             std::equal(first, last, ints_copy.begin(), ints_copy.end()));
132     }
133 
134     {
135         std::vector<int> ints_copy;
136         std::reverse_copy(first, last, std::back_inserter(ints_copy));
137         BOOST_TEST(ints_copy == ints);
138     }
139 
140     {
141         std::size_t count = 0;
142         for (auto it = first; it != last; ++it) {
143             ++count;
144         }
145         BOOST_TEST(count == ints.size());
146     }
147 
148     {
149         std::size_t count = 0;
150         for (auto it = first; it != last; it++) {
151             ++count;
152         }
153         BOOST_TEST(count == ints.size());
154     }
155 
156     {
157         std::size_t count = 0;
158         for (auto it = last; it != first; --it) {
159             ++count;
160         }
161         BOOST_TEST(count == ints.size());
162     }
163 
164     {
165         std::size_t count = 0;
166         for (auto it = last; it != first; it--) {
167             ++count;
168         }
169         BOOST_TEST(count == ints.size());
170     }
171 }
172 
173 
174 {
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 = {{
178         {0, 1},
179         {1, 1},
180         {2, 1},
181         {3, 1},
182         {4, 1},
183         {5, 1},
184         {6, 1},
185         {7, 1},
186         {8, 1},
187         {9, 1},
188     }};
189 
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()));
194 
195     {
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());
199         BOOST_TEST(
200             std::equal(first, last, tuples_copy.begin(), tuples_copy.end()));
201     }
202 
203     {
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);
207     }
208 
209     {
210         std::size_t count = 0;
211         for (auto it = first; it != last; ++it) {
212             ++count;
213         }
214         BOOST_TEST(count == tuples.size());
215     }
216 
217     {
218         std::size_t count = 0;
219         for (auto it = first; it != last; it++) {
220             ++count;
221         }
222         BOOST_TEST(count == tuples.size());
223     }
224 
225     {
226         std::size_t count = 0;
227         for (auto it = last; it != first; --it) {
228             ++count;
229         }
230         BOOST_TEST(count == tuples.size());
231     }
232 
233     {
234         std::size_t count = 0;
235         for (auto it = last; it != first; it--) {
236             ++count;
237         }
238         BOOST_TEST(count == tuples.size());
239     }
240 }
241 
242     return boost::report_errors();
243 }
244