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 | |
---|
37 | void function_ptr( int ) |
---|
38 | { |
---|
39 | // do nothing |
---|
40 | } |
---|
41 | |
---|
42 | struct functor |
---|
43 | { |
---|
44 | template< class T > |
---|
45 | void operator()( T ) const |
---|
46 | { |
---|
47 | // do nothing |
---|
48 | } |
---|
49 | }; |
---|
50 | |
---|
51 | |
---|
52 | |
---|
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 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) |
---|
67 | // BCB would crash |
---|
68 | typedef void (vector<int>::* push_back_t)(const int&); |
---|
69 | push_back_t push_back_func = &vector<int>::push_back; |
---|
70 | make_list_inserter( bind( push_back_func, &v, _1 ) )( 6 ),4; |
---|
71 | #else |
---|
72 | make_list_inserter( bind( &vector<int>::push_back, &v, _1 ) )( 6 ),4; |
---|
73 | #endif |
---|
74 | |
---|
75 | BOOST_CHECK_EQUAL( v.size(), 2u ); |
---|
76 | BOOST_CHECK_EQUAL( v[0], 6 ); |
---|
77 | BOOST_CHECK_EQUAL( v[1], 4 ); |
---|
78 | |
---|
79 | push_back( v ) = 1,2,3,4,5; |
---|
80 | BOOST_CHECK_EQUAL( v.size(), 7u ); |
---|
81 | BOOST_CHECK_EQUAL( v[6], 5 ); |
---|
82 | |
---|
83 | push_back( v )(6).repeat( 10, 7 )(8); |
---|
84 | BOOST_CHECK_EQUAL( v.size(), 19u ); |
---|
85 | BOOST_CHECK_EQUAL( v[18], 8 ); |
---|
86 | BOOST_CHECK_EQUAL( v[8], 7 ); |
---|
87 | BOOST_CHECK_EQUAL( v[16], 7 ); |
---|
88 | |
---|
89 | #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1200) |
---|
90 | push_back( v ) = repeat_fun( 10, &rand ); |
---|
91 | #else |
---|
92 | push_back( v ).repeat_fun( 10, &rand ); |
---|
93 | #endif |
---|
94 | |
---|
95 | BOOST_CHECK_EQUAL( v.size(), 29u ); |
---|
96 | |
---|
97 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 ) |
---|
98 | push_back( v )(1).repeat( 10, 2 )(3); |
---|
99 | #else |
---|
100 | push_back( v ) = 1,repeat( 10, 2 ),3; |
---|
101 | #endif |
---|
102 | BOOST_CHECK_EQUAL( v.size(), 41u ); |
---|
103 | |
---|
104 | #if BOOST_WORKAROUND(BOOST_MSVC, <= 1200) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580 ) |
---|
105 | push_back( v )(1).repeat_fun( 10, &rand )(2); |
---|
106 | #else |
---|
107 | push_back( v ) = 1,repeat_fun( 10, &rand ),2; |
---|
108 | #endif |
---|
109 | |
---|
110 | BOOST_CHECK_EQUAL( v.size(), 53u ); |
---|
111 | |
---|
112 | typedef map<string,int> map_t; |
---|
113 | typedef map_t::value_type V; |
---|
114 | map_t m; |
---|
115 | |
---|
116 | make_list_inserter( assign_detail::call_insert< map_t >( m ) ) |
---|
117 | ( V("bar",3) )( V("foo", 2) ); |
---|
118 | BOOST_CHECK_EQUAL( m.size(), 2u ); |
---|
119 | BOOST_CHECK_EQUAL( m["foo"], 2 ); |
---|
120 | |
---|
121 | |
---|
122 | #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564)) || BOOST_WORKAROUND(BOOST_MSVC, <=1300) |
---|
123 | #else |
---|
124 | |
---|
125 | typedef vector<int> score_type; |
---|
126 | typedef map<string,score_type> team_score_map; |
---|
127 | typedef std::pair<string,score_type> score_pair; |
---|
128 | team_score_map team_score; |
---|
129 | insert( team_score )( "Team Foo", list_of(1)(1)(0) ) |
---|
130 | ( "Team Bar", list_of(0)(0)(0) ) |
---|
131 | ( "Team FooBar", list_of(0)(0)(1) ); |
---|
132 | BOOST_CHECK_EQUAL( team_score.size(), 3u ); |
---|
133 | BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 ); |
---|
134 | BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 ); |
---|
135 | |
---|
136 | team_score = list_of< score_pair > |
---|
137 | ( "Team Foo", list_of(1)(1)(0) ) |
---|
138 | ( "Team Bar", list_of(0)(0)(0) ) |
---|
139 | ( "Team FooBar", list_of(0)(0)(1) ); |
---|
140 | BOOST_CHECK_EQUAL( team_score.size(), 3u ); |
---|
141 | BOOST_CHECK_EQUAL( team_score[ "Team Foo" ][1], 1 ); |
---|
142 | BOOST_CHECK_EQUAL( team_score[ "Team Bar" ][0], 0 ); |
---|
143 | |
---|
144 | #endif |
---|
145 | |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | |
---|
150 | #include <boost/test/unit_test.hpp> |
---|
151 | using boost::unit_test::test_suite; |
---|
152 | |
---|
153 | test_suite* init_unit_test_suite( int argc, char* argv[] ) |
---|
154 | { |
---|
155 | test_suite* test = BOOST_TEST_SUITE( "List Test Suite" ); |
---|
156 | |
---|
157 | test->add( BOOST_TEST_CASE( &check_list_inserter ) ); |
---|
158 | |
---|
159 | return test; |
---|
160 | } |
---|
161 | |
---|