1 // Boost test program for next() and prior() utilities.
2
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.)
7
8 // See http://www.boost.org/libs/utility for documentation.
9
10 // Revision History 13 Dec 2003 Initial Version (Daniel Walker)
11
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.
20
21 #include <boost/core/lightweight_test.hpp>
22
23 #include <list>
24 #include <vector>
25
26 #include <boost/next_prior.hpp>
27
28 template<class RandomAccessIterator, class ForwardIterator>
plus_one_test(RandomAccessIterator first,RandomAccessIterator last,ForwardIterator first2)29 bool plus_one_test(RandomAccessIterator first, RandomAccessIterator last, ForwardIterator first2)
30 {
31 RandomAccessIterator i = first;
32 ForwardIterator j = first2;
33 while(i != last)
34 i = i + 1, j = boost::next(j);
35 return std::distance(first, i) == std::distance(first2, j);
36 }
37
38 template<class RandomAccessIterator, class ForwardIterator>
plus_n_test(RandomAccessIterator first,RandomAccessIterator last,ForwardIterator first2)39 bool plus_n_test(RandomAccessIterator first, RandomAccessIterator last, ForwardIterator first2)
40 {
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);
46 }
47
48 template<class RandomAccessIterator, class BidirectionalIterator>
minus_one_test(RandomAccessIterator first,RandomAccessIterator last,BidirectionalIterator last2)49 bool minus_one_test(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator last2)
50 {
51 RandomAccessIterator i = last;
52 BidirectionalIterator j = last2;
53 while(i != first)
54 i = i - 1, j = boost::prior(j);
55 return std::distance(i, last) == std::distance(j, last2);
56 }
57
58 template<class RandomAccessIterator, class BidirectionalIterator>
minus_n_test(RandomAccessIterator first,RandomAccessIterator last,BidirectionalIterator last2)59 bool minus_n_test(RandomAccessIterator first, RandomAccessIterator last, BidirectionalIterator last2)
60 {
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);
66 }
67
68 template<class Iterator, class Distance>
minus_n_unsigned_test(Iterator first,Iterator last,Distance size)69 bool minus_n_unsigned_test(Iterator first, Iterator last, Distance size)
70 {
71 Iterator i = boost::prior(last, size);
72 return i == first;
73 }
74
main(int,char * [])75 int main(int, char*[])
76 {
77 std::vector<int> x(8);
78 std::list<int> y(x.begin(), x.end());
79
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()));
87
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()));
94
95 // Test with pointers
96 std::vector<int> z(x.size());
97 int* p = &z[0];
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()));
103
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);
110
111 return boost::report_errors();
112 }
113