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/algorithm/for_each.hpp> |
12 | |
13 | #include <boost/test/test_tools.hpp> |
14 | #include <boost/test/unit_test.hpp> |
15 | |
16 | #include <boost/array.hpp> |
17 | #include <boost/assign.hpp> |
18 | #include <boost/range/algorithm.hpp> |
19 | |
20 | #include <list> |
21 | #include <set> |
22 | #include <vector> |
23 | #include "../test_function/check_equal_fn.hpp" |
24 | |
25 | namespace boost |
26 | { |
27 | namespace |
28 | { |
29 | template< class Range > |
30 | unsigned int udistance(Range& rng) |
31 | { |
32 | return static_cast<unsigned int>(boost::distance(rng)); |
33 | } |
34 | |
35 | template< class SinglePassRange > |
36 | void test_for_each_impl( SinglePassRange rng ) |
37 | { |
38 | using namespace boost::range_test_function; |
39 | |
40 | typedef check_equal_fn< SinglePassRange > fn_t; |
41 | |
42 | // Test the mutable version |
43 | fn_t result_fn = boost::for_each(rng, fn_t(rng)); |
44 | BOOST_CHECK_EQUAL( boost::udistance(rng), result_fn.invocation_count() ); |
45 | |
46 | fn_t result_fn2 = boost::for_each(boost::make_iterator_range(rng), fn_t(rng)); |
47 | BOOST_CHECK_EQUAL( boost::udistance(rng), result_fn2.invocation_count() ); |
48 | |
49 | // Test the constant version |
50 | const SinglePassRange& cref_rng = rng; |
51 | result_fn = boost::for_each(cref_rng, fn_t(cref_rng)); |
52 | BOOST_CHECK_EQUAL( boost::udistance(cref_rng), result_fn.invocation_count() ); |
53 | } |
54 | |
55 | template< class Container > |
56 | void test_for_each_t() |
57 | { |
58 | using namespace boost::assign; |
59 | |
60 | // Test empty |
61 | Container cont; |
62 | test_for_each_impl(cont); |
63 | |
64 | // Test one element |
65 | cont += 0; |
66 | test_for_each_impl(cont); |
67 | |
68 | // Test many elements |
69 | cont += 1,2,3,4,5,6,7,8,9; |
70 | test_for_each_impl(cont); |
71 | } |
72 | |
73 | void test_for_each() |
74 | { |
75 | boost::array<int, 10> a = {.elems: { 0,1,2,3,4,5,6,7,8,9 }}; |
76 | test_for_each_impl(rng: a); |
77 | |
78 | test_for_each_t< std::vector<int> >(); |
79 | test_for_each_t< std::list<int> >(); |
80 | test_for_each_t< std::set<int> >(); |
81 | test_for_each_t< std::multiset<int> >(); |
82 | } |
83 | } |
84 | } |
85 | |
86 | boost::unit_test::test_suite* |
87 | init_unit_test_suite(int argc, char* argv[]) |
88 | { |
89 | boost::unit_test::test_suite* test |
90 | = BOOST_TEST_SUITE( "RangeTestSuite.algorithm.for_each" ); |
91 | |
92 | test->add( BOOST_TEST_CASE( &boost::test_for_each ) ); |
93 | |
94 | return test; |
95 | } |
96 | |