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/map.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_ext.hpp>
19
20 #include <algorithm>
21 #include <map>
22 #include <vector>
23
24 namespace boost
25 {
26 namespace
27 {
28 template< class Container >
map_test_keys(Container & c)29 void map_test_keys( Container& c )
30 {
31 using namespace boost::adaptors;
32
33 std::vector<int> keys;
34 boost::push_back(keys, c | map_keys);
35
36 std::vector<int> keys2;
37 boost::push_back(keys2, adaptors::keys(c));
38
39 std::vector<int> reference_keys;
40 typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
41 for (iter_t it = c.begin(); it != c.end(); ++it)
42 {
43 reference_keys.push_back(it->first);
44 }
45
46 BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
47 reference_keys.end(),
48 keys.begin(),
49 keys.end() );
50
51 BOOST_CHECK_EQUAL_COLLECTIONS( reference_keys.begin(),
52 reference_keys.end(),
53 keys2.begin(),
54 keys2.end() );
55 }
56
57 template< class Container >
map_test_values(Container & c)58 void map_test_values( Container& c )
59 {
60 using namespace boost::adaptors;
61
62 std::vector<int> values;
63 boost::push_back(values, c | map_values);
64
65 std::vector<int> values2;
66 boost::push_back(values2, adaptors::values(c));
67
68 std::vector<int> reference_values;
69 typedef BOOST_DEDUCED_TYPENAME Container::const_iterator iter_t;
70 for (iter_t it = c.begin(); it != c.end(); ++it)
71 {
72 reference_values.push_back(it->second);
73 }
74
75 BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
76 reference_values.end(),
77 values.begin(),
78 values.end() );
79
80 BOOST_CHECK_EQUAL_COLLECTIONS( reference_values.begin(),
81 reference_values.end(),
82 values2.begin(),
83 values2.end() );
84 }
85
86 template< class Container >
map_test_impl()87 void map_test_impl()
88 {
89 using namespace boost::assign;
90
91 Container c;
92
93 // Test empty
94 map_test_keys(c);
95 map_test_values(c);
96
97 // Test one element
98 c.insert(std::make_pair(1,2));
99 map_test_keys(c);
100 map_test_values(c);
101
102 // Test many elements
103 for (int x = 2; x < 10; ++x)
104 {
105 c.insert(std::make_pair(x, x * 2));
106 }
107 map_test_keys(c);
108 map_test_values(c);
109 }
110
map_test()111 void map_test()
112 {
113 map_test_impl< std::map<int,int> >();
114 }
115
test_trac_item_4388()116 void test_trac_item_4388()
117 {
118 typedef std::pair<int,char> pair_t;
119 const boost::array<pair_t,3> ar = {{
120 pair_t(3, 'a'),
121 pair_t(1, 'b'),
122 pair_t(4, 'c')
123 }};
124
125 const boost::array<int, 3> expected_keys = {{ 3, 1, 4 }};
126 const boost::array<char, 3> expected_values = {{ 'a', 'b', 'c' }};
127
128 {
129 std::vector<int> test;
130 boost::push_back(test, ar | boost::adaptors::map_keys);
131 BOOST_CHECK_EQUAL_COLLECTIONS(
132 expected_keys.begin(), expected_keys.end(),
133 test.begin(), test.end()
134 );
135 }
136
137 {
138 std::vector<char> test;
139 boost::push_back(test, ar | boost::adaptors::map_values);
140 BOOST_CHECK_EQUAL_COLLECTIONS(
141 expected_values.begin(), expected_values.end(),
142 test.begin(), test.end()
143 );
144 }
145
146 {
147 std::vector<char> test;
148 boost::array<std::pair<int, char>, 3> src(ar);
149 boost::push_back(test, src | boost::adaptors::map_values);
150 BOOST_CHECK_EQUAL_COLLECTIONS(
151 expected_values.begin(), expected_values.end(),
152 test.begin(), test.end()
153 );
154 }
155 }
156
157 }
158 }
159
160
161 boost::unit_test::test_suite*
init_unit_test_suite(int argc,char * argv[])162 init_unit_test_suite(int argc, char* argv[])
163 {
164 boost::unit_test::test_suite* test
165 = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.map" );
166
167 test->add( BOOST_TEST_CASE( &boost::map_test ) );
168 test->add( BOOST_TEST_CASE( &boost::test_trac_item_4388 ) );
169
170 return test;
171 }
172