Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / test / test_bimap_sequenced.cpp
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 // Boost.Test
23 #include <boost/test/minimal.hpp>
24
25 // std
26 #include <set>
27 #include <map>
28 #include <algorithm>
29 #include <string>
30 #include <functional>
31
32
33 // Set type specifications
34 #include <boost/bimap/list_of.hpp>
35 #include <boost/bimap/vector_of.hpp>
36
37 // bimap container
38 #include <boost/bimap/bimap.hpp>
39 #include <boost/bimap/support/lambda.hpp>
40
41 #include <libs/bimap/test/test_bimap.hpp>
42
43 struct  left_tag {};
44 struct right_tag {};
45
46
47 template< class Container, class Data >
48 void test_list_operations(Container & b, Container& c, const Data & d)
49 {
50     c.clear() ;
51     c.assign(d.begin(),d.end());
52         
53     BOOST_CHECK( std::equal( c.begin(), c.end(), d.begin() ) );
54     c.reverse();
55     BOOST_CHECK( std::equal( c.begin(), c.end(), d.rbegin() ) );
56
57     c.sort();
58     BOOST_CHECK( std::equal( c.begin(), c.end(), d.begin() ) );
59
60     c.push_front( *d.begin() );
61     BOOST_CHECK( c.size() == d.size()+1 );
62     c.unique();
63     BOOST_CHECK( c.size() == d.size() );
64  
65     c.relocate( c.begin(), ++c.begin() );
66     c.relocate( c.end(), c.begin(), ++c.begin() );
67         
68     b.clear();
69     c.clear();
70     
71     c.assign(d.begin(),d.end());
72     b.splice(b.begin(),c);
73
74     BOOST_CHECK( c.size() == 0 );
75     BOOST_CHECK( b.size() == d.size() );
76
77     c.splice(c.begin(),b,++b.begin());
78
79     BOOST_CHECK( c.size() == 1 );
80
81     c.splice(c.begin(),b,b.begin(),b.end());
82
83     BOOST_CHECK( b.size() == 0 );
84
85     b.assign(d.begin(),d.end());
86     c.assign(d.begin(),d.end());
87     b.sort();
88     c.sort();
89     b.merge(c);
90     BOOST_CHECK( b.size() == 2*d.size() );
91  
92     b.unique();
93 }
94     
95 void test_bimap()
96 {
97     using namespace boost::bimaps;
98
99     typedef std::map<std::string,long> left_data_type;
100     left_data_type left_data;
101     left_data.insert( left_data_type::value_type("1",1) );
102     left_data.insert( left_data_type::value_type("2",2) );
103     left_data.insert( left_data_type::value_type("3",3) );
104     left_data.insert( left_data_type::value_type("4",4) );
105
106     typedef std::map<long,std::string> right_data_type;
107     right_data_type right_data;
108     right_data.insert( right_data_type::value_type(1,"1") );
109     right_data.insert( right_data_type::value_type(2,"2") );
110     right_data.insert( right_data_type::value_type(3,"3") );
111     right_data.insert( right_data_type::value_type(4,"4") );
112
113
114     //--------------------------------------------------------------------
115     {
116         typedef bimap<
117             list_of< std::string >, vector_of< long >
118             
119         > bm_type;
120
121         std::set< bm_type::value_type > data;
122         data.insert( bm_type::value_type("1",1) );
123         data.insert( bm_type::value_type("2",2) );
124         data.insert( bm_type::value_type("3",3) );
125         data.insert( bm_type::value_type("4",4) );
126
127         bm_type b;
128
129         test_bimap_init_copy_swap<bm_type>(data) ;
130         test_sequence_container(b,data);
131         test_sequence_container(b.left , left_data);
132         test_vector_container(b.right,right_data);
133
134         test_mapped_container(b.left );
135         test_mapped_container(b.right);
136
137         bm_type c;
138         test_list_operations(b,c,data) ;
139         test_list_operations(b.left,c.left,left_data) ;
140         test_list_operations(b.right,c.right,right_data) ;
141
142         c.assign(data.begin(),data.end());
143         b.assign(data.begin(),data.end());
144         c.remove_if(_key<=bm_type::value_type("1",1));
145         c.sort(std::less<bm_type::value_type>());
146         b.sort(std::less<bm_type::value_type>());
147         c.merge(b,std::less<bm_type::value_type>());
148         c.unique(std::equal_to<bm_type::value_type>());
149         
150         c.assign(data.begin(),data.end());
151         b.assign(data.begin(),data.end());
152         c.left.remove_if(_key<="1");
153         c.left.sort(std::less<std::string>());
154         b.left.sort(std::less<std::string>());
155         c.left.merge(b.left,std::less<std::string>());
156         c.left.unique(std::equal_to<std::string>());
157         
158         c.assign(data.begin(),data.end());
159         b.assign(data.begin(),data.end());
160         c.right.remove_if(_key<=1);
161         c.right.sort(std::less<long>());
162         b.right.sort(std::less<long>());
163         c.right.merge(b.right,std::less<long>());
164         c.right.unique(std::equal_to<long>());
165
166         c.assign(data.begin(),data.end());
167         c.right[0].first = -1;
168         c.right.at(0).second = "[1]";
169     }
170     //--------------------------------------------------------------------
171
172     
173     //--------------------------------------------------------------------
174     {
175         typedef bimap
176         <
177             vector_of<std::string>, list_of<long>,
178             vector_of_relation
179
180         > bm_type;
181
182         std::set< bm_type::value_type > data;
183         data.insert( bm_type::value_type("1",1) );
184         data.insert( bm_type::value_type("2",2) );
185         data.insert( bm_type::value_type("3",3) );
186         data.insert( bm_type::value_type("4",4) );
187         
188         bm_type b;
189         
190         test_bimap_init_copy_swap<bm_type>(data) ;
191         test_vector_container(b,data) ;
192         
193         bm_type c;
194         test_list_operations(b,c,data) ;
195         test_list_operations(b.left,c.left,left_data) ;
196         test_list_operations(b.right,c.right,right_data) ;
197         
198         c.assign(data.begin(),data.end());
199         b.assign(data.begin(),data.end());
200         c.remove_if(_key<=bm_type::value_type("1",1));
201         c.sort(std::less<bm_type::value_type>());
202         b.sort(std::less<bm_type::value_type>());
203         c.merge(b,std::less<bm_type::value_type>());
204         c.unique(std::equal_to<bm_type::value_type>());
205         
206         c.assign(data.begin(),data.end());
207         b.assign(data.begin(),data.end());
208         c.left.remove_if(_key<="1");
209         c.left.sort(std::less<std::string>());
210         b.left.sort(std::less<std::string>());
211         c.left.merge(b.left,std::less<std::string>());
212         c.left.unique(std::equal_to<std::string>());
213         
214         c.assign(data.begin(),data.end());
215         b.assign(data.begin(),data.end());
216         c.right.remove_if(_key<=1);
217         c.right.sort(std::less<long>());
218         b.right.sort(std::less<long>());
219         c.right.merge(b.right,std::less<long>());
220         c.right.unique(std::equal_to<long>());
221         
222         c.assign(data.begin(),data.end());
223         c[0].left = "(1)";
224         c.at(0).right = -1;
225         c.left[0].first = "[1]";
226         c.left.at(0).second = -1;
227     }
228     //--------------------------------------------------------------------
229
230     
231     //--------------------------------------------------------------------
232     {
233         typedef bimap
234         <
235             vector_of<std::string>, list_of<long>,
236             list_of_relation
237
238         > bm_type;
239
240         std::set< bm_type::value_type > data;
241         data.insert( bm_type::value_type("1",1) );
242         data.insert( bm_type::value_type("2",2) );
243         data.insert( bm_type::value_type("3",3) );
244         data.insert( bm_type::value_type("4",4) );
245         
246         bm_type b;
247         
248         test_bimap_init_copy_swap<bm_type>(data) ;
249         test_sequence_container(b,data) ;
250         
251         bm_type c;
252         test_list_operations(b,c,data) ;
253         test_list_operations(b.left,c.left,left_data) ;
254         test_list_operations(b.right,c.right,right_data) ;
255
256         
257         c.assign(data.begin(),data.end());
258         b.assign(data.begin(),data.end());
259         c.remove_if(_key<=bm_type::value_type("1",1));
260         c.sort(std::less<bm_type::value_type>());
261         b.sort(std::less<bm_type::value_type>());
262         c.merge(b,std::less<bm_type::value_type>());
263         c.unique(std::equal_to<bm_type::value_type>());
264         
265         c.assign(data.begin(),data.end());
266         c.left[0].first = "[1]";
267         c.left.at(0).second = -1;
268     }
269     //--------------------------------------------------------------------
270
271 }
272
273
274 int test_main( int, char* [] )
275 {
276     test_bimap();
277     return 0;
278 }
279