• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 #include <boost/config.hpp>
19 
20 #define BOOST_BIMAP_DISABLE_SERIALIZATION
21 
22 #include <boost/core/lightweight_test.hpp>
23 
24 // std
25 #include <set>
26 #include <map>
27 #include <algorithm>
28 #include <string>
29 #include <functional>
30 
31 
32 // Set type specifications
33 #include <boost/bimap/list_of.hpp>
34 #include <boost/bimap/vector_of.hpp>
35 
36 // bimap container
37 #include <boost/bimap/bimap.hpp>
38 #include <boost/bimap/support/lambda.hpp>
39 
40 #include <libs/bimap/test/test_bimap.hpp>
41 
42 struct  left_tag {};
43 struct right_tag {};
44 
45 
46 template< class Container, class Data >
test_list_operations(Container & b,Container & c,const Data & d)47 void test_list_operations(Container & b, Container& c, const Data & d)
48 {
49     c.clear() ;
50     c.assign(d.begin(),d.end());
51 
52     BOOST_TEST( std::equal( c.begin(), c.end(), d.begin() ) );
53     c.reverse();
54     BOOST_TEST( std::equal( c.begin(), c.end(), d.rbegin() ) );
55 
56     c.sort();
57     BOOST_TEST( std::equal( c.begin(), c.end(), d.begin() ) );
58 
59     c.push_front( *d.begin() );
60     BOOST_TEST( c.size() == d.size()+1 );
61     c.unique();
62     BOOST_TEST( c.size() == d.size() );
63 
64     c.relocate( c.begin(), ++c.begin() );
65     c.relocate( c.end(), c.begin(), ++c.begin() );
66 
67     b.clear();
68     c.clear();
69 
70     c.assign(d.begin(),d.end());
71     b.splice(b.begin(),c);
72 
73     BOOST_TEST( c.size() == 0 );
74     BOOST_TEST( b.size() == d.size() );
75 
76     c.splice(c.begin(),b,++b.begin());
77 
78     BOOST_TEST( c.size() == 1 );
79 
80     c.splice(c.begin(),b,b.begin(),b.end());
81 
82     BOOST_TEST( b.size() == 0 );
83 
84     b.assign(d.begin(),d.end());
85     c.assign(d.begin(),d.end());
86     b.sort();
87     c.sort();
88     b.merge(c);
89     BOOST_TEST( b.size() == 2*d.size() );
90 
91     b.unique();
92 }
93 
test_bimap()94 void test_bimap()
95 {
96     using namespace boost::bimaps;
97 
98     typedef std::map<std::string,long> left_data_type;
99     left_data_type left_data;
100     left_data.insert( left_data_type::value_type("1",1) );
101     left_data.insert( left_data_type::value_type("2",2) );
102     left_data.insert( left_data_type::value_type("3",3) );
103     left_data.insert( left_data_type::value_type("4",4) );
104 
105     typedef std::map<long,std::string> right_data_type;
106     right_data_type right_data;
107     right_data.insert( right_data_type::value_type(1,"1") );
108     right_data.insert( right_data_type::value_type(2,"2") );
109     right_data.insert( right_data_type::value_type(3,"3") );
110     right_data.insert( right_data_type::value_type(4,"4") );
111 
112 
113     //--------------------------------------------------------------------
114     {
115         typedef bimap<
116             list_of< std::string >, vector_of< long >
117 
118         > bm_type;
119 
120         std::set< bm_type::value_type > data;
121         data.insert( bm_type::value_type("1",1) );
122         data.insert( bm_type::value_type("2",2) );
123         data.insert( bm_type::value_type("3",3) );
124         data.insert( bm_type::value_type("4",4) );
125 
126         bm_type b;
127 
128         test_bimap_init_copy_swap<bm_type>(data) ;
129         test_sequence_container(b,data);
130         test_sequence_container(b.left , left_data);
131         test_vector_container(b.right,right_data);
132 
133         test_mapped_container(b.left );
134         test_mapped_container(b.right);
135 
136         bm_type c;
137         test_list_operations(b,c,data) ;
138         test_list_operations(b.left,c.left,left_data) ;
139         test_list_operations(b.right,c.right,right_data) ;
140 
141         c.assign(data.begin(),data.end());
142         b.assign(data.begin(),data.end());
143         c.remove_if(_key<=bm_type::value_type("1",1));
144         c.sort(std::less<bm_type::value_type>());
145         b.sort(std::less<bm_type::value_type>());
146         c.merge(b,std::less<bm_type::value_type>());
147         c.unique(std::equal_to<bm_type::value_type>());
148 
149         c.assign(data.begin(),data.end());
150         b.assign(data.begin(),data.end());
151         c.left.remove_if(_key<="1");
152         c.left.sort(std::less<std::string>());
153         b.left.sort(std::less<std::string>());
154         c.left.merge(b.left,std::less<std::string>());
155         c.left.unique(std::equal_to<std::string>());
156 
157         c.assign(data.begin(),data.end());
158         b.assign(data.begin(),data.end());
159         c.right.remove_if(_key<=1);
160         c.right.sort(std::less<long>());
161         b.right.sort(std::less<long>());
162         c.right.merge(b.right,std::less<long>());
163         c.right.unique(std::equal_to<long>());
164 
165         c.assign(data.begin(),data.end());
166         c.right[0].first = -1;
167         c.right.at(0).second = "[1]";
168     }
169     //--------------------------------------------------------------------
170 
171 
172     //--------------------------------------------------------------------
173     {
174         typedef bimap
175         <
176             vector_of<std::string>, list_of<long>,
177             vector_of_relation
178 
179         > bm_type;
180 
181         std::set< bm_type::value_type > data;
182         data.insert( bm_type::value_type("1",1) );
183         data.insert( bm_type::value_type("2",2) );
184         data.insert( bm_type::value_type("3",3) );
185         data.insert( bm_type::value_type("4",4) );
186 
187         bm_type b;
188 
189         test_bimap_init_copy_swap<bm_type>(data) ;
190         test_vector_container(b,data) ;
191 
192         bm_type c;
193         test_list_operations(b,c,data) ;
194         test_list_operations(b.left,c.left,left_data) ;
195         test_list_operations(b.right,c.right,right_data) ;
196 
197         c.assign(data.begin(),data.end());
198         b.assign(data.begin(),data.end());
199         c.remove_if(_key<=bm_type::value_type("1",1));
200         c.sort(std::less<bm_type::value_type>());
201         b.sort(std::less<bm_type::value_type>());
202         c.merge(b,std::less<bm_type::value_type>());
203         c.unique(std::equal_to<bm_type::value_type>());
204 
205         c.assign(data.begin(),data.end());
206         b.assign(data.begin(),data.end());
207         c.left.remove_if(_key<="1");
208         c.left.sort(std::less<std::string>());
209         b.left.sort(std::less<std::string>());
210         c.left.merge(b.left,std::less<std::string>());
211         c.left.unique(std::equal_to<std::string>());
212 
213         c.assign(data.begin(),data.end());
214         b.assign(data.begin(),data.end());
215         c.right.remove_if(_key<=1);
216         c.right.sort(std::less<long>());
217         b.right.sort(std::less<long>());
218         c.right.merge(b.right,std::less<long>());
219         c.right.unique(std::equal_to<long>());
220 
221         c.assign(data.begin(),data.end());
222         c[0].left = "(1)";
223         c.at(0).right = -1;
224         c.left[0].first = "[1]";
225         c.left.at(0).second = -1;
226     }
227     //--------------------------------------------------------------------
228 
229 
230     //--------------------------------------------------------------------
231     {
232         typedef bimap
233         <
234             vector_of<std::string>, list_of<long>,
235             list_of_relation
236 
237         > bm_type;
238 
239         std::set< bm_type::value_type > data;
240         data.insert( bm_type::value_type("1",1) );
241         data.insert( bm_type::value_type("2",2) );
242         data.insert( bm_type::value_type("3",3) );
243         data.insert( bm_type::value_type("4",4) );
244 
245         bm_type b;
246 
247         test_bimap_init_copy_swap<bm_type>(data) ;
248         test_sequence_container(b,data) ;
249 
250         bm_type c;
251         test_list_operations(b,c,data) ;
252         test_list_operations(b.left,c.left,left_data) ;
253         test_list_operations(b.right,c.right,right_data) ;
254 
255 
256         c.assign(data.begin(),data.end());
257         b.assign(data.begin(),data.end());
258         c.remove_if(_key<=bm_type::value_type("1",1));
259         c.sort(std::less<bm_type::value_type>());
260         b.sort(std::less<bm_type::value_type>());
261         c.merge(b,std::less<bm_type::value_type>());
262         c.unique(std::equal_to<bm_type::value_type>());
263 
264         c.assign(data.begin(),data.end());
265         c.left[0].first = "[1]";
266         c.left.at(0).second = -1;
267     }
268     //--------------------------------------------------------------------
269 
270 }
271 
272 
main()273 int main()
274 {
275     test_bimap();
276     return boost::report_errors();
277 }
278 
279