]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/generate.hpp> | |
12 | ||
13 | #include <boost/test/test_tools.hpp> | |
14 | #include <boost/test/unit_test.hpp> | |
15 | ||
16 | #include <boost/assign.hpp> | |
7c673cae FG |
17 | #include <algorithm> |
18 | #include <functional> | |
19 | #include <list> | |
20 | #include <numeric> | |
21 | #include <deque> | |
22 | #include <vector> | |
23 | ||
24 | namespace boost | |
25 | { | |
26 | namespace | |
27 | { | |
28 | class generator_fn | |
29 | { | |
30 | public: | |
31 | typedef int result_type; | |
32 | ||
33 | generator_fn() : m_count(0) {} | |
34 | int operator()() { return ++m_count; } | |
35 | ||
36 | private: | |
37 | int m_count; | |
38 | }; | |
39 | ||
40 | template< class Container > | |
41 | void test_generate_impl(Container& cont) | |
42 | { | |
43 | Container reference(cont); | |
44 | std::generate(reference.begin(), reference.end(), generator_fn()); | |
45 | ||
46 | Container test(cont); | |
47 | boost::generate(test, generator_fn()); | |
48 | ||
49 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), | |
50 | test.begin(), test.end() ); | |
51 | ||
52 | Container test2(cont); | |
53 | boost::generate(boost::make_iterator_range(test2), generator_fn()); | |
54 | ||
55 | BOOST_CHECK_EQUAL_COLLECTIONS( reference.begin(), reference.end(), | |
56 | test2.begin(), test2.end() ); | |
57 | } | |
58 | ||
59 | template< class Container > | |
60 | void test_generate_impl() | |
61 | { | |
62 | using namespace boost::assign; | |
63 | ||
64 | Container cont; | |
65 | test_generate_impl(cont); | |
66 | ||
67 | cont.clear(); | |
68 | cont += 9; | |
69 | test_generate_impl(cont); | |
70 | ||
71 | cont.clear(); | |
72 | cont += 9,8,7,6,5,4,3,2,1; | |
73 | test_generate_impl(cont); | |
74 | } | |
75 | ||
76 | void test_generate() | |
77 | { | |
78 | test_generate_impl< std::vector<int> >(); | |
79 | test_generate_impl< std::list<int> >(); | |
80 | test_generate_impl< std::deque<int> >(); | |
81 | } | |
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.generate" ); | |
91 | ||
92 | test->add( BOOST_TEST_CASE( &boost::test_generate ) ); | |
93 | ||
94 | return test; | |
95 | } |