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(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
14# pragma warn -8091 // suppress 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
35namespace ba = boost::assign;
36
37void function_ptr( int )
38{
39 // do nothing
40}
41
42struct functor
43{
44 template< class T >
45 void operator()( T ) const
46 {
47 // do nothing
48 }
49};
50
51
52
53void 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( fun: (void (*)(int))&function_ptr )( 5 ),3;
64 make_list_inserter( fun: 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( fun: boost::bind( f: push_back_func, a1: &v, a2: _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( c&: v ) = 1,2,3,4,5;
75 BOOST_CHECK_EQUAL( v.size(), 7u );
76 BOOST_CHECK_EQUAL( v[6], 5 );
77
78 push_back( c&: v )(6).repeat( sz: 10, r: 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( c&: v ) = repeat_fun( sz: 10, r: &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( c&: v ) = 1,repeat( sz: 10, r: 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( c&: v ) = 1,repeat_fun( sz: 10, r: &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( fun: 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(BOOST_BORLANDC, BOOST_TESTED_AT(0x564)) \
118 || BOOST_WORKAROUND(BOOST_MSVC, <=1300) \
119 || BOOST_WORKAROUND(BOOST_MSVC, ==1700) \
120 || !defined( BOOST_NO_CXX11_HDR_INITIALIZER_LIST )
121#else
122
123 typedef vector<int> score_type;
124 typedef map<string,score_type> team_score_map;
125 typedef std::pair<string,score_type> score_pair;
126 team_score_map team_score;
127 insert( team_score )( "Team Foo", list_of(1)(1)(0) )
128 ( "Team Bar", list_of(0)(0)(0) )
129 ( "Team FooBar", list_of(0)(0)(1) );
130 BOOST_CHECK_EQUAL( team_score.size(), 3u );
131 BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
132 BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
133
134 team_score = list_of< score_pair >
135 ( "Team Foo", list_of(1)(1)(0) )
136 ( "Team Bar", list_of(0)(0)(0) )
137 ( "Team FooBar", list_of(0)(0)(1) );
138 BOOST_CHECK_EQUAL( team_score.size(), 3u );
139 BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 );
140 BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 );
141#endif
142
143}
144
145
146
147#include <boost/test/unit_test.hpp>
148using boost::unit_test::test_suite;
149
150test_suite* init_unit_test_suite( int argc, char* argv[] )
151{
152 test_suite* test = BOOST_TEST_SUITE( "List Test Suite" );
153
154 test->add( BOOST_TEST_CASE( &check_list_inserter ) );
155
156 return test;
157}
158
159

source code of boost/libs/assign/test/list_inserter.cpp