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