]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/assign/test/list_of.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / assign / test / list_of.cpp
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 // suppress 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 using boost::array;
134
135 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(BOOST_MSVC, <=1300)
136 #else
137
138 const int sz = 3;
139 typedef array<int,sz> row3;
140 typedef array<row3,sz> matrix3x3;
141
142
143 matrix3x3 m = list_of( list_of(1)(2)(3) )
144 ( list_of(4)(5)(6) )
145 ( list_of(7)(8)(9) );
146
147 for( int i = 0; i != sz; ++i )
148 for( int j = 0; j != sz; ++j )
149 BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 );
150
151 typedef vector<int> row;
152 typedef vector<row> matrix;
153
154 //
155 // note: some libraries need a little help
156 // with the conversion, hence the 'row' template parameter.
157 //
158 matrix m2 = list_of< row >( list_of(1)(2)(3) )
159 ( list_of(4)(5) )
160 ( list_of(6) );
161
162 for( int i = 0; i != sz; ++i )
163 for( int j = 0; j != sz - i; ++j )
164 BOOST_CHECK_EQUAL( m[i][j], i*sz + j + 1 );
165
166 #endif
167
168 }
169
170 void test_map_list_of()
171 {
172 /*
173 maybe in the future...
174
175 using namespace std;
176 using namespace boost::assign;
177
178 typedef vector<int> score_type;
179 typedef map<string,score_type> team_score_map;
180
181 team_score_map team_score = map_list_of
182 ( "Team Foo", list_of(1)(1)(0) )
183 ( "Team Bar", list_of(0)(0)(0) )
184 ( "Team FooBar", list_of(0)(0)(1) );
185 BOOST_CHECK_EQUAL( team_score.size(), 3 );
186 BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
187 BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
188 */
189
190 }
191
192 /*
193 void test_complex_list_of()
194 {
195 typedef std::complex<float> complex_t;
196 std::vector<complex_t> v;
197 v = ba::list_of<complex_t>(1,2)(2,3)(4,5)(0).
198 repeat_from_to( complex_t(0,0), complex_t(10,10), complex_t(1,1) );
199 }
200 */
201
202 struct five
203 {
204 five( int, int, int, int, int )
205 {
206 }
207 };
208
209 void test_list_of()
210 {
211 ba::list_of< five >(1,2,3,4,5)(6,7,8,9,10);
212
213 /* Maybe this could be usefull in a later version?
214
215 // an anonymous lists, fulfills Range concept
216 for_each( ba::list_of( T() )( T() )( T() ) );
217
218 // non-anonymous lists
219 ba::generic_list<T> list_1 = ba::list_of( T() );
220 BOOST_CHECK_EQUAL( list_1.size(), 1 );
221 ba::generic_list<T> list_2 = list_1 + ba::list_of( T() )( T() ) + list_1;
222 BOOST_CHECK_EQUAL( list_2.size(), 4 );
223 list_1 += list_2;
224 BOOST_CHECK_EQUAL( list_1.size(), 5 );
225 */
226 }
227
228 //
229 // @remark: ADL is required here, but it is a bit wierd to
230 // open up namespace std. Perhaps Boost.Test needs a
231 // better configuration option.
232 //
233 namespace std
234 {
235 template< class T, class Elem, class Traits >
236 inline std::basic_ostream<Elem,Traits>&
237 operator<<( std::basic_ostream<Elem, Traits>& Os,
238 const std::vector<T>& r )
239 {
240 return Os << ::boost::make_iterator_range( r.begin(), r.end() );
241 }
242 }
243
244 template <class Seq>
245 inline std::vector<int> as_seq( const Seq& s )
246 {
247 std::vector<int> c;
248 return s.to_container( c );
249 }
250
251 void test_comparison_operations()
252 {
253 BOOST_CHECK_EQUAL( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
254 BOOST_CHECK_NE( ba::list_of(0)(1)(2), as_seq(ba::list_of(-1)(1)(2)) );
255 BOOST_CHECK_LT( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(3)) );
256 BOOST_CHECK_LE( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
257 BOOST_CHECK_GT( ba::list_of(0)(1)(3), as_seq(ba::list_of(0)(1)(2)) );
258 BOOST_CHECK_GE( ba::list_of(0)(1)(2), as_seq(ba::list_of(0)(1)(2)) );
259 BOOST_CHECK_EQUAL( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
260 BOOST_CHECK_NE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(-1)(1)(2) );
261 BOOST_CHECK_LT( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(3) );
262 BOOST_CHECK_LE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
263 BOOST_CHECK_GT( as_seq(ba::list_of(0)(1)(3)), ba::list_of(0)(1)(2) );
264 BOOST_CHECK_GE( as_seq(ba::list_of(0)(1)(2)), ba::list_of(0)(1)(2) );
265 }
266
267 void check_list_of()
268 {
269 test_sequence_list_of_int< std::vector<int> >();
270 test_sequence_list_of_int< std::list<int> >();
271 test_sequence_list_of_int< std::deque<int> >();
272 test_sequence_list_of_int< std::set<int> >();
273 test_sequence_list_of_int< std::multiset<int> >();
274 test_sequence_list_of_int< std::vector<float> >();
275
276 test_sequence_list_of_string< std::vector<std::string> >();
277
278 test_map_list_of< std::map<std::string,int> >();
279 test_map_list_of< std::multimap<std::string,int> >();
280
281 std::stack<std::string> s = ba::list_of( "Foo" )( "Bar" )( "FooBar" ).to_adapter( s );
282 test_list_of();
283 test_vector_matrix();
284 test_comparison_operations();
285 }
286
287
288
289 #include <boost/test/unit_test.hpp>
290 using boost::unit_test::test_suite;
291
292 test_suite* init_unit_test_suite( int argc, char* argv[] )
293 {
294 test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
295
296 test->add( BOOST_TEST_CASE( &check_list_of ) );
297
298 return test;
299 }
300
301