1 // Boost.Range library
2 //
3 //  Copyright Neil Groves 2010. 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_ext/for_each.hpp>
12 
13 #include <boost/test/test_tools.hpp>
14 #include <boost/test/unit_test.hpp>
15 
16 #include <boost/range/iterator.hpp>
17 #include <algorithm>
18 #include <list>
19 #include <vector>
20 
21 namespace
22 {
23     struct MockBinaryFn
24     {
25         typedef void result_type;
26         typedef int first_argument_type;
27         typedef int second_argument_type;
28 
operator ()__anon1099d68c0111::MockBinaryFn29         void operator()(int x, int y)
30         {
31             xs.push_back(x);
32             ys.push_back(y);
33         }
34 
35         std::vector<int> xs;
36         std::vector<int> ys;
37     };
38 
39     template< class Range1, class Range2 >
test_for_each_impl(Range1 & rng1,Range2 & rng2)40     void test_for_each_impl( Range1& rng1, Range2& rng2 )
41     {
42         MockBinaryFn fn = boost::range::for_each(rng1, rng2, MockBinaryFn());
43 
44         BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng1), ::boost::end(rng1),
45             fn.xs.begin(), fn.xs.end() );
46 
47         BOOST_CHECK_EQUAL_COLLECTIONS( ::boost::begin(rng2), ::boost::end(rng2),
48             fn.ys.begin(), fn.ys.end() );
49     }
50 
51     template< class Collection1, class Collection2 >
test_for_each_impl(const int max_count)52     void test_for_each_impl(const int max_count)
53     {
54         Collection1 c1;
55         for (int i = 0; i < max_count; ++i)
56             c1.push_back(i);
57 
58         Collection2 c2;
59         for (int i = 0; i < max_count; ++i)
60             c2.push_back(i);
61 
62         test_for_each_impl(c1, c2);
63 
64         const Collection1& const_c1 = c1;
65         const Collection2& const_c2 = c2;
66 
67         test_for_each_impl(c1, const_c2);
68         test_for_each_impl(const_c1, c2);
69         test_for_each_impl(const_c1, const_c2);
70     }
71 
72     template< class Collection1, class Collection2 >
test_for_each_impl()73     void test_for_each_impl()
74     {
75         test_for_each_impl< Collection1, Collection2 >(0);
76         test_for_each_impl< Collection1, Collection2 >(1);
77         test_for_each_impl< Collection1, Collection2 >(10);
78     }
79 
test_for_each()80     void test_for_each()
81     {
82         test_for_each_impl< std::vector<int>, std::vector<int> >();
83         test_for_each_impl< std::list<int>, std::list<int> >();
84         test_for_each_impl< std::vector<int>, std::list<int> >();
85         test_for_each_impl< std::list<int>, std::vector<int> >();
86     }
87 }
88 
89 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])90 init_unit_test_suite(int argc, char* argv[])
91 {
92     boost::unit_test::test_suite* test
93         = BOOST_TEST_SUITE( "RangeTestSuite.algorithm_ext.for_each" );
94 
95     test->add( BOOST_TEST_CASE( &test_for_each ) );
96 
97     return test;
98 }
99