1 // Boost test program for next() and prior() utilities.
3 // Copyright 2003 Daniel Walker. Use, modification, and distribution
4 // are subject to the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or a copy at
6 // http://www.boost.org/LICENSE_1_0.txt.)
8 // See http://www.boost.org/libs/utility for documentation.
10 // Revision History 13 Dec 2003 Initial Version (Daniel Walker)
12 // next() and prior() are replacements for operator+ and operator- for
13 // non-random-access iterators. The semantics of these operators are
14 // such that after executing j = i + n, std::distance(i, j) equals
15 // n. Tests are provided to ensure next() has the same
16 // result. Parallel tests are provided for prior(). The tests call
17 // next() and prior() several times. next() and prior() are very
18 // simple functions, though, and it would be very strange if these
19 // tests were to fail.
21 #include <boost/core/lightweight_test.hpp>
26 #include <boost/next_prior.hpp>
28 template<class RandomAccessIterator
, class ForwardIterator
>
29 bool plus_one_test(RandomAccessIterator first
, RandomAccessIterator last
, ForwardIterator first2
)
31 RandomAccessIterator i
= first
;
32 ForwardIterator j
= first2
;
34 i
= i
+ 1, j
= boost::next(j
);
35 return std::distance(first
, i
) == std::distance(first2
, j
);
38 template<class RandomAccessIterator
, class ForwardIterator
>
39 bool plus_n_test(RandomAccessIterator first
, RandomAccessIterator last
, ForwardIterator first2
)
41 RandomAccessIterator i
= first
;
42 ForwardIterator j
= first2
;
43 for(int n
= 0; i
!= last
; ++n
)
44 i
= first
+ n
, j
= boost::next(first2
, n
);
45 return std::distance(first
, i
) == std::distance(first2
, j
);
48 template<class RandomAccessIterator
, class BidirectionalIterator
>
49 bool minus_one_test(RandomAccessIterator first
, RandomAccessIterator last
, BidirectionalIterator last2
)
51 RandomAccessIterator i
= last
;
52 BidirectionalIterator j
= last2
;
54 i
= i
- 1, j
= boost::prior(j
);
55 return std::distance(i
, last
) == std::distance(j
, last2
);
58 template<class RandomAccessIterator
, class BidirectionalIterator
>
59 bool minus_n_test(RandomAccessIterator first
, RandomAccessIterator last
, BidirectionalIterator last2
)
61 RandomAccessIterator i
= last
;
62 BidirectionalIterator j
= last2
;
63 for(int n
= 0; i
!= first
; ++n
)
64 i
= last
- n
, j
= boost::prior(last2
, n
);
65 return std::distance(i
, last
) == std::distance(j
, last2
);
68 template<class Iterator
, class Distance
>
69 bool minus_n_unsigned_test(Iterator first
, Iterator last
, Distance size
)
71 Iterator i
= boost::prior(last
, size
);
75 int main(int, char*[])
77 std::vector
<int> x(8);
78 std::list
<int> y(x
.begin(), x
.end());
80 // Tests with iterators
81 BOOST_TEST(plus_one_test(x
.begin(), x
.end(), y
.begin()));
82 BOOST_TEST(plus_n_test(x
.begin(), x
.end(), y
.begin()));
83 BOOST_TEST(minus_one_test(x
.begin(), x
.end(), y
.end()));
84 BOOST_TEST(minus_n_test(x
.begin(), x
.end(), y
.end()));
85 BOOST_TEST(minus_n_unsigned_test(x
.begin(), x
.end(), x
.size()));
86 BOOST_TEST(minus_n_unsigned_test(y
.begin(), y
.end(), y
.size()));
88 BOOST_TEST(plus_one_test(x
.rbegin(), x
.rend(), y
.begin()));
89 BOOST_TEST(plus_n_test(x
.rbegin(), x
.rend(), y
.begin()));
90 BOOST_TEST(minus_one_test(x
.rbegin(), x
.rend(), y
.end()));
91 BOOST_TEST(minus_n_test(x
.rbegin(), x
.rend(), y
.end()));
92 BOOST_TEST(minus_n_unsigned_test(x
.rbegin(), x
.rend(), x
.size()));
93 BOOST_TEST(minus_n_unsigned_test(x
.rbegin(), x
.rend(), y
.size()));
96 std::vector
<int> z(x
.size());
98 BOOST_TEST(plus_one_test(x
.begin(), x
.end(), p
));
99 BOOST_TEST(plus_n_test(x
.begin(), x
.end(), p
));
100 BOOST_TEST(minus_one_test(x
.begin(), x
.end(), p
+ z
.size()));
101 BOOST_TEST(minus_n_test(x
.begin(), x
.end(), p
+ z
.size()));
102 BOOST_TEST(minus_n_unsigned_test(p
, p
+ z
.size(), z
.size()));
104 // Tests with integers
105 BOOST_TEST(boost::next(5) == 6);
106 BOOST_TEST(boost::next(5, 7) == 12);
107 BOOST_TEST(boost::prior(5) == 4);
108 BOOST_TEST(boost::prior(5, 7) == -2);
109 BOOST_TEST(boost::prior(5, 7u) == -2);
111 return boost::report_errors();