1 | // Boost.Range library |
2 | // |
3 | // Copyright Neil Groves 2009. Use, modification and |
4 | // distribution is subject to the Boost Software License, Version |
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
6 | // http://www.boost.org/LICENSE_1_0.txt) |
7 | // |
8 | // |
9 | // For more information, see http://www.boost.org/libs/range/ |
10 | // |
11 | #include <boost/range/adaptor/adjacent_filtered.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/assign.hpp> |
17 | #include <algorithm> |
18 | #include <list> |
19 | #include <set> |
20 | #include <string> |
21 | #include <vector> |
22 | |
23 | namespace boost |
24 | { |
25 | namespace |
26 | { |
27 | template< class Container > |
28 | void adjacent_filtered_test_impl( Container& c ) |
29 | { |
30 | using namespace boost::adaptors; |
31 | |
32 | typedef BOOST_DEDUCED_TYPENAME Container::value_type value_t; |
33 | |
34 | // This is my preferred syntax using the | operator. |
35 | std::vector< value_t > test_result1 |
36 | = boost::copy_range< std::vector< value_t > >( |
37 | c | adjacent_filtered(std::not_equal_to< value_t >())); |
38 | |
39 | // This is an alternative syntax preferred by some. |
40 | std::vector< value_t > test_result2 |
41 | = boost::copy_range< std::vector< value_t > >( |
42 | adaptors::adjacent_filter(c, std::not_equal_to< value_t >())); |
43 | |
44 | // Calculate the reference result. |
45 | std::vector< value_t > reference_result; |
46 | typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t; |
47 | value_t prev_v = value_t(); |
48 | for (iter_t it = c.begin(); it != c.end(); ++it) |
49 | { |
50 | if (it == c.begin()) |
51 | { |
52 | reference_result.push_back(*it); |
53 | } |
54 | else if (*it != prev_v) |
55 | { |
56 | reference_result.push_back(*it); |
57 | } |
58 | prev_v = *it; |
59 | } |
60 | |
61 | BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(), |
62 | reference_result.end(), |
63 | test_result1.begin(), |
64 | test_result1.end() ); |
65 | |
66 | BOOST_CHECK_EQUAL_COLLECTIONS( reference_result.begin(), |
67 | reference_result.end(), |
68 | test_result2.begin(), |
69 | test_result2.end() ); |
70 | } |
71 | |
72 | template< class Collection > |
73 | void adjacent_filtered_test_impl() |
74 | { |
75 | using namespace boost::assign; |
76 | |
77 | Collection c; |
78 | |
79 | // test empty collection |
80 | adjacent_filtered_test_impl(c); |
81 | |
82 | // test one element; |
83 | c += 1; |
84 | adjacent_filtered_test_impl(c); |
85 | |
86 | // test many elements; |
87 | c += 1,2,2,2,3,4,4,4,4,5,6,7,8,9,9; |
88 | adjacent_filtered_test_impl(c); |
89 | } |
90 | |
91 | void adjacent_filtered_test() |
92 | { |
93 | adjacent_filtered_test_impl< std::vector< int > >(); |
94 | adjacent_filtered_test_impl< std::list< int > >(); |
95 | adjacent_filtered_test_impl< std::set< int > >(); |
96 | adjacent_filtered_test_impl< std::multiset< int > >(); |
97 | } |
98 | } |
99 | } |
100 | |
101 | boost::unit_test::test_suite* |
102 | init_unit_test_suite(int argc, char* argv[]) |
103 | { |
104 | boost::unit_test::test_suite* test |
105 | = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.adjacent_filtered" ); |
106 | |
107 | test->add( BOOST_TEST_CASE( &boost::adjacent_filtered_test ) ); |
108 | |
109 | return test; |
110 | } |
111 | |