1 | // Boost.Assign library |
---|
2 | // |
---|
3 | // Copyright Thorsten Ottosen 2003-2004. 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 | // For more information, see http://www.boost.org/libs/assign/ |
---|
9 | // |
---|
10 | |
---|
11 | |
---|
12 | #include <boost/detail/workaround.hpp> |
---|
13 | |
---|
14 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
15 | # pragma warn -8091 // supress warning in Boost.Test |
---|
16 | # pragma warn -8057 // unused argument argc/argv in Boost.Test |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <boost/assign/list_of.hpp> |
---|
20 | #include <boost/test/test_tools.hpp> |
---|
21 | #include <boost/array.hpp> |
---|
22 | #include <algorithm> |
---|
23 | #include <vector> |
---|
24 | #include <list> |
---|
25 | #include <deque> |
---|
26 | #include <set> |
---|
27 | #include <map> |
---|
28 | #include <stack> |
---|
29 | #include <string> |
---|
30 | #include <cstdlib> |
---|
31 | #include <complex> |
---|
32 | |
---|
33 | struct nothing |
---|
34 | { |
---|
35 | template< class T > |
---|
36 | void operator()( T ) |
---|
37 | { } |
---|
38 | |
---|
39 | }; |
---|
40 | |
---|
41 | template< class Range > |
---|
42 | void for_each( const Range& r ) |
---|
43 | { |
---|
44 | std::for_each( r.begin(), r.end(), nothing() ); |
---|
45 | } |
---|
46 | |
---|
47 | namespace ba = boost::assign; |
---|
48 | |
---|
49 | template< class C > |
---|
50 | void test_sequence_list_of_string() |
---|
51 | { |
---|
52 | #if BOOST_WORKAROUND(BOOST_MSVC, <=1300) |
---|
53 | const C c = ba::list_of( "foo" )( "bar" ).to_container( c ); |
---|
54 | #else |
---|
55 | const C c = ba::list_of( "foo" )( "bar" ); |
---|
56 | #endif |
---|
57 | BOOST_CHECK_EQUAL( c.size(), 2u ); |
---|
58 | } |
---|
59 | |
---|
60 | struct parameter_list |
---|
61 | { |
---|
62 | int val; |
---|
63 | |
---|
64 | template< class T > |
---|
65 | parameter_list( T ) |
---|
66 | : val(0) |
---|
67 | { } |
---|
68 | |
---|
69 | template< class T > |
---|
70 | parameter_list( const T&, int ) |
---|
71 | : val(1) |
---|
72 | { } |
---|
73 | }; |
---|
74 | |
---|
75 | template< class C > |
---|
76 | void test_sequence_list_of_int() |
---|
77 | { |
---|
78 | using namespace std; |
---|
79 | #if BOOST_WORKAROUND(BOOST_MSVC, <=1300) |
---|
80 | |
---|
81 | const C c = ba::list_of<int>(1)(2)(3)(4).to_container( c ); |
---|
82 | const C c2 = ba::list_of(1)(2)(3)(4).to_container( c2 ); |
---|
83 | BOOST_CHECK_EQUAL( c.size(), 4u ); |
---|
84 | BOOST_CHECK_EQUAL( c2.size(), 4u ); |
---|
85 | C c3 = ba::list_of(1).repeat( 1, 2 )(3).to_container( c3 ); |
---|
86 | BOOST_CHECK_EQUAL( c3.size(), 3u ); |
---|
87 | |
---|
88 | c3 = ba::list_of(1).repeat_fun( 10, &rand )(2)(3).to_container( c3 ); |
---|
89 | BOOST_CHECK_EQUAL( c3.size(), 13u ); |
---|
90 | |
---|
91 | #else |
---|
92 | |
---|
93 | const C c = ba::list_of<int>(1)(2)(3)(4); |
---|
94 | const C c2 = ba::list_of(1)(2)(3)(4); |
---|
95 | BOOST_CHECK_EQUAL( c.size(), 4u ); |
---|
96 | BOOST_CHECK_EQUAL( c2.size(), 4u ); |
---|
97 | C c3 = ba::list_of(1).repeat( 1, 2 )(3); |
---|
98 | BOOST_CHECK_EQUAL( c3.size(), 3u ); |
---|
99 | |
---|
100 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
101 | // BCB fails to use operator=() directly, |
---|
102 | // it must be worked around using e.g. auxiliary variable |
---|
103 | C aux = ba::list_of(1).repeat_fun( 10, &rand )(2)(3); |
---|
104 | BOOST_CHECK_EQUAL( aux.size(), 13u ); |
---|
105 | c3 = aux; |
---|
106 | BOOST_CHECK_EQUAL( c3.size(), 13u ); |
---|
107 | #else |
---|
108 | c3 = ba::list_of(1).repeat_fun( 10, &rand )(2)(3); |
---|
109 | BOOST_CHECK_EQUAL( c3.size(), 13u ); |
---|
110 | #endif |
---|
111 | |
---|
112 | #endif |
---|
113 | |
---|
114 | parameter_list p( ba::list_of(1)(2), 3u ); |
---|
115 | BOOST_CHECK_EQUAL( p.val, 1 ); |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | template< class C > |
---|
120 | void test_map_list_of() |
---|
121 | { |
---|
122 | const C c = ba::list_of< std::pair<std::string,int> >( "foo", 1 )( "bar", 2 )( "buh", 3 )( "bah", 4 ); |
---|
123 | BOOST_CHECK_EQUAL( c.size(), 4u ); |
---|
124 | const C c2 = ba::map_list_of( "foo", 1 )( "bar", 2 )( "buh", 3 )( "bah", 4 ); |
---|
125 | BOOST_CHECK_EQUAL( c2.size(), 4u ); |
---|
126 | } |
---|
127 | |
---|
128 | void test_vector_matrix() |
---|
129 | { |
---|
130 | using namespace boost; |
---|
131 | using namespace boost::assign; |
---|
132 | using namespace std; |
---|
133 | |
---|
134 | #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(BOOST_MSVC, <=1300) |
---|
135 | #else |
---|
136 | |
---|
137 | const int sz = 3; |
---|
138 | typedef array<int,sz> row3; |
---|
139 | typedef array<row3,sz> matrix3x3; |
---|
140 | |
---|
141 | |
---|
142 | matrix3x3 m = list_of( list_of(1)(2)(3) ) |
---|
143 | ( list_of(4)(5)(6) ) |
---|
144 | ( list_of(7)(8)(9) ); |
---|
145 | |
---|
146 | for( int i = 0; i != sz; ++i ) |
---|
147 | for( int j = 0; j != sz; ++j ) |
---|
148 | BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 ); |
---|
149 | |
---|
150 | typedef vector<int> row; |
---|
151 | typedef vector<row> matrix; |
---|
152 | |
---|
153 | // |
---|
154 | // note: some libraries need a little help |
---|
155 | // with the conversion, hence the 'row' template parameter. |
---|
156 | // |
---|
157 | matrix m2 = list_of< row >( list_of(1)(2)(3) ) |
---|
158 | ( list_of(4)(5) ) |
---|
159 | ( list_of(6) ); |
---|
160 | |
---|
161 | for( int i = 0; i != sz; ++i ) |
---|
162 | for( int j = 0; j != sz - i; ++j ) |
---|
163 | BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 ); |
---|
164 | |
---|
165 | #endif |
---|
166 | |
---|
167 | } |
---|
168 | |
---|
169 | void test_map_list_of() |
---|
170 | { |
---|
171 | /* |
---|
172 | maybe in the future... |
---|
173 | |
---|
174 | using namespace std; |
---|
175 | using namespace boost::assign; |
---|
176 | |
---|
177 | typedef vector<int> score_type; |
---|
178 | typedef map<string,score_type> team_score_map; |
---|
179 | |
---|
180 | team_score_map team_score = map_list_of |
---|
181 | ( "Team Foo", list_of(1)(1)(0) ) |
---|
182 | ( "Team Bar", list_of(0)(0)(0) ) |
---|
183 | ( "Team FooBar", list_of(0)(0)(1) ); |
---|
184 | BOOST_CHECK_EQUAL( team_score.size(), 3 ); |
---|
185 | BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 ); |
---|
186 | BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 ); |
---|
187 | */ |
---|
188 | |
---|
189 | } |
---|
190 | |
---|
191 | /* |
---|
192 | void test_complex_list_of() |
---|
193 | { |
---|
194 | typedef std::complex<float> complex_t; |
---|
195 | std::vector<complex_t> v; |
---|
196 | v = ba::list_of<complex_t>(1,2)(2,3)(4,5)(0). |
---|
197 | repeat_from_to( complex_t(0,0), complex_t(10,10), complex_t(1,1) ); |
---|
198 | } |
---|
199 | */ |
---|
200 | |
---|
201 | struct five |
---|
202 | { |
---|
203 | five( int, int, int, int, int ) |
---|
204 | { |
---|
205 | } |
---|
206 | }; |
---|
207 | |
---|
208 | void test_list_of() |
---|
209 | { |
---|
210 | ba::list_of< five >(1,2,3,4,5)(6,7,8,9,10); |
---|
211 | |
---|
212 | /* Maybe this could be usefull in a later version? |
---|
213 | |
---|
214 | // an anonymous lists, fulfills Range concept |
---|
215 | for_each( ba::list_of( T() )( T() )( T() ) ); |
---|
216 | |
---|
217 | // non-anonymous lists |
---|
218 | ba::generic_list<T> list_1 = ba::list_of( T() ); |
---|
219 | BOOST_CHECK_EQUAL( list_1.size(), 1 ); |
---|
220 | ba::generic_list<T> list_2 = list_1 + ba::list_of( T() )( T() ) + list_1; |
---|
221 | BOOST_CHECK_EQUAL( list_2.size(), 4 ); |
---|
222 | list_1 += list_2; |
---|
223 | BOOST_CHECK_EQUAL( list_1.size(), 5 ); |
---|
224 | */ |
---|
225 | } |
---|
226 | |
---|
227 | |
---|
228 | |
---|
229 | void check_list_of() |
---|
230 | { |
---|
231 | test_sequence_list_of_int< std::vector<int> >(); |
---|
232 | test_sequence_list_of_int< std::list<int> >(); |
---|
233 | test_sequence_list_of_int< std::deque<int> >(); |
---|
234 | test_sequence_list_of_int< std::set<int> >(); |
---|
235 | test_sequence_list_of_int< std::multiset<int> >(); |
---|
236 | test_sequence_list_of_int< std::vector<float> >(); |
---|
237 | |
---|
238 | test_sequence_list_of_string< std::vector<std::string> >(); |
---|
239 | |
---|
240 | test_map_list_of< std::map<std::string,int> >(); |
---|
241 | test_map_list_of< std::multimap<std::string,int> >(); |
---|
242 | |
---|
243 | std::stack<std::string> s = ba::list_of( "Foo" )( "Bar" )( "FooBar" ).to_adapter( s ); |
---|
244 | test_list_of(); |
---|
245 | |
---|
246 | test_vector_matrix(); |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | |
---|
252 | #include <boost/test/unit_test.hpp> |
---|
253 | using boost::unit_test::test_suite; |
---|
254 | |
---|
255 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
256 | { |
---|
257 | test_suite* test = BOOST_TEST_SUITE( "List Test Suite" ); |
---|
258 | |
---|
259 | test->add( BOOST_TEST_CASE( &check_list_of ) ); |
---|
260 | |
---|
261 | return test; |
---|
262 | } |
---|
263 | |
---|
264 | |
---|