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 #include <boost/core/ignore_unused.hpp>
21 #include <boost/core/lightweight_test.hpp>
22
23 // Boost.Bimap
24 #include <boost/bimap/support/lambda.hpp>
25 #include <boost/bimap/bimap.hpp>
26 #include <boost/bimap/unordered_set_of.hpp>
27 #include <boost/bimap/list_of.hpp>
28 #include <boost/bimap/vector_of.hpp>
29 #include <boost/bimap/unconstrained_set_of.hpp>
30
31
test_bimap_operator_bracket()32 void test_bimap_operator_bracket()
33 {
34 using namespace boost::bimaps;
35
36 // Simple test
37 {
38 typedef bimap< int, std::string > bm;
39
40 bm b;
41 b.insert( bm::value_type(0,"0") );
42 b.insert( bm::value_type(1,"1") );
43 b.insert( bm::value_type(2,"2") );
44 b.insert( bm::value_type(3,"3") );
45
46 BOOST_TEST( b.left.at(1) == "1" );
47
48 // Out of range test
49 {
50 bool value_not_found_test_passed = false;
51 b.clear();
52 try
53 {
54 bool comp;
55 comp = b.left.at(2) < "banana";
56 boost::ignore_unused(comp);
57 }
58 catch( std::out_of_range & )
59 {
60 value_not_found_test_passed = true;
61 }
62
63 BOOST_TEST( value_not_found_test_passed );
64 }
65 }
66
67 // Mutable data test (1)
68 {
69 typedef bimap<int, list_of<std::string> > bm;
70 bm b;
71
72 // Out of range test
73 {
74 bool value_not_found_test_passed = false;
75 b.clear();
76 try
77 {
78 bool comp;
79 comp = b.left.at(1) < "banana";
80 boost::ignore_unused(comp);
81 }
82 catch( std::out_of_range & )
83 {
84 value_not_found_test_passed = true;
85 }
86
87 BOOST_TEST( value_not_found_test_passed );
88
89 }
90
91 // Out of range test (const version)
92 {
93 bool value_not_found_test_passed = false;
94 b.clear();
95 try
96 {
97 const bm & cb(b);
98 bool comp;
99 comp = cb.left.at(1) < "banana";
100 boost::ignore_unused(comp);
101 }
102 catch( std::out_of_range & )
103 {
104 value_not_found_test_passed = true;
105 }
106
107 BOOST_TEST( value_not_found_test_passed );
108 }
109
110 BOOST_TEST( b.left[1] == "" );
111 BOOST_TEST( b.left.at(1) == "" );
112 b.left[2] = "two";
113 BOOST_TEST( b.left.at(2) == "two" );
114 b.left[2] = "<<two>>";
115 BOOST_TEST( b.left.at(2) == "<<two>>" );
116 b.left.at(2) = "two";
117 BOOST_TEST( b.left.at(2) == "two" );
118
119 }
120
121 // Mutable data test (2)
122 {
123 typedef bimap< vector_of<int>, unordered_set_of<std::string> > bm;
124 bm b;
125
126 // Out of range test
127 {
128 bool value_not_found_test_passed = false;
129 b.clear();
130 try
131 {
132 bool comp;
133 comp = b.right.at("banana") < 1;
134 boost::ignore_unused(comp);
135 }
136 catch( std::out_of_range & )
137 {
138 value_not_found_test_passed = true;
139 }
140 BOOST_TEST( value_not_found_test_passed );
141 }
142
143 // Out of range test (const version)
144 {
145 bool value_not_found_test_passed = false;
146 b.clear();
147 try
148 {
149 const bm & cb(b);
150 bool comp;
151 comp = cb.right.at("banana") < 1;
152 boost::ignore_unused(comp);
153 }
154 catch( std::out_of_range & )
155 {
156 value_not_found_test_passed = true;
157 }
158
159 BOOST_TEST( value_not_found_test_passed );
160 }
161
162 b.right["one"];
163 BOOST_TEST( b.size() == 1 );
164 b.right["two"] = 2;
165 BOOST_TEST( b.right.at("two") == 2 );
166 b.right["two"] = -2;
167 BOOST_TEST( b.right.at("two") == -2 );
168 b.right.at("two") = 2;
169 BOOST_TEST( b.right.at("two") == 2 );
170 }
171
172 // Mutable data test (3)
173 {
174 typedef bimap< unconstrained_set_of<int>,
175 unordered_set_of<std::string>,
176 right_based > bm;
177
178 bm b;
179
180 b.right["one"];
181 BOOST_TEST( b.size() == 1 );
182 b.right["two"] = 2;
183 BOOST_TEST( b.right.at("two") == 2 );
184 b.right["two"] = -2;
185 BOOST_TEST( b.right.at("two") == -2 );
186 b.right.at("two") = 2;
187 BOOST_TEST( b.right.at("two") == 2 );
188 }
189
190 }
191
main()192 int main()
193 {
194 test_bimap_operator_bracket();
195
196 return boost::report_errors();
197 }
198
199