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 #include <boost/detail/workaround.hpp>
12 
13 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
14 #  pragma warn -8091 // supress warning in Boost.Test
15 #  pragma warn -8057 // unused argument argc/argv in Boost.Test
16 #endif
17 
18 
19 #include <boost/assign/list_inserter.hpp>
20 #include <boost/assign/list_of.hpp>
21 #include <boost/test/test_tools.hpp>
22 #include <boost/array.hpp>
23 #include <boost/function.hpp>
24 #include <boost/bind.hpp>
25 #include <boost/functional.hpp>
26 #include <iostream>
27 #include <vector>
28 #include <map>
29 #include <string>
30 #include <utility>
31 #include <stdexcept>
32 #include <cstdlib>
33 
34 
35 namespace ba = boost::assign;
36 
function_ptr(int)37 void function_ptr( int )
38 {
39     // do nothing
40 }
41 
42 struct functor
43 {
44     template< class T >
operator ()functor45     void operator()( T ) const
46     {
47         // do nothing
48     }
49 };
50 
51 
52 
check_list_inserter()53 void check_list_inserter()
54 {
55     using namespace std;
56     using namespace boost;
57     using namespace boost::assign;
58     vector<int> v;
59 
60     //
61     // @note: cast only necessary on CodeWarrior
62     //
63     make_list_inserter( (void (*)(int))&function_ptr )( 5 ),3;
64     make_list_inserter( functor() )( 4 ),2;
65 
66     typedef void (vector<int>::* push_back_t)(const int&);
67     push_back_t push_back_func = &vector<int>::push_back;
68     make_list_inserter( boost::bind( push_back_func, &v, _1 ) )( 6 ),4;
69 
70     BOOST_CHECK_EQUAL( v.size(), 2u );
71     BOOST_CHECK_EQUAL( v[0], 6 );
72     BOOST_CHECK_EQUAL( v[1], 4 );
73 
74     push_back( v ) = 1,2,3,4,5;
75     BOOST_CHECK_EQUAL( v.size(), 7u );
76     BOOST_CHECK_EQUAL( v[6], 5 );
77 
78     push_back( v )(6).repeat( 10, 7 )(8);
79     BOOST_CHECK_EQUAL( v.size(), 19u );
80     BOOST_CHECK_EQUAL( v[18], 8 );
81     BOOST_CHECK_EQUAL( v[8], 7 );
82     BOOST_CHECK_EQUAL( v[16], 7 );
83 
84 #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200)
85     push_back( v ) = repeat_fun( 10, &rand );
86 #else
87     push_back( v ).repeat_fun( 10, &rand );
88 #endif
89 
90     BOOST_CHECK_EQUAL( v.size(), 29u );
91 
92 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 )
93     push_back( v )(1).repeat( 10, 2 )(3);
94 #else
95     push_back( v ) = 1,repeat( 10, 2 ),3;
96 #endif
97     BOOST_CHECK_EQUAL( v.size(), 41u );
98 
99 #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 )
100     push_back( v )(1).repeat_fun( 10, &rand )(2);
101 #else
102     push_back( v ) = 1,repeat_fun( 10, &rand ),2;
103 #endif
104 
105     BOOST_CHECK_EQUAL( v.size(), 53u );
106 
107     typedef map<string,int> map_t;
108     typedef map_t::value_type V;
109     map_t m;
110 
111     make_list_inserter( assign_detail::call_insert< map_t >( m ) )
112                       ( V("bar",3) )( V("foo", 2) );
113     BOOST_CHECK_EQUAL( m.size(), 2u );
114     BOOST_CHECK_EQUAL( m["foo"], 2 );
115 
116 
117 #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) || BOOST_WORKAROUND(BOOST_MSVC, <=1300)
118 #else
119 
120     typedef vector<int>                   score_type;
121     typedef map<string,score_type>        team_score_map;
122     typedef std::pair<string,score_type>  score_pair;
123     team_score_map team_score;
124     insert( team_score )( "Team Foo",    list_of(1)(1)(0) )
125                         ( "Team Bar",    list_of(0)(0)(0) )
126                         ( "Team FooBar", list_of(0)(0)(1) );
127     BOOST_CHECK_EQUAL( team_score.size(), 3u );
128     BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
129     BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
130 
131     team_score = list_of< score_pair >
132                         ( "Team Foo",    list_of(1)(1)(0) )
133                         ( "Team Bar",    list_of(0)(0)(0) )
134                         ( "Team FooBar", list_of(0)(0)(1) );
135     BOOST_CHECK_EQUAL( team_score.size(), 3u );
136     BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
137     BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
138 
139 #endif
140 
141 }
142 
143 
144 
145 #include <boost/test/unit_test.hpp>
146 using boost::unit_test::test_suite;
147 
init_unit_test_suite(int argc,char * argv[])148 test_suite* init_unit_test_suite( int argc, char* argv[] )
149 {
150     test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
151 
152     test->add( BOOST_TEST_CASE( &check_list_inserter ) );
153 
154     return test;
155 }
156 
157