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 #define BOOST_INCLUDE_MAIN
22 #include <boost/test/test_tools.hpp>
27 #include <boost/next_prior.hpp>
29 template<class RandomAccessIterator
, class ForwardIterator
>
30 bool plus_one_test(RandomAccessIterator first
, RandomAccessIterator last
, ForwardIterator first2
)
32 RandomAccessIterator i
= first
;
33 ForwardIterator j
= first2
;
35 i
= i
+ 1, j
= boost::next(j
);
36 return std::distance(first
, i
) == std::distance(first2
, j
);
39 template<class RandomAccessIterator
, class ForwardIterator
>
40 bool plus_n_test(RandomAccessIterator first
, RandomAccessIterator last
, ForwardIterator first2
)
42 RandomAccessIterator i
= first
;
43 ForwardIterator j
= first2
;
44 for(int n
= 0; i
!= last
; ++n
)
45 i
= first
+ n
, j
= boost::next(first2
, n
);
46 return std::distance(first
, i
) == std::distance(first2
, j
);
49 template<class RandomAccessIterator
, class BidirectionalIterator
>
50 bool minus_one_test(RandomAccessIterator first
, RandomAccessIterator last
, BidirectionalIterator last2
)
52 RandomAccessIterator i
= last
;
53 BidirectionalIterator j
= last2
;
55 i
= i
- 1, j
= boost::prior(j
);
56 return std::distance(i
, last
) == std::distance(j
, last2
);
59 template<class RandomAccessIterator
, class BidirectionalIterator
>
60 bool minus_n_test(RandomAccessIterator first
, RandomAccessIterator last
, BidirectionalIterator last2
)
62 RandomAccessIterator i
= last
;
63 BidirectionalIterator j
= last2
;
64 for(int n
= 0; i
!= first
; ++n
)
65 i
= last
- n
, j
= boost::prior(last2
, n
);
66 return std::distance(i
, last
) == std::distance(j
, last2
);
69 template<class Iterator
, class Distance
>
70 bool minus_n_unsigned_test(Iterator first
, Iterator last
, Distance size
)
72 Iterator i
= boost::prior(last
, size
);
76 int test_main(int, char*[])
78 std::vector
<int> x(8);
79 std::list
<int> y(x
.begin(), x
.end());
81 // Tests with iterators
82 BOOST_REQUIRE(plus_one_test(x
.begin(), x
.end(), y
.begin()));
83 BOOST_REQUIRE(plus_n_test(x
.begin(), x
.end(), y
.begin()));
84 BOOST_REQUIRE(minus_one_test(x
.begin(), x
.end(), y
.end()));
85 BOOST_REQUIRE(minus_n_test(x
.begin(), x
.end(), y
.end()));
86 BOOST_REQUIRE(minus_n_unsigned_test(x
.begin(), x
.end(), x
.size()));
87 BOOST_REQUIRE(minus_n_unsigned_test(y
.begin(), y
.end(), y
.size()));
89 // Tests with integers
90 BOOST_REQUIRE(boost::next(5) == 6);
91 BOOST_REQUIRE(boost::next(5, 7) == 12);
92 BOOST_REQUIRE(boost::prior(5) == 4);
93 BOOST_REQUIRE(boost::prior(5, 7) == -2);
94 BOOST_REQUIRE(boost::prior(5, 7u) == -2);