1 // Boost.Assign library
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)
8 // For more information, see http://www.boost.org/libs/assign/
12 #include <boost/detail/workaround.hpp>
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
19 #include <boost/assign/list_of.hpp>
20 #include <boost/test/test_tools.hpp>
21 #include <boost/array.hpp>
41 template< class Range
>
42 void for_each( const Range
& r
)
44 std::for_each( r
.begin(), r
.end(), nothing() );
47 namespace ba
= boost::assign
;
50 void test_sequence_list_of_string()
52 #if BOOST_WORKAROUND(BOOST_MSVC, <=1300)
53 const C c
= ba::list_of( "foo" )( "bar" ).to_container( c
);
55 const C c
= ba::list_of( "foo" )( "bar" );
57 BOOST_CHECK_EQUAL( c
.size(), 2u );
70 parameter_list( const T
&, int )
76 void test_sequence_list_of_int()
79 #if BOOST_WORKAROUND(BOOST_MSVC, <=1300)
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 );
88 c3
= ba::list_of(1).repeat_fun( 10, &rand
)(2)(3).to_container( c3
);
89 BOOST_CHECK_EQUAL( c3
.size(), 13u );
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 );
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 );
106 BOOST_CHECK_EQUAL( c3
.size(), 13u );
108 c3
= ba::list_of(1).repeat_fun( 10, &rand
)(2)(3);
109 BOOST_CHECK_EQUAL( c3
.size(), 13u );
114 parameter_list
p( ba::list_of(1)(2), 3u );
115 BOOST_CHECK_EQUAL( p
.val
, 1 );
120 void test_map_list_of()
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 );
128 void test_vector_matrix()
130 using namespace boost
;
131 using namespace boost::assign
;
135 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1) || BOOST_WORKAROUND(BOOST_MSVC, <=1300)
139 typedef array
<int,sz
> row3
;
140 typedef array
<row3
,sz
> matrix3x3
;
143 matrix3x3 m
= list_of( list_of(1)(2)(3) )
145 ( list_of(7)(8)(9) );
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 );
151 typedef vector
<int> row
;
152 typedef vector
<row
> matrix
;
155 // note: some libraries need a little help
156 // with the conversion, hence the 'row' template parameter.
158 matrix m2
= list_of
< row
>( list_of(1)(2)(3) )
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 );
170 void test_map_list_of()
173 maybe in the future...
176 using namespace boost::assign;
178 typedef vector<int> score_type;
179 typedef map<string,score_type> team_score_map;
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 );
193 void test_complex_list_of()
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) );
204 five( int, int, int, int, int )
211 ba::list_of
< five
>(1,2,3,4,5)(6,7,8,9,10);
213 /* Maybe this could be usefull in a later version?
215 // an anonymous lists, fulfills Range concept
216 for_each( ba::list_of( T() )( T() )( T() ) );
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 );
224 BOOST_CHECK_EQUAL( list_1.size(), 5 );
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.
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
)
240 return Os
<< ::boost::make_iterator_range( r
.begin(), r
.end() );
245 inline std::vector
<int> as_seq( const Seq
& s
)
248 return s
.to_container( c
);
251 void test_comparison_operations()
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) );
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> >();
276 test_sequence_list_of_string
< std::vector
<std::string
> >();
278 test_map_list_of
< std::map
<std::string
,int> >();
279 test_map_list_of
< std::multimap
<std::string
,int> >();
281 std::stack
<std::string
> s
= ba::list_of( "Foo" )( "Bar" )( "FooBar" ).to_adapter( s
);
283 test_vector_matrix();
284 test_comparison_operations();
289 #include <boost/test/unit_test.hpp>
290 using boost::unit_test::test_suite
;
292 test_suite
* init_unit_test_suite( int argc
, char* argv
[] )
294 test_suite
* test
= BOOST_TEST_SUITE( "List Test Suite" );
296 test
->add( BOOST_TEST_CASE( &check_list_of
) );