]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/algorithm/test/find_backward_test.cpp
2 Copyright (c) T. Zachary Laine 2018.
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 For more information, see http://www.boost.org
11 #include <boost/algorithm/find_backward.hpp>
13 #define BOOST_TEST_MAIN
14 #include <boost/test/unit_test.hpp>
20 namespace ba
= boost::algorithm
;
22 template <typename Container
>
25 dist_t(Container
& cont
) : cont_(cont
) {}
26 template<typename Iter
>
27 std::ptrdiff_t operator()(Iter it
) const
29 return std::distance(cont_
.begin(), it
);
35 BOOST_CXX14_CONSTEXPR
bool check_constexpr_backward()
37 int in_data
[] = {1, 2, 3, 4, 5};
40 const int* from
= in_data
;
41 const int* to
= in_data
+ 5;
43 const int* start
= ba::find_backward(from
, to
, 1); // stops on first
44 res
= (res
&& start
== from
);
46 start
= ba::find_backward(in_data
, 1); // stops on first
47 res
= (res
&& start
== from
);
49 const int* end
= ba::find_backward(from
, to
, 6); // stops on the end
50 res
= (res
&& end
== to
);
52 end
= ba::find_backward(in_data
, 6); // stops on the end
53 res
= (res
&& end
== to
);
55 const int* three
= ba::find_backward(from
, to
, 3); // stops on third element
56 res
= (res
&& three
== in_data
+ 2);
58 three
= ba::find_backward(in_data
, 3); // stops on third element
59 res
= (res
&& three
== in_data
+ 2);
64 void test_find_backward()
68 const dist_t
<std::vector
<int> > dist(v1
);
70 for (int i
= 5; i
< 15; ++i
)
73 dist(ba::find_backward(v1
.begin(), v1
.end(), 0)), v1
.size());
75 dist(ba::find_backward(v1
.begin(), v1
.end(), 100)), v1
.size());
77 dist(ba::find_backward(v1
.begin(), v1
.end(), v1
.back())),
80 dist(ba::find_backward(v1
.begin(), v1
.end(), v1
.front())), 0);
82 BOOST_CHECK_EQUAL(dist(ba::find_backward(v1
, 0)), v1
.size());
83 BOOST_CHECK_EQUAL(dist(ba::find_backward(v1
, 100)), v1
.size());
85 dist(ba::find_backward(v1
, v1
.back())), v1
.size() - 1);
86 BOOST_CHECK_EQUAL(dist(ba::find_backward(v1
, v1
.front())), 0);
89 // With bidirectional iterators.
92 const dist_t
<std::list
<int> > dist(l1
);
94 for (int i
= 5; i
< 15; ++i
)
97 dist(ba::find_backward(l1
.begin(), l1
.end(), 0)), l1
.size());
99 dist(ba::find_backward(l1
.begin(), l1
.end(), 100)), l1
.size());
101 dist(ba::find_backward(l1
.begin(), l1
.end(), l1
.back())),
104 dist(ba::find_backward(l1
.begin(), l1
.end(), l1
.front())), 0);
106 BOOST_CHECK_EQUAL(dist(ba::find_backward(l1
, 0)), l1
.size());
107 BOOST_CHECK_EQUAL(dist(ba::find_backward(l1
, 100)), l1
.size());
109 dist(ba::find_backward(l1
, l1
.back())), l1
.size() - 1);
110 BOOST_CHECK_EQUAL(dist(ba::find_backward(l1
, l1
.front())), 0);
113 BOOST_CXX14_CONSTEXPR
bool ce_result
= check_constexpr_backward();
114 BOOST_CHECK(ce_result
);
119 BOOST_CXX14_CONSTEXPR
equals(int n
) : n_(n
) {}
120 BOOST_CXX14_CONSTEXPR
bool operator()(int i
) { return i
== n_
; }
124 BOOST_CXX14_CONSTEXPR
bool check_constexpr_if_backward()
126 int in_data
[] = {1, 2, 3, 4, 5};
129 const int* from
= in_data
;
130 const int* to
= in_data
+ 5;
132 const int* start
= ba::find_if_backward(from
, to
, equals(1)); // stops on first
133 res
= (res
&& start
== from
);
135 start
= ba::find_if_backward(in_data
, equals(1)); // stops on first
136 res
= (res
&& start
== from
);
138 const int* end
= ba::find_if_backward(from
, to
, equals(6)); // stops on the end
139 res
= (res
&& end
== to
);
141 end
= ba::find_if_backward(in_data
, equals(6)); // stops on the end
142 res
= (res
&& end
== to
);
144 const int* three
= ba::find_if_backward(from
, to
, equals(3)); // stops on third element
145 res
= (res
&& three
== in_data
+ 2);
147 three
= ba::find_if_backward(in_data
, equals(3)); // stops on third element
148 res
= (res
&& three
== in_data
+ 2);
153 void test_find_if_backward()
157 const dist_t
<std::vector
<int> > dist(v1
);
159 for (int i
= 5; i
< 15; ++i
)
162 dist(ba::find_if_backward(v1
.begin(), v1
.end(), equals(0))),
165 dist(ba::find_if_backward(v1
.begin(), v1
.end(), equals(100))),
168 dist(ba::find_if_backward(v1
.begin(), v1
.end(), equals(v1
.back()))),
172 ba::find_if_backward(v1
.begin(), v1
.end(), equals(v1
.front()))),
175 BOOST_CHECK_EQUAL(dist(ba::find_if_backward(v1
, equals(0))), v1
.size());
177 dist(ba::find_if_backward(v1
, equals(100))), v1
.size());
179 dist(ba::find_if_backward(v1
, equals(v1
.back()))), v1
.size() - 1);
181 dist(ba::find_if_backward(v1
, equals(v1
.front()))), 0);
184 // With bidirectional iterators.
187 const dist_t
<std::list
<int> > dist(l1
);
189 for (int i
= 5; i
< 15; ++i
)
192 dist(ba::find_if_backward(l1
.begin(), l1
.end(), equals(0))),
195 dist(ba::find_if_backward(l1
.begin(), l1
.end(), equals(100))),
198 dist(ba::find_if_backward(l1
.begin(), l1
.end(), equals(l1
.back()))),
202 ba::find_if_backward(l1
.begin(), l1
.end(), equals(l1
.front()))),
205 BOOST_CHECK_EQUAL(dist(ba::find_if_backward(l1
, equals(0))), l1
.size());
207 dist(ba::find_if_backward(l1
, equals(100))), l1
.size());
209 dist(ba::find_if_backward(l1
, equals(l1
.back()))), l1
.size() - 1);
211 dist(ba::find_if_backward(l1
, equals(l1
.front()))), 0);
214 BOOST_CXX14_CONSTEXPR
bool ce_result
= check_constexpr_if_backward();
215 BOOST_CHECK(ce_result
);
220 BOOST_CXX14_CONSTEXPR
not_equals(int n
) : n_(n
) {}
221 BOOST_CXX14_CONSTEXPR
bool operator()(int i
) { return i
!= n_
; }
225 BOOST_CXX14_CONSTEXPR
bool check_constexpr_if_not_backward()
227 int in_data
[] = {1, 2, 3, 4, 5};
230 const int* from
= in_data
;
231 const int* to
= in_data
+ 5;
233 const int* start
= ba::find_if_not_backward(from
, to
, not_equals(1)); // stops on first
234 res
= (res
&& start
== from
);
236 start
= ba::find_if_not_backward(in_data
, not_equals(1)); // stops on first
237 res
= (res
&& start
== from
);
239 const int* end
= ba::find_if_not_backward(from
, to
, not_equals(6)); // stops on the end
240 res
= (res
&& end
== to
);
242 end
= ba::find_if_not_backward(in_data
, not_equals(6)); // stops on the end
243 res
= (res
&& end
== to
);
245 const int* three
= ba::find_if_not_backward(from
, to
, not_equals(3)); // stops on third element
246 res
= (res
&& three
== in_data
+ 2);
248 three
= ba::find_if_not_backward(in_data
, not_equals(3)); // stops on third element
249 res
= (res
&& three
== in_data
+ 2);
254 void test_find_if_not_backward()
258 const dist_t
<std::vector
<int> > dist(v1
);
260 for (int i
= 5; i
< 15; ++i
)
263 dist(ba::find_if_not_backward(v1
.begin(), v1
.end(), not_equals(0))),
266 dist(ba::find_if_not_backward(
267 v1
.begin(), v1
.end(), not_equals(100))),
270 dist(ba::find_if_not_backward(
271 v1
.begin(), v1
.end(), not_equals(v1
.back()))),
274 dist(ba::find_if_not_backward(
275 v1
.begin(), v1
.end(), not_equals(v1
.front()))),
279 dist(ba::find_if_not_backward(v1
, not_equals(0))), v1
.size());
281 dist(ba::find_if_not_backward(v1
, not_equals(100))), v1
.size());
283 dist(ba::find_if_not_backward(v1
, not_equals(v1
.back()))),
286 dist(ba::find_if_not_backward(v1
, not_equals(v1
.front()))), 0);
289 // With bidirectional iterators.
292 const dist_t
<std::list
<int> > dist(l1
);
294 for (int i
= 5; i
< 15; ++i
)
297 dist(ba::find_if_not_backward(l1
.begin(), l1
.end(), not_equals(0))),
300 dist(ba::find_if_not_backward(
301 l1
.begin(), l1
.end(), not_equals(100))),
304 dist(ba::find_if_not_backward(
305 l1
.begin(), l1
.end(), not_equals(l1
.back()))),
308 dist(ba::find_if_not_backward(
309 l1
.begin(), l1
.end(), not_equals(l1
.front()))),
313 dist(ba::find_if_not_backward(l1
, not_equals(0))), l1
.size());
315 dist(ba::find_if_not_backward(l1
, not_equals(100))), l1
.size());
317 dist(ba::find_if_not_backward(l1
, not_equals(l1
.back()))),
320 dist(ba::find_if_not_backward(l1
, not_equals(l1
.front()))), 0);
323 BOOST_CXX14_CONSTEXPR
bool ce_result
= check_constexpr_if_not_backward();
324 BOOST_CHECK(ce_result
);
327 BOOST_CXX14_CONSTEXPR
bool check_constexpr_not_backward()
329 int in_data
[] = {1, 5, 5, 5, 5};
332 const int* from
= in_data
;
333 const int* to
= in_data
+ 5;
335 const int* start
= ba::find_not_backward(from
, to
, 5); // stops on first
336 res
= (res
&& start
== from
);
338 start
= ba::find_not_backward(in_data
, 5); // stops on first
339 res
= (res
&& start
== from
);
341 const int in_data_2
[] = {6, 6, 6, 6, 6};
342 const int* end
= ba::find_not_backward(in_data_2
, in_data_2
+ 5, 6); // stops on the end
343 res
= (res
&& end
== in_data_2
+ 5);
345 end
= ba::find_not_backward(in_data_2
, 6); // stops on the end
346 res
= (res
&& end
== in_data_2
+ 5);
351 void test_find_not_backward()
355 const dist_t
<std::vector
<int> > dist(v1
);
357 for (int i
= 0; i
< 5; ++i
)
359 for (int i
= 0; i
< 5; ++i
)
362 dist(ba::find_not_backward(v1
.begin(), v1
.end(), 1)), 4);
364 dist(ba::find_not_backward(v1
.begin(), v1
.end(), 0)),
367 dist(ba::find_not_backward(v1
.begin(), v1
.end(), 2)),
370 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1
, 1)), 4);
371 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1
, 0)), v1
.size() - 1);
372 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1
, 2)), v1
.size() - 1);
376 dist(ba::find_not_backward(v1
.begin(), v1
.end(), 0)), v1
.size());
378 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(v1
, 0)), v1
.size());
381 // With bidirectional iterators.
384 const dist_t
<std::list
<int> > dist(l1
);
386 for (int i
= 0; i
< 5; ++i
)
388 for (int i
= 0; i
< 5; ++i
)
391 dist(ba::find_not_backward(l1
.begin(), l1
.end(), 1)), 4);
393 dist(ba::find_not_backward(l1
.begin(), l1
.end(), 0)),
396 dist(ba::find_not_backward(l1
.begin(), l1
.end(), 2)),
399 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1
, 1)), 4);
400 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1
, 0)), l1
.size() - 1);
401 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1
, 2)), l1
.size() - 1);
405 dist(ba::find_not_backward(l1
.begin(), l1
.end(), 0)), l1
.size());
407 BOOST_CHECK_EQUAL(dist(ba::find_not_backward(l1
, 0)), l1
.size());
410 BOOST_CXX14_CONSTEXPR
bool ce_result
= check_constexpr_not_backward();
411 BOOST_CHECK(ce_result
);
414 BOOST_AUTO_TEST_CASE(test_main
)
416 test_find_backward();
417 test_find_if_backward();
418 test_find_if_not_backward();
419 test_find_not_backward();