1 | // Boost.Bimap |
2 | // |
3 | // Copyright (c) 2006-2007 Matias Capeletto |
4 | // Copyright (c) 2024 Joaquin M Lopez Munoz |
5 | // |
6 | // Distributed under the Boost Software License, Version 1.0. |
7 | // (See accompanying file LICENSE_1_0.txt or copy at |
8 | // http://www.boost.org/LICENSE_1_0.txt) |
9 | |
10 | // VC++ 8.0 warns on usage of certain Standard Library and API functions that |
11 | // can be cause buffer overruns or other possible security issues if misused. |
12 | // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx |
13 | // But the wording of the warning is misleading and unsettling, there are no |
14 | // portable alternative functions, and VC++ 8.0's own libraries use the |
15 | // functions in question. So turn off the warnings. |
16 | #define _CRT_SECURE_NO_DEPRECATE |
17 | #define _SCL_SECURE_NO_DEPRECATE |
18 | |
19 | #include <boost/config.hpp> |
20 | |
21 | #include <boost/core/ignore_unused.hpp> |
22 | #include <boost/core/lightweight_test.hpp> |
23 | |
24 | // Boost.Bimap |
25 | #include <boost/bimap/support/lambda.hpp> |
26 | #include <boost/bimap/bimap.hpp> |
27 | #include <boost/bimap/unordered_set_of.hpp> |
28 | #include <boost/bimap/list_of.hpp> |
29 | #include <boost/bimap/vector_of.hpp> |
30 | #include <boost/bimap/unconstrained_set_of.hpp> |
31 | |
32 | #include <libs/bimap/test/strong_type.hpp> |
33 | |
34 | void test_bimap_operator_bracket() |
35 | { |
36 | using namespace boost::bimaps; |
37 | |
38 | // Simple test |
39 | { |
40 | typedef bimap< int, std::string > bm; |
41 | |
42 | bm b; |
43 | b.insert( bm::value_type(0,"0" ) ); |
44 | b.insert( bm::value_type(1,"1" ) ); |
45 | b.insert( bm::value_type(2,"2" ) ); |
46 | b.insert( bm::value_type(3,"3" ) ); |
47 | |
48 | BOOST_TEST( b.left.at(1) == "1" ); |
49 | |
50 | // Out of range test |
51 | { |
52 | bool value_not_found_test_passed = false; |
53 | b.clear(); |
54 | try |
55 | { |
56 | bool comp; |
57 | comp = b.left.at(2) < "banana" ; |
58 | boost::ignore_unused(comp); |
59 | } |
60 | catch( std::out_of_range & ) |
61 | { |
62 | value_not_found_test_passed = true; |
63 | } |
64 | |
65 | BOOST_TEST( value_not_found_test_passed ); |
66 | } |
67 | } |
68 | |
69 | // Mutable data test (1) |
70 | { |
71 | typedef bimap<int, list_of<std::string> > bm; |
72 | bm b; |
73 | |
74 | // Out of range test |
75 | { |
76 | bool value_not_found_test_passed = false; |
77 | b.clear(); |
78 | try |
79 | { |
80 | bool comp; |
81 | comp = b.left.at(1) < "banana" ; |
82 | boost::ignore_unused(comp); |
83 | } |
84 | catch( std::out_of_range & ) |
85 | { |
86 | value_not_found_test_passed = true; |
87 | } |
88 | |
89 | BOOST_TEST( value_not_found_test_passed ); |
90 | |
91 | } |
92 | |
93 | // Out of range test (const version) |
94 | { |
95 | bool value_not_found_test_passed = false; |
96 | b.clear(); |
97 | try |
98 | { |
99 | const bm & cb(b); |
100 | bool comp; |
101 | comp = cb.left.at(1) < "banana" ; |
102 | boost::ignore_unused(comp); |
103 | } |
104 | catch( std::out_of_range & ) |
105 | { |
106 | value_not_found_test_passed = true; |
107 | } |
108 | |
109 | BOOST_TEST( value_not_found_test_passed ); |
110 | } |
111 | |
112 | BOOST_TEST( b.left[1] == "" ); |
113 | BOOST_TEST( b.left.at(1) == "" ); |
114 | b.left[2] = "two" ; |
115 | BOOST_TEST( b.left.at(2) == "two" ); |
116 | b.left[2] = "<<two>>" ; |
117 | BOOST_TEST( b.left.at(2) == "<<two>>" ); |
118 | b.left.at(2) = "two" ; |
119 | BOOST_TEST( b.left.at(2) == "two" ); |
120 | |
121 | } |
122 | |
123 | // Mutable data test (2) |
124 | { |
125 | typedef bimap< vector_of<int>, unordered_set_of<std::string> > bm; |
126 | bm b; |
127 | |
128 | // Out of range test |
129 | { |
130 | bool value_not_found_test_passed = false; |
131 | b.clear(); |
132 | try |
133 | { |
134 | bool comp; |
135 | comp = b.right.at("banana" ) < 1; |
136 | boost::ignore_unused(comp); |
137 | } |
138 | catch( std::out_of_range & ) |
139 | { |
140 | value_not_found_test_passed = true; |
141 | } |
142 | BOOST_TEST( value_not_found_test_passed ); |
143 | } |
144 | |
145 | // Out of range test (const version) |
146 | { |
147 | bool value_not_found_test_passed = false; |
148 | b.clear(); |
149 | try |
150 | { |
151 | const bm & cb(b); |
152 | bool comp; |
153 | comp = cb.right.at("banana" ) < 1; |
154 | boost::ignore_unused(comp); |
155 | } |
156 | catch( std::out_of_range & ) |
157 | { |
158 | value_not_found_test_passed = true; |
159 | } |
160 | |
161 | BOOST_TEST( value_not_found_test_passed ); |
162 | } |
163 | |
164 | b.right["one" ]; |
165 | BOOST_TEST( b.size() == 1 ); |
166 | b.right["two" ] = 2; |
167 | BOOST_TEST( b.right.at("two" ) == 2 ); |
168 | b.right["two" ] = -2; |
169 | BOOST_TEST( b.right.at("two" ) == -2 ); |
170 | b.right.at("two" ) = 2; |
171 | BOOST_TEST( b.right.at("two" ) == 2 ); |
172 | } |
173 | |
174 | // Mutable data test (3) |
175 | { |
176 | typedef bimap< unconstrained_set_of<int>, |
177 | unordered_set_of<std::string>, |
178 | right_based > bm; |
179 | |
180 | bm b; |
181 | |
182 | b.right["one" ]; |
183 | BOOST_TEST( b.size() == 1 ); |
184 | b.right["two" ] = 2; |
185 | BOOST_TEST( b.right.at("two" ) == 2 ); |
186 | b.right["two" ] = -2; |
187 | BOOST_TEST( b.right.at("two" ) == -2 ); |
188 | b.right.at("two" ) = 2; |
189 | BOOST_TEST( b.right.at("two" ) == 2 ); |
190 | } |
191 | |
192 | // Heterogeneous access test (1) |
193 | { |
194 | typedef bimap |
195 | < |
196 | set_of<int, std::less< strong<int> > >, |
197 | list_of<std::string> |
198 | > bm; |
199 | |
200 | bm b; |
201 | |
202 | b.left[1] = "0" ; |
203 | b.left[semistrong<int>(1)] = "1" ; |
204 | BOOST_TEST( b.left.at(strong<int>(1)) == "1" ); |
205 | b.left.at(strong<int>(1)) = "2" ; |
206 | BOOST_TEST( b.left.at(strong<int>(1)) == "2" ); |
207 | } |
208 | |
209 | // Heterogeneous access test (2) |
210 | { |
211 | typedef bimap |
212 | < |
213 | list_of<int>, |
214 | unordered_set_of |
215 | < |
216 | std::string, |
217 | boost::hash< strong<std::string> >, |
218 | std::equal_to< strong<std::string> > |
219 | > |
220 | > bm; |
221 | |
222 | bm b; |
223 | |
224 | b.right["1" ]=0; |
225 | b.right[semistrong<std::string>("1" )] = 1; |
226 | BOOST_TEST( b.right.at(strong<std::string>("1" )) == 1); |
227 | b.right.at(strong<std::string>("1" )) = 2; |
228 | BOOST_TEST( b.right.at(strong<std::string>("1" )) == 2); |
229 | } |
230 | } |
231 | |
232 | int main() |
233 | { |
234 | test_bimap_operator_bracket(); |
235 | |
236 | return boost::report_errors(); |
237 | } |
238 | |
239 | |